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

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

fix #15766, see #15688 - fix performance regression introduced in r13229 when drawing a way of many nodes while the filter dialog is open

  • Property svn:eol-style set to native
File size: 7.8 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.osm.OsmPrimitive;
15import org.openstreetmap.josm.gui.MainApplication;
16import org.openstreetmap.josm.gui.MapFrame;
17import org.openstreetmap.josm.gui.layer.Layer;
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.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 * Makes this map mode active.
68 */
69 public void enterMode() {
70 putValue("active", Boolean.TRUE);
71 Config.getPref().addPreferenceChangeListener(this);
72 readPreferences();
73 MainApplication.getMap().mapView.setNewCursor(cursor, this);
74 updateStatusLine();
75 }
76
77 /**
78 * Makes this map mode inactive.
79 */
80 public void exitMode() {
81 putValue("active", Boolean.FALSE);
82 Config.getPref().removePreferenceChangeListener(this);
83 MainApplication.getMap().mapView.resetCursor(this);
84 }
85
86 protected void updateStatusLine() {
87 MapFrame map = MainApplication.getMap();
88 if (map != null && map.statusLine != null) {
89 map.statusLine.setHelpText(getModeHelpText());
90 map.statusLine.repaint();
91 }
92 }
93
94 /**
95 * Returns a short translated help message describing how this map mode can be used, to be displayed in status line.
96 * @return a short translated help message describing how this map mode can be used
97 */
98 public String getModeHelpText() {
99 return "";
100 }
101
102 protected void readPreferences() {}
103
104 /**
105 * Call selectMapMode(this) on the parent mapFrame.
106 */
107 @Override
108 public void actionPerformed(ActionEvent e) {
109 if (MainApplication.isDisplayingMapView()) {
110 MainApplication.getMap().selectMapMode(this);
111 }
112 }
113
114 /**
115 * Determines if layer {@code l} is supported by this map mode.
116 * By default, all tools will work with all layers.
117 * Can be overwritten to require a special type of layer
118 * @param l layer
119 * @return {@code true} if the layer is supported by this map mode
120 */
121 public boolean layerIsSupported(Layer l) {
122 return l != null;
123 }
124
125 /**
126 * Update internal ctrl, alt, shift mask from given input event.
127 * @param e input event
128 */
129 protected void updateKeyModifiers(InputEvent e) {
130 updateKeyModifiersEx(e.getModifiersEx());
131 }
132
133 /**
134 * Update internal ctrl, alt, shift mask from given mouse event.
135 * @param e mouse event
136 */
137 protected void updateKeyModifiers(MouseEvent e) {
138 updateKeyModifiersEx(e.getModifiersEx());
139 }
140
141 /**
142 * Update internal ctrl, alt, shift mask from given action event.
143 * @param e action event
144 * @since 12526
145 */
146 protected void updateKeyModifiers(ActionEvent e) {
147 // ActionEvent does not have a getModifiersEx() method like other events :(
148 updateKeyModifiersEx(mapOldModifiers(e.getModifiers()));
149 }
150
151 /**
152 * Update internal ctrl, alt, shift mask from given extended modifiers mask.
153 * @param modifiers event extended modifiers mask
154 * @since 12517
155 */
156 protected void updateKeyModifiersEx(int modifiers) {
157 ctrl = (modifiers & InputEvent.CTRL_DOWN_MASK) != 0;
158 alt = (modifiers & (InputEvent.ALT_DOWN_MASK | InputEvent.ALT_GRAPH_DOWN_MASK)) != 0;
159 shift = (modifiers & InputEvent.SHIFT_DOWN_MASK) != 0;
160 }
161
162 /**
163 * Map old (pre jdk 1.4) modifiers to extended modifiers (only for Ctrl, Alt, Shift).
164 * @param modifiers old modifiers
165 * @return extended modifiers
166 */
167 @SuppressWarnings("deprecation")
168 private static int mapOldModifiers(int modifiers) {
169 if ((modifiers & InputEvent.CTRL_MASK) != 0) {
170 modifiers |= InputEvent.CTRL_DOWN_MASK;
171 }
172 if ((modifiers & InputEvent.ALT_MASK) != 0) {
173 modifiers |= InputEvent.ALT_DOWN_MASK;
174 }
175 if ((modifiers & InputEvent.ALT_GRAPH_MASK) != 0) {
176 modifiers |= InputEvent.ALT_GRAPH_DOWN_MASK;
177 }
178 if ((modifiers & InputEvent.SHIFT_MASK) != 0) {
179 modifiers |= InputEvent.SHIFT_DOWN_MASK;
180 }
181
182 return modifiers;
183 }
184
185 protected void requestFocusInMapView() {
186 if (isEnabled()) {
187 // request focus in order to enable the expected keyboard shortcuts (see #8710)
188 MainApplication.getMap().mapView.requestFocus();
189 }
190 }
191
192 @Override
193 public void mouseReleased(MouseEvent e) {
194 requestFocusInMapView();
195 }
196
197 @Override
198 public void mouseExited(MouseEvent e) {
199 // Do nothing
200 }
201
202 @Override
203 public void mousePressed(MouseEvent e) {
204 requestFocusInMapView();
205 }
206
207 @Override
208 public void mouseClicked(MouseEvent e) {
209 // Do nothing
210 }
211
212 @Override
213 public void mouseEntered(MouseEvent e) {
214 // Do nothing
215 }
216
217 @Override
218 public void mouseMoved(MouseEvent e) {
219 // Do nothing
220 }
221
222 @Override
223 public void mouseDragged(MouseEvent e) {
224 // Do nothing
225 }
226
227 @Override
228 public void preferenceChanged(PreferenceChangeEvent e) {
229 readPreferences();
230 }
231
232 /**
233 * Gets a collection of primitives that should not be hidden by the filter.
234 * @return The primitives that the filter should not hide.
235 * @deprecated use {@link org.openstreetmap.josm.data.osm.DataSet#allPreservedPrimitives}
236 * @since 11993
237 */
238 @Deprecated
239 public Collection<? extends OsmPrimitive> getPreservedPrimitives() {
240 return Collections.emptySet();
241 }
242}
Note: See TracBrowser for help on using the repository browser.