source: josm/trunk/src/org/openstreetmap/josm/gui/IconToggleButton.java@ 10008

Last change on this file since 10008 was 9795, checked in by Don-vip, 8 years ago

findbugs

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