source: josm/trunk/src/org/openstreetmap/josm/actions/ExpertToggleAction.java@ 17379

Last change on this file since 17379 was 17333, checked in by Don-vip, 3 years ago

see #20129 - Fix typos and misspellings in the code (patch by gaben)

  • 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.actions;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.Component;
7import java.awt.event.ActionEvent;
8
9import org.openstreetmap.josm.data.preferences.BooleanProperty;
10import org.openstreetmap.josm.gui.MainApplication;
11import org.openstreetmap.josm.tools.ImageProvider;
12import org.openstreetmap.josm.tools.ListenerList;
13
14/**
15 * This action toggles the Expert mode.
16 * @since 4840
17 */
18public class ExpertToggleAction extends ToggleAction {
19
20 /**
21 * This listener is notified whenever the expert mode setting changed.
22 */
23 @FunctionalInterface
24 public interface ExpertModeChangeListener {
25 /**
26 * The expert mode changed.
27 * @param isExpert <code>true</code> if expert mode was enabled, false otherwise.
28 */
29 void expertChanged(boolean isExpert);
30 }
31
32 // TODO: Switch to checked list. We can do this as soon as we do not see any more warnings.
33 private static final ListenerList<ExpertModeChangeListener> listeners = ListenerList.createUnchecked();
34 private static final ListenerList<Component> visibilityToggleListeners = ListenerList.createUnchecked();
35
36 private static final BooleanProperty PREF_EXPERT = new BooleanProperty("expert", false);
37
38 private static final ExpertToggleAction INSTANCE = new ExpertToggleAction();
39
40 private static synchronized void fireExpertModeChanged(boolean isExpert) {
41 listeners.fireEvent(listener -> listener.expertChanged(isExpert));
42 visibilityToggleListeners.fireEvent(c -> c.setVisible(isExpert));
43 }
44
45 /**
46 * Register a expert mode change listener.
47 *
48 * @param listener the listener. Ignored if null.
49 */
50 public static void addExpertModeChangeListener(ExpertModeChangeListener listener) {
51 addExpertModeChangeListener(listener, false);
52 }
53
54 /**
55 * Register a expert mode change listener, and optionally fires it.
56 * @param listener the listener. Ignored if null.
57 * @param fireWhenAdding if true, the listener will be fired immediately after added
58 */
59 public static synchronized void addExpertModeChangeListener(ExpertModeChangeListener listener, boolean fireWhenAdding) {
60 if (listener == null) return;
61 listeners.addWeakListener(listener);
62 if (fireWhenAdding) {
63 listener.expertChanged(isExpert());
64 }
65 }
66
67 /**
68 * Removes a expert mode change listener
69 *
70 * @param listener the listener. Ignored if null.
71 */
72 public static synchronized void removeExpertModeChangeListener(ExpertModeChangeListener listener) {
73 if (listener == null) return;
74 listeners.removeListener(listener);
75 }
76
77 /**
78 * Marks a component to be only visible when expert mode is enabled. The visibility of the component is changed automatically.
79 * @param c The component.
80 */
81 public static synchronized void addVisibilitySwitcher(Component c) {
82 if (c == null) return;
83 visibilityToggleListeners.addWeakListener(c);
84 c.setVisible(isExpert());
85 }
86
87 /**
88 * Stops tracking visibility changes for the given component.
89 * @param c The component.
90 * @see #addVisibilitySwitcher(Component)
91 */
92 public static synchronized void removeVisibilitySwitcher(Component c) {
93 if (c == null) return;
94 visibilityToggleListeners.removeListener(c);
95 }
96
97 /**
98 * Determines if the given component tracks visibility changes.
99 * @param c The component.
100 * @return {@code true} if the given component tracks visibility changes
101 * @since 15649
102 */
103 public static synchronized boolean hasVisibilitySwitcher(Component c) {
104 if (c == null) return false;
105 return visibilityToggleListeners.containsListener(c);
106 }
107
108 /**
109 * Constructs a new {@code ExpertToggleAction}.
110 */
111 public ExpertToggleAction() {
112 super(tr("Expert Mode"),
113 new ImageProvider("expert").setOptional(true),
114 tr("Enable/disable expert mode"),
115 null,
116 false /* register toolbar */, null, false
117 );
118 setToolbarId("expertmode");
119 if (MainApplication.getToolbar() != null) {
120 MainApplication.getToolbar().register(this);
121 }
122 setSelected(PREF_EXPERT.get());
123 notifySelectedState();
124 }
125
126 @Override
127 protected final void notifySelectedState() {
128 super.notifySelectedState();
129 PREF_EXPERT.put(isSelected());
130 fireExpertModeChanged(isSelected());
131 }
132
133 /**
134 * Forces the expert mode state to the given state.
135 * @param isExpert if expert mode should be used.
136 * @since 11224
137 */
138 public void setExpert(boolean isExpert) {
139 if (isSelected() != isExpert) {
140 setSelected(isExpert);
141 notifySelectedState();
142 }
143 }
144
145 @Override
146 public void actionPerformed(ActionEvent e) {
147 toggleSelectedState(e);
148 notifySelectedState();
149 }
150
151 /**
152 * Replies the unique instance of this action.
153 * @return The unique instance of this action
154 */
155 public static ExpertToggleAction getInstance() {
156 return INSTANCE;
157 }
158
159 /**
160 * Determines if expert mode is enabled.
161 * @return {@code true} if expert mode is enabled, {@code false} otherwise.
162 */
163 public static boolean isExpert() {
164 return INSTANCE.isSelected();
165 }
166}
Note: See TracBrowser for help on using the repository browser.