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

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

checkstyle: enable relevant whitespace checks and fix them

  • Property svn:eol-style set to native
File size: 5.4 KB
RevLine 
[6380]1// License: GPL. For details, see LICENSE file.
[1]2package org.openstreetmap.josm.gui;
3
[91]4import java.awt.event.MouseAdapter;
5import java.awt.event.MouseEvent;
[7]6import java.beans.PropertyChangeEvent;
7import java.beans.PropertyChangeListener;
8
[1]9import javax.swing.Action;
[4609]10import javax.swing.Icon;
[1]11import javax.swing.JToggleButton;
12
[4609]13import org.openstreetmap.josm.Main;
[4840]14import org.openstreetmap.josm.actions.ExpertToggleAction;
15import org.openstreetmap.josm.actions.ExpertToggleAction.ExpertModeChangeListener;
[2871]16import org.openstreetmap.josm.tools.Destroyable;
17
[1]18/**
19 * Just a toggle button, with smaller border and icon only to display in
20 * MapFrame toolbars.
[4609]21 * Also provides methods for storing hidden state in preferences
22 * @author imi, akks
[1]23 */
[4840]24public class IconToggleButton extends JToggleButton implements HideableButton, PropertyChangeListener, Destroyable, ExpertModeChangeListener {
[1]25
[1169]26 public boolean groupbutton;
[8308]27 private transient ShowHideButtonListener listener;
[4840]28 private boolean hideIfDisabled = false;
29 private boolean isExpert;
[91]30
[1169]31 /**
32 * Construct the toggle button with the given action.
33 */
34 public IconToggleButton(Action action) {
[4835]35 this(action, false);
36 }
37
38 /**
39 * Construct the toggle button with the given action.
40 */
[4840]41 public IconToggleButton(Action action, boolean isExpert) {
[1169]42 super(action);
[4840]43 this.isExpert = isExpert;
[1169]44 setText(null);
[80]45
[1169]46 Object o = action.getValue(Action.SHORT_DESCRIPTION);
[2005]47 if (o != null) {
[1169]48 setToolTipText(o.toString());
[2005]49 }
[91]50
[1169]51 action.addPropertyChangeListener(this);
52
[8510]53 addMouseListener(new MouseAdapter() {
[1169]54 @Override public void mousePressed(MouseEvent e) {
55 groupbutton = e.getX() > getWidth()/2 && e.getY() > getHeight()/2;
[91]56 }
[1169]57 });
[4840]58
59 ExpertToggleAction.addExpertModeChangeListener(this);
[1169]60 }
[7]61
[6084]62 @Override
[1169]63 public void propertyChange(PropertyChangeEvent evt) {
[6990]64 if ("active".equals(evt.getPropertyName())) {
[8510]65 setSelected((Boolean) evt.getNewValue());
[1169]66 requestFocusInWindow();
[6990]67 } else if ("selected".equals(evt.getPropertyName())) {
[8510]68 setSelected((Boolean) evt.getNewValue());
[1169]69 }
70 }
[2871]71
[6084]72 @Override
[2871]73 public void destroy() {
74 Action action = getAction();
75 if (action instanceof Destroyable) {
76 ((Destroyable) action).destroy();
77 }
[4666]78 if (action != null) {
79 action.removePropertyChangeListener(this);
80 }
[2871]81 }
[4840]82
[4669]83 String getPreferenceKey() {
84 String s = (String) getSafeActionValue("toolbar");
[4840]85 if (s == null) {
[8510]86 if (getAction() != null) {
[4840]87 s = getAction().getClass().getName();
88 }
[4669]89 }
90 return "sidetoolbar.hidden."+s;
[4840]91
[4669]92 }
[4840]93
[4609]94 @Override
[4840]95 public void expertChanged(boolean isExpert) {
96 applyButtonHiddenPreferences();
97 }
98
99 @Override
[4609]100 public void applyButtonHiddenPreferences() {
[4669]101 boolean alwaysHideDisabled = Main.pref.getBoolean("sidetoolbar.hideDisabledButtons", false);
[4840]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) {
[4843]108 if (isExpert && !ExpertToggleAction.isExpert()) {
[4840]109 hiddenFlag = true;
110 }
111 } else {
112 hiddenFlag = Boolean.parseBoolean(hiddenFlagStr);
113 }
[8443]114 setVisible(!hiddenFlag); // show or hide, do what preferences say
[4840]115 }
[4609]116 }
117
118 @Override
119 public void setButtonHidden(boolean b) {
[4666]120 setVisible(!b);
[8510]121 if (listener != null) { // if someone wants to know about changes of visibility
[4666]122 if (!b) listener.buttonShown(); else listener.buttonHidden();
123 }
[4843]124 if ((b && isExpert && !ExpertToggleAction.isExpert()) ||
125 (!b && isExpert && ExpertToggleAction.isExpert())) {
[4840]126 Main.pref.put(getPreferenceKey(), null);
127 } else {
128 Main.pref.put(getPreferenceKey(), b);
129 }
[4609]130 }
[4840]131
132 /*
[4669]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) {
[4840]137 hideIfDisabled = b;
138 if (b && !isEnabled()) {
139 setVisible(false);
140 }
[4669]141 }
[4840]142
[4609]143 @Override
144 public void showButton() {
145 setButtonHidden(false);
146 }
[4840]147
[4609]148 @Override
149 public void hideButton() {
150 setButtonHidden(true);
151 }
152
153 @Override
154 public String getActionName() {
[4666]155 return (String) getSafeActionValue(Action.NAME);
[4609]156 }
157
158 @Override
159 public Icon getIcon() {
[7694]160 Object o = getSafeActionValue(Action.LARGE_ICON_KEY);
161 if (o == null)
162 o = getSafeActionValue(Action.SMALL_ICON);
163 return (Icon) o;
[4609]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
[4666]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 }
[1]180}
Note: See TracBrowser for help on using the repository browser.