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

Last change on this file since 15649 was 15649, checked in by Don-vip, 4 years ago

see #18514 - disable Window menu when empty

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