1 | package org.openstreetmap.josm.actions;
|
---|
2 |
|
---|
3 | import java.awt.event.ActionEvent;
|
---|
4 | import java.awt.event.ActionListener;
|
---|
5 | import java.awt.event.KeyEvent;
|
---|
6 | import java.beans.PropertyChangeEvent;
|
---|
7 | import java.beans.PropertyChangeListener;
|
---|
8 | import java.util.ArrayList;
|
---|
9 | import java.util.List;
|
---|
10 |
|
---|
11 | import javax.swing.AbstractAction;
|
---|
12 | import javax.swing.Action;
|
---|
13 | import javax.swing.Icon;
|
---|
14 | import javax.swing.JComponent;
|
---|
15 | import javax.swing.JMenuItem;
|
---|
16 | import javax.swing.JPopupMenu;
|
---|
17 | import javax.swing.KeyStroke;
|
---|
18 |
|
---|
19 | import org.openstreetmap.josm.Main;
|
---|
20 | import org.openstreetmap.josm.gui.IconToggleButton;
|
---|
21 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
22 | import org.openstreetmap.josm.tools.ShortCutLabel;
|
---|
23 | import org.openstreetmap.josm.tools.ImageProvider.OverlayPosition;
|
---|
24 |
|
---|
25 |
|
---|
26 | public class GroupAction extends JosmAction {
|
---|
27 |
|
---|
28 | protected final List<Action> actions = new ArrayList<Action>();
|
---|
29 | private int current = -1;
|
---|
30 | private String shortCutName = "";
|
---|
31 |
|
---|
32 | private PropertyChangeListener forwardActiveListener = new PropertyChangeListener(){
|
---|
33 | public void propertyChange(PropertyChangeEvent evt) {
|
---|
34 | if (evt.getPropertyName().equals("active"))
|
---|
35 | putValue("active", evt.getNewValue());
|
---|
36 | }
|
---|
37 | };
|
---|
38 |
|
---|
39 | protected void setCurrent(int current) {
|
---|
40 | if (this.current != -1)
|
---|
41 | actions.get(this.current).removePropertyChangeListener(forwardActiveListener);
|
---|
42 | actions.get(current).addPropertyChangeListener(forwardActiveListener);
|
---|
43 |
|
---|
44 | this.current = current;
|
---|
45 | putValue(SMALL_ICON, ImageProvider.overlay((Icon)actions.get(current).getValue(SMALL_ICON), "overlay/right", OverlayPosition.SOUTHEAST));
|
---|
46 | Object tooltip = actions.get(current).getValue(SHORT_DESCRIPTION);
|
---|
47 | putValue(SHORT_DESCRIPTION, "<html>"+tooltip+" <font size='-2'>"+shortCutName+"</font> </html>");
|
---|
48 | }
|
---|
49 |
|
---|
50 | public GroupAction(int shortCut, int modifiers) {
|
---|
51 | String idName = getClass().getName();
|
---|
52 | Main.contentPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(shortCut, modifiers), idName);
|
---|
53 | Main.contentPane.getActionMap().put(idName, this);
|
---|
54 | shortCutName = ShortCutLabel.name(shortCut, modifiers);
|
---|
55 | Main.contentPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(shortCut, KeyEvent.SHIFT_DOWN_MASK), idName+".cycle");
|
---|
56 | Main.contentPane.getActionMap().put(idName+".cycle", new AbstractAction(){
|
---|
57 | public void actionPerformed(ActionEvent e) {
|
---|
58 | setCurrent((current+1)%actions.size());
|
---|
59 | actions.get(current).actionPerformed(e);
|
---|
60 | }
|
---|
61 | });
|
---|
62 | }
|
---|
63 |
|
---|
64 | public void actionPerformed(ActionEvent e) {
|
---|
65 | if (e.getSource() instanceof IconToggleButton && ((IconToggleButton)e.getSource()).groupbutton) {
|
---|
66 | IconToggleButton b = (IconToggleButton)e.getSource();
|
---|
67 | b.setSelected(!b.isSelected());
|
---|
68 | openPopup(b);
|
---|
69 | } else
|
---|
70 | actions.get(current).actionPerformed(e);
|
---|
71 | }
|
---|
72 |
|
---|
73 | private void openPopup(IconToggleButton b) {
|
---|
74 | JPopupMenu popup = new JPopupMenu();
|
---|
75 | for (int i = 0; i < actions.size(); ++i) {
|
---|
76 | final int j = i;
|
---|
77 | JMenuItem item = new JMenuItem(actions.get(i));
|
---|
78 | item.addActionListener(new ActionListener(){
|
---|
79 | public void actionPerformed(ActionEvent e) {
|
---|
80 | setCurrent(j);
|
---|
81 | }
|
---|
82 | });
|
---|
83 | popup.add(item);
|
---|
84 | }
|
---|
85 | popup.show(b, b.getWidth(), 0);
|
---|
86 | }
|
---|
87 | }
|
---|