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

Last change on this file since 13586 was 12846, checked in by bastiK, 7 years ago

see #15229 - use Config.getPref() wherever possible

  • 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
[7]4import java.beans.PropertyChangeEvent;
5import java.beans.PropertyChangeListener;
[11553]6import java.util.Optional;
[7]7
[1]8import javax.swing.Action;
[4609]9import javax.swing.Icon;
[1]10import javax.swing.JToggleButton;
11
[4840]12import org.openstreetmap.josm.actions.ExpertToggleAction;
13import org.openstreetmap.josm.actions.ExpertToggleAction.ExpertModeChangeListener;
[12846]14import org.openstreetmap.josm.spi.preferences.Config;
[10384]15import org.openstreetmap.josm.tools.CheckParameterUtil;
[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
[8308]26 private transient ShowHideButtonListener listener;
[8840]27 private boolean hideIfDisabled;
[4840]28 private boolean isExpert;
[91]29
[1169]30 /**
31 * Construct the toggle button with the given action.
[9243]32 * @param action associated action
[1169]33 */
34 public IconToggleButton(Action action) {
[4835]35 this(action, false);
36 }
37
38 /**
39 * Construct the toggle button with the given action.
[9243]40 * @param action associated action
41 * @param isExpert {@code true} if it's reserved to expert mode
[4835]42 */
[4840]43 public IconToggleButton(Action action, boolean isExpert) {
[1169]44 super(action);
[10384]45 CheckParameterUtil.ensureParameterNotNull(action, "action");
[4840]46 this.isExpert = isExpert;
[1169]47 setText(null);
[80]48
[1169]49 Object o = action.getValue(Action.SHORT_DESCRIPTION);
[2005]50 if (o != null) {
[1169]51 setToolTipText(o.toString());
[2005]52 }
[91]53
[1169]54 action.addPropertyChangeListener(this);
55
[4840]56 ExpertToggleAction.addExpertModeChangeListener(this);
[1169]57 }
[7]58
[6084]59 @Override
[1169]60 public void propertyChange(PropertyChangeEvent evt) {
[6990]61 if ("active".equals(evt.getPropertyName())) {
[8510]62 setSelected((Boolean) evt.getNewValue());
[1169]63 requestFocusInWindow();
[6990]64 } else if ("selected".equals(evt.getPropertyName())) {
[8510]65 setSelected((Boolean) evt.getNewValue());
[1169]66 }
67 }
[2871]68
[6084]69 @Override
[2871]70 public void destroy() {
71 Action action = getAction();
72 if (action instanceof Destroyable) {
73 ((Destroyable) action).destroy();
74 }
[4666]75 if (action != null) {
76 action.removePropertyChangeListener(this);
77 }
[2871]78 }
[4840]79
[4669]80 String getPreferenceKey() {
81 String s = (String) getSafeActionValue("toolbar");
[11386]82 if (s == null && getAction() != null) {
83 s = getAction().getClass().getName();
[4669]84 }
85 return "sidetoolbar.hidden."+s;
86 }
[4840]87
[4609]88 @Override
[4840]89 public void expertChanged(boolean isExpert) {
90 applyButtonHiddenPreferences();
91 }
92
93 @Override
[4609]94 public void applyButtonHiddenPreferences() {
[12846]95 boolean alwaysHideDisabled = Config.getPref().getBoolean("sidetoolbar.hideDisabledButtons", false);
[4840]96 if (!isEnabled() && (hideIfDisabled || alwaysHideDisabled)) {
97 setVisible(false); // hide because of disabled button
98 } else {
99 boolean hiddenFlag = false;
[12846]100 String hiddenFlagStr = Config.getPref().get(getPreferenceKey(), null);
[4840]101 if (hiddenFlagStr == null) {
[4843]102 if (isExpert && !ExpertToggleAction.isExpert()) {
[4840]103 hiddenFlag = true;
104 }
105 } else {
106 hiddenFlag = Boolean.parseBoolean(hiddenFlagStr);
107 }
[8443]108 setVisible(!hiddenFlag); // show or hide, do what preferences say
[4840]109 }
[4609]110 }
111
112 @Override
113 public void setButtonHidden(boolean b) {
[4666]114 setVisible(!b);
[8510]115 if (listener != null) { // if someone wants to know about changes of visibility
[4666]116 if (!b) listener.buttonShown(); else listener.buttonHidden();
117 }
[4843]118 if ((b && isExpert && !ExpertToggleAction.isExpert()) ||
119 (!b && isExpert && ExpertToggleAction.isExpert())) {
[12846]120 Config.getPref().put(getPreferenceKey(), null);
[4840]121 } else {
[12846]122 Config.getPref().putBoolean(getPreferenceKey(), b);
[4840]123 }
[4609]124 }
[4840]125
[9795]126 /**
127 * This function should be called for plugins that want to enable auto-hiding
[4669]128 * custom buttons when they are disabled (because of incorrect layer, for example)
[9795]129 * @param b hide if disabled
[4669]130 */
131 public void setAutoHideDisabledButton(boolean b) {
[4840]132 hideIfDisabled = b;
133 if (b && !isEnabled()) {
134 setVisible(false);
135 }
[4669]136 }
[4840]137
[4609]138 @Override
139 public void showButton() {
140 setButtonHidden(false);
141 }
[4840]142
[4609]143 @Override
144 public void hideButton() {
145 setButtonHidden(true);
146 }
147
148 @Override
149 public String getActionName() {
[4666]150 return (String) getSafeActionValue(Action.NAME);
[4609]151 }
152
153 @Override
154 public Icon getIcon() {
[11553]155 return (Icon) Optional.ofNullable(getSafeActionValue(Action.LARGE_ICON_KEY)).orElseGet(() -> getSafeActionValue(Action.SMALL_ICON));
[4609]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
[4666]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 }
[1]172}
Note: See TracBrowser for help on using the repository browser.