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 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
|
package com.explodingpixels.macwidgets;
import com.explodingpixels.data.Rating;
import com.explodingpixels.data.RatingChangeListener;
import com.jgoodies.forms.builder.PanelBuilder;
import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.List;
public class RatingComponent {
private JPanel fComponent = new JPanel();
private Rating fRating;
private boolean fSelected;
private boolean fFocused = true;
private List<JLabel> fRatingIndicators = new ArrayList<JLabel>();
private List<RatingChangeListener> fListeners =
new ArrayList<RatingChangeListener>();
private static ImageIcon FOCUSED_SELECTED_STAR =
new ImageIcon(ITunesRatingTableCellRenderer.class.getResource(
"/com/explodingpixels/macwidgets/images/itunes_star_focused_selected.png"));
private static ImageIcon UNFOCUSED_SELECTED_STAR =
new ImageIcon(ITunesRatingTableCellRenderer.class.getResource(
"/com/explodingpixels/macwidgets/images/itunes_star_unfocused_selected.png"));
private static ImageIcon UNSELECTED_STAR =
new ImageIcon(ITunesRatingTableCellRenderer.class.getResource(
"/com/explodingpixels/macwidgets/images/itunes_star_unselected.png"));
private static ImageIcon UNFOCUSED_DOT =
new ImageIcon(ITunesRatingTableCellRenderer.class.getResource(
"/com/explodingpixels/macwidgets/images/itunes_dot_unfocused.png"));
private static ImageIcon FOCUSED_DOT =
new ImageIcon(ITunesRatingTableCellRenderer.class.getResource(
"/com/explodingpixels/macwidgets/images/itunes_dot_focused.png"));
public RatingComponent(Rating rating) {
setRating(rating);
fComponent.setOpaque(false);
}
private void buildRatingPanel() {
fRatingIndicators.clear();
fComponent.removeAll();
// definte the FormLayout columns and rows.
FormLayout layout = new FormLayout("", "fill:p:grow");
PanelBuilder builder = new PanelBuilder(layout, fComponent);
for (int i = 0; i < Rating.values().length - 1; i++) {
RatingLabel label = new RatingLabel(i);
fRatingIndicators.add(label);
builder.appendColumn("p");
builder.add(label, new CellConstraints().xy(builder.getColumn(), 1));
builder.nextColumn();
}
}
private MouseListener createMouseListener() {
return new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
// TODO do something;
}
};
}
public JComponent getComponent() {
return fComponent;
}
public void setSelected(boolean selected) {
fSelected = selected;
}
public void setFocused(boolean focused) {
fFocused = focused;
}
public void setRating(Rating rating) {
fRating = rating;
buildRatingPanel();
}
// Custom label implementation. ///////////////////////////////////////////////////////////////
/**
* Custom class to return icon for rating indicator based on its position in the larger
* component and the current rating.
*/
private class RatingLabel extends JLabel {
private int fPosition;
private RatingLabel(int position) {
fPosition = position;
}
@Override
public Icon getIcon() {
Icon retVal = null;
if (fRating == null || fRating == Rating.NO_RATING) {
retVal = null;
} else if (fPosition < fRating.ordinal()) {
retVal = getStarIcon();
} else if (fSelected) {
retVal = getDotIcon();
}
return retVal;
}
private Icon getStarIcon() {
Icon retVal;
if (!fSelected) {
retVal = UNSELECTED_STAR;
} else if (fFocused) {
retVal = FOCUSED_SELECTED_STAR;
} else {
retVal = UNFOCUSED_SELECTED_STAR;
}
return retVal;
}
private Icon getDotIcon() {
Icon retVal = null;
if (fFocused) {
retVal = FOCUSED_DOT;
} else {
retVal = UNFOCUSED_DOT;
}
return retVal;
}
}
// Rating change listener support. ////////////////////////////////////////////////////////////
public void addRatingChangeListener(RatingChangeListener listener) {
fListeners.add(listener);
}
public void removeRatingChangeListener(RatingChangeListener listener) {
fListeners.remove(listener);
}
}
|