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

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

see #15182 - move the Swing-based ProgressMonitor implementations from gui.progress to gui.progress.swing. Progress monitor concept is used in very large parts of JOSM, a console-based implementation could be added later

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