source: josm/trunk/src/org/openstreetmap/josm/actions/JosmAction.java@ 12620

Last change on this file since 12620 was 12620, checked in by Don-vip, 7 years ago

see #15182 - deprecate all Main logging methods and introduce suitable replacements in Logging for most of them

  • Property svn:eol-style set to native
File size: 16.3 KB
RevLine 
[6380]1// License: GPL. For details, see LICENSE file.
[30]2package org.openstreetmap.josm.actions;
3
[1182]4import static org.openstreetmap.josm.tools.I18n.tr;
5
[4982]6import java.awt.event.KeyEvent;
[1820]7import java.util.Collection;
[10212]8import java.util.concurrent.CancellationException;
9import java.util.concurrent.ExecutionException;
[10074]10import java.util.concurrent.Future;
[1820]11
[30]12import javax.swing.AbstractAction;
13
[43]14import org.openstreetmap.josm.Main;
[1820]15import org.openstreetmap.josm.data.SelectionChangedListener;
[558]16import org.openstreetmap.josm.data.osm.DataSet;
[1820]17import org.openstreetmap.josm.data.osm.OsmPrimitive;
[12051]18import org.openstreetmap.josm.data.osm.event.DatasetEventManager.FireMode;
19import org.openstreetmap.josm.data.osm.event.SelectionEventManager;
[10353]20import org.openstreetmap.josm.gui.layer.LayerManager.LayerAddEvent;
[10345]21import org.openstreetmap.josm.gui.layer.LayerManager.LayerChangeListener;
[10353]22import org.openstreetmap.josm.gui.layer.LayerManager.LayerOrderChangeEvent;
23import org.openstreetmap.josm.gui.layer.LayerManager.LayerRemoveEvent;
24import org.openstreetmap.josm.gui.layer.MainLayerManager;
[10345]25import org.openstreetmap.josm.gui.layer.MainLayerManager.ActiveLayerChangeEvent;
26import org.openstreetmap.josm.gui.layer.MainLayerManager.ActiveLayerChangeListener;
[10074]27import org.openstreetmap.josm.gui.progress.PleaseWaitProgressMonitor;
[208]28import org.openstreetmap.josm.tools.Destroyable;
[71]29import org.openstreetmap.josm.tools.ImageProvider;
[12620]30import org.openstreetmap.josm.tools.Logging;
[1084]31import org.openstreetmap.josm.tools.Shortcut;
[30]32
33/**
34 * Base class helper for all Actions in JOSM. Just to make the life easier.
[2305]35 *
[10353]36 * This action allows you to set up an icon, a tooltip text, a globally registered shortcut, register it in the main toolbar and set up
37 * layer/selection listeners that call {@link #updateEnabledState()} whenever the global context is changed.
38 *
39 * A JosmAction can register a {@link LayerChangeListener} and a {@link SelectionChangedListener}. Upon
[5275]40 * a layer change event or a selection change event it invokes {@link #updateEnabledState()}.
41 * Subclasses can override {@link #updateEnabledState()} in order to update the {@link #isEnabled()}-state
[10972]42 * of a JosmAction depending on the {@link #getLayerManager()} state.
[2305]43 *
[208]44 * destroy() from interface Destroyable is called e.g. for MapModes, when the last layer has
45 * been removed and so the mapframe will be destroyed. For other JosmActions, destroy() may never
46 * be called (currently).
[1023]47 *
[30]48 * @author imi
49 */
[6889]50public abstract class JosmAction extends AbstractAction implements Destroyable {
[30]51
[8308]52 protected transient Shortcut sc;
53 private transient LayerChangeAdapter layerChangeAdapter;
[10353]54 private transient ActiveLayerChangeAdapter activeLayerChangeAdapter;
[8308]55 private transient SelectionChangeAdapter selectionChangeAdapter;
[208]56
[6814]57 /**
[5110]58 * Constructs a {@code JosmAction}.
[1169]59 *
[1935]60 * @param name the action's text as displayed on the menu (if it is added to a menu)
[5110]61 * @param icon the icon to use
[1935]62 * @param tooltip a longer description of the action that will be displayed in the tooltip. Please note
63 * that html is not supported for menu actions on some platforms.
64 * @param shortcut a ready-created shortcut object or null if you don't want a shortcut. But you always
65 * do want a shortcut, remember you can always register it with group=none, so you
66 * won't be assigned a shortcut unless the user configures one. If you pass null here,
[1169]67 * the user CANNOT configure a shortcut for your action.
[5110]68 * @param registerInToolbar register this action for the toolbar preferences?
[4733]69 * @param toolbarId identifier for the toolbar preferences. The iconName is used, if this parameter is null
70 * @param installAdapters false, if you don't want to install layer changed and selection changed adapters
[7693]71 */
[8509]72 public JosmAction(String name, ImageProvider icon, String tooltip, Shortcut shortcut, boolean registerInToolbar,
73 String toolbarId, boolean installAdapters) {
[7693]74 super(name);
[8510]75 if (icon != null)
[10428]76 icon.getResource().attachImageIcon(this, true);
[7693]77 setHelpId();
78 sc = shortcut;
[12320]79 if (sc != null && !sc.isAutomatic()) {
[7693]80 Main.registerActionShortcut(this, sc);
81 }
82 setTooltip(tooltip);
83 if (getValue("toolbar") == null) {
84 putValue("toolbar", toolbarId);
85 }
86 if (registerInToolbar && Main.toolbar != null) {
87 Main.toolbar.register(this);
88 }
89 if (installAdapters) {
90 installAdapters();
91 }
92 }
93
94 /**
[5110]95 * The new super for all actions.
96 *
97 * Use this super constructor to setup your action.
98 *
99 * @param name the action's text as displayed on the menu (if it is added to a menu)
100 * @param iconName the filename of the icon to use
101 * @param tooltip a longer description of the action that will be displayed in the tooltip. Please note
102 * that html is not supported for menu actions on some platforms.
103 * @param shortcut a ready-created shortcut object or null if you don't want a shortcut. But you always
104 * do want a shortcut, remember you can always register it with group=none, so you
105 * won't be assigned a shortcut unless the user configures one. If you pass null here,
106 * the user CANNOT configure a shortcut for your action.
[5526]107 * @param registerInToolbar register this action for the toolbar preferences?
[5110]108 * @param toolbarId identifier for the toolbar preferences. The iconName is used, if this parameter is null
109 * @param installAdapters false, if you don't want to install layer changed and selection changed adapters
110 */
[8509]111 public JosmAction(String name, String iconName, String tooltip, Shortcut shortcut, boolean registerInToolbar,
112 String toolbarId, boolean installAdapters) {
[7693]113 this(name, iconName == null ? null : new ImageProvider(iconName), tooltip, shortcut, registerInToolbar,
[5110]114 toolbarId == null ? iconName : toolbarId, installAdapters);
115 }
116
[6814]117 /**
118 * Constructs a new {@code JosmAction}.
119 *
120 * Use this super constructor to setup your action.
121 *
122 * @param name the action's text as displayed on the menu (if it is added to a menu)
123 * @param iconName the filename of the icon to use
124 * @param tooltip a longer description of the action that will be displayed in the tooltip. Please note
125 * that html is not supported for menu actions on some platforms.
126 * @param shortcut a ready-created shortcut object or null if you don't want a shortcut. But you always
127 * do want a shortcut, remember you can always register it with group=none, so you
128 * won't be assigned a shortcut unless the user configures one. If you pass null here,
129 * the user CANNOT configure a shortcut for your action.
130 * @param registerInToolbar register this action for the toolbar preferences?
131 * @param installAdapters false, if you don't want to install layer changed and selection changed adapters
132 */
[5526]133 public JosmAction(String name, String iconName, String tooltip, Shortcut shortcut, boolean registerInToolbar, boolean installAdapters) {
134 this(name, iconName, tooltip, shortcut, registerInToolbar, null, installAdapters);
[4733]135 }
136
[6814]137 /**
138 * Constructs a new {@code JosmAction}.
139 *
140 * Use this super constructor to setup your action.
141 *
142 * @param name the action's text as displayed on the menu (if it is added to a menu)
143 * @param iconName the filename of the icon to use
144 * @param tooltip a longer description of the action that will be displayed in the tooltip. Please note
145 * that html is not supported for menu actions on some platforms.
146 * @param shortcut a ready-created shortcut object or null if you don't want a shortcut. But you always
147 * do want a shortcut, remember you can always register it with group=none, so you
148 * won't be assigned a shortcut unless the user configures one. If you pass null here,
149 * the user CANNOT configure a shortcut for your action.
150 * @param registerInToolbar register this action for the toolbar preferences?
151 */
[5526]152 public JosmAction(String name, String iconName, String tooltip, Shortcut shortcut, boolean registerInToolbar) {
153 this(name, iconName, tooltip, shortcut, registerInToolbar, null, true);
[4733]154 }
155
[6814]156 /**
157 * Constructs a new {@code JosmAction}.
158 */
[1820]159 public JosmAction() {
[3327]160 this(true);
161 }
162
[6814]163 /**
164 * Constructs a new {@code JosmAction}.
165 *
166 * @param installAdapters false, if you don't want to install layer changed and selection changed adapters
167 */
[3327]168 public JosmAction(boolean installAdapters) {
[1820]169 setHelpId();
[3327]170 if (installAdapters) {
171 installAdapters();
172 }
[1820]173 }
[10409]174
[10353]175 /**
176 * Installs the listeners to this action.
177 * <p>
178 * This should either never be called or only called in the constructor of this action.
179 * <p>
180 * All registered adapters should be removed in {@link #destroy()}
181 */
182 protected void installAdapters() {
183 // make this action listen to layer change and selection change events
184 if (listenToLayerChange()) {
185 layerChangeAdapter = new LayerChangeAdapter();
186 activeLayerChangeAdapter = new ActiveLayerChangeAdapter();
187 getLayerManager().addLayerChangeListener(layerChangeAdapter);
188 getLayerManager().addActiveLayerChangeListener(activeLayerChangeAdapter);
189 }
190 if (listenToSelectionChange()) {
191 selectionChangeAdapter = new SelectionChangeAdapter();
[12051]192 SelectionEventManager.getInstance()
193 .addSelectionListener(selectionChangeAdapter, FireMode.IN_EDT_CONSOLIDATED);
[10353]194 }
195 initEnabledState();
196 }
197
198 /**
199 * Overwrite this if {@link #updateEnabledState()} should be called when the active / availabe layers change. Default is true.
200 * @return <code>true</code> if a {@link LayerChangeListener} and a {@link ActiveLayerChangeListener} should be registered.
[10354]201 * @since 10353
[10353]202 */
203 protected boolean listenToLayerChange() {
204 return true;
205 }
206
207 /**
208 * Overwrite this if {@link #updateEnabledState()} should be called when the selection changed. Default is true.
209 * @return <code>true</code> if a {@link SelectionChangedListener} should be registered.
[10354]210 * @since 10353
[10353]211 */
212 protected boolean listenToSelectionChange() {
213 return true;
214 }
215
[5459]216 @Override
[1169]217 public void destroy() {
[12320]218 if (sc != null && !sc.isAutomatic()) {
[3444]219 Main.unregisterActionShortcut(this);
[1169]220 }
[10345]221 if (layerChangeAdapter != null) {
[10353]222 getLayerManager().removeLayerChangeListener(layerChangeAdapter);
223 getLayerManager().removeActiveLayerChangeListener(activeLayerChangeAdapter);
[10345]224 }
[10353]225 if (selectionChangeAdapter != null) {
226 DataSet.removeSelectionListener(selectionChangeAdapter);
227 }
[1169]228 }
[1023]229
[1169]230 private void setHelpId() {
231 String helpId = "Action/"+getClass().getName().substring(getClass().getName().lastIndexOf('.')+1);
[1814]232 if (helpId.endsWith("Action")) {
[1169]233 helpId = helpId.substring(0, helpId.length()-6);
[1814]234 }
[1169]235 putValue("help", helpId);
236 }
[1814]237
[6814]238 /**
[10353]239 * Returns the shortcut for this action.
240 * @return the shortcut for this action, or "No shortcut" if none is defined
241 */
242 public Shortcut getShortcut() {
243 if (sc == null) {
244 sc = Shortcut.registerShortcut("core:none", tr("No Shortcut"), KeyEvent.CHAR_UNDEFINED, Shortcut.NONE);
245 // as this shortcut is shared by all action that don't want to have a shortcut,
246 // we shouldn't allow the user to change it...
247 // this is handled by special name "core:none"
248 }
249 return sc;
250 }
251
252 /**
[6814]253 * Sets the tooltip text of this action.
254 * @param tooltip The text to display in tooltip. Can be {@code null}
255 */
[6890]256 public final void setTooltip(String tooltip) {
[5026]257 if (tooltip != null) {
258 putValue(SHORT_DESCRIPTION, Main.platform.makeTooltip(tooltip, sc));
259 }
[4908]260 }
261
[1814]262 /**
[10353]263 * Gets the layer manager used for this action. Defaults to the main layer manager but you can overwrite this.
264 * <p>
265 * The layer manager must be available when {@link #installAdapters()} is called and must not change.
266 *
267 * @return The layer manager.
[10354]268 * @since 10353
[10353]269 */
270 public MainLayerManager getLayerManager() {
271 return Main.getLayerManager();
272 }
273
[10074]274 protected static void waitFuture(final Future<?> future, final PleaseWaitProgressMonitor monitor) {
[10601]275 Main.worker.submit(() -> {
[10074]276 try {
277 future.get();
[10212]278 } catch (InterruptedException | ExecutionException | CancellationException e) {
[12620]279 Logging.error(e);
[10074]280 return;
281 }
282 monitor.close();
[10601]283 });
[10074]284 }
285
[2260]286 /**
287 * Override in subclasses to init the enabled state of an action when it is
[5266]288 * created. Default behaviour is to call {@link #updateEnabledState()}
[2305]289 *
[2260]290 * @see #updateEnabledState()
291 * @see #updateEnabledState(Collection)
292 */
[2256]293 protected void initEnabledState() {
[2260]294 updateEnabledState();
[2256]295 }
296
[2260]297 /**
298 * Override in subclasses to update the enabled state of the action when
299 * something in the JOSM state changes, i.e. when a layer is removed or added.
[2305]300 *
[5266]301 * See {@link #updateEnabledState(Collection)} to respond to changes in the collection
[2260]302 * of selected primitives.
[2305]303 *
[2260]304 * Default behavior is empty.
[2305]305 *
[2260]306 * @see #updateEnabledState(Collection)
307 * @see #initEnabledState()
[10353]308 * @see #listenToLayerChange()
[2260]309 */
[1820]310 protected void updateEnabledState() {
311 }
312
[2260]313 /**
314 * Override in subclasses to update the enabled state of the action if the
315 * collection of selected primitives changes. This method is called with the
[3504]316 * new selection.
[2305]317 *
[3504]318 * @param selection the collection of selected primitives; may be empty, but not null
[2305]319 *
[2260]320 * @see #updateEnabledState()
321 * @see #initEnabledState()
[10353]322 * @see #listenToSelectionChange()
[2260]323 */
[2256]324 protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) {
325 }
326
[1820]327 /**
[10409]328 * Updates enabled state according to primitives currently selected in edit data set, if any.
329 * Can be called in {@link #updateEnabledState()} implementations.
330 * @since 10409
331 */
332 protected final void updateEnabledStateOnCurrentSelection() {
333 DataSet ds = getLayerManager().getEditDataSet();
334 if (ds == null) {
335 setEnabled(false);
336 } else {
337 updateEnabledState(ds.getSelected());
338 }
339 }
340
341 /**
[10345]342 * Adapter for layer change events. Runs updateEnabledState() whenever the active layer changed.
[1820]343 */
[10353]344 protected class LayerChangeAdapter implements LayerChangeListener {
[6084]345 @Override
[10353]346 public void layerAdded(LayerAddEvent e) {
[10345]347 updateEnabledState();
[1820]348 }
349
[6084]350 @Override
[10353]351 public void layerRemoving(LayerRemoveEvent e) {
352 updateEnabledState();
353 }
354
355 @Override
356 public void layerOrderChanged(LayerOrderChangeEvent e) {
357 updateEnabledState();
358 }
359
360 @Override
[10345]361 public String toString() {
[11538]362 return "LayerChangeAdapter [" + JosmAction.this + ']';
[1820]363 }
364 }
365
366 /**
[10353]367 * Adapter for layer change events. Runs updateEnabledState() whenever the active layer changed.
368 */
369 protected class ActiveLayerChangeAdapter implements ActiveLayerChangeListener {
370 @Override
371 public void activeOrEditLayerChanged(ActiveLayerChangeEvent e) {
372 updateEnabledState();
373 }
374
375 @Override
376 public String toString() {
[11538]377 return "ActiveLayerChangeAdapter [" + JosmAction.this + ']';
[10353]378 }
379 }
380
381 /**
[10345]382 * Adapter for selection change events. Runs updateEnabledState() whenever the selection changed.
[1820]383 */
[8957]384 protected class SelectionChangeAdapter implements SelectionChangedListener {
[6084]385 @Override
[1820]386 public void selectionChanged(Collection<? extends OsmPrimitive> newSelection) {
[2256]387 updateEnabledState(newSelection);
[1820]388 }
[10345]389
390 @Override
391 public String toString() {
[11538]392 return "SelectionChangeAdapter [" + JosmAction.this + ']';
[10345]393 }
[1820]394 }
[30]395}
Note: See TracBrowser for help on using the repository browser.