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

Last change on this file since 6252 was 6220, checked in by Don-vip, 11 years ago

refactor Toggle actions, use Action.SELECTED_KEY since we have switched to Java 6 long ago, javadoc

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