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

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

sonar - fb-contrib:ISB_TOSTRING_APPENDING - Correctness - Method concatenates the result of a toString() call

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