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

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

see #15182 - deprecate Main.toolbar. Replacement: gui.MainApplication.getToolbar()

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