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