001// License: GPL. For details, see LICENSE file. 002package org.openstreetmap.josm.actions; 003 004import static org.openstreetmap.josm.gui.help.HelpUtil.ht; 005import static org.openstreetmap.josm.tools.I18n.tr; 006 007import java.awt.Component; 008import java.awt.event.ActionEvent; 009 010import org.openstreetmap.josm.data.preferences.BooleanProperty; 011import org.openstreetmap.josm.gui.MainApplication; 012import org.openstreetmap.josm.tools.ImageProvider; 013import org.openstreetmap.josm.tools.ListenerList; 014 015/** 016 * This action toggles the Expert mode. 017 * @since 4840 018 */ 019public class ExpertToggleAction extends ToggleAction { 020 021 /** 022 * This listener is notified whenever the expert mode setting changed. 023 */ 024 @FunctionalInterface 025 public interface ExpertModeChangeListener { 026 /** 027 * The expert mode changed. 028 * @param isExpert <code>true</code> if expert mode was enabled, false otherwise. 029 */ 030 void expertChanged(boolean isExpert); 031 } 032 033 // TODO: Switch to checked list. We can do this as soon as we do not see any more warnings. 034 private static final ListenerList<ExpertModeChangeListener> listeners = ListenerList.createUnchecked(); 035 private static final ListenerList<Component> visibilityToggleListeners = ListenerList.createUnchecked(); 036 037 private static final BooleanProperty PREF_EXPERT = new BooleanProperty("expert", false); 038 039 private static final ExpertToggleAction INSTANCE = new ExpertToggleAction(); 040 041 private static synchronized void fireExpertModeChanged(boolean isExpert) { 042 listeners.fireEvent(listener -> listener.expertChanged(isExpert)); 043 visibilityToggleListeners.fireEvent(c -> c.setVisible(isExpert)); 044 } 045 046 /** 047 * Register a expert mode change listener. 048 * 049 * @param listener the listener. Ignored if null. 050 */ 051 public static void addExpertModeChangeListener(ExpertModeChangeListener listener) { 052 addExpertModeChangeListener(listener, false); 053 } 054 055 /** 056 * Register a expert mode change listener, and optionally fires it. 057 * @param listener the listener. Ignored if null. 058 * @param fireWhenAdding if true, the listener will be fired immediately after added 059 */ 060 public static synchronized void addExpertModeChangeListener(ExpertModeChangeListener listener, boolean fireWhenAdding) { 061 if (listener == null) return; 062 listeners.addWeakListener(listener); 063 if (fireWhenAdding) { 064 listener.expertChanged(isExpert()); 065 } 066 } 067 068 /** 069 * Removes a expert mode change listener 070 * 071 * @param listener the listener. Ignored if null. 072 */ 073 public static synchronized void removeExpertModeChangeListener(ExpertModeChangeListener listener) { 074 if (listener == null) return; 075 listeners.removeListener(listener); 076 } 077 078 /** 079 * Marks a component to be only visible when expert mode is enabled. The visibility of the component is changed automatically. 080 * @param c The component. 081 */ 082 public static synchronized void addVisibilitySwitcher(Component c) { 083 if (c == null) return; 084 visibilityToggleListeners.addWeakListener(c); 085 c.setVisible(isExpert()); 086 } 087 088 /** 089 * Stops tracking visibility changes for the given component. 090 * @param c The component. 091 * @see #addVisibilitySwitcher(Component) 092 */ 093 public static synchronized void removeVisibilitySwitcher(Component c) { 094 if (c == null) return; 095 visibilityToggleListeners.removeListener(c); 096 } 097 098 /** 099 * 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}