[298] | 1 | // License: GPL. Copyright 2007 by Immanuel Scholz and others
|
---|
[30] | 2 | package org.openstreetmap.josm.actions;
|
---|
| 3 |
|
---|
[1182] | 4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
| 5 |
|
---|
[1820] | 6 | import java.util.Collection;
|
---|
| 7 |
|
---|
[30] | 8 | import javax.swing.AbstractAction;
|
---|
[68] | 9 | import javax.swing.JComponent;
|
---|
[30] | 10 |
|
---|
[43] | 11 | import org.openstreetmap.josm.Main;
|
---|
[1820] | 12 | import org.openstreetmap.josm.data.SelectionChangedListener;
|
---|
[558] | 13 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
[1820] | 14 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
[2305] | 15 | import org.openstreetmap.josm.data.osm.PrimitiveDeepCopy;
|
---|
[2621] | 16 | import org.openstreetmap.josm.gui.MapView;
|
---|
[1820] | 17 | import org.openstreetmap.josm.gui.layer.Layer;
|
---|
[1814] | 18 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
[208] | 19 | import org.openstreetmap.josm.tools.Destroyable;
|
---|
[71] | 20 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
[1084] | 21 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
[30] | 22 |
|
---|
| 23 | /**
|
---|
| 24 | * Base class helper for all Actions in JOSM. Just to make the life easier.
|
---|
[2305] | 25 | *
|
---|
[1820] | 26 | * A JosmAction is a {@see LayerChangeListener} and a {@see SelectionChangedListener}. Upon
|
---|
| 27 | * a layer change event or a selection change event it invokes {@see #updateEnabled()}.
|
---|
| 28 | * Subclasses can override {@see #updateEnabled()} in order to update the {@see #isEnabled()}-state
|
---|
| 29 | * of a JosmAction depending on the {@see #getCurrentDataSet()} and the current layers
|
---|
| 30 | * (see also {@see #getEditLayer()}).
|
---|
[2305] | 31 | *
|
---|
[208] | 32 | * destroy() from interface Destroyable is called e.g. for MapModes, when the last layer has
|
---|
| 33 | * been removed and so the mapframe will be destroyed. For other JosmActions, destroy() may never
|
---|
| 34 | * be called (currently).
|
---|
[1023] | 35 | *
|
---|
[30] | 36 | * @author imi
|
---|
| 37 | */
|
---|
[208] | 38 | abstract public class JosmAction extends AbstractAction implements Destroyable {
|
---|
[30] | 39 |
|
---|
[1169] | 40 | protected Shortcut sc;
|
---|
[1820] | 41 | private LayerChangeAdapter layerChangeAdapter;
|
---|
| 42 | private SelectionChangeAdapter selectionChangeAdapter;
|
---|
[208] | 43 |
|
---|
[1169] | 44 | public Shortcut getShortcut() {
|
---|
| 45 | if (sc == null) {
|
---|
[1182] | 46 | sc = Shortcut.registerShortcut("core:none", tr("No Shortcut"), 0, Shortcut.GROUP_NONE);
|
---|
| 47 | // as this shortcut is shared by all action that don't want to have a shortcut,
|
---|
| 48 | // we shouldn't allow the user to change it...
|
---|
| 49 | // this is handled by special name "core:none"
|
---|
[1169] | 50 | }
|
---|
| 51 | return sc;
|
---|
| 52 | }
|
---|
[1023] | 53 |
|
---|
[1169] | 54 | /**
|
---|
| 55 | * The new super for all actions.
|
---|
| 56 | *
|
---|
| 57 | * Use this super constructor to setup your action. It takes 5 parameters:
|
---|
| 58 | *
|
---|
[1935] | 59 | * @param name the action's text as displayed on the menu (if it is added to a menu)
|
---|
| 60 | * @param iconName the filename of the icon to use
|
---|
| 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.
|
---|
[1935] | 67 | * @param register register this action for the toolbar preferences?
|
---|
[1169] | 68 | */
|
---|
| 69 | public JosmAction(String name, String iconName, String tooltip, Shortcut shortcut, boolean register) {
|
---|
| 70 | super(name, iconName == null ? null : ImageProvider.get(iconName));
|
---|
| 71 | setHelpId();
|
---|
| 72 | sc = shortcut;
|
---|
| 73 | if (sc != null) {
|
---|
| 74 | Main.contentPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(sc.getKeyStroke(), name);
|
---|
| 75 | Main.contentPane.getActionMap().put(name, this);
|
---|
| 76 | }
|
---|
| 77 | putValue(SHORT_DESCRIPTION, Main.platform.makeTooltip(tooltip, sc));
|
---|
| 78 | putValue("toolbar", iconName);
|
---|
[1814] | 79 | if (register) {
|
---|
[1335] | 80 | Main.toolbar.register(this);
|
---|
[1814] | 81 | }
|
---|
[1820] | 82 | installAdapters();
|
---|
[1169] | 83 | }
|
---|
[1023] | 84 |
|
---|
[1820] | 85 | public JosmAction() {
|
---|
| 86 | setHelpId();
|
---|
| 87 | installAdapters();
|
---|
| 88 | }
|
---|
| 89 |
|
---|
[1169] | 90 | public void destroy() {
|
---|
[1180] | 91 | if (sc != null) {
|
---|
[1169] | 92 | Main.contentPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).remove(sc.getKeyStroke());
|
---|
| 93 | Main.contentPane.getActionMap().remove(sc.getKeyStroke());
|
---|
| 94 | }
|
---|
[2621] | 95 | MapView.removeLayerChangeListener(layerChangeAdapter);
|
---|
[1820] | 96 | if (DataSet.selListeners != null) {
|
---|
| 97 | DataSet.selListeners.remove(selectionChangeAdapter);
|
---|
| 98 | }
|
---|
[1169] | 99 | }
|
---|
[1023] | 100 |
|
---|
[1169] | 101 | /**
|
---|
| 102 | * needs to be overridden to be useful
|
---|
| 103 | */
|
---|
[2305] | 104 | public void pasteBufferChanged(PrimitiveDeepCopy newPasteBuffer) {
|
---|
[1169] | 105 | return;
|
---|
| 106 | }
|
---|
[1023] | 107 |
|
---|
[1169] | 108 | /**
|
---|
| 109 | * needs to be overridden to be useful
|
---|
| 110 | */
|
---|
| 111 | public void addListener(JosmAction a) {
|
---|
| 112 | return;
|
---|
| 113 | }
|
---|
[1023] | 114 |
|
---|
[1169] | 115 | private void setHelpId() {
|
---|
| 116 | String helpId = "Action/"+getClass().getName().substring(getClass().getName().lastIndexOf('.')+1);
|
---|
[1814] | 117 | if (helpId.endsWith("Action")) {
|
---|
[1169] | 118 | helpId = helpId.substring(0, helpId.length()-6);
|
---|
[1814] | 119 | }
|
---|
[1169] | 120 | putValue("help", helpId);
|
---|
| 121 | }
|
---|
[1814] | 122 |
|
---|
| 123 | /**
|
---|
| 124 | * Replies the current edit layer
|
---|
[2305] | 125 | *
|
---|
[1814] | 126 | * @return the current edit layer. null, if no edit layer exists
|
---|
| 127 | */
|
---|
| 128 | protected OsmDataLayer getEditLayer() {
|
---|
| 129 | return Main.main.getEditLayer();
|
---|
| 130 | }
|
---|
| 131 |
|
---|
| 132 | /**
|
---|
| 133 | * Replies the current dataset
|
---|
[2305] | 134 | *
|
---|
[1814] | 135 | * @return the current dataset. null, if no current dataset exists
|
---|
| 136 | */
|
---|
| 137 | protected DataSet getCurrentDataSet() {
|
---|
| 138 | return Main.main.getCurrentDataSet();
|
---|
| 139 | }
|
---|
[1820] | 140 |
|
---|
| 141 | private void installAdapters() {
|
---|
| 142 | // make this action listen to layer change and selection change events
|
---|
| 143 | //
|
---|
| 144 | layerChangeAdapter = new LayerChangeAdapter();
|
---|
| 145 | selectionChangeAdapter = new SelectionChangeAdapter();
|
---|
[2621] | 146 | MapView.addLayerChangeListener(layerChangeAdapter);
|
---|
[1820] | 147 | DataSet.selListeners.add(selectionChangeAdapter);
|
---|
[2256] | 148 | initEnabledState();
|
---|
[1820] | 149 | }
|
---|
| 150 |
|
---|
[2260] | 151 | /**
|
---|
| 152 | * Override in subclasses to init the enabled state of an action when it is
|
---|
| 153 | * created. Default behaviour is to call {@see #updateEnabledState()}
|
---|
[2305] | 154 | *
|
---|
[2260] | 155 | * @see #updateEnabledState()
|
---|
| 156 | * @see #updateEnabledState(Collection)
|
---|
| 157 | */
|
---|
[2256] | 158 | protected void initEnabledState() {
|
---|
[2260] | 159 | updateEnabledState();
|
---|
[2256] | 160 | }
|
---|
| 161 |
|
---|
[2260] | 162 | /**
|
---|
| 163 | * Override in subclasses to update the enabled state of the action when
|
---|
| 164 | * something in the JOSM state changes, i.e. when a layer is removed or added.
|
---|
[2305] | 165 | *
|
---|
[2260] | 166 | * See {@see #updateEnabledState(Collection)} to respond to changes in the collection
|
---|
| 167 | * of selected primitives.
|
---|
[2305] | 168 | *
|
---|
[2260] | 169 | * Default behavior is empty.
|
---|
[2305] | 170 | *
|
---|
[2260] | 171 | * @see #updateEnabledState(Collection)
|
---|
| 172 | * @see #initEnabledState()
|
---|
| 173 | */
|
---|
[1820] | 174 | protected void updateEnabledState() {
|
---|
| 175 | }
|
---|
| 176 |
|
---|
[2260] | 177 | /**
|
---|
| 178 | * Override in subclasses to update the enabled state of the action if the
|
---|
| 179 | * collection of selected primitives changes. This method is called with the
|
---|
| 180 | * new selection. Avoid calling getCurrentDataSet().getSelected() because this
|
---|
| 181 | * loops over the complete data set.
|
---|
[2305] | 182 | *
|
---|
[2260] | 183 | * @param selection the collection of selected primitives
|
---|
[2305] | 184 | *
|
---|
[2260] | 185 | * @see #updateEnabledState()
|
---|
| 186 | * @see #initEnabledState()
|
---|
| 187 | */
|
---|
[2256] | 188 | protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) {
|
---|
| 189 | }
|
---|
| 190 |
|
---|
[1820] | 191 | /**
|
---|
| 192 | * Adapter for layer change events
|
---|
| 193 | *
|
---|
| 194 | */
|
---|
[2621] | 195 | private class LayerChangeAdapter implements MapView.LayerChangeListener {
|
---|
[1820] | 196 | public void activeLayerChange(Layer oldLayer, Layer newLayer) {
|
---|
| 197 | updateEnabledState();
|
---|
| 198 | }
|
---|
| 199 |
|
---|
| 200 | public void layerAdded(Layer newLayer) {
|
---|
| 201 | updateEnabledState();
|
---|
| 202 | }
|
---|
| 203 |
|
---|
| 204 | public void layerRemoved(Layer oldLayer) {
|
---|
| 205 | updateEnabledState();
|
---|
| 206 | }
|
---|
| 207 | }
|
---|
| 208 |
|
---|
| 209 | /**
|
---|
| 210 | * Adapter for selection change events
|
---|
| 211 | *
|
---|
| 212 | */
|
---|
| 213 | private class SelectionChangeAdapter implements SelectionChangedListener {
|
---|
| 214 | public void selectionChanged(Collection<? extends OsmPrimitive> newSelection) {
|
---|
[2256] | 215 | updateEnabledState(newSelection);
|
---|
[1820] | 216 | }
|
---|
| 217 | }
|
---|
[30] | 218 | }
|
---|