| 1 | // License: GPL. For details, see LICENSE file.
|
|---|
| 2 | package org.openstreetmap.josm.actions.mapmode;
|
|---|
| 3 |
|
|---|
| 4 | import java.awt.Cursor;
|
|---|
| 5 | import java.awt.event.ActionEvent;
|
|---|
| 6 | import java.awt.event.InputEvent;
|
|---|
| 7 | import java.awt.event.MouseEvent;
|
|---|
| 8 | import java.awt.event.MouseListener;
|
|---|
| 9 | import java.awt.event.MouseMotionListener;
|
|---|
| 10 |
|
|---|
| 11 | import javax.swing.Action;
|
|---|
| 12 |
|
|---|
| 13 | import org.openstreetmap.josm.actions.JosmAction;
|
|---|
| 14 | import org.openstreetmap.josm.gui.MainApplication;
|
|---|
| 15 | import org.openstreetmap.josm.gui.MapFrame;
|
|---|
| 16 | import org.openstreetmap.josm.gui.layer.Layer;
|
|---|
| 17 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
|---|
| 18 | import org.openstreetmap.josm.spi.preferences.Config;
|
|---|
| 19 | import org.openstreetmap.josm.spi.preferences.PreferenceChangeEvent;
|
|---|
| 20 | import org.openstreetmap.josm.spi.preferences.PreferenceChangedListener;
|
|---|
| 21 | import org.openstreetmap.josm.tools.ImageProvider;
|
|---|
| 22 | import org.openstreetmap.josm.tools.Logging;
|
|---|
| 23 | import org.openstreetmap.josm.tools.PlatformHook;
|
|---|
| 24 | import org.openstreetmap.josm.tools.PlatformManager;
|
|---|
| 25 | import org.openstreetmap.josm.tools.Shortcut;
|
|---|
| 26 |
|
|---|
| 27 | /**
|
|---|
| 28 | * A class implementing MapMode is able to be selected as an mode for map editing.
|
|---|
| 29 | * As example scrolling the map is a MapMode, connecting Nodes to new Ways is another.
|
|---|
| 30 | *
|
|---|
| 31 | * MapModes should register/deregister all necessary listeners on the map's view control.
|
|---|
| 32 | */
|
|---|
| 33 | public abstract class MapMode extends JosmAction implements MouseListener, MouseMotionListener, PreferenceChangedListener {
|
|---|
| 34 | protected final Cursor cursor;
|
|---|
| 35 | protected boolean ctrl;
|
|---|
| 36 | protected boolean alt;
|
|---|
| 37 | protected boolean shift;
|
|---|
| 38 | /**
|
|---|
| 39 | * {@code true} if the meta key was pressed (the "Windows" key or the Mac "Command" key)
|
|---|
| 40 | * @since 18456
|
|---|
| 41 | */
|
|---|
| 42 | protected boolean meta;
|
|---|
| 43 | /**
|
|---|
| 44 | * {@code true} if the platform specific menu key was pressed ("ctrl" on Linux/Windows, "cmd" on Mac)
|
|---|
| 45 | * @see PlatformHook#getMenuShortcutKeyMaskEx()
|
|---|
| 46 | * @since 18456
|
|---|
| 47 | */
|
|---|
| 48 | protected boolean platformMenuShortcutKeyMask;
|
|---|
| 49 |
|
|---|
| 50 | /**
|
|---|
| 51 | * Constructor for mapmodes without a menu
|
|---|
| 52 | * @param name the action's text
|
|---|
| 53 | * @param iconName icon filename in {@code mapmode} directory
|
|---|
| 54 | * @param tooltip a longer description of the action that will be displayed in the tooltip.
|
|---|
| 55 | * @param shortcut a ready-created shortcut object or null if you don't want a shortcut.
|
|---|
| 56 | * @param cursor cursor displayed when map mode is active
|
|---|
| 57 | * @since 11713
|
|---|
| 58 | */
|
|---|
| 59 | protected MapMode(String name, String iconName, String tooltip, Shortcut shortcut, Cursor cursor) {
|
|---|
| 60 | super(name, "mapmode/"+iconName, tooltip, shortcut, false);
|
|---|
| 61 | this.cursor = cursor;
|
|---|
| 62 | putValue("active", Boolean.FALSE);
|
|---|
| 63 | }
|
|---|
| 64 |
|
|---|
| 65 | /**
|
|---|
| 66 | * Constructor for mapmodes with a menu (no shortcut will be registered)
|
|---|
| 67 | * @param name the action's text
|
|---|
| 68 | * @param iconName icon filename in {@code mapmode} directory
|
|---|
| 69 | * @param tooltip a longer description of the action that will be displayed in the tooltip.
|
|---|
| 70 | * @param cursor cursor displayed when map mode is active
|
|---|
| 71 | * @since 11713
|
|---|
| 72 | */
|
|---|
| 73 | protected MapMode(String name, String iconName, String tooltip, Cursor cursor) {
|
|---|
| 74 | putValue(NAME, name);
|
|---|
| 75 | new ImageProvider("mapmode", iconName).getResource().attachImageIcon(this);
|
|---|
| 76 | putValue(SHORT_DESCRIPTION, tooltip);
|
|---|
| 77 | this.cursor = cursor;
|
|---|
| 78 | }
|
|---|
| 79 |
|
|---|
| 80 | /**
|
|---|
| 81 | * Makes this map mode active.
|
|---|
| 82 | */
|
|---|
| 83 | public void enterMode() {
|
|---|
| 84 | Logging.debug("Entering map mode: {0}", getValue(Action.NAME));
|
|---|
| 85 | putValue("active", Boolean.TRUE);
|
|---|
| 86 | Config.getPref().addPreferenceChangeListener(this);
|
|---|
| 87 | readPreferences();
|
|---|
| 88 | MainApplication.getMap().mapView.setNewCursor(cursor, this);
|
|---|
| 89 | MainApplication.getMap().statusLine.setAutoLength(true);
|
|---|
| 90 | updateStatusLine();
|
|---|
| 91 | }
|
|---|
| 92 |
|
|---|
| 93 | /**
|
|---|
| 94 | * Makes this map mode inactive.
|
|---|
| 95 | */
|
|---|
| 96 | public void exitMode() {
|
|---|
| 97 | Logging.debug("Exiting map mode: {0}", getValue(Action.NAME));
|
|---|
| 98 | putValue("active", Boolean.FALSE);
|
|---|
| 99 | Config.getPref().removePreferenceChangeListener(this);
|
|---|
| 100 | MainApplication.getMap().mapView.resetCursor(this);
|
|---|
| 101 | MainApplication.getMap().statusLine.setAutoLength(true);
|
|---|
| 102 | }
|
|---|
| 103 |
|
|---|
| 104 | protected void updateStatusLine() {
|
|---|
| 105 | MapFrame map = MainApplication.getMap();
|
|---|
| 106 | if (map != null && map.statusLine != null) {
|
|---|
| 107 | map.statusLine.setHelpText(getModeHelpText());
|
|---|
| 108 | map.statusLine.repaint();
|
|---|
| 109 | }
|
|---|
| 110 | }
|
|---|
| 111 |
|
|---|
| 112 | /**
|
|---|
| 113 | * Returns a short translated help message describing how this map mode can be used, to be displayed in status line.
|
|---|
| 114 | * @return a short translated help message describing how this map mode can be used
|
|---|
| 115 | */
|
|---|
| 116 | public String getModeHelpText() {
|
|---|
| 117 | return "";
|
|---|
| 118 | }
|
|---|
| 119 |
|
|---|
| 120 | protected void readPreferences() {}
|
|---|
| 121 |
|
|---|
| 122 | /**
|
|---|
| 123 | * Call selectMapMode(this) on the parent mapFrame.
|
|---|
| 124 | */
|
|---|
| 125 | @Override
|
|---|
| 126 | public void actionPerformed(ActionEvent e) {
|
|---|
| 127 | if (MainApplication.isDisplayingMapView()) {
|
|---|
| 128 | MainApplication.getMap().selectMapMode(this);
|
|---|
| 129 | }
|
|---|
| 130 | }
|
|---|
| 131 |
|
|---|
| 132 | /**
|
|---|
| 133 | * Determines if layer {@code l} is supported by this map mode.
|
|---|
| 134 | * By default, all tools will work with all layers.
|
|---|
| 135 | * Can be overwritten to require a special type of layer
|
|---|
| 136 | * @param l layer
|
|---|
| 137 | * @return {@code true} if the layer is supported by this map mode
|
|---|
| 138 | */
|
|---|
| 139 | public boolean layerIsSupported(Layer l) {
|
|---|
| 140 | return l != null;
|
|---|
| 141 | }
|
|---|
| 142 |
|
|---|
| 143 | /**
|
|---|
| 144 | * Update internal ctrl, alt, shift mask from given input event.
|
|---|
| 145 | * @param e input event
|
|---|
| 146 | */
|
|---|
| 147 | protected void updateKeyModifiers(InputEvent e) {
|
|---|
| 148 | updateKeyModifiersEx(e.getModifiersEx());
|
|---|
| 149 | }
|
|---|
| 150 |
|
|---|
| 151 | /**
|
|---|
| 152 | * Update internal ctrl, alt, shift mask from given mouse event.
|
|---|
| 153 | * @param e mouse event
|
|---|
| 154 | */
|
|---|
| 155 | protected void updateKeyModifiers(MouseEvent e) {
|
|---|
| 156 | updateKeyModifiersEx(e.getModifiersEx());
|
|---|
| 157 | }
|
|---|
| 158 |
|
|---|
| 159 | /**
|
|---|
| 160 | * Update internal ctrl, alt, shift mask from given action event.
|
|---|
| 161 | * @param e action event
|
|---|
| 162 | * @since 12526
|
|---|
| 163 | */
|
|---|
| 164 | protected void updateKeyModifiers(ActionEvent e) {
|
|---|
| 165 | // ActionEvent does not have a getModifiersEx() method like other events :(
|
|---|
| 166 | updateKeyModifiersEx(mapOldModifiers(e.getModifiers()));
|
|---|
| 167 | }
|
|---|
| 168 |
|
|---|
| 169 | /**
|
|---|
| 170 | * Update internal ctrl, alt, shift, meta mask from given extended modifiers mask.
|
|---|
| 171 | * @param modifiers event extended modifiers mask
|
|---|
| 172 | * @since 12517
|
|---|
| 173 | */
|
|---|
| 174 | protected void updateKeyModifiersEx(int modifiers) {
|
|---|
| 175 | ctrl = (modifiers & InputEvent.CTRL_DOWN_MASK) != 0;
|
|---|
| 176 | alt = (modifiers & (InputEvent.ALT_DOWN_MASK | InputEvent.ALT_GRAPH_DOWN_MASK)) != 0;
|
|---|
| 177 | shift = (modifiers & InputEvent.SHIFT_DOWN_MASK) != 0;
|
|---|
| 178 | meta = (modifiers & InputEvent.META_DOWN_MASK) != 0;
|
|---|
| 179 | platformMenuShortcutKeyMask = (modifiers & PlatformManager.getPlatform().getMenuShortcutKeyMaskEx()) != 0;
|
|---|
| 180 | }
|
|---|
| 181 |
|
|---|
| 182 | /**
|
|---|
| 183 | * Map old (pre jdk 1.4) modifiers to extended modifiers (only for Ctrl, Alt, Shift).
|
|---|
| 184 | * @param modifiers old modifiers
|
|---|
| 185 | * @return extended modifiers
|
|---|
| 186 | */
|
|---|
| 187 | private static int mapOldModifiers(int modifiers) {
|
|---|
| 188 | if ((modifiers & ActionEvent.CTRL_MASK) != 0) {
|
|---|
| 189 | modifiers |= InputEvent.CTRL_DOWN_MASK;
|
|---|
| 190 | }
|
|---|
| 191 | if ((modifiers & ActionEvent.META_MASK) != 0) {
|
|---|
| 192 | modifiers |= InputEvent.META_DOWN_MASK;
|
|---|
| 193 | }
|
|---|
| 194 | if ((modifiers & ActionEvent.ALT_MASK) != 0) {
|
|---|
| 195 | modifiers |= InputEvent.ALT_DOWN_MASK;
|
|---|
| 196 | }
|
|---|
| 197 | /* Old modifier in InputEvent is deprecated, but ActionEvent has no ALT_GRAPH */
|
|---|
| 198 | if ((modifiers & InputEvent.ALT_GRAPH_MASK) != 0) {
|
|---|
| 199 | modifiers |= InputEvent.ALT_GRAPH_DOWN_MASK;
|
|---|
| 200 | }
|
|---|
| 201 | if ((modifiers & ActionEvent.SHIFT_MASK) != 0) {
|
|---|
| 202 | modifiers |= InputEvent.SHIFT_DOWN_MASK;
|
|---|
| 203 | }
|
|---|
| 204 |
|
|---|
| 205 | return modifiers;
|
|---|
| 206 | }
|
|---|
| 207 |
|
|---|
| 208 | protected void requestFocusInMapView() {
|
|---|
| 209 | if (isEnabled()) {
|
|---|
| 210 | // request focus in order to enable the expected keyboard shortcuts (see #8710)
|
|---|
| 211 | MainApplication.getMap().mapView.requestFocus();
|
|---|
| 212 | }
|
|---|
| 213 | }
|
|---|
| 214 |
|
|---|
| 215 | @Override
|
|---|
| 216 | public void mouseReleased(MouseEvent e) {
|
|---|
| 217 | requestFocusInMapView();
|
|---|
| 218 | }
|
|---|
| 219 |
|
|---|
| 220 | @Override
|
|---|
| 221 | public void mouseExited(MouseEvent e) {
|
|---|
| 222 | // Do nothing
|
|---|
| 223 | }
|
|---|
| 224 |
|
|---|
| 225 | @Override
|
|---|
| 226 | public void mousePressed(MouseEvent e) {
|
|---|
| 227 | requestFocusInMapView();
|
|---|
| 228 | }
|
|---|
| 229 |
|
|---|
| 230 | @Override
|
|---|
| 231 | public void mouseClicked(MouseEvent e) {
|
|---|
| 232 | // Do nothing
|
|---|
| 233 | }
|
|---|
| 234 |
|
|---|
| 235 | @Override
|
|---|
| 236 | public void mouseEntered(MouseEvent e) {
|
|---|
| 237 | // Do nothing
|
|---|
| 238 | }
|
|---|
| 239 |
|
|---|
| 240 | @Override
|
|---|
| 241 | public void mouseMoved(MouseEvent e) {
|
|---|
| 242 | // Do nothing
|
|---|
| 243 | }
|
|---|
| 244 |
|
|---|
| 245 | @Override
|
|---|
| 246 | public void mouseDragged(MouseEvent e) {
|
|---|
| 247 | // Do nothing
|
|---|
| 248 | }
|
|---|
| 249 |
|
|---|
| 250 | @Override
|
|---|
| 251 | public void preferenceChanged(PreferenceChangeEvent e) {
|
|---|
| 252 | readPreferences();
|
|---|
| 253 | }
|
|---|
| 254 |
|
|---|
| 255 | /**
|
|---|
| 256 | * Determines if the given layer is a data layer that can be modified.
|
|---|
| 257 | * Useful for {@link #layerIsSupported(Layer)} implementations.
|
|---|
| 258 | * @param l layer
|
|---|
| 259 | * @return {@code true} if the given layer is a data layer that can be modified
|
|---|
| 260 | * @since 13434
|
|---|
| 261 | */
|
|---|
| 262 | protected boolean isEditableDataLayer(Layer l) {
|
|---|
| 263 | return l instanceof OsmDataLayer && !((OsmDataLayer) l).isLocked();
|
|---|
| 264 | }
|
|---|
| 265 | }
|
|---|