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

Last change on this file since 13559 was 13106, checked in by Don-vip, 6 years ago

add some javadoc

  • Property svn:eol-style set to native
File size: 4.9 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 * Constructs a new {@code ExpertToggleAction}.
98 */
99 public ExpertToggleAction() {
100 super(tr("Expert Mode"),
101 "expert",
102 tr("Enable/disable expert mode"),
103 null,
104 false /* register toolbar */
105 );
106 putValue("toolbar", "expertmode");
107 if (MainApplication.getToolbar() != null) {
108 MainApplication.getToolbar().register(this);
109 }
110 setSelected(PREF_EXPERT.get());
111 notifySelectedState();
112 }
113
114 @Override
115 protected final void notifySelectedState() {
116 super.notifySelectedState();
117 PREF_EXPERT.put(isSelected());
118 fireExpertModeChanged(isSelected());
119 }
120
121 /**
122 * Forces the expert mode state to the given state.
123 * @param isExpert if expert mode should be used.
124 * @since 11224
125 */
126 public void setExpert(boolean isExpert) {
127 if (isSelected() != isExpert) {
128 setSelected(isExpert);
129 notifySelectedState();
130 }
131 }
132
133 @Override
134 public void actionPerformed(ActionEvent e) {
135 toggleSelectedState(e);
136 notifySelectedState();
137 }
138
139 /**
140 * Replies the unique instance of this action.
141 * @return The unique instance of this action
142 */
143 public static ExpertToggleAction getInstance() {
144 return INSTANCE;
145 }
146
147 /**
148 * Determines if expert mode is enabled.
149 * @return {@code true} if expert mode is enabled, {@code false} otherwise.
150 */
151 public static boolean isExpert() {
152 return INSTANCE.isSelected();
153 }
154}
Note: See TracBrowser for help on using the repository browser.