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