001// License: GPL. For details, see LICENSE file. 002package org.openstreetmap.josm.actions.mapmode; 003 004import java.awt.Cursor; 005import java.awt.event.ActionEvent; 006import java.awt.event.InputEvent; 007import java.awt.event.MouseEvent; 008import java.awt.event.MouseListener; 009import java.awt.event.MouseMotionListener; 010 011import javax.swing.Action; 012 013import org.openstreetmap.josm.actions.JosmAction; 014import org.openstreetmap.josm.gui.MainApplication; 015import org.openstreetmap.josm.gui.MapFrame; 016import org.openstreetmap.josm.gui.layer.Layer; 017import org.openstreetmap.josm.gui.layer.OsmDataLayer; 018import org.openstreetmap.josm.spi.preferences.Config; 019import org.openstreetmap.josm.spi.preferences.PreferenceChangeEvent; 020import org.openstreetmap.josm.spi.preferences.PreferenceChangedListener; 021import org.openstreetmap.josm.tools.ImageProvider; 022import org.openstreetmap.josm.tools.Logging; 023import org.openstreetmap.josm.tools.PlatformHook; 024import org.openstreetmap.josm.tools.PlatformManager; 025import org.openstreetmap.josm.tools.Shortcut; 026 027/** 028 * A class implementing MapMode is able to be selected as an mode for map editing. 029 * As example scrolling the map is a MapMode, connecting Nodes to new Ways is another. 030 * 031 * MapModes should register/deregister all necessary listeners on the map's view control. 032 */ 033public abstract class MapMode extends JosmAction implements MouseListener, MouseMotionListener, PreferenceChangedListener { 034 protected final Cursor cursor; 035 protected boolean ctrl; 036 protected boolean alt; 037 protected boolean shift; 038 /** 039 * {@code true} if the meta key was pressed (the "Windows" key or the Mac "Command" key) 040 * @since 18456 041 */ 042 protected boolean meta; 043 /** 044 * {@code true} if the platform specific menu key was pressed ("ctrl" on Linux/Windows, "cmd" on Mac) 045 * @see PlatformHook#getMenuShortcutKeyMaskEx() 046 * @since 18456 047 */ 048 protected boolean platformMenuShortcutKeyMask; 049 050 /** 051 * Constructor for mapmodes without a menu 052 * @param name the action's text 053 * @param iconName icon filename in {@code mapmode} directory 054 * @param tooltip a longer description of the action that will be displayed in the tooltip. 055 * @param shortcut a ready-created shortcut object or null if you don't want a shortcut. 056 * @param cursor cursor displayed when map mode is active 057 * @since 11713 058 */ 059 protected MapMode(String name, String iconName, String tooltip, Shortcut shortcut, Cursor cursor) { 060 super(name, "mapmode/"+iconName, tooltip, shortcut, false); 061 this.cursor = cursor; 062 putValue("active", Boolean.FALSE); 063 } 064 065 /** 066 * Constructor for mapmodes with a menu (no shortcut will be registered) 067 * @param name the action's text 068 * @param iconName icon filename in {@code mapmode} directory 069 * @param tooltip a longer description of the action that will be displayed in the tooltip. 070 * @param cursor cursor displayed when map mode is active 071 * @since 11713 072 */ 073 protected MapMode(String name, String iconName, String tooltip, Cursor cursor) { 074 putValue(NAME, name); 075 new ImageProvider("mapmode", iconName).getResource().attachImageIcon(this); 076 putValue(SHORT_DESCRIPTION, tooltip); 077 this.cursor = cursor; 078 } 079 080 /** 081 * Makes this map mode active. 082 */ 083 public void enterMode() { 084 Logging.debug("Entering map mode: {0}", getValue(Action.NAME)); 085 putValue("active", Boolean.TRUE); 086 Config.getPref().addPreferenceChangeListener(this); 087 readPreferences(); 088 MainApplication.getMap().mapView.setNewCursor(cursor, this); 089 MainApplication.getMap().statusLine.setAutoLength(true); 090 updateStatusLine(); 091 } 092 093 /** 094 * Makes this map mode inactive. 095 */ 096 public void exitMode() { 097 Logging.debug("Exiting map mode: {0}", getValue(Action.NAME)); 098 putValue("active", Boolean.FALSE); 099 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 @SuppressWarnings("deprecation") 188 private static int mapOldModifiers(int modifiers) { 189 if ((modifiers & ActionEvent.CTRL_MASK) != 0) { 190 modifiers |= InputEvent.CTRL_DOWN_MASK; 191 } 192 if ((modifiers & ActionEvent.META_MASK) != 0) { 193 modifiers |= InputEvent.META_DOWN_MASK; 194 } 195 if ((modifiers & ActionEvent.ALT_MASK) != 0) { 196 modifiers |= InputEvent.ALT_DOWN_MASK; 197 } 198 /* Old modifier in InputEvent is deprecated, but ActionEvent has no ALT_GRAPH */ 199 if ((modifiers & InputEvent.ALT_GRAPH_MASK) != 0) { 200 modifiers |= InputEvent.ALT_GRAPH_DOWN_MASK; 201 } 202 if ((modifiers & ActionEvent.SHIFT_MASK) != 0) { 203 modifiers |= InputEvent.SHIFT_DOWN_MASK; 204 } 205 206 return modifiers; 207 } 208 209 protected void requestFocusInMapView() { 210 if (isEnabled()) { 211 // request focus in order to enable the expected keyboard shortcuts (see #8710) 212 MainApplication.getMap().mapView.requestFocus(); 213 } 214 } 215 216 @Override 217 public void mouseReleased(MouseEvent e) { 218 requestFocusInMapView(); 219 } 220 221 @Override 222 public void mouseExited(MouseEvent e) { 223 // Do nothing 224 } 225 226 @Override 227 public void mousePressed(MouseEvent e) { 228 requestFocusInMapView(); 229 } 230 231 @Override 232 public void mouseClicked(MouseEvent e) { 233 // Do nothing 234 } 235 236 @Override 237 public void mouseEntered(MouseEvent e) { 238 // Do nothing 239 } 240 241 @Override 242 public void mouseMoved(MouseEvent e) { 243 // Do nothing 244 } 245 246 @Override 247 public void mouseDragged(MouseEvent e) { 248 // Do nothing 249 } 250 251 @Override 252 public void preferenceChanged(PreferenceChangeEvent e) { 253 readPreferences(); 254 } 255 256 /** 257 * Determines if the given layer is a data layer that can be modified. 258 * Useful for {@link #layerIsSupported(Layer)} implementations. 259 * @param l layer 260 * @return {@code true} if the given layer is a data layer that can be modified 261 * @since 13434 262 */ 263 protected boolean isEditableDataLayer(Layer l) { 264 return l instanceof OsmDataLayer && !((OsmDataLayer) l).isLocked(); 265 } 266 267 /** 268 * Determines if the user is currently panning the map. 269 * @return {@code true} if a pan is in progress 270 * @since 19701 271 */ 272 protected static boolean isPanning() { 273 return MainApplication.isDisplayingMapView() && MainApplication.getMap().mapView.isPanning(); 274 } 275}