1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.actions;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.Component;
|
---|
7 | import java.awt.event.ActionEvent;
|
---|
8 | import java.lang.ref.WeakReference;
|
---|
9 | import java.util.ArrayList;
|
---|
10 | import java.util.Iterator;
|
---|
11 | import java.util.List;
|
---|
12 |
|
---|
13 | import org.openstreetmap.josm.Main;
|
---|
14 |
|
---|
15 | /**
|
---|
16 | * This action toggles the Expert mode.
|
---|
17 | * @since 4840
|
---|
18 | */
|
---|
19 | public class ExpertToggleAction extends ToggleAction {
|
---|
20 |
|
---|
21 | public interface ExpertModeChangeListener {
|
---|
22 | void expertChanged(boolean isExpert);
|
---|
23 | }
|
---|
24 |
|
---|
25 | private static final List<WeakReference<ExpertModeChangeListener>> listeners = new ArrayList<>();
|
---|
26 | private static final List<WeakReference<Component>> visibilityToggleListeners = new ArrayList<>();
|
---|
27 |
|
---|
28 | private static final ExpertToggleAction INSTANCE = new ExpertToggleAction();
|
---|
29 |
|
---|
30 | private static synchronized void fireExpertModeChanged(boolean isExpert) {
|
---|
31 | Iterator<WeakReference<ExpertModeChangeListener>> it1 = listeners.iterator();
|
---|
32 | while (it1.hasNext()) {
|
---|
33 | WeakReference<ExpertModeChangeListener> wr = it1.next();
|
---|
34 | ExpertModeChangeListener listener = wr.get();
|
---|
35 | if (listener == null) {
|
---|
36 | it1.remove();
|
---|
37 | continue;
|
---|
38 | }
|
---|
39 | listener.expertChanged(isExpert);
|
---|
40 | }
|
---|
41 | Iterator<WeakReference<Component>> it2 = visibilityToggleListeners.iterator();
|
---|
42 | while (it2.hasNext()) {
|
---|
43 | WeakReference<Component> wr = it2.next();
|
---|
44 | Component c = wr.get();
|
---|
45 | if (c == null) {
|
---|
46 | it2.remove();
|
---|
47 | continue;
|
---|
48 | }
|
---|
49 | c.setVisible(isExpert);
|
---|
50 | }
|
---|
51 | }
|
---|
52 |
|
---|
53 | /**
|
---|
54 | * Register a expert mode change listener
|
---|
55 | *
|
---|
56 | * @param listener the listener. Ignored if null.
|
---|
57 | */
|
---|
58 | public static void addExpertModeChangeListener(ExpertModeChangeListener listener) {
|
---|
59 | addExpertModeChangeListener(listener, false);
|
---|
60 | }
|
---|
61 |
|
---|
62 | public static synchronized void addExpertModeChangeListener(ExpertModeChangeListener listener, boolean fireWhenAdding) {
|
---|
63 | if (listener == null) return;
|
---|
64 | for (WeakReference<ExpertModeChangeListener> wr : listeners) {
|
---|
65 | // already registered ? => abort
|
---|
66 | if (wr.get() == listener) return;
|
---|
67 | }
|
---|
68 | listeners.add(new WeakReference<>(listener));
|
---|
69 | if (fireWhenAdding) {
|
---|
70 | listener.expertChanged(isExpert());
|
---|
71 | }
|
---|
72 | }
|
---|
73 |
|
---|
74 | /**
|
---|
75 | * Removes a expert mode change listener
|
---|
76 | *
|
---|
77 | * @param listener the listener. Ignored if null.
|
---|
78 | */
|
---|
79 | public static synchronized void removeExpertModeChangeListener(ExpertModeChangeListener listener) {
|
---|
80 | if (listener == null) return;
|
---|
81 | Iterator<WeakReference<ExpertModeChangeListener>> it = listeners.iterator();
|
---|
82 | while (it.hasNext()) {
|
---|
83 | WeakReference<ExpertModeChangeListener> wr = it.next();
|
---|
84 | // remove the listener - and any other listener which god garbage
|
---|
85 | // collected in the meantime
|
---|
86 | if (wr.get() == null || wr.get() == listener) {
|
---|
87 | it.remove();
|
---|
88 | }
|
---|
89 | }
|
---|
90 | }
|
---|
91 |
|
---|
92 | public static synchronized void addVisibilitySwitcher(Component c) {
|
---|
93 | if (c == null) return;
|
---|
94 | for (WeakReference<Component> wr : visibilityToggleListeners) {
|
---|
95 | // already registered ? => abort
|
---|
96 | if (wr.get() == c) return;
|
---|
97 | }
|
---|
98 | visibilityToggleListeners.add(new WeakReference<>(c));
|
---|
99 | c.setVisible(isExpert());
|
---|
100 | }
|
---|
101 |
|
---|
102 | public static synchronized void removeVisibilitySwitcher(Component c) {
|
---|
103 | if (c == null) return;
|
---|
104 | Iterator<WeakReference<Component>> it = visibilityToggleListeners.iterator();
|
---|
105 | while (it.hasNext()) {
|
---|
106 | WeakReference<Component> wr = it.next();
|
---|
107 | // remove the listener - and any other listener which god garbage
|
---|
108 | // collected in the meantime
|
---|
109 | if (wr.get() == null || wr.get() == c) {
|
---|
110 | it.remove();
|
---|
111 | }
|
---|
112 | }
|
---|
113 | }
|
---|
114 |
|
---|
115 | /**
|
---|
116 | * Constructs a new {@code ExpertToggleAction}.
|
---|
117 | */
|
---|
118 | public ExpertToggleAction() {
|
---|
119 | super(tr("Expert Mode"),
|
---|
120 | "expert",
|
---|
121 | tr("Enable/disable expert mode"),
|
---|
122 | null,
|
---|
123 | false /* register toolbar */
|
---|
124 | );
|
---|
125 | putValue("toolbar", "expertmode");
|
---|
126 | if (Main.toolbar != null) {
|
---|
127 | Main.toolbar.register(this);
|
---|
128 | }
|
---|
129 | setSelected(Main.pref.getBoolean("expert", false));
|
---|
130 | notifySelectedState();
|
---|
131 | }
|
---|
132 |
|
---|
133 | @Override
|
---|
134 | protected final void notifySelectedState() {
|
---|
135 | super.notifySelectedState();
|
---|
136 | fireExpertModeChanged(isSelected());
|
---|
137 | }
|
---|
138 |
|
---|
139 | @Override
|
---|
140 | public void actionPerformed(ActionEvent e) {
|
---|
141 | toggleSelectedState(e);
|
---|
142 | Main.pref.put("expert", isSelected());
|
---|
143 | notifySelectedState();
|
---|
144 | }
|
---|
145 |
|
---|
146 | /**
|
---|
147 | * Replies the unique instance of this action.
|
---|
148 | * @return The unique instance of this action
|
---|
149 | */
|
---|
150 | public static ExpertToggleAction getInstance() {
|
---|
151 | return INSTANCE;
|
---|
152 | }
|
---|
153 |
|
---|
154 | /**
|
---|
155 | * Determines if expert mode is enabled.
|
---|
156 | * @return {@code true} if expert mode is enabled, {@code false} otherwise.
|
---|
157 | */
|
---|
158 | public static boolean isExpert() {
|
---|
159 | return INSTANCE.isSelected();
|
---|
160 | }
|
---|
161 | }
|
---|