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

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

fix #8039, fix #10456: final fixes for the read-only/locked layers:

  • rename "read-only" to "locked" (in XML and Java classes/interfaces)
  • add a new download policy (true/never) to allow private layers forbidding only to download data, but allowing everything else

This leads to:

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