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

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

see #11924 - use extended event modifiers, deprecate old methods - see https://bugs.openjdk.java.net/browse/JDK-8143077

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