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