2007-12-23 14:28:04 -05:00
|
|
|
|
|
|
|
package net.sourceforge.tuned.ui;
|
|
|
|
|
|
|
|
|
|
|
|
import java.awt.Color;
|
|
|
|
import java.awt.Component;
|
|
|
|
import java.awt.Dimension;
|
|
|
|
import java.awt.Font;
|
|
|
|
import java.awt.Graphics;
|
|
|
|
import java.awt.Graphics2D;
|
|
|
|
import java.awt.Insets;
|
|
|
|
import java.awt.event.ActionEvent;
|
|
|
|
import java.awt.event.ActionListener;
|
|
|
|
import java.awt.event.MouseEvent;
|
|
|
|
import java.awt.geom.GeneralPath;
|
|
|
|
import java.awt.geom.Path2D;
|
2008-02-28 20:16:52 -05:00
|
|
|
import java.util.ArrayList;
|
2007-12-23 14:28:04 -05:00
|
|
|
import java.util.List;
|
|
|
|
|
2008-06-21 15:24:18 -04:00
|
|
|
import javax.swing.DefaultSingleSelectionModel;
|
2007-12-23 14:28:04 -05:00
|
|
|
import javax.swing.Icon;
|
|
|
|
import javax.swing.JButton;
|
|
|
|
import javax.swing.JMenuItem;
|
|
|
|
import javax.swing.JPopupMenu;
|
2008-06-21 15:24:18 -04:00
|
|
|
import javax.swing.SingleSelectionModel;
|
2007-12-23 14:28:04 -05:00
|
|
|
import javax.swing.SwingConstants;
|
|
|
|
|
|
|
|
|
2008-06-21 15:24:18 -04:00
|
|
|
public class SelectButton<T> extends JButton {
|
2007-12-23 14:28:04 -05:00
|
|
|
|
2008-06-21 15:24:18 -04:00
|
|
|
public static final String SELECTED_VALUE = "selected value";
|
2007-12-23 14:28:04 -05:00
|
|
|
|
2008-06-21 15:24:18 -04:00
|
|
|
private final Color beginColor = new Color(0xF0EEE4);
|
|
|
|
private final Color endColor = new Color(0xE0DED4);
|
2007-12-23 14:28:04 -05:00
|
|
|
|
2008-06-21 15:24:18 -04:00
|
|
|
private final Color beginColorHover = beginColor;
|
|
|
|
private final Color endColorHover = new Color(0xD8D7CD);
|
2007-12-23 14:28:04 -05:00
|
|
|
|
2008-06-21 15:24:18 -04:00
|
|
|
private final SelectIcon selectIcon = new SelectIcon();
|
2007-12-23 14:28:04 -05:00
|
|
|
|
2008-06-21 15:24:18 -04:00
|
|
|
private List<T> model = new ArrayList<T>(0);
|
|
|
|
private SingleSelectionModel selectionModel = new DefaultSingleSelectionModel();
|
2007-12-23 14:28:04 -05:00
|
|
|
|
2008-07-13 13:59:05 -04:00
|
|
|
private LabelProvider<T> labelProvider = new NullLabelProvider<T>();
|
2007-12-23 14:28:04 -05:00
|
|
|
|
2008-06-21 15:24:18 -04:00
|
|
|
private boolean hover = false;
|
2007-12-23 14:28:04 -05:00
|
|
|
|
2008-06-21 15:24:18 -04:00
|
|
|
|
|
|
|
public SelectButton() {
|
2007-12-23 14:28:04 -05:00
|
|
|
setContentAreaFilled(false);
|
|
|
|
setFocusable(false);
|
|
|
|
|
2008-06-21 15:24:18 -04:00
|
|
|
super.setIcon(selectIcon);
|
2007-12-23 14:28:04 -05:00
|
|
|
|
|
|
|
setHorizontalAlignment(SwingConstants.CENTER);
|
|
|
|
setVerticalAlignment(SwingConstants.CENTER);
|
|
|
|
|
|
|
|
setPreferredSize(new Dimension(32, 22));
|
2008-02-28 20:16:52 -05:00
|
|
|
|
2008-06-21 15:24:18 -04:00
|
|
|
addActionListener(new OpenPopupOnClick());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void setModel(List<T> model) {
|
2008-07-13 13:59:05 -04:00
|
|
|
this.model.clear();
|
|
|
|
this.model.addAll(model);
|
|
|
|
|
2008-02-28 20:16:52 -05:00
|
|
|
setSelectedIndex(0);
|
2007-12-23 14:28:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-07-13 13:59:05 -04:00
|
|
|
public LabelProvider<T> getLabelProvider() {
|
|
|
|
return labelProvider;
|
2008-06-21 15:24:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-07-13 13:59:05 -04:00
|
|
|
public void setLabelProvider(LabelProvider<T> labelProvider) {
|
|
|
|
this.labelProvider = labelProvider;
|
2007-12-23 14:28:04 -05:00
|
|
|
|
2008-06-21 15:24:18 -04:00
|
|
|
// update icon
|
2008-07-13 13:59:05 -04:00
|
|
|
this.setIcon(labelProvider.getIcon(getSelectedValue()));
|
2008-02-28 20:16:52 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-06-21 15:24:18 -04:00
|
|
|
@Override
|
|
|
|
public void setIcon(Icon icon) {
|
|
|
|
selectIcon.setInnerIcon(icon);
|
|
|
|
repaint();
|
2008-02-28 20:16:52 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-06-21 15:24:18 -04:00
|
|
|
public void setSelectedValue(T value) {
|
|
|
|
setSelectedIndex(model.indexOf(value));
|
2008-02-28 20:16:52 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-06-21 15:24:18 -04:00
|
|
|
public T getSelectedValue() {
|
|
|
|
if (!selectionModel.isSelected())
|
|
|
|
return null;
|
|
|
|
|
|
|
|
return model.get(selectionModel.getSelectedIndex());
|
2008-02-28 20:16:52 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-06-21 15:24:18 -04:00
|
|
|
public void setSelectedIndex(int i) {
|
|
|
|
if (i < 0 || i >= model.size()) {
|
|
|
|
selectionModel.clearSelection();
|
|
|
|
setIcon(null);
|
|
|
|
return;
|
2008-02-28 20:16:52 -05:00
|
|
|
}
|
2007-12-23 14:28:04 -05:00
|
|
|
|
2008-06-21 15:24:18 -04:00
|
|
|
if (i != selectionModel.getSelectedIndex()) {
|
|
|
|
selectionModel.setSelectedIndex(i);
|
|
|
|
|
|
|
|
T value = model.get(i);
|
|
|
|
|
2008-07-13 13:59:05 -04:00
|
|
|
setIcon(labelProvider.getIcon(value));
|
2008-06-21 15:24:18 -04:00
|
|
|
|
|
|
|
firePropertyChange(SELECTED_VALUE, null, value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public int getSelectedIndex() {
|
|
|
|
return selectionModel.getSelectedIndex();
|
2007-12-23 14:28:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-02-28 20:16:52 -05:00
|
|
|
public void spinValue(int spin) {
|
2008-06-21 15:24:18 -04:00
|
|
|
int size = model.size();
|
|
|
|
|
|
|
|
spin = spin % size;
|
2008-02-28 20:16:52 -05:00
|
|
|
|
|
|
|
int next = getSelectedIndex() + spin;
|
|
|
|
|
|
|
|
if (next < 0)
|
2008-06-21 15:24:18 -04:00
|
|
|
next += size;
|
|
|
|
else if (next >= size)
|
|
|
|
next -= size;
|
2008-02-28 20:16:52 -05:00
|
|
|
|
|
|
|
setSelectedIndex(next);
|
2007-12-23 14:28:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void paintComponent(Graphics g) {
|
|
|
|
Graphics2D g2d = (Graphics2D) g;
|
|
|
|
|
|
|
|
if (hover)
|
|
|
|
g2d.setPaint(GradientStyle.TOP_TO_BOTTOM.getGradientPaint(getBounds(), beginColorHover, endColorHover));
|
|
|
|
else
|
|
|
|
g2d.setPaint(GradientStyle.TOP_TO_BOTTOM.getGradientPaint(getBounds(), beginColor, endColor));
|
|
|
|
|
|
|
|
g2d.fill(getBounds());
|
|
|
|
|
|
|
|
super.paintComponent(g);
|
|
|
|
}
|
|
|
|
|
2008-06-21 15:24:18 -04:00
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void processMouseEvent(MouseEvent e) {
|
|
|
|
switch (e.getID()) {
|
|
|
|
case MouseEvent.MOUSE_ENTERED:
|
|
|
|
hover = true;
|
|
|
|
repaint();
|
|
|
|
break;
|
|
|
|
case MouseEvent.MOUSE_EXITED:
|
|
|
|
hover = false;
|
|
|
|
repaint();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
super.processMouseEvent(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private class OpenPopupOnClick implements ActionListener {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void actionPerformed(ActionEvent e) {
|
|
|
|
JPopupMenu popup = new JPopupMenu();
|
|
|
|
|
|
|
|
for (T value : model) {
|
2008-07-13 13:59:05 -04:00
|
|
|
SelectPopupMenuItem item = new SelectPopupMenuItem(labelProvider.getText(value), labelProvider.getIcon(value), value);
|
2008-06-21 15:24:18 -04:00
|
|
|
|
|
|
|
if (value == getSelectedValue())
|
|
|
|
item.setSelected(true);
|
|
|
|
|
|
|
|
popup.add(item);
|
|
|
|
}
|
|
|
|
|
|
|
|
popup.show(SelectButton.this, -4, getHeight() - 5);
|
|
|
|
}
|
|
|
|
}
|
2007-12-23 14:28:04 -05:00
|
|
|
|
2008-06-21 15:24:18 -04:00
|
|
|
|
|
|
|
private class SelectPopupMenuItem extends JMenuItem implements ActionListener {
|
2007-12-23 14:28:04 -05:00
|
|
|
|
2008-06-21 15:24:18 -04:00
|
|
|
private final T value;
|
2007-12-23 14:28:04 -05:00
|
|
|
|
|
|
|
|
2008-07-13 13:59:05 -04:00
|
|
|
public SelectPopupMenuItem(String text, Icon icon, T value) {
|
|
|
|
super(text, icon);
|
2008-02-28 20:16:52 -05:00
|
|
|
|
2008-06-21 15:24:18 -04:00
|
|
|
this.value = value;
|
2007-12-23 14:28:04 -05:00
|
|
|
|
2008-06-21 15:24:18 -04:00
|
|
|
setMargin(new Insets(3, 0, 3, 0));
|
|
|
|
addActionListener(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void setSelected(boolean selected) {
|
|
|
|
setFont(getFont().deriveFont(selected ? Font.BOLD : Font.PLAIN));
|
2007-12-23 14:28:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-02-28 20:16:52 -05:00
|
|
|
@Override
|
2007-12-23 14:28:04 -05:00
|
|
|
public void actionPerformed(ActionEvent e) {
|
|
|
|
setSelectedValue(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private static class SelectIcon implements Icon {
|
|
|
|
|
2008-06-21 15:24:18 -04:00
|
|
|
private final GeneralPath arrow;
|
|
|
|
|
2007-12-23 14:28:04 -05:00
|
|
|
private Icon icon;
|
|
|
|
|
|
|
|
|
2008-06-21 15:24:18 -04:00
|
|
|
public SelectIcon() {
|
2007-12-23 14:28:04 -05:00
|
|
|
arrow = new GeneralPath(Path2D.WIND_EVEN_ODD, 3);
|
|
|
|
int x = 25;
|
|
|
|
int y = 10;
|
|
|
|
|
|
|
|
arrow.moveTo(x - 2, y);
|
|
|
|
arrow.lineTo(x, y + 3);
|
|
|
|
arrow.lineTo(x + 3, y);
|
|
|
|
arrow.lineTo(x - 2, y);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-06-21 15:24:18 -04:00
|
|
|
public void setInnerIcon(Icon icon) {
|
|
|
|
this.icon = icon;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void paintIcon(Component c, Graphics g, int x, int y) {
|
2007-12-23 14:28:04 -05:00
|
|
|
Graphics2D g2d = (Graphics2D) g;
|
|
|
|
|
2008-06-21 15:24:18 -04:00
|
|
|
if (icon != null) {
|
|
|
|
icon.paintIcon(c, g2d, 4, 3);
|
|
|
|
}
|
2007-12-23 14:28:04 -05:00
|
|
|
|
|
|
|
g2d.setPaint(Color.BLACK);
|
|
|
|
g2d.fill(arrow);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public int getIconWidth() {
|
|
|
|
return 30;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public int getIconHeight() {
|
|
|
|
return 20;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|