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

Last change on this file since 16553 was 16553, checked in by Don-vip, 4 years ago

see #19334 - javadoc fixes + protected constructors for abstract classes

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