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

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

see #14794 - javadoc

  • Property svn:eol-style set to native
File size: 7.4 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 Main.map.statusLine.setHelpText(getModeHelpText());
116 Main.map.statusLine.repaint();
117 }
118
119 /**
120 * Returns a short translated help message describing how this map mode can be used, to be displayed in status line.
121 * @return a short translated help message describing how this map mode can be used
122 */
123 public String getModeHelpText() {
124 return "";
125 }
126
127 protected void readPreferences() {}
128
129 /**
130 * Call selectMapMode(this) on the parent mapFrame.
131 */
132 @Override
133 public void actionPerformed(ActionEvent e) {
134 if (Main.isDisplayingMapView()) {
135 Main.map.selectMapMode(this);
136 }
137 }
138
139 /**
140 * Determines if layer {@code l} is supported by this map mode.
141 * By default, all tools will work with all layers.
142 * Can be overwritten to require a special type of layer
143 * @param l layer
144 * @return {@code true} if the layer is supported by this map mode
145 */
146 public boolean layerIsSupported(Layer l) {
147 return l != null;
148 }
149
150 protected void updateKeyModifiers(InputEvent e) {
151 updateKeyModifiers(e.getModifiers());
152 }
153
154 protected void updateKeyModifiers(MouseEvent e) {
155 updateKeyModifiers(e.getModifiers());
156 }
157
158 protected void updateKeyModifiers(int modifiers) {
159 ctrl = (modifiers & ActionEvent.CTRL_MASK) != 0;
160 alt = (modifiers & (ActionEvent.ALT_MASK | InputEvent.ALT_GRAPH_MASK)) != 0;
161 shift = (modifiers & ActionEvent.SHIFT_MASK) != 0;
162 }
163
164 protected void requestFocusInMapView() {
165 if (isEnabled()) {
166 // request focus in order to enable the expected keyboard shortcuts (see #8710)
167 Main.map.mapView.requestFocus();
168 }
169 }
170
171 @Override
172 public void mouseReleased(MouseEvent e) {
173 requestFocusInMapView();
174 }
175
176 @Override
177 public void mouseExited(MouseEvent e) {
178 // Do nothing
179 }
180
181 @Override
182 public void mousePressed(MouseEvent e) {
183 requestFocusInMapView();
184 }
185
186 @Override
187 public void mouseClicked(MouseEvent e) {
188 // Do nothing
189 }
190
191 @Override
192 public void mouseEntered(MouseEvent e) {
193 // Do nothing
194 }
195
196 @Override
197 public void mouseMoved(MouseEvent e) {
198 // Do nothing
199 }
200
201 @Override
202 public void mouseDragged(MouseEvent e) {
203 // Do nothing
204 }
205
206 @Override
207 public void preferenceChanged(PreferenceChangeEvent e) {
208 readPreferences();
209 }
210
211 /**
212 * Gets a collection of primitives that should not be hidden by the filter.
213 * @return The primitives that the filter should not hide.
214 * @since 11993
215 */
216 public Collection<? extends OsmPrimitive> getPreservedPrimitives() {
217 return Collections.emptySet();
218 }
219}
Note: See TracBrowser for help on using the repository browser.