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

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

javadoc update

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