2007-12-23 19:28:04 +00: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-29 01:16:52 +00:00
|
|
|
import java.util.ArrayList;
|
2009-06-02 10:27:28 +00:00
|
|
|
import java.util.Collection;
|
|
|
|
import java.util.Collections;
|
2007-12-23 19:28:04 +00:00
|
|
|
import java.util.List;
|
|
|
|
|
2009-01-01 22:27:53 +00:00
|
|
|
import javax.swing.BorderFactory;
|
2008-06-21 19:24:18 +00:00
|
|
|
import javax.swing.DefaultSingleSelectionModel;
|
2007-12-23 19:28:04 +00:00
|
|
|
import javax.swing.Icon;
|
|
|
|
import javax.swing.JButton;
|
|
|
|
import javax.swing.JMenuItem;
|
|
|
|
import javax.swing.JPopupMenu;
|
2008-06-21 19:24:18 +00:00
|
|
|
import javax.swing.SingleSelectionModel;
|
2007-12-23 19:28:04 +00:00
|
|
|
import javax.swing.SwingConstants;
|
|
|
|
|
|
|
|
|
2008-06-21 19:24:18 +00:00
|
|
|
public class SelectButton<T> extends JButton {
|
2007-12-23 19:28:04 +00:00
|
|
|
|
2008-06-21 19:24:18 +00:00
|
|
|
public static final String SELECTED_VALUE = "selected value";
|
2007-12-23 19:28:04 +00:00
|
|
|
|
2008-06-21 19:24:18 +00:00
|
|
|
private final Color beginColor = new Color(0xF0EEE4);
|
|
|
|
private final Color endColor = new Color(0xE0DED4);
|
2007-12-23 19:28:04 +00:00
|
|
|
|
2008-06-21 19:24:18 +00:00
|
|
|
private final Color beginColorHover = beginColor;
|
|
|
|
private final Color endColorHover = new Color(0xD8D7CD);
|
2007-12-23 19:28:04 +00:00
|
|
|
|
2008-06-21 19:24:18 +00:00
|
|
|
private final SelectIcon selectIcon = new SelectIcon();
|
2007-12-23 19:28:04 +00:00
|
|
|
|
2009-06-02 10:27:28 +00:00
|
|
|
private List<T> model = Collections.emptyList();
|
2008-06-21 19:24:18 +00:00
|
|
|
private SingleSelectionModel selectionModel = new DefaultSingleSelectionModel();
|
2007-12-23 19:28:04 +00:00
|
|
|
|
2008-07-13 17:59:05 +00:00
|
|
|
private LabelProvider<T> labelProvider = new NullLabelProvider<T>();
|
2007-12-23 19:28:04 +00:00
|
|
|
|
2008-06-21 19:24:18 +00:00
|
|
|
private boolean hover = false;
|
2007-12-23 19:28:04 +00:00
|
|
|
|
2009-06-02 10:27:28 +00:00
|
|
|
|
2008-06-21 19:24:18 +00:00
|
|
|
public SelectButton() {
|
2007-12-23 19:28:04 +00:00
|
|
|
setContentAreaFilled(false);
|
|
|
|
setFocusable(false);
|
|
|
|
|
2008-06-21 19:24:18 +00:00
|
|
|
super.setIcon(selectIcon);
|
2007-12-23 19:28:04 +00:00
|
|
|
|
|
|
|
setHorizontalAlignment(SwingConstants.CENTER);
|
|
|
|
setVerticalAlignment(SwingConstants.CENTER);
|
|
|
|
|
2009-01-01 22:27:53 +00:00
|
|
|
setBorder(BorderFactory.createLineBorder(new Color(0xA4A4A4), 1));
|
2007-12-23 19:28:04 +00:00
|
|
|
setPreferredSize(new Dimension(32, 22));
|
2008-02-29 01:16:52 +00:00
|
|
|
|
2008-06-21 19:24:18 +00:00
|
|
|
addActionListener(new OpenPopupOnClick());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-06-02 10:27:28 +00:00
|
|
|
public void setModel(Collection<T> model) {
|
|
|
|
this.model = new ArrayList<T>(model);
|
2008-07-13 17:59:05 +00:00
|
|
|
|
2008-02-29 01:16:52 +00:00
|
|
|
setSelectedIndex(0);
|
2007-12-23 19:28:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-07-13 17:59:05 +00:00
|
|
|
public LabelProvider<T> getLabelProvider() {
|
|
|
|
return labelProvider;
|
2008-06-21 19:24:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-07-13 17:59:05 +00:00
|
|
|
public void setLabelProvider(LabelProvider<T> labelProvider) {
|
|
|
|
this.labelProvider = labelProvider;
|
2007-12-23 19:28:04 +00:00
|
|
|
|
2008-06-21 19:24:18 +00:00
|
|
|
// update icon
|
2008-07-13 17:59:05 +00:00
|
|
|
this.setIcon(labelProvider.getIcon(getSelectedValue()));
|
2008-02-29 01:16:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-06-21 19:24:18 +00:00
|
|
|
@Override
|
|
|
|
public void setIcon(Icon icon) {
|
|
|
|
selectIcon.setInnerIcon(icon);
|
|
|
|
repaint();
|
2008-02-29 01:16:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-06-21 19:24:18 +00:00
|
|
|
public void setSelectedValue(T value) {
|
|
|
|
setSelectedIndex(model.indexOf(value));
|
2008-02-29 01:16:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-06-21 19:24:18 +00:00
|
|
|
public T getSelectedValue() {
|
|
|
|
if (!selectionModel.isSelected())
|
|
|
|
return null;
|
|
|
|
|
|
|
|
return model.get(selectionModel.getSelectedIndex());
|
2008-02-29 01:16:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-06-21 19:24:18 +00:00
|
|
|
public void setSelectedIndex(int i) {
|
|
|
|
if (i < 0 || i >= model.size()) {
|
|
|
|
selectionModel.clearSelection();
|
|
|
|
setIcon(null);
|
|
|
|
return;
|
2008-02-29 01:16:52 +00:00
|
|
|
}
|
2007-12-23 19:28:04 +00:00
|
|
|
|
2008-06-21 19:24:18 +00:00
|
|
|
if (i != selectionModel.getSelectedIndex()) {
|
|
|
|
selectionModel.setSelectedIndex(i);
|
|
|
|
|
|
|
|
T value = model.get(i);
|
|
|
|
|
2008-07-13 17:59:05 +00:00
|
|
|
setIcon(labelProvider.getIcon(value));
|
2008-06-21 19:24:18 +00:00
|
|
|
|
|
|
|
firePropertyChange(SELECTED_VALUE, null, value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public int getSelectedIndex() {
|
|
|
|
return selectionModel.getSelectedIndex();
|
2007-12-23 19:28:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-14 16:02:27 +00:00
|
|
|
public SingleSelectionModel getSelectionModel() {
|
|
|
|
return selectionModel;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-02-29 01:16:52 +00:00
|
|
|
public void spinValue(int spin) {
|
2008-06-21 19:24:18 +00:00
|
|
|
int size = model.size();
|
|
|
|
|
|
|
|
spin = spin % size;
|
2008-02-29 01:16:52 +00:00
|
|
|
|
|
|
|
int next = getSelectedIndex() + spin;
|
|
|
|
|
|
|
|
if (next < 0)
|
2008-06-21 19:24:18 +00:00
|
|
|
next += size;
|
|
|
|
else if (next >= size)
|
|
|
|
next -= size;
|
2008-02-29 01:16:52 +00:00
|
|
|
|
|
|
|
setSelectedIndex(next);
|
2007-12-23 19:28:04 +00: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 19:24:18 +00: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);
|
|
|
|
}
|
|
|
|
|
2009-06-02 10:27:28 +00:00
|
|
|
|
2008-06-21 19:24:18 +00:00
|
|
|
private class OpenPopupOnClick implements ActionListener {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void actionPerformed(ActionEvent e) {
|
|
|
|
JPopupMenu popup = new JPopupMenu();
|
|
|
|
|
|
|
|
for (T value : model) {
|
2008-07-13 17:59:05 +00:00
|
|
|
SelectPopupMenuItem item = new SelectPopupMenuItem(labelProvider.getText(value), labelProvider.getIcon(value), value);
|
2008-06-21 19:24:18 +00:00
|
|
|
|
|
|
|
if (value == getSelectedValue())
|
|
|
|
item.setSelected(true);
|
|
|
|
|
|
|
|
popup.add(item);
|
|
|
|
}
|
|
|
|
|
2009-01-01 22:27:53 +00:00
|
|
|
popup.show(SelectButton.this, 0, getHeight() - 1);
|
2008-06-21 19:24:18 +00:00
|
|
|
}
|
|
|
|
}
|
2007-12-23 19:28:04 +00:00
|
|
|
|
2008-06-21 19:24:18 +00:00
|
|
|
|
|
|
|
private class SelectPopupMenuItem extends JMenuItem implements ActionListener {
|
2007-12-23 19:28:04 +00:00
|
|
|
|
2008-06-21 19:24:18 +00:00
|
|
|
private final T value;
|
2007-12-23 19:28:04 +00:00
|
|
|
|
2009-06-02 10:27:28 +00:00
|
|
|
|
2008-07-13 17:59:05 +00:00
|
|
|
public SelectPopupMenuItem(String text, Icon icon, T value) {
|
|
|
|
super(text, icon);
|
2008-02-29 01:16:52 +00:00
|
|
|
|
2008-06-21 19:24:18 +00:00
|
|
|
this.value = value;
|
2007-12-23 19:28:04 +00:00
|
|
|
|
2008-06-21 19:24:18 +00: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 19:28:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-02-29 01:16:52 +00:00
|
|
|
@Override
|
2007-12-23 19:28:04 +00:00
|
|
|
public void actionPerformed(ActionEvent e) {
|
|
|
|
setSelectedValue(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private static class SelectIcon implements Icon {
|
|
|
|
|
2008-06-21 19:24:18 +00:00
|
|
|
private final GeneralPath arrow;
|
|
|
|
|
2007-12-23 19:28:04 +00:00
|
|
|
private Icon icon;
|
|
|
|
|
2009-06-02 10:27:28 +00:00
|
|
|
|
2008-06-21 19:24:18 +00:00
|
|
|
public SelectIcon() {
|
2007-12-23 19:28:04 +00: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 19:24:18 +00:00
|
|
|
public void setInnerIcon(Icon icon) {
|
|
|
|
this.icon = icon;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void paintIcon(Component c, Graphics g, int x, int y) {
|
2007-12-23 19:28:04 +00:00
|
|
|
Graphics2D g2d = (Graphics2D) g;
|
|
|
|
|
2008-06-21 19:24:18 +00:00
|
|
|
if (icon != null) {
|
|
|
|
icon.paintIcon(c, g2d, 4, 3);
|
|
|
|
}
|
2007-12-23 19:28:04 +00:00
|
|
|
|
|
|
|
g2d.setPaint(Color.BLACK);
|
|
|
|
g2d.fill(arrow);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public int getIconWidth() {
|
|
|
|
return 30;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public int getIconHeight() {
|
|
|
|
return 20;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|