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