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
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.actions;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.event.KeyEvent;
7import java.util.Collection;
8import java.util.concurrent.CancellationException;
9import java.util.concurrent.ExecutionException;
10import java.util.concurrent.Future;
11
12import javax.swing.AbstractAction;
13
14import org.openstreetmap.josm.Main;
15import org.openstreetmap.josm.data.SelectionChangedListener;
16import org.openstreetmap.josm.data.osm.DataSet;
17import org.openstreetmap.josm.data.osm.OsmPrimitive;
18import org.openstreetmap.josm.data.osm.event.DatasetEventManager.FireMode;
19import org.openstreetmap.josm.data.osm.event.SelectionEventManager;
20import org.openstreetmap.josm.gui.layer.LayerManager.LayerAddEvent;
21import org.openstreetmap.josm.gui.layer.LayerManager.LayerChangeListener;
22import org.openstreetmap.josm.gui.layer.LayerManager.LayerOrderChangeEvent;
23import org.openstreetmap.josm.gui.layer.LayerManager.LayerRemoveEvent;
24import org.openstreetmap.josm.gui.layer.MainLayerManager;
25import org.openstreetmap.josm.gui.layer.MainLayerManager.ActiveLayerChangeEvent;
26import org.openstreetmap.josm.gui.layer.MainLayerManager.ActiveLayerChangeListener;
27import org.openstreetmap.josm.gui.progress.PleaseWaitProgressMonitor;
28import org.openstreetmap.josm.tools.Destroyable;
29import org.openstreetmap.josm.tools.ImageProvider;
30import org.openstreetmap.josm.tools.Logging;
31import org.openstreetmap.josm.tools.Shortcut;
32
33/**
34 * Base class helper for all Actions in JOSM. Just to make the life easier.
35 *
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
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
42 * of a JosmAction depending on the {@link #getLayerManager()} state.
43 *
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).
47 *
48 * @author imi
49 */
50public abstract class JosmAction extends AbstractAction implements Destroyable {
51
52 protected transient Shortcut sc;
53 private transient LayerChangeAdapter layerChangeAdapter;
54 private transient ActiveLayerChangeAdapter activeLayerChangeAdapter;
55 private transient SelectionChangeAdapter selectionChangeAdapter;
56
57 /**
58 * Constructs a {@code JosmAction}.
59 *
60 * @param name the action's text as displayed on the menu (if it is added to a menu)
61 * @param icon the icon to use
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,
67 * the user CANNOT configure a shortcut for your action.
68 * @param registerInToolbar register this action for the toolbar preferences?
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
71 */
72 public JosmAction(String name, ImageProvider icon, String tooltip, Shortcut shortcut, boolean registerInToolbar,
73 String toolbarId, boolean installAdapters) {
74 super(name);
75 if (icon != null)
76 icon.getResource().attachImageIcon(this, true);
77 setHelpId();
78 sc = shortcut;
79 if (sc != null && !sc.isAutomatic()) {
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 /**
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.
107 * @param registerInToolbar register this action for the toolbar preferences?
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 */
111 public JosmAction(String name, String iconName, String tooltip, Shortcut shortcut, boolean registerInToolbar,
112 String toolbarId, boolean installAdapters) {
113 this(name, iconName == null ? null : new ImageProvider(iconName), tooltip, shortcut, registerInToolbar,
114 toolbarId == null ? iconName : toolbarId, installAdapters);
115 }
116
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 */
133 public JosmAction(String name, String iconName, String tooltip, Shortcut shortcut, boolean registerInToolbar, boolean installAdapters) {
134 this(name, iconName, tooltip, shortcut, registerInToolbar, null, installAdapters);
135 }
136
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 */
152 public JosmAction(String name, String iconName, String tooltip, Shortcut shortcut, boolean registerInToolbar) {
153 this(name, iconName, tooltip, shortcut, registerInToolbar, null, true);
154 }
155
156 /**
157 * Constructs a new {@code JosmAction}.
158 */
159 public JosmAction() {
160 this(true);
161 }
162
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 */
168 public JosmAction(boolean installAdapters) {
169 setHelpId();
170 if (installAdapters) {
171 installAdapters();
172 }
173 }
174
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();
192 SelectionEventManager.getInstance()
193 .addSelectionListener(selectionChangeAdapter, FireMode.IN_EDT_CONSOLIDATED);
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.
201 * @since 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.
210 * @since 10353
211 */
212 protected boolean listenToSelectionChange() {
213 return true;
214 }
215
216 @Override
217 public void destroy() {
218 if (sc != null && !sc.isAutomatic()) {
219 Main.unregisterActionShortcut(this);
220 }
221 if (layerChangeAdapter != null) {
222 getLayerManager().removeLayerChangeListener(layerChangeAdapter);
223 getLayerManager().removeActiveLayerChangeListener(activeLayerChangeAdapter);
224 }
225 if (selectionChangeAdapter != null) {
226 DataSet.removeSelectionListener(selectionChangeAdapter);
227 }
228 }
229
230 private void setHelpId() {
231 String helpId = "Action/"+getClass().getName().substring(getClass().getName().lastIndexOf('.')+1);
232 if (helpId.endsWith("Action")) {
233 helpId = helpId.substring(0, helpId.length()-6);
234 }
235 putValue("help", helpId);
236 }
237
238 /**
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 /**
253 * Sets the tooltip text of this action.
254 * @param tooltip The text to display in tooltip. Can be {@code null}
255 */
256 public final void setTooltip(String tooltip) {
257 if (tooltip != null) {
258 putValue(SHORT_DESCRIPTION, Main.platform.makeTooltip(tooltip, sc));
259 }
260 }
261
262 /**
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.
268 * @since 10353
269 */
270 public MainLayerManager getLayerManager() {
271 return Main.getLayerManager();
272 }
273
274 protected static void waitFuture(final Future<?> future, final PleaseWaitProgressMonitor monitor) {
275 Main.worker.submit(() -> {
276 try {
277 future.get();
278 } catch (InterruptedException | ExecutionException | CancellationException e) {
279 Logging.error(e);
280 return;
281 }
282 monitor.close();
283 });
284 }
285
286 /**
287 * Override in subclasses to init the enabled state of an action when it is
288 * created. Default behaviour is to call {@link #updateEnabledState()}
289 *
290 * @see #updateEnabledState()
291 * @see #updateEnabledState(Collection)
292 */
293 protected void initEnabledState() {
294 updateEnabledState();
295 }
296
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.
300 *
301 * See {@link #updateEnabledState(Collection)} to respond to changes in the collection
302 * of selected primitives.
303 *
304 * Default behavior is empty.
305 *
306 * @see #updateEnabledState(Collection)
307 * @see #initEnabledState()
308 * @see #listenToLayerChange()
309 */
310 protected void updateEnabledState() {
311 }
312
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
316 * new selection.
317 *
318 * @param selection the collection of selected primitives; may be empty, but not null
319 *
320 * @see #updateEnabledState()
321 * @see #initEnabledState()
322 * @see #listenToSelectionChange()
323 */
324 protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) {
325 }
326
327 /**
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 /**
342 * Adapter for layer change events. Runs updateEnabledState() whenever the active layer changed.
343 */
344 protected class LayerChangeAdapter implements LayerChangeListener {
345 @Override
346 public void layerAdded(LayerAddEvent e) {
347 updateEnabledState();
348 }
349
350 @Override
351 public void layerRemoving(LayerRemoveEvent e) {
352 updateEnabledState();
353 }
354
355 @Override
356 public void layerOrderChanged(LayerOrderChangeEvent e) {
357 updateEnabledState();
358 }
359
360 @Override
361 public String toString() {
362 return "LayerChangeAdapter [" + JosmAction.this + ']';
363 }
364 }
365
366 /**
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() {
377 return "ActiveLayerChangeAdapter [" + JosmAction.this + ']';
378 }
379 }
380
381 /**
382 * Adapter for selection change events. Runs updateEnabledState() whenever the selection changed.
383 */
384 protected class SelectionChangeAdapter implements SelectionChangedListener {
385 @Override
386 public void selectionChanged(Collection<? extends OsmPrimitive> newSelection) {
387 updateEnabledState(newSelection);
388 }
389
390 @Override
391 public String toString() {
392 return "SelectionChangeAdapter [" + JosmAction.this + ']';
393 }
394 }
395}
Note: See TracBrowser for help on using the repository browser.