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

Last change on this file since 12654 was 11553, checked in by Don-vip, 7 years ago

refactor handling of null values - use Java 8 Optional where possible

  • 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;
6import java.util.Optional;
7
8import javax.swing.Action;
9import javax.swing.Icon;
10import javax.swing.JToggleButton;
11
12import org.openstreetmap.josm.Main;
13import org.openstreetmap.josm.actions.ExpertToggleAction;
14import org.openstreetmap.josm.actions.ExpertToggleAction.ExpertModeChangeListener;
15import org.openstreetmap.josm.tools.CheckParameterUtil;
16import org.openstreetmap.josm.tools.Destroyable;
17
18/**
19 * Just a toggle button, with smaller border and icon only to display in
20 * MapFrame toolbars.
21 * Also provides methods for storing hidden state in preferences
22 * @author imi, akks
23 */
24public class IconToggleButton extends JToggleButton implements HideableButton, PropertyChangeListener, Destroyable, ExpertModeChangeListener {
25
26 private transient ShowHideButtonListener listener;
27 private boolean hideIfDisabled;
28 private boolean isExpert;
29
30 /**
31 * Construct the toggle button with the given action.
32 * @param action associated action
33 */
34 public IconToggleButton(Action action) {
35 this(action, false);
36 }
37
38 /**
39 * Construct the toggle button with the given action.
40 * @param action associated action
41 * @param isExpert {@code true} if it's reserved to expert mode
42 */
43 public IconToggleButton(Action action, boolean isExpert) {
44 super(action);
45 CheckParameterUtil.ensureParameterNotNull(action, "action");
46 this.isExpert = isExpert;
47 setText(null);
48
49 Object o = action.getValue(Action.SHORT_DESCRIPTION);
50 if (o != null) {
51 setToolTipText(o.toString());
52 }
53
54 action.addPropertyChangeListener(this);
55
56 ExpertToggleAction.addExpertModeChangeListener(this);
57 }
58
59 @Override
60 public void propertyChange(PropertyChangeEvent evt) {
61 if ("active".equals(evt.getPropertyName())) {
62 setSelected((Boolean) evt.getNewValue());
63 requestFocusInWindow();
64 } else if ("selected".equals(evt.getPropertyName())) {
65 setSelected((Boolean) evt.getNewValue());
66 }
67 }
68
69 @Override
70 public void destroy() {
71 Action action = getAction();
72 if (action instanceof Destroyable) {
73 ((Destroyable) action).destroy();
74 }
75 if (action != null) {
76 action.removePropertyChangeListener(this);
77 }
78 }
79
80 String getPreferenceKey() {
81 String s = (String) getSafeActionValue("toolbar");
82 if (s == null && getAction() != null) {
83 s = getAction().getClass().getName();
84 }
85 return "sidetoolbar.hidden."+s;
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 return (Icon) Optional.ofNullable(getSafeActionValue(Action.LARGE_ICON_KEY)).orElseGet(() -> getSafeActionValue(Action.SMALL_ICON));
156 }
157
158 @Override
159 public boolean isButtonVisible() {
160 return isVisible();
161 }
162
163 @Override
164 public void setShowHideButtonListener(ShowHideButtonListener l) {
165 listener = l;
166 }
167
168 protected final Object getSafeActionValue(String key) {
169 // Mac OS X Aqua L&F can call accessors from constructor, so getAction() can be null in those cases
170 return getAction() != null ? getAction().getValue(key) : null;
171 }
172}
Note: See TracBrowser for help on using the repository browser.