source: josm/trunk/src/org/openstreetmap/josm/actions/mapmode/MapMode.java@ 12846

Last change on this file since 12846 was 12846, checked in by bastiK, 7 years ago

see #15229 - use Config.getPref() wherever possible

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