| 1 | // License: GPL. For details, see LICENSE file.
|
|---|
| 2 | package org.openstreetmap.josm.actions;
|
|---|
| 3 |
|
|---|
| 4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
|---|
| 5 |
|
|---|
| 6 | import java.awt.event.KeyEvent;
|
|---|
| 7 | import java.util.Collection;
|
|---|
| 8 | import java.util.concurrent.CancellationException;
|
|---|
| 9 | import java.util.concurrent.ExecutionException;
|
|---|
| 10 | import java.util.concurrent.Future;
|
|---|
| 11 |
|
|---|
| 12 | import javax.swing.AbstractAction;
|
|---|
| 13 |
|
|---|
| 14 | import org.openstreetmap.josm.Main;
|
|---|
| 15 | import org.openstreetmap.josm.data.SelectionChangedListener;
|
|---|
| 16 | import org.openstreetmap.josm.data.osm.DataSet;
|
|---|
| 17 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
|---|
| 18 | import org.openstreetmap.josm.gui.layer.LayerManager.LayerChangeListener;
|
|---|
| 19 | import org.openstreetmap.josm.gui.layer.MainLayerManager.ActiveLayerChangeEvent;
|
|---|
| 20 | import org.openstreetmap.josm.gui.layer.MainLayerManager.ActiveLayerChangeListener;
|
|---|
| 21 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
|---|
| 22 | import org.openstreetmap.josm.gui.progress.PleaseWaitProgressMonitor;
|
|---|
| 23 | import org.openstreetmap.josm.tools.Destroyable;
|
|---|
| 24 | import org.openstreetmap.josm.tools.ImageProvider;
|
|---|
| 25 | import org.openstreetmap.josm.tools.Shortcut;
|
|---|
| 26 |
|
|---|
| 27 | /**
|
|---|
| 28 | * Base class helper for all Actions in JOSM. Just to make the life easier.
|
|---|
| 29 | *
|
|---|
| 30 | * A JosmAction is a {@link LayerChangeListener} and a {@link SelectionChangedListener}. Upon
|
|---|
| 31 | * a layer change event or a selection change event it invokes {@link #updateEnabledState()}.
|
|---|
| 32 | * Subclasses can override {@link #updateEnabledState()} in order to update the {@link #isEnabled()}-state
|
|---|
| 33 | * of a JosmAction depending on the {@link #getCurrentDataSet()} and the current layers
|
|---|
| 34 | * (see also {@link #getEditLayer()}).
|
|---|
| 35 | *
|
|---|
| 36 | * destroy() from interface Destroyable is called e.g. for MapModes, when the last layer has
|
|---|
| 37 | * been removed and so the mapframe will be destroyed. For other JosmActions, destroy() may never
|
|---|
| 38 | * be called (currently).
|
|---|
| 39 | *
|
|---|
| 40 | * @author imi
|
|---|
| 41 | */
|
|---|
| 42 | public abstract class JosmAction extends AbstractAction implements Destroyable {
|
|---|
| 43 |
|
|---|
| 44 | protected transient Shortcut sc;
|
|---|
| 45 | private transient LayerChangeAdapter layerChangeAdapter;
|
|---|
| 46 | private transient SelectionChangeAdapter selectionChangeAdapter;
|
|---|
| 47 |
|
|---|
| 48 | /**
|
|---|
| 49 | * Returns the shortcut for this action.
|
|---|
| 50 | * @return the shortcut for this action, or "No shortcut" if none is defined
|
|---|
| 51 | */
|
|---|
| 52 | public Shortcut getShortcut() {
|
|---|
| 53 | if (sc == null) {
|
|---|
| 54 | sc = Shortcut.registerShortcut("core:none", tr("No Shortcut"), KeyEvent.CHAR_UNDEFINED, Shortcut.NONE);
|
|---|
| 55 | // as this shortcut is shared by all action that don't want to have a shortcut,
|
|---|
| 56 | // we shouldn't allow the user to change it...
|
|---|
| 57 | // this is handled by special name "core:none"
|
|---|
| 58 | }
|
|---|
| 59 | return sc;
|
|---|
| 60 | }
|
|---|
| 61 |
|
|---|
| 62 | /**
|
|---|
| 63 | * Constructs a {@code JosmAction}.
|
|---|
| 64 | *
|
|---|
| 65 | * @param name the action's text as displayed on the menu (if it is added to a menu)
|
|---|
| 66 | * @param icon the icon to use
|
|---|
| 67 | * @param tooltip a longer description of the action that will be displayed in the tooltip. Please note
|
|---|
| 68 | * that html is not supported for menu actions on some platforms.
|
|---|
| 69 | * @param shortcut a ready-created shortcut object or null if you don't want a shortcut. But you always
|
|---|
| 70 | * do want a shortcut, remember you can always register it with group=none, so you
|
|---|
| 71 | * won't be assigned a shortcut unless the user configures one. If you pass null here,
|
|---|
| 72 | * the user CANNOT configure a shortcut for your action.
|
|---|
| 73 | * @param registerInToolbar register this action for the toolbar preferences?
|
|---|
| 74 | * @param toolbarId identifier for the toolbar preferences. The iconName is used, if this parameter is null
|
|---|
| 75 | * @param installAdapters false, if you don't want to install layer changed and selection changed adapters
|
|---|
| 76 | */
|
|---|
| 77 | public JosmAction(String name, ImageProvider icon, String tooltip, Shortcut shortcut, boolean registerInToolbar,
|
|---|
| 78 | String toolbarId, boolean installAdapters) {
|
|---|
| 79 | super(name);
|
|---|
| 80 | if (icon != null)
|
|---|
| 81 | icon.getResource().getImageIcon(this);
|
|---|
| 82 | setHelpId();
|
|---|
| 83 | sc = shortcut;
|
|---|
| 84 | if (sc != null) {
|
|---|
| 85 | Main.registerActionShortcut(this, sc);
|
|---|
| 86 | }
|
|---|
| 87 | setTooltip(tooltip);
|
|---|
| 88 | if (getValue("toolbar") == null) {
|
|---|
| 89 | putValue("toolbar", toolbarId);
|
|---|
| 90 | }
|
|---|
| 91 | if (registerInToolbar && Main.toolbar != null) {
|
|---|
| 92 | Main.toolbar.register(this);
|
|---|
| 93 | }
|
|---|
| 94 | if (installAdapters) {
|
|---|
| 95 | installAdapters();
|
|---|
| 96 | }
|
|---|
| 97 | }
|
|---|
| 98 |
|
|---|
| 99 | /**
|
|---|
| 100 | * The new super for all actions.
|
|---|
| 101 | *
|
|---|
| 102 | * Use this super constructor to setup your action.
|
|---|
| 103 | *
|
|---|
| 104 | * @param name the action's text as displayed on the menu (if it is added to a menu)
|
|---|
| 105 | * @param iconName the filename of the icon to use
|
|---|
| 106 | * @param tooltip a longer description of the action that will be displayed in the tooltip. Please note
|
|---|
| 107 | * that html is not supported for menu actions on some platforms.
|
|---|
| 108 | * @param shortcut a ready-created shortcut object or null if you don't want a shortcut. But you always
|
|---|
| 109 | * do want a shortcut, remember you can always register it with group=none, so you
|
|---|
| 110 | * won't be assigned a shortcut unless the user configures one. If you pass null here,
|
|---|
| 111 | * the user CANNOT configure a shortcut for your action.
|
|---|
| 112 | * @param registerInToolbar register this action for the toolbar preferences?
|
|---|
| 113 | * @param toolbarId identifier for the toolbar preferences. The iconName is used, if this parameter is null
|
|---|
| 114 | * @param installAdapters false, if you don't want to install layer changed and selection changed adapters
|
|---|
| 115 | */
|
|---|
| 116 | public JosmAction(String name, String iconName, String tooltip, Shortcut shortcut, boolean registerInToolbar,
|
|---|
| 117 | String toolbarId, boolean installAdapters) {
|
|---|
| 118 | this(name, iconName == null ? null : new ImageProvider(iconName), tooltip, shortcut, registerInToolbar,
|
|---|
| 119 | toolbarId == null ? iconName : toolbarId, installAdapters);
|
|---|
| 120 | }
|
|---|
| 121 |
|
|---|
| 122 | /**
|
|---|
| 123 | * Constructs a new {@code JosmAction}.
|
|---|
| 124 | *
|
|---|
| 125 | * Use this super constructor to setup your action.
|
|---|
| 126 | *
|
|---|
| 127 | * @param name the action's text as displayed on the menu (if it is added to a menu)
|
|---|
| 128 | * @param iconName the filename of the icon to use
|
|---|
| 129 | * @param tooltip a longer description of the action that will be displayed in the tooltip. Please note
|
|---|
| 130 | * that html is not supported for menu actions on some platforms.
|
|---|
| 131 | * @param shortcut a ready-created shortcut object or null if you don't want a shortcut. But you always
|
|---|
| 132 | * do want a shortcut, remember you can always register it with group=none, so you
|
|---|
| 133 | * won't be assigned a shortcut unless the user configures one. If you pass null here,
|
|---|
| 134 | * the user CANNOT configure a shortcut for your action.
|
|---|
| 135 | * @param registerInToolbar register this action for the toolbar preferences?
|
|---|
| 136 | * @param installAdapters false, if you don't want to install layer changed and selection changed adapters
|
|---|
| 137 | */
|
|---|
| 138 | public JosmAction(String name, String iconName, String tooltip, Shortcut shortcut, boolean registerInToolbar, boolean installAdapters) {
|
|---|
| 139 | this(name, iconName, tooltip, shortcut, registerInToolbar, null, installAdapters);
|
|---|
| 140 | }
|
|---|
| 141 |
|
|---|
| 142 | /**
|
|---|
| 143 | * Constructs a new {@code JosmAction}.
|
|---|
| 144 | *
|
|---|
| 145 | * Use this super constructor to setup your action.
|
|---|
| 146 | *
|
|---|
| 147 | * @param name the action's text as displayed on the menu (if it is added to a menu)
|
|---|
| 148 | * @param iconName the filename of the icon to use
|
|---|
| 149 | * @param tooltip a longer description of the action that will be displayed in the tooltip. Please note
|
|---|
| 150 | * that html is not supported for menu actions on some platforms.
|
|---|
| 151 | * @param shortcut a ready-created shortcut object or null if you don't want a shortcut. But you always
|
|---|
| 152 | * do want a shortcut, remember you can always register it with group=none, so you
|
|---|
| 153 | * won't be assigned a shortcut unless the user configures one. If you pass null here,
|
|---|
| 154 | * the user CANNOT configure a shortcut for your action.
|
|---|
| 155 | * @param registerInToolbar register this action for the toolbar preferences?
|
|---|
| 156 | */
|
|---|
| 157 | public JosmAction(String name, String iconName, String tooltip, Shortcut shortcut, boolean registerInToolbar) {
|
|---|
| 158 | this(name, iconName, tooltip, shortcut, registerInToolbar, null, true);
|
|---|
| 159 | }
|
|---|
| 160 |
|
|---|
| 161 | /**
|
|---|
| 162 | * Constructs a new {@code JosmAction}.
|
|---|
| 163 | */
|
|---|
| 164 | public JosmAction() {
|
|---|
| 165 | this(true);
|
|---|
| 166 | }
|
|---|
| 167 |
|
|---|
| 168 | /**
|
|---|
| 169 | * Constructs a new {@code JosmAction}.
|
|---|
| 170 | *
|
|---|
| 171 | * @param installAdapters false, if you don't want to install layer changed and selection changed adapters
|
|---|
| 172 | */
|
|---|
| 173 | public JosmAction(boolean installAdapters) {
|
|---|
| 174 | setHelpId();
|
|---|
| 175 | if (installAdapters) {
|
|---|
| 176 | installAdapters();
|
|---|
| 177 | }
|
|---|
| 178 | }
|
|---|
| 179 |
|
|---|
| 180 | @Override
|
|---|
| 181 | public void destroy() {
|
|---|
| 182 | if (sc != null) {
|
|---|
| 183 | Main.unregisterActionShortcut(this);
|
|---|
| 184 | }
|
|---|
| 185 | if (layerChangeAdapter != null) {
|
|---|
| 186 | Main.getLayerManager().removeActiveLayerChangeListener(layerChangeAdapter);
|
|---|
| 187 | }
|
|---|
| 188 | DataSet.removeSelectionListener(selectionChangeAdapter);
|
|---|
| 189 | }
|
|---|
| 190 |
|
|---|
| 191 | private void setHelpId() {
|
|---|
| 192 | String helpId = "Action/"+getClass().getName().substring(getClass().getName().lastIndexOf('.')+1);
|
|---|
| 193 | if (helpId.endsWith("Action")) {
|
|---|
| 194 | helpId = helpId.substring(0, helpId.length()-6);
|
|---|
| 195 | }
|
|---|
| 196 | putValue("help", helpId);
|
|---|
| 197 | }
|
|---|
| 198 |
|
|---|
| 199 | /**
|
|---|
| 200 | * Sets the tooltip text of this action.
|
|---|
| 201 | * @param tooltip The text to display in tooltip. Can be {@code null}
|
|---|
| 202 | */
|
|---|
| 203 | public final void setTooltip(String tooltip) {
|
|---|
| 204 | if (tooltip != null) {
|
|---|
| 205 | putValue(SHORT_DESCRIPTION, Main.platform.makeTooltip(tooltip, sc));
|
|---|
| 206 | }
|
|---|
| 207 | }
|
|---|
| 208 |
|
|---|
| 209 | /**
|
|---|
| 210 | * Replies the current edit layer
|
|---|
| 211 | *
|
|---|
| 212 | * @return the current edit layer. null, if no edit layer exists
|
|---|
| 213 | */
|
|---|
| 214 | public static OsmDataLayer getEditLayer() {
|
|---|
| 215 | return Main.main != null ? Main.main.getEditLayer() : null;
|
|---|
| 216 | }
|
|---|
| 217 |
|
|---|
| 218 | /**
|
|---|
| 219 | * Replies the current dataset.
|
|---|
| 220 | *
|
|---|
| 221 | * @return the current dataset. null, if no current dataset exists
|
|---|
| 222 | */
|
|---|
| 223 | public static DataSet getCurrentDataSet() {
|
|---|
| 224 | return Main.main != null ? Main.main.getCurrentDataSet() : null;
|
|---|
| 225 | }
|
|---|
| 226 |
|
|---|
| 227 | protected void installAdapters() {
|
|---|
| 228 | // make this action listen to layer change and selection change events
|
|---|
| 229 | layerChangeAdapter = new LayerChangeAdapter();
|
|---|
| 230 | selectionChangeAdapter = new SelectionChangeAdapter();
|
|---|
| 231 | Main.getLayerManager().addActiveLayerChangeListener(layerChangeAdapter);
|
|---|
| 232 | DataSet.addSelectionListener(selectionChangeAdapter);
|
|---|
| 233 | initEnabledState();
|
|---|
| 234 | }
|
|---|
| 235 |
|
|---|
| 236 | protected static void waitFuture(final Future<?> future, final PleaseWaitProgressMonitor monitor) {
|
|---|
| 237 | Main.worker.submit(
|
|---|
| 238 | new Runnable() {
|
|---|
| 239 | @Override
|
|---|
| 240 | public void run() {
|
|---|
| 241 | try {
|
|---|
| 242 | future.get();
|
|---|
| 243 | } catch (InterruptedException | ExecutionException | CancellationException e) {
|
|---|
| 244 | Main.error(e);
|
|---|
| 245 | return;
|
|---|
| 246 | }
|
|---|
| 247 | monitor.close();
|
|---|
| 248 | }
|
|---|
| 249 | }
|
|---|
| 250 | );
|
|---|
| 251 | }
|
|---|
| 252 |
|
|---|
| 253 | /**
|
|---|
| 254 | * Override in subclasses to init the enabled state of an action when it is
|
|---|
| 255 | * created. Default behaviour is to call {@link #updateEnabledState()}
|
|---|
| 256 | *
|
|---|
| 257 | * @see #updateEnabledState()
|
|---|
| 258 | * @see #updateEnabledState(Collection)
|
|---|
| 259 | */
|
|---|
| 260 | protected void initEnabledState() {
|
|---|
| 261 | updateEnabledState();
|
|---|
| 262 | }
|
|---|
| 263 |
|
|---|
| 264 | /**
|
|---|
| 265 | * Override in subclasses to update the enabled state of the action when
|
|---|
| 266 | * something in the JOSM state changes, i.e. when a layer is removed or added.
|
|---|
| 267 | *
|
|---|
| 268 | * See {@link #updateEnabledState(Collection)} to respond to changes in the collection
|
|---|
| 269 | * of selected primitives.
|
|---|
| 270 | *
|
|---|
| 271 | * Default behavior is empty.
|
|---|
| 272 | *
|
|---|
| 273 | * @see #updateEnabledState(Collection)
|
|---|
| 274 | * @see #initEnabledState()
|
|---|
| 275 | */
|
|---|
| 276 | protected void updateEnabledState() {
|
|---|
| 277 | }
|
|---|
| 278 |
|
|---|
| 279 | /**
|
|---|
| 280 | * Override in subclasses to update the enabled state of the action if the
|
|---|
| 281 | * collection of selected primitives changes. This method is called with the
|
|---|
| 282 | * new selection.
|
|---|
| 283 | *
|
|---|
| 284 | * @param selection the collection of selected primitives; may be empty, but not null
|
|---|
| 285 | *
|
|---|
| 286 | * @see #updateEnabledState()
|
|---|
| 287 | * @see #initEnabledState()
|
|---|
| 288 | */
|
|---|
| 289 | protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) {
|
|---|
| 290 | }
|
|---|
| 291 |
|
|---|
| 292 | /**
|
|---|
| 293 | * Adapter for layer change events. Runs updateEnabledState() whenever the active layer changed.
|
|---|
| 294 | */
|
|---|
| 295 | protected class LayerChangeAdapter implements ActiveLayerChangeListener {
|
|---|
| 296 | @Override
|
|---|
| 297 | public void activeOrEditLayerChanged(ActiveLayerChangeEvent e) {
|
|---|
| 298 | updateEnabledState();
|
|---|
| 299 | }
|
|---|
| 300 |
|
|---|
| 301 | @Override
|
|---|
| 302 | public String toString() {
|
|---|
| 303 | return "LayerChangeAdapter [" + JosmAction.this.toString() + ']';
|
|---|
| 304 | }
|
|---|
| 305 | }
|
|---|
| 306 |
|
|---|
| 307 | /**
|
|---|
| 308 | * Adapter for selection change events. Runs updateEnabledState() whenever the selection changed.
|
|---|
| 309 | */
|
|---|
| 310 | protected class SelectionChangeAdapter implements SelectionChangedListener {
|
|---|
| 311 | @Override
|
|---|
| 312 | public void selectionChanged(Collection<? extends OsmPrimitive> newSelection) {
|
|---|
| 313 | updateEnabledState(newSelection);
|
|---|
| 314 | }
|
|---|
| 315 |
|
|---|
| 316 | @Override
|
|---|
| 317 | public String toString() {
|
|---|
| 318 | return "SelectionChangeAdapter [" + JosmAction.this.toString() + ']';
|
|---|
| 319 | }
|
|---|
| 320 | }
|
|---|
| 321 | }
|
|---|