[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;
|
---|
| 8 |
|
---|
[30] | 9 | import javax.swing.AbstractAction;
|
---|
[5110] | 10 | import javax.swing.Icon;
|
---|
[30] | 11 |
|
---|
[43] | 12 | import org.openstreetmap.josm.Main;
|
---|
[1820] | 13 | import org.openstreetmap.josm.data.SelectionChangedListener;
|
---|
[558] | 14 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
[1820] | 15 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
[2621] | 16 | import org.openstreetmap.josm.gui.MapView;
|
---|
[5275] | 17 | import org.openstreetmap.josm.gui.MapView.LayerChangeListener;
|
---|
[1820] | 18 | import org.openstreetmap.josm.gui.layer.Layer;
|
---|
[1814] | 19 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
[5984] | 20 | import org.openstreetmap.josm.gui.util.GuiHelper;
|
---|
[208] | 21 | import org.openstreetmap.josm.tools.Destroyable;
|
---|
[71] | 22 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
[1084] | 23 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
[30] | 24 |
|
---|
| 25 | /**
|
---|
| 26 | * Base class helper for all Actions in JOSM. Just to make the life easier.
|
---|
[2305] | 27 | *
|
---|
[5266] | 28 | * A JosmAction is a {@link LayerChangeListener} and a {@link SelectionChangedListener}. Upon
|
---|
[5275] | 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
|
---|
[5266] | 31 | * of a JosmAction depending on the {@link #getCurrentDataSet()} and the current layers
|
---|
| 32 | * (see also {@link #getEditLayer()}).
|
---|
[2305] | 33 | *
|
---|
[208] | 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).
|
---|
[1023] | 37 | *
|
---|
[30] | 38 | * @author imi
|
---|
| 39 | */
|
---|
[208] | 40 | abstract public class JosmAction extends AbstractAction implements Destroyable {
|
---|
[30] | 41 |
|
---|
[1169] | 42 | protected Shortcut sc;
|
---|
[1820] | 43 | private LayerChangeAdapter layerChangeAdapter;
|
---|
| 44 | private SelectionChangeAdapter selectionChangeAdapter;
|
---|
[208] | 45 |
|
---|
[1169] | 46 | public Shortcut getShortcut() {
|
---|
| 47 | if (sc == null) {
|
---|
[4982] | 48 | sc = Shortcut.registerShortcut("core:none", tr("No Shortcut"), KeyEvent.CHAR_UNDEFINED, Shortcut.NONE);
|
---|
[1182] | 49 | // as this shortcut is shared by all action that don't want to have a shortcut,
|
---|
| 50 | // we shouldn't allow the user to change it...
|
---|
| 51 | // this is handled by special name "core:none"
|
---|
[1169] | 52 | }
|
---|
| 53 | return sc;
|
---|
| 54 | }
|
---|
[1023] | 55 |
|
---|
[1169] | 56 | /**
|
---|
[5110] | 57 | * Constructs a {@code JosmAction}.
|
---|
[1169] | 58 | *
|
---|
[1935] | 59 | * @param name the action's text as displayed on the menu (if it is added to a menu)
|
---|
[5110] | 60 | * @param icon the icon to use
|
---|
[1935] | 61 | * @param tooltip a longer description of the action that will be displayed in the tooltip. Please note
|
---|
| 62 | * that html is not supported for menu actions on some platforms.
|
---|
| 63 | * @param shortcut a ready-created shortcut object or null if you don't want a shortcut. But you always
|
---|
| 64 | * do want a shortcut, remember you can always register it with group=none, so you
|
---|
| 65 | * won't be assigned a shortcut unless the user configures one. If you pass null here,
|
---|
[1169] | 66 | * the user CANNOT configure a shortcut for your action.
|
---|
[5110] | 67 | * @param registerInToolbar register this action for the toolbar preferences?
|
---|
[4733] | 68 | * @param toolbarId identifier for the toolbar preferences. The iconName is used, if this parameter is null
|
---|
| 69 | * @param installAdapters false, if you don't want to install layer changed and selection changed adapters
|
---|
[1169] | 70 | */
|
---|
[5110] | 71 | public JosmAction(String name, Icon icon, String tooltip, Shortcut shortcut, boolean registerInToolbar, String toolbarId, boolean installAdapters) {
|
---|
| 72 | super(name, icon);
|
---|
[1169] | 73 | setHelpId();
|
---|
| 74 | sc = shortcut;
|
---|
| 75 | if (sc != null) {
|
---|
[3252] | 76 | Main.registerActionShortcut(this, sc);
|
---|
[1169] | 77 | }
|
---|
[4908] | 78 | setTooltip(tooltip);
|
---|
[4139] | 79 | if (getValue("toolbar") == null) {
|
---|
[5110] | 80 | putValue("toolbar", toolbarId);
|
---|
[4139] | 81 | }
|
---|
[5110] | 82 | if (registerInToolbar) {
|
---|
[1335] | 83 | Main.toolbar.register(this);
|
---|
[1814] | 84 | }
|
---|
[3443] | 85 | if (installAdapters) {
|
---|
| 86 | installAdapters();
|
---|
| 87 | }
|
---|
[1169] | 88 | }
|
---|
[1023] | 89 |
|
---|
[5110] | 90 | /**
|
---|
| 91 | * The new super for all actions.
|
---|
| 92 | *
|
---|
| 93 | * Use this super constructor to setup your action.
|
---|
| 94 | *
|
---|
| 95 | * @param name the action's text as displayed on the menu (if it is added to a menu)
|
---|
| 96 | * @param iconName the filename of the icon to use
|
---|
| 97 | * @param tooltip a longer description of the action that will be displayed in the tooltip. Please note
|
---|
| 98 | * that html is not supported for menu actions on some platforms.
|
---|
| 99 | * @param shortcut a ready-created shortcut object or null if you don't want a shortcut. But you always
|
---|
| 100 | * do want a shortcut, remember you can always register it with group=none, so you
|
---|
| 101 | * won't be assigned a shortcut unless the user configures one. If you pass null here,
|
---|
| 102 | * the user CANNOT configure a shortcut for your action.
|
---|
[5526] | 103 | * @param registerInToolbar register this action for the toolbar preferences?
|
---|
[5110] | 104 | * @param toolbarId identifier for the toolbar preferences. The iconName is used, if this parameter is null
|
---|
| 105 | * @param installAdapters false, if you don't want to install layer changed and selection changed adapters
|
---|
| 106 | */
|
---|
[5526] | 107 | public JosmAction(String name, String iconName, String tooltip, Shortcut shortcut, boolean registerInToolbar, String toolbarId, boolean installAdapters) {
|
---|
| 108 | this(name, iconName == null ? null : ImageProvider.get(iconName), tooltip, shortcut, registerInToolbar,
|
---|
[5110] | 109 | toolbarId == null ? iconName : toolbarId, installAdapters);
|
---|
| 110 | }
|
---|
| 111 |
|
---|
[5526] | 112 | public JosmAction(String name, String iconName, String tooltip, Shortcut shortcut, boolean registerInToolbar, boolean installAdapters) {
|
---|
| 113 | this(name, iconName, tooltip, shortcut, registerInToolbar, null, installAdapters);
|
---|
[4733] | 114 | }
|
---|
| 115 |
|
---|
[5526] | 116 | public JosmAction(String name, String iconName, String tooltip, Shortcut shortcut, boolean registerInToolbar) {
|
---|
| 117 | this(name, iconName, tooltip, shortcut, registerInToolbar, null, true);
|
---|
[4733] | 118 | }
|
---|
| 119 |
|
---|
[1820] | 120 | public JosmAction() {
|
---|
[3327] | 121 | this(true);
|
---|
| 122 | }
|
---|
| 123 |
|
---|
| 124 | public JosmAction(boolean installAdapters) {
|
---|
[1820] | 125 | setHelpId();
|
---|
[3327] | 126 | if (installAdapters) {
|
---|
| 127 | installAdapters();
|
---|
| 128 | }
|
---|
[1820] | 129 | }
|
---|
| 130 |
|
---|
[5459] | 131 | @Override
|
---|
[1169] | 132 | public void destroy() {
|
---|
[1180] | 133 | if (sc != null) {
|
---|
[3444] | 134 | Main.unregisterActionShortcut(this);
|
---|
[1169] | 135 | }
|
---|
[2621] | 136 | MapView.removeLayerChangeListener(layerChangeAdapter);
|
---|
[3416] | 137 | DataSet.removeSelectionListener(selectionChangeAdapter);
|
---|
[1169] | 138 | }
|
---|
[1023] | 139 |
|
---|
[1169] | 140 | private void setHelpId() {
|
---|
| 141 | String helpId = "Action/"+getClass().getName().substring(getClass().getName().lastIndexOf('.')+1);
|
---|
[1814] | 142 | if (helpId.endsWith("Action")) {
|
---|
[1169] | 143 | helpId = helpId.substring(0, helpId.length()-6);
|
---|
[1814] | 144 | }
|
---|
[1169] | 145 | putValue("help", helpId);
|
---|
| 146 | }
|
---|
[1814] | 147 |
|
---|
[4908] | 148 | public void setTooltip(String tooltip) {
|
---|
[5026] | 149 | if (tooltip != null) {
|
---|
| 150 | putValue(SHORT_DESCRIPTION, Main.platform.makeTooltip(tooltip, sc));
|
---|
| 151 | }
|
---|
[4908] | 152 | }
|
---|
| 153 |
|
---|
[1814] | 154 | /**
|
---|
| 155 | * Replies the current edit layer
|
---|
[2305] | 156 | *
|
---|
[1814] | 157 | * @return the current edit layer. null, if no edit layer exists
|
---|
| 158 | */
|
---|
[3835] | 159 | protected static OsmDataLayer getEditLayer() {
|
---|
[1814] | 160 | return Main.main.getEditLayer();
|
---|
| 161 | }
|
---|
| 162 |
|
---|
| 163 | /**
|
---|
| 164 | * Replies the current dataset
|
---|
[2305] | 165 | *
|
---|
[1814] | 166 | * @return the current dataset. null, if no current dataset exists
|
---|
| 167 | */
|
---|
[3835] | 168 | protected static DataSet getCurrentDataSet() {
|
---|
[1814] | 169 | return Main.main.getCurrentDataSet();
|
---|
| 170 | }
|
---|
[1820] | 171 |
|
---|
[2759] | 172 | protected void installAdapters() {
|
---|
[1820] | 173 | // make this action listen to layer change and selection change events
|
---|
| 174 | //
|
---|
| 175 | layerChangeAdapter = new LayerChangeAdapter();
|
---|
| 176 | selectionChangeAdapter = new SelectionChangeAdapter();
|
---|
[2621] | 177 | MapView.addLayerChangeListener(layerChangeAdapter);
|
---|
[3416] | 178 | DataSet.addSelectionListener(selectionChangeAdapter);
|
---|
[2256] | 179 | initEnabledState();
|
---|
[1820] | 180 | }
|
---|
| 181 |
|
---|
[2260] | 182 | /**
|
---|
| 183 | * Override in subclasses to init the enabled state of an action when it is
|
---|
[5266] | 184 | * created. Default behaviour is to call {@link #updateEnabledState()}
|
---|
[2305] | 185 | *
|
---|
[2260] | 186 | * @see #updateEnabledState()
|
---|
| 187 | * @see #updateEnabledState(Collection)
|
---|
| 188 | */
|
---|
[2256] | 189 | protected void initEnabledState() {
|
---|
[2260] | 190 | updateEnabledState();
|
---|
[2256] | 191 | }
|
---|
| 192 |
|
---|
[2260] | 193 | /**
|
---|
| 194 | * Override in subclasses to update the enabled state of the action when
|
---|
| 195 | * something in the JOSM state changes, i.e. when a layer is removed or added.
|
---|
[2305] | 196 | *
|
---|
[5266] | 197 | * See {@link #updateEnabledState(Collection)} to respond to changes in the collection
|
---|
[2260] | 198 | * of selected primitives.
|
---|
[2305] | 199 | *
|
---|
[2260] | 200 | * Default behavior is empty.
|
---|
[2305] | 201 | *
|
---|
[2260] | 202 | * @see #updateEnabledState(Collection)
|
---|
| 203 | * @see #initEnabledState()
|
---|
| 204 | */
|
---|
[1820] | 205 | protected void updateEnabledState() {
|
---|
| 206 | }
|
---|
| 207 |
|
---|
[2260] | 208 | /**
|
---|
| 209 | * Override in subclasses to update the enabled state of the action if the
|
---|
| 210 | * collection of selected primitives changes. This method is called with the
|
---|
[3504] | 211 | * new selection.
|
---|
[2305] | 212 | *
|
---|
[3504] | 213 | * @param selection the collection of selected primitives; may be empty, but not null
|
---|
[2305] | 214 | *
|
---|
[2260] | 215 | * @see #updateEnabledState()
|
---|
| 216 | * @see #initEnabledState()
|
---|
| 217 | */
|
---|
[2256] | 218 | protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) {
|
---|
| 219 | }
|
---|
| 220 |
|
---|
[1820] | 221 | /**
|
---|
| 222 | * Adapter for layer change events
|
---|
| 223 | *
|
---|
| 224 | */
|
---|
[2621] | 225 | private class LayerChangeAdapter implements MapView.LayerChangeListener {
|
---|
[5984] | 226 | private void updateEnabledStateInEDT() {
|
---|
| 227 | GuiHelper.runInEDT(new Runnable() {
|
---|
| 228 | @Override public void run() {
|
---|
| 229 | updateEnabledState();
|
---|
| 230 | }
|
---|
| 231 | });
|
---|
| 232 | }
|
---|
[6084] | 233 | @Override
|
---|
[1820] | 234 | public void activeLayerChange(Layer oldLayer, Layer newLayer) {
|
---|
[5984] | 235 | updateEnabledStateInEDT();
|
---|
[1820] | 236 | }
|
---|
| 237 |
|
---|
[6084] | 238 | @Override
|
---|
[1820] | 239 | public void layerAdded(Layer newLayer) {
|
---|
[5984] | 240 | updateEnabledStateInEDT();
|
---|
[1820] | 241 | }
|
---|
| 242 |
|
---|
[6084] | 243 | @Override
|
---|
[1820] | 244 | public void layerRemoved(Layer oldLayer) {
|
---|
[5984] | 245 | updateEnabledStateInEDT();
|
---|
[1820] | 246 | }
|
---|
| 247 | }
|
---|
| 248 |
|
---|
| 249 | /**
|
---|
| 250 | * Adapter for selection change events
|
---|
| 251 | *
|
---|
| 252 | */
|
---|
| 253 | private class SelectionChangeAdapter implements SelectionChangedListener {
|
---|
[6084] | 254 | @Override
|
---|
[1820] | 255 | public void selectionChanged(Collection<? extends OsmPrimitive> newSelection) {
|
---|
[2256] | 256 | updateEnabledState(newSelection);
|
---|
[1820] | 257 | }
|
---|
| 258 | }
|
---|
[30] | 259 | }
|
---|