1 | package org.openstreetmap.josm.gui;
|
---|
2 |
|
---|
3 | import java.beans.PropertyChangeEvent;
|
---|
4 | import java.beans.PropertyChangeListener;
|
---|
5 |
|
---|
6 | import javax.swing.Action;
|
---|
7 | import javax.swing.JComponent;
|
---|
8 | import javax.swing.JToggleButton;
|
---|
9 |
|
---|
10 | /**
|
---|
11 | * Just a toggle button, with smaller border and icon only to display in
|
---|
12 | * MapFrame toolbars.
|
---|
13 | *
|
---|
14 | * @author imi
|
---|
15 | */
|
---|
16 | public class IconToggleButton extends JToggleButton implements PropertyChangeListener {
|
---|
17 |
|
---|
18 | /**
|
---|
19 | * Construct the toggle button with the given action.
|
---|
20 | */
|
---|
21 | public IconToggleButton(JComponent acceleratorReceiver, Action action) {
|
---|
22 | super(action);
|
---|
23 | setText(null);
|
---|
24 |
|
---|
25 | // Tooltip
|
---|
26 | String toolTipText = "";
|
---|
27 | Object o = action.getValue(Action.LONG_DESCRIPTION);
|
---|
28 | if (o != null)
|
---|
29 | toolTipText += o.toString();
|
---|
30 | o = action.getValue(Action.ACCELERATOR_KEY);
|
---|
31 | if (o != null) {
|
---|
32 | String ksName = o.toString();
|
---|
33 | if (ksName.startsWith("pressed "))
|
---|
34 | ksName = ksName.substring("pressed ".length());
|
---|
35 | else if (ksName.startsWith("released "))
|
---|
36 | ksName = ksName.substring("released ".length());
|
---|
37 | toolTipText += " Shortcut: "+ksName;
|
---|
38 | }
|
---|
39 | setToolTipText(toolTipText);
|
---|
40 |
|
---|
41 | action.addPropertyChangeListener(this);
|
---|
42 | }
|
---|
43 |
|
---|
44 | public void propertyChange(PropertyChangeEvent evt) {
|
---|
45 | if (evt.getPropertyName().equals("active"))
|
---|
46 | setSelected((Boolean)evt.getNewValue());
|
---|
47 | }
|
---|
48 | }
|
---|