1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui;
|
---|
3 |
|
---|
4 | import java.beans.PropertyChangeEvent;
|
---|
5 | import java.beans.PropertyChangeListener;
|
---|
6 |
|
---|
7 | import javax.swing.Action;
|
---|
8 | import javax.swing.Icon;
|
---|
9 | import javax.swing.JToggleButton;
|
---|
10 |
|
---|
11 | import org.openstreetmap.josm.Main;
|
---|
12 | import org.openstreetmap.josm.actions.ExpertToggleAction;
|
---|
13 | import org.openstreetmap.josm.actions.ExpertToggleAction.ExpertModeChangeListener;
|
---|
14 | import org.openstreetmap.josm.tools.CheckParameterUtil;
|
---|
15 | import org.openstreetmap.josm.tools.Destroyable;
|
---|
16 |
|
---|
17 | /**
|
---|
18 | * Just a toggle button, with smaller border and icon only to display in
|
---|
19 | * MapFrame toolbars.
|
---|
20 | * Also provides methods for storing hidden state in preferences
|
---|
21 | * @author imi, akks
|
---|
22 | */
|
---|
23 | public class IconToggleButton extends JToggleButton implements HideableButton, PropertyChangeListener, Destroyable, ExpertModeChangeListener {
|
---|
24 |
|
---|
25 | private transient ShowHideButtonListener listener;
|
---|
26 | private boolean hideIfDisabled;
|
---|
27 | private boolean isExpert;
|
---|
28 |
|
---|
29 | /**
|
---|
30 | * Construct the toggle button with the given action.
|
---|
31 | * @param action associated action
|
---|
32 | */
|
---|
33 | public IconToggleButton(Action action) {
|
---|
34 | this(action, false);
|
---|
35 | }
|
---|
36 |
|
---|
37 | /**
|
---|
38 | * Construct the toggle button with the given action.
|
---|
39 | * @param action associated action
|
---|
40 | * @param isExpert {@code true} if it's reserved to expert mode
|
---|
41 | */
|
---|
42 | public IconToggleButton(Action action, boolean isExpert) {
|
---|
43 | super(action);
|
---|
44 | CheckParameterUtil.ensureParameterNotNull(action, "action");
|
---|
45 | this.isExpert = isExpert;
|
---|
46 | setText(null);
|
---|
47 |
|
---|
48 | Object o = action.getValue(Action.SHORT_DESCRIPTION);
|
---|
49 | if (o != null) {
|
---|
50 | setToolTipText(o.toString());
|
---|
51 | }
|
---|
52 |
|
---|
53 | action.addPropertyChangeListener(this);
|
---|
54 |
|
---|
55 | ExpertToggleAction.addExpertModeChangeListener(this);
|
---|
56 | }
|
---|
57 |
|
---|
58 | @Override
|
---|
59 | public void propertyChange(PropertyChangeEvent evt) {
|
---|
60 | if ("active".equals(evt.getPropertyName())) {
|
---|
61 | setSelected((Boolean) evt.getNewValue());
|
---|
62 | requestFocusInWindow();
|
---|
63 | } else if ("selected".equals(evt.getPropertyName())) {
|
---|
64 | setSelected((Boolean) evt.getNewValue());
|
---|
65 | }
|
---|
66 | }
|
---|
67 |
|
---|
68 | @Override
|
---|
69 | public void destroy() {
|
---|
70 | Action action = getAction();
|
---|
71 | if (action instanceof Destroyable) {
|
---|
72 | ((Destroyable) action).destroy();
|
---|
73 | }
|
---|
74 | if (action != null) {
|
---|
75 | action.removePropertyChangeListener(this);
|
---|
76 | }
|
---|
77 | }
|
---|
78 |
|
---|
79 | String getPreferenceKey() {
|
---|
80 | String s = (String) getSafeActionValue("toolbar");
|
---|
81 | if (s == null && getAction() != null) {
|
---|
82 | s = getAction().getClass().getName();
|
---|
83 | }
|
---|
84 | return "sidetoolbar.hidden."+s;
|
---|
85 | }
|
---|
86 |
|
---|
87 | @Override
|
---|
88 | public void expertChanged(boolean isExpert) {
|
---|
89 | applyButtonHiddenPreferences();
|
---|
90 | }
|
---|
91 |
|
---|
92 | @Override
|
---|
93 | public void applyButtonHiddenPreferences() {
|
---|
94 | boolean alwaysHideDisabled = Main.pref.getBoolean("sidetoolbar.hideDisabledButtons", false);
|
---|
95 | if (!isEnabled() && (hideIfDisabled || alwaysHideDisabled)) {
|
---|
96 | setVisible(false); // hide because of disabled button
|
---|
97 | } else {
|
---|
98 | boolean hiddenFlag = false;
|
---|
99 | String hiddenFlagStr = Main.pref.get(getPreferenceKey(), null);
|
---|
100 | if (hiddenFlagStr == null) {
|
---|
101 | if (isExpert && !ExpertToggleAction.isExpert()) {
|
---|
102 | hiddenFlag = true;
|
---|
103 | }
|
---|
104 | } else {
|
---|
105 | hiddenFlag = Boolean.parseBoolean(hiddenFlagStr);
|
---|
106 | }
|
---|
107 | setVisible(!hiddenFlag); // show or hide, do what preferences say
|
---|
108 | }
|
---|
109 | }
|
---|
110 |
|
---|
111 | @Override
|
---|
112 | public void setButtonHidden(boolean b) {
|
---|
113 | setVisible(!b);
|
---|
114 | if (listener != null) { // if someone wants to know about changes of visibility
|
---|
115 | if (!b) listener.buttonShown(); else listener.buttonHidden();
|
---|
116 | }
|
---|
117 | if ((b && isExpert && !ExpertToggleAction.isExpert()) ||
|
---|
118 | (!b && isExpert && ExpertToggleAction.isExpert())) {
|
---|
119 | Main.pref.put(getPreferenceKey(), null);
|
---|
120 | } else {
|
---|
121 | Main.pref.put(getPreferenceKey(), b);
|
---|
122 | }
|
---|
123 | }
|
---|
124 |
|
---|
125 | /**
|
---|
126 | * This function should be called for plugins that want to enable auto-hiding
|
---|
127 | * custom buttons when they are disabled (because of incorrect layer, for example)
|
---|
128 | * @param b hide if disabled
|
---|
129 | */
|
---|
130 | public void setAutoHideDisabledButton(boolean b) {
|
---|
131 | hideIfDisabled = b;
|
---|
132 | if (b && !isEnabled()) {
|
---|
133 | setVisible(false);
|
---|
134 | }
|
---|
135 | }
|
---|
136 |
|
---|
137 | @Override
|
---|
138 | public void showButton() {
|
---|
139 | setButtonHidden(false);
|
---|
140 | }
|
---|
141 |
|
---|
142 | @Override
|
---|
143 | public void hideButton() {
|
---|
144 | setButtonHidden(true);
|
---|
145 | }
|
---|
146 |
|
---|
147 | @Override
|
---|
148 | public String getActionName() {
|
---|
149 | return (String) getSafeActionValue(Action.NAME);
|
---|
150 | }
|
---|
151 |
|
---|
152 | @Override
|
---|
153 | public Icon getIcon() {
|
---|
154 | Object o = getSafeActionValue(Action.LARGE_ICON_KEY);
|
---|
155 | if (o == null)
|
---|
156 | o = getSafeActionValue(Action.SMALL_ICON);
|
---|
157 | return (Icon) o;
|
---|
158 | }
|
---|
159 |
|
---|
160 | @Override
|
---|
161 | public boolean isButtonVisible() {
|
---|
162 | return isVisible();
|
---|
163 | }
|
---|
164 |
|
---|
165 | @Override
|
---|
166 | public void setShowHideButtonListener(ShowHideButtonListener l) {
|
---|
167 | listener = l;
|
---|
168 | }
|
---|
169 |
|
---|
170 | protected final Object getSafeActionValue(String key) {
|
---|
171 | // Mac OS X Aqua L&F can call accessors from constructor, so getAction() can be null in those cases
|
---|
172 | return getAction() != null ? getAction().getValue(key) : null;
|
---|
173 | }
|
---|
174 | }
|
---|