File: RatingStarEditorPanel.java

package info (click to toggle)
mac-widgets 0.10.0%2Bsvn416-dfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 1,968 kB
  • ctags: 2,003
  • sloc: java: 9,909; makefile: 13; sh: 12
file content (138 lines) | stat: -rw-r--r-- 3,670 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package com.explodingpixels.macwidgets;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JPanel;

/**
 * This editor panel emulates iTunes Star rating editor functionality
 * @author Paul Connolly paulcconnolly gmail
 */
public class RatingStarEditorPanel
        extends JPanel
        implements MouseListener, MouseMotionListener {


    public RatingStarEditorPanel() {
        setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));

        repaint();
    }

    private int lastX = Integer.MIN_VALUE;
    private int lastY = Integer.MIN_VALUE;
    private int xoffset = 5; //these offsets are there for display purposes, to position the stars correctly
    private int yoffset = 2;


    public void mouseClicked(MouseEvent e) {
        processCoordinates(e);
        repaint();
    }

    /**
     * keep track of the last mouse position.
     *
     * @param e
     */
    private void recordCoordinates(MouseEvent e) {
        lastX = e.getX();
        lastY = e.getY();
    }

    /**
     * Before recording coordinates, we have to make sure that they fall in the right range.
     *
     * @param e
     * @return
     */
    private boolean validateCoordinates(MouseEvent e) {
        Point location = getLocation();
        Dimension size = getSize();
        if (e.getX() < location.getX() || e.getX() > location.getX() + size.getWidth()) {
            return false;
        }
        if (e.getY() < location.getY() || e.getY() > location.getY() + size.getHeight()) {
            return false;
        }
        return true;
    }

    public void mousePressed(MouseEvent e) {
        processCoordinates(e);
        repaint();
    }


    private void processCoordinates(MouseEvent e) {
        if (e != null) {
            if (validateCoordinates(e)) {
                recordCoordinates(e);
            }
        }
    }

    public void mouseReleased(MouseEvent e) {
        processCoordinates(e);
        repaint();
    }

    public void mouseEntered(MouseEvent e) {

    }

    public void mouseExited(MouseEvent e) {

    }

    /**
     * Calculate the level between 0-5. This correlates to the star rating system.
     *
     * @return
     */
    public int getLevel() {
        if (lastX == Integer.MIN_VALUE || lastY == Integer.MIN_VALUE) {
            return 0;
        }

        int min = (int) getLocation().getX();
        int max = min + (int) getSize().getWidth();
        //essentially, calculate a percentile for what section of this component the mouse
        //is positioned within.
        return (int) Math.ceil(((double) (lastX - min) / (max - min)) * 5);
    }

    @Override
    protected void paintComponent(Graphics g) {

        Graphics2D g2 = ((Graphics2D) g);
        Image star = new ImageIcon(ITunesRatingTableCellRenderer.class.getResource(
                "/com/explodingpixels/macwidgets/images/itunes_star_unselected.png")).getImage();

        int level = getLevel();

        g2.clearRect(0, 0, getWidth(), getHeight());
        for (int i = 0; i < level; i++) {
            g2.drawImage(star, (i * star.getWidth(null)) + xoffset, yoffset, null, null);
        }
    }

    public void mouseDragged(MouseEvent e) {
        processCoordinates(e);
        repaint();
    }

    public void mouseMoved(MouseEvent e) {
        //To change body of implemented methods use File | Settings | File Templates.
    }
}