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

Last change on this file since 8907 was 8840, checked in by Don-vip, 9 years ago

sonar - squid:S3052 - Fields should not be initialized to default values

  • Property svn:eol-style set to native
File size: 5.4 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui;
3
4import java.awt.event.MouseAdapter;
5import java.awt.event.MouseEvent;
6import java.beans.PropertyChangeEvent;
7import java.beans.PropertyChangeListener;
8
9import javax.swing.Action;
10import javax.swing.Icon;
11import javax.swing.JToggleButton;
12
13import org.openstreetmap.josm.Main;
14import org.openstreetmap.josm.actions.ExpertToggleAction;
15import org.openstreetmap.josm.actions.ExpertToggleAction.ExpertModeChangeListener;
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 public boolean groupbutton;
27 private transient ShowHideButtonListener listener;
28 private boolean hideIfDisabled;
29 private boolean isExpert;
30
31 /**
32 * Construct the toggle button with the given action.
33 */
34 public IconToggleButton(Action action) {
35 this(action, false);
36 }
37
38 /**
39 * Construct the toggle button with the given action.
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 addMouseListener(new MouseAdapter() {
54 @Override public void mousePressed(MouseEvent e) {
55 groupbutton = e.getX() > getWidth()/2 && e.getY() > getHeight()/2;
56 }
57 });
58
59 ExpertToggleAction.addExpertModeChangeListener(this);
60 }
61
62 @Override
63 public void propertyChange(PropertyChangeEvent evt) {
64 if ("active".equals(evt.getPropertyName())) {
65 setSelected((Boolean) evt.getNewValue());
66 requestFocusInWindow();
67 } else if ("selected".equals(evt.getPropertyName())) {
68 setSelected((Boolean) evt.getNewValue());
69 }
70 }
71
72 @Override
73 public void destroy() {
74 Action action = getAction();
75 if (action instanceof Destroyable) {
76 ((Destroyable) action).destroy();
77 }
78 if (action != null) {
79 action.removePropertyChangeListener(this);
80 }
81 }
82
83 String getPreferenceKey() {
84 String s = (String) getSafeActionValue("toolbar");
85 if (s == null) {
86 if (getAction() != null) {
87 s = getAction().getClass().getName();
88 }
89 }
90 return "sidetoolbar.hidden."+s;
91
92 }
93
94 @Override
95 public void expertChanged(boolean isExpert) {
96 applyButtonHiddenPreferences();
97 }
98
99 @Override
100 public void applyButtonHiddenPreferences() {
101 boolean alwaysHideDisabled = Main.pref.getBoolean("sidetoolbar.hideDisabledButtons", false);
102 if (!isEnabled() && (hideIfDisabled || alwaysHideDisabled)) {
103 setVisible(false); // hide because of disabled button
104 } else {
105 boolean hiddenFlag = false;
106 String hiddenFlagStr = Main.pref.get(getPreferenceKey(), null);
107 if (hiddenFlagStr == null) {
108 if (isExpert && !ExpertToggleAction.isExpert()) {
109 hiddenFlag = true;
110 }
111 } else {
112 hiddenFlag = Boolean.parseBoolean(hiddenFlagStr);
113 }
114 setVisible(!hiddenFlag); // show or hide, do what preferences say
115 }
116 }
117
118 @Override
119 public void setButtonHidden(boolean b) {
120 setVisible(!b);
121 if (listener != null) { // if someone wants to know about changes of visibility
122 if (!b) listener.buttonShown(); else listener.buttonHidden();
123 }
124 if ((b && isExpert && !ExpertToggleAction.isExpert()) ||
125 (!b && isExpert && ExpertToggleAction.isExpert())) {
126 Main.pref.put(getPreferenceKey(), null);
127 } else {
128 Main.pref.put(getPreferenceKey(), b);
129 }
130 }
131
132 /*
133 * This fuction should be called for plugins that want to enable auto-hiding
134 * custom buttons when they are disabled (because of incorrect layer, for example)
135 */
136 public void setAutoHideDisabledButton(boolean b) {
137 hideIfDisabled = b;
138 if (b && !isEnabled()) {
139 setVisible(false);
140 }
141 }
142
143 @Override
144 public void showButton() {
145 setButtonHidden(false);
146 }
147
148 @Override
149 public void hideButton() {
150 setButtonHidden(true);
151 }
152
153 @Override
154 public String getActionName() {
155 return (String) getSafeActionValue(Action.NAME);
156 }
157
158 @Override
159 public Icon getIcon() {
160 Object o = getSafeActionValue(Action.LARGE_ICON_KEY);
161 if (o == null)
162 o = getSafeActionValue(Action.SMALL_ICON);
163 return (Icon) o;
164 }
165
166 @Override
167 public boolean isButtonVisible() {
168 return isVisible();
169 }
170
171 @Override
172 public void setShowHideButtonListener(ShowHideButtonListener l) {
173 listener = l;
174 }
175
176 protected final Object getSafeActionValue(String key) {
177 // Mac OS X Aqua L&F can call accessors from constructor, so getAction() can be null in those cases
178 return getAction() != null ? getAction().getValue(key) : null;
179 }
180}
Note: See TracBrowser for help on using the repository browser.