[8378] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
[626] | 2 | package org.openstreetmap.josm.actions.mapmode;
|
---|
| 3 |
|
---|
| 4 | import java.awt.Cursor;
|
---|
| 5 | import java.awt.event.ActionEvent;
|
---|
[4327] | 6 | import java.awt.event.InputEvent;
|
---|
[626] | 7 | import java.awt.event.MouseEvent;
|
---|
| 8 | import java.awt.event.MouseListener;
|
---|
| 9 | import java.awt.event.MouseMotionListener;
|
---|
| 10 |
|
---|
[13847] | 11 | import javax.swing.Action;
|
---|
| 12 |
|
---|
[626] | 13 | import org.openstreetmap.josm.actions.JosmAction;
|
---|
[12630] | 14 | import org.openstreetmap.josm.gui.MainApplication;
|
---|
[2017] | 15 | import org.openstreetmap.josm.gui.MapFrame;
|
---|
[1379] | 16 | import org.openstreetmap.josm.gui.layer.Layer;
|
---|
[13434] | 17 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
[12846] | 18 | import org.openstreetmap.josm.spi.preferences.Config;
|
---|
[13173] | 19 | import org.openstreetmap.josm.spi.preferences.PreferenceChangeEvent;
|
---|
| 20 | import org.openstreetmap.josm.spi.preferences.PreferenceChangedListener;
|
---|
[626] | 21 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
[13847] | 22 | import org.openstreetmap.josm.tools.Logging;
|
---|
[18456] | 23 | import org.openstreetmap.josm.tools.PlatformHook;
|
---|
| 24 | import org.openstreetmap.josm.tools.PlatformManager;
|
---|
[1084] | 25 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
[626] | 26 |
|
---|
| 27 | /**
|
---|
| 28 | * A class implementing MapMode is able to be selected as an mode for map editing.
|
---|
[9231] | 29 | * As example scrolling the map is a MapMode, connecting Nodes to new Ways is another.
|
---|
[1023] | 30 | *
|
---|
[8855] | 31 | * MapModes should register/deregister all necessary listeners on the map's view control.
|
---|
[626] | 32 | */
|
---|
[9572] | 33 | public abstract class MapMode extends JosmAction implements MouseListener, MouseMotionListener, PreferenceChangedListener {
|
---|
[3919] | 34 | protected final Cursor cursor;
|
---|
[4327] | 35 | protected boolean ctrl;
|
---|
| 36 | protected boolean alt;
|
---|
| 37 | protected boolean shift;
|
---|
[18456] | 38 | /**
|
---|
| 39 | * {@code true} if the meta key was pressed (the "Windows" key or the Mac "Command" key)
|
---|
[18458] | 40 | * @since 18456
|
---|
[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()
|
---|
[18458] | 46 | * @since 18456
|
---|
[18456] | 47 | */
|
---|
| 48 | protected boolean platformMenuShortcutKeyMask;
|
---|
[626] | 49 |
|
---|
[1169] | 50 | /**
|
---|
[9231] | 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
|
---|
[11713] | 57 | * @since 11713
|
---|
[1169] | 58 | */
|
---|
[16553] | 59 | protected MapMode(String name, String iconName, String tooltip, Shortcut shortcut, Cursor cursor) {
|
---|
[1169] | 60 | super(name, "mapmode/"+iconName, tooltip, shortcut, false);
|
---|
| 61 | this.cursor = cursor;
|
---|
[8377] | 62 | putValue("active", Boolean.FALSE);
|
---|
[1169] | 63 | }
|
---|
[1023] | 64 |
|
---|
[1169] | 65 | /**
|
---|
[9231] | 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
|
---|
[11713] | 71 | * @since 11713
|
---|
[1169] | 72 | */
|
---|
[16553] | 73 | protected MapMode(String name, String iconName, String tooltip, Cursor cursor) {
|
---|
[1169] | 74 | putValue(NAME, name);
|
---|
[11421] | 75 | new ImageProvider("mapmode", iconName).getResource().attachImageIcon(this);
|
---|
[1169] | 76 | putValue(SHORT_DESCRIPTION, tooltip);
|
---|
| 77 | this.cursor = cursor;
|
---|
| 78 | }
|
---|
[626] | 79 |
|
---|
[5960] | 80 | /**
|
---|
| 81 | * Makes this map mode active.
|
---|
| 82 | */
|
---|
[1169] | 83 | public void enterMode() {
|
---|
[13847] | 84 | Logging.debug("Entering map mode: {0}", getValue(Action.NAME));
|
---|
[8377] | 85 | putValue("active", Boolean.TRUE);
|
---|
[12846] | 86 | Config.getPref().addPreferenceChangeListener(this);
|
---|
[9572] | 87 | readPreferences();
|
---|
[12630] | 88 | MainApplication.getMap().mapView.setNewCursor(cursor, this);
|
---|
[17108] | 89 | MainApplication.getMap().statusLine.setAutoLength(true);
|
---|
[1169] | 90 | updateStatusLine();
|
---|
| 91 | }
|
---|
[6069] | 92 |
|
---|
[5960] | 93 | /**
|
---|
| 94 | * Makes this map mode inactive.
|
---|
| 95 | */
|
---|
[1169] | 96 | public void exitMode() {
|
---|
[13847] | 97 | Logging.debug("Exiting map mode: {0}", getValue(Action.NAME));
|
---|
[8377] | 98 | putValue("active", Boolean.FALSE);
|
---|
[12846] | 99 | Config.getPref().removePreferenceChangeListener(this);
|
---|
[12630] | 100 | MainApplication.getMap().mapView.resetCursor(this);
|
---|
[17157] | 101 | MainApplication.getMap().statusLine.setAutoLength(true);
|
---|
[1169] | 102 | }
|
---|
[373] | 103 |
|
---|
[1169] | 104 | protected void updateStatusLine() {
|
---|
[12630] | 105 | MapFrame map = MainApplication.getMap();
|
---|
| 106 | if (map != null && map.statusLine != null) {
|
---|
| 107 | map.statusLine.setHelpText(getModeHelpText());
|
---|
| 108 | map.statusLine.repaint();
|
---|
[12483] | 109 | }
|
---|
[1169] | 110 | }
|
---|
[1023] | 111 |
|
---|
[12284] | 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 | */
|
---|
[1169] | 116 | public String getModeHelpText() {
|
---|
| 117 | return "";
|
---|
| 118 | }
|
---|
[9059] | 119 |
|
---|
[9572] | 120 | protected void readPreferences() {}
|
---|
| 121 |
|
---|
[1169] | 122 | /**
|
---|
| 123 | * Call selectMapMode(this) on the parent mapFrame.
|
---|
| 124 | */
|
---|
[6084] | 125 | @Override
|
---|
[1169] | 126 | public void actionPerformed(ActionEvent e) {
|
---|
[12630] | 127 | if (MainApplication.isDisplayingMapView()) {
|
---|
| 128 | MainApplication.getMap().selectMapMode(this);
|
---|
[5460] | 129 | }
|
---|
[1169] | 130 | }
|
---|
[1677] | 131 |
|
---|
[8855] | 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 | */
|
---|
[1379] | 139 | public boolean layerIsSupported(Layer l) {
|
---|
[8855] | 140 | return l != null;
|
---|
[1379] | 141 | }
|
---|
[626] | 142 |
|
---|
[12526] | 143 | /**
|
---|
| 144 | * Update internal ctrl, alt, shift mask from given input event.
|
---|
| 145 | * @param e input event
|
---|
| 146 | */
|
---|
[4327] | 147 | protected void updateKeyModifiers(InputEvent e) {
|
---|
[12517] | 148 | updateKeyModifiersEx(e.getModifiersEx());
|
---|
[4327] | 149 | }
|
---|
| 150 |
|
---|
[12526] | 151 | /**
|
---|
| 152 | * Update internal ctrl, alt, shift mask from given mouse event.
|
---|
| 153 | * @param e mouse event
|
---|
| 154 | */
|
---|
[4327] | 155 | protected void updateKeyModifiers(MouseEvent e) {
|
---|
[12517] | 156 | updateKeyModifiersEx(e.getModifiersEx());
|
---|
[4327] | 157 | }
|
---|
| 158 |
|
---|
[12517] | 159 | /**
|
---|
[12526] | 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 | /**
|
---|
[18456] | 170 | * Update internal ctrl, alt, shift, meta mask from given extended modifiers mask.
|
---|
[12517] | 171 | * @param modifiers event extended modifiers mask
|
---|
[12518] | 172 | * @since 12517
|
---|
[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;
|
---|
[18456] | 178 | meta = (modifiers & InputEvent.META_DOWN_MASK) != 0;
|
---|
| 179 | platformMenuShortcutKeyMask = (modifiers & PlatformManager.getPlatform().getMenuShortcutKeyMaskEx()) != 0;
|
---|
[12517] | 180 | }
|
---|
| 181 |
|
---|
[12526] | 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) {
|
---|
[19074] | 188 | if ((modifiers & ActionEvent.CTRL_MASK) != 0) {
|
---|
[12526] | 189 | modifiers |= InputEvent.CTRL_DOWN_MASK;
|
---|
| 190 | }
|
---|
[19074] | 191 | if ((modifiers & ActionEvent.META_MASK) != 0) {
|
---|
[18759] | 192 | modifiers |= InputEvent.META_DOWN_MASK;
|
---|
| 193 | }
|
---|
[19074] | 194 | if ((modifiers & ActionEvent.ALT_MASK) != 0) {
|
---|
[12526] | 195 | modifiers |= InputEvent.ALT_DOWN_MASK;
|
---|
| 196 | }
|
---|
[19074] | 197 | /* Old modifier in InputEvent is deprecated, but ActionEvent has no ALT_GRAPH */
|
---|
[12526] | 198 | if ((modifiers & InputEvent.ALT_GRAPH_MASK) != 0) {
|
---|
| 199 | modifiers |= InputEvent.ALT_GRAPH_DOWN_MASK;
|
---|
| 200 | }
|
---|
[19074] | 201 | if ((modifiers & ActionEvent.SHIFT_MASK) != 0) {
|
---|
[12526] | 202 | modifiers |= InputEvent.SHIFT_DOWN_MASK;
|
---|
| 203 | }
|
---|
| 204 |
|
---|
| 205 | return modifiers;
|
---|
| 206 | }
|
---|
| 207 |
|
---|
[5960] | 208 | protected void requestFocusInMapView() {
|
---|
| 209 | if (isEnabled()) {
|
---|
| 210 | // request focus in order to enable the expected keyboard shortcuts (see #8710)
|
---|
[12630] | 211 | MainApplication.getMap().mapView.requestFocus();
|
---|
[5960] | 212 | }
|
---|
| 213 | }
|
---|
[4327] | 214 |
|
---|
[6084] | 215 | @Override
|
---|
[8510] | 216 | public void mouseReleased(MouseEvent e) {
|
---|
| 217 | requestFocusInMapView();
|
---|
| 218 | }
|
---|
| 219 |
|
---|
[6084] | 220 | @Override
|
---|
[8510] | 221 | public void mouseExited(MouseEvent e) {
|
---|
| 222 | // Do nothing
|
---|
| 223 | }
|
---|
| 224 |
|
---|
[6084] | 225 | @Override
|
---|
[8510] | 226 | public void mousePressed(MouseEvent e) {
|
---|
| 227 | requestFocusInMapView();
|
---|
| 228 | }
|
---|
| 229 |
|
---|
[6084] | 230 | @Override
|
---|
[8510] | 231 | public void mouseClicked(MouseEvent e) {
|
---|
| 232 | // Do nothing
|
---|
| 233 | }
|
---|
| 234 |
|
---|
[6084] | 235 | @Override
|
---|
[8510] | 236 | public void mouseEntered(MouseEvent e) {
|
---|
| 237 | // Do nothing
|
---|
| 238 | }
|
---|
| 239 |
|
---|
[6084] | 240 | @Override
|
---|
[8510] | 241 | public void mouseMoved(MouseEvent e) {
|
---|
| 242 | // Do nothing
|
---|
| 243 | }
|
---|
| 244 |
|
---|
[6084] | 245 | @Override
|
---|
[8510] | 246 | public void mouseDragged(MouseEvent e) {
|
---|
| 247 | // Do nothing
|
---|
| 248 | }
|
---|
[9572] | 249 |
|
---|
| 250 | @Override
|
---|
| 251 | public void preferenceChanged(PreferenceChangeEvent e) {
|
---|
| 252 | readPreferences();
|
---|
| 253 | }
|
---|
[11993] | 254 |
|
---|
| 255 | /**
|
---|
[13434] | 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) {
|
---|
[13453] | 263 | return l instanceof OsmDataLayer && !((OsmDataLayer) l).isLocked();
|
---|
[13434] | 264 | }
|
---|
[626] | 265 | }
|
---|