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 javax.swing.ButtonModel;
|
---|
14 |
|
---|
15 | import org.openstreetmap.josm.Main;
|
---|
16 |
|
---|
17 | public class ExpertToggleAction extends JosmAction {
|
---|
18 |
|
---|
19 | private final List<ButtonModel> buttonModels = new ArrayList<ButtonModel>();
|
---|
20 | private boolean selected;
|
---|
21 |
|
---|
22 | public interface ExpertModeChangeListener {
|
---|
23 | void expertChanged(boolean isExpert);
|
---|
24 | }
|
---|
25 |
|
---|
26 | private static final List<WeakReference<ExpertModeChangeListener>> listeners = new ArrayList<WeakReference<ExpertModeChangeListener>>();
|
---|
27 | private static final List<WeakReference<Component>> visibilityToggleListeners = new ArrayList<WeakReference<Component>>();
|
---|
28 |
|
---|
29 | private static ExpertToggleAction INSTANCE = new ExpertToggleAction();
|
---|
30 |
|
---|
31 | private synchronized static void fireExpertModeChanged(boolean isExpert) {
|
---|
32 | {
|
---|
33 | Iterator<WeakReference<ExpertModeChangeListener>> it = listeners.iterator();
|
---|
34 | while (it.hasNext()) {
|
---|
35 | WeakReference<ExpertModeChangeListener> wr = it.next();
|
---|
36 | ExpertModeChangeListener listener = wr.get();
|
---|
37 | if (listener == null) {
|
---|
38 | it.remove();
|
---|
39 | continue;
|
---|
40 | }
|
---|
41 | listener.expertChanged(isExpert);
|
---|
42 | }
|
---|
43 | }
|
---|
44 | {
|
---|
45 | Iterator<WeakReference<Component>> it = visibilityToggleListeners.iterator();
|
---|
46 | while (it.hasNext()) {
|
---|
47 | WeakReference<Component> wr = it.next();
|
---|
48 | Component c = wr.get();
|
---|
49 | if (c == null) {
|
---|
50 | it.remove();
|
---|
51 | continue;
|
---|
52 | }
|
---|
53 | c.setVisible(isExpert);
|
---|
54 | }
|
---|
55 | }
|
---|
56 | }
|
---|
57 |
|
---|
58 | /**
|
---|
59 | * Register a expert mode change listener
|
---|
60 | *
|
---|
61 | * @param listener the listener. Ignored if null.
|
---|
62 | */
|
---|
63 | public static void addExpertModeChangeListener(ExpertModeChangeListener listener) {
|
---|
64 | addExpertModeChangeListener(listener, false);
|
---|
65 | }
|
---|
66 |
|
---|
67 | public synchronized static void addExpertModeChangeListener(ExpertModeChangeListener listener, boolean fireWhenAdding) {
|
---|
68 | if (listener == null) return;
|
---|
69 | for (WeakReference<ExpertModeChangeListener> wr : listeners) {
|
---|
70 | // already registered ? => abort
|
---|
71 | if (wr.get() == listener) return;
|
---|
72 | }
|
---|
73 | listeners.add(new WeakReference<ExpertModeChangeListener>(listener));
|
---|
74 | if (fireWhenAdding) {
|
---|
75 | listener.expertChanged(isExpert());
|
---|
76 | }
|
---|
77 | }
|
---|
78 |
|
---|
79 | /**
|
---|
80 | * Removes a expert mode change listener
|
---|
81 | *
|
---|
82 | * @param listener the listener. Ignored if null.
|
---|
83 | */
|
---|
84 | public synchronized static void removeExpertModeChangeListener(ExpertModeChangeListener listener) {
|
---|
85 | if (listener == null) return;
|
---|
86 | Iterator<WeakReference<ExpertModeChangeListener>> it = listeners.iterator();
|
---|
87 | while (it.hasNext()) {
|
---|
88 | WeakReference<ExpertModeChangeListener> wr = it.next();
|
---|
89 | // remove the listener - and any other listener which god garbage
|
---|
90 | // collected in the meantime
|
---|
91 | if (wr.get() == null || wr.get() == listener) {
|
---|
92 | it.remove();
|
---|
93 | }
|
---|
94 | }
|
---|
95 | }
|
---|
96 |
|
---|
97 | public synchronized static void addVisibilitySwitcher(Component c) {
|
---|
98 | if (c == null) return;
|
---|
99 | for (WeakReference<Component> wr : visibilityToggleListeners) {
|
---|
100 | // already registered ? => abort
|
---|
101 | if (wr.get() == c) return;
|
---|
102 | }
|
---|
103 | visibilityToggleListeners.add(new WeakReference<Component>(c));
|
---|
104 | c.setVisible(isExpert());
|
---|
105 | }
|
---|
106 |
|
---|
107 | public synchronized static void removeVisibilitySwitcher(Component c) {
|
---|
108 | if (c == null) return;
|
---|
109 | Iterator<WeakReference<Component>> it = visibilityToggleListeners.iterator();
|
---|
110 | while (it.hasNext()) {
|
---|
111 | WeakReference<Component> wr = it.next();
|
---|
112 | // remove the listener - and any other listener which god garbage
|
---|
113 | // collected in the meantime
|
---|
114 | if (wr.get() == null || wr.get() == c) {
|
---|
115 | it.remove();
|
---|
116 | }
|
---|
117 | }
|
---|
118 | }
|
---|
119 |
|
---|
120 | public ExpertToggleAction() {
|
---|
121 | super(
|
---|
122 | tr("Expert Mode"),
|
---|
123 | "expert",
|
---|
124 | tr("Enable/disable expert mode"),
|
---|
125 | null,
|
---|
126 | false /* register toolbar */
|
---|
127 | );
|
---|
128 | putValue("toolbar", "expertmode");
|
---|
129 | Main.toolbar.register(this);
|
---|
130 | selected = Main.pref.getBoolean("expert", false);
|
---|
131 | notifySelectedState();
|
---|
132 | }
|
---|
133 |
|
---|
134 | public void addButtonModel(ButtonModel model) {
|
---|
135 | if (model != null && !buttonModels.contains(model)) {
|
---|
136 | buttonModels.add(model);
|
---|
137 | model.setSelected(selected);
|
---|
138 | }
|
---|
139 | }
|
---|
140 |
|
---|
141 | public void removeButtonModel(ButtonModel model) {
|
---|
142 | if (model != null && buttonModels.contains(model)) {
|
---|
143 | buttonModels.remove(model);
|
---|
144 | }
|
---|
145 | }
|
---|
146 |
|
---|
147 | protected void notifySelectedState() {
|
---|
148 | for (ButtonModel model: buttonModels) {
|
---|
149 | if (model.isSelected() != selected) {
|
---|
150 | model.setSelected(selected);
|
---|
151 | }
|
---|
152 | }
|
---|
153 | fireExpertModeChanged(selected);
|
---|
154 | }
|
---|
155 |
|
---|
156 | protected void toggleSelectedState() {
|
---|
157 | selected = !selected;
|
---|
158 | Main.pref.put("expert", selected);
|
---|
159 | notifySelectedState();
|
---|
160 | }
|
---|
161 |
|
---|
162 | public void actionPerformed(ActionEvent e) {
|
---|
163 | toggleSelectedState();
|
---|
164 | }
|
---|
165 |
|
---|
166 | public boolean isSelected() {
|
---|
167 | return selected;
|
---|
168 | }
|
---|
169 |
|
---|
170 | public static ExpertToggleAction getInstance() {
|
---|
171 | return INSTANCE;
|
---|
172 | }
|
---|
173 |
|
---|
174 | public static boolean isExpert() {
|
---|
175 | return INSTANCE.isSelected();
|
---|
176 | }
|
---|
177 | }
|
---|