1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.actions.mapmode;
|
---|
3 |
|
---|
4 | import java.awt.Cursor;
|
---|
5 | import java.awt.event.ActionEvent;
|
---|
6 | import java.awt.event.InputEvent;
|
---|
7 | import java.awt.event.MouseEvent;
|
---|
8 | import java.awt.event.MouseListener;
|
---|
9 | import java.awt.event.MouseMotionListener;
|
---|
10 |
|
---|
11 | import org.openstreetmap.josm.Main;
|
---|
12 | import org.openstreetmap.josm.actions.JosmAction;
|
---|
13 | import org.openstreetmap.josm.data.Preferences.PreferenceChangeEvent;
|
---|
14 | import org.openstreetmap.josm.data.Preferences.PreferenceChangedListener;
|
---|
15 | import org.openstreetmap.josm.gui.MapFrame;
|
---|
16 | import org.openstreetmap.josm.gui.layer.Layer;
|
---|
17 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
18 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
19 |
|
---|
20 | /**
|
---|
21 | * A class implementing MapMode is able to be selected as an mode for map editing.
|
---|
22 | * As example scrolling the map is a MapMode, connecting Nodes to new Ways is another.
|
---|
23 | *
|
---|
24 | * MapModes should register/deregister all necessary listeners on the map's view control.
|
---|
25 | */
|
---|
26 | public abstract class MapMode extends JosmAction implements MouseListener, MouseMotionListener, PreferenceChangedListener {
|
---|
27 | protected final Cursor cursor;
|
---|
28 | protected boolean ctrl;
|
---|
29 | protected boolean alt;
|
---|
30 | protected boolean shift;
|
---|
31 |
|
---|
32 | /**
|
---|
33 | * Constructor for mapmodes without a menu
|
---|
34 | * @param name the action's text
|
---|
35 | * @param iconName icon filename in {@code mapmode} directory
|
---|
36 | * @param tooltip a longer description of the action that will be displayed in the tooltip.
|
---|
37 | * @param shortcut a ready-created shortcut object or null if you don't want a shortcut.
|
---|
38 | * @param cursor cursor displayed when map mode is active
|
---|
39 | * @since 11713
|
---|
40 | */
|
---|
41 | public MapMode(String name, String iconName, String tooltip, Shortcut shortcut, Cursor cursor) {
|
---|
42 | super(name, "mapmode/"+iconName, tooltip, shortcut, false);
|
---|
43 | this.cursor = cursor;
|
---|
44 | putValue("active", Boolean.FALSE);
|
---|
45 | }
|
---|
46 |
|
---|
47 | /**
|
---|
48 | * Constructor for mapmodes with a menu (no shortcut will be registered)
|
---|
49 | * @param name the action's text
|
---|
50 | * @param iconName icon filename in {@code mapmode} directory
|
---|
51 | * @param tooltip a longer description of the action that will be displayed in the tooltip.
|
---|
52 | * @param cursor cursor displayed when map mode is active
|
---|
53 | * @since 11713
|
---|
54 | */
|
---|
55 | public MapMode(String name, String iconName, String tooltip, Cursor cursor) {
|
---|
56 | putValue(NAME, name);
|
---|
57 | new ImageProvider("mapmode", iconName).getResource().attachImageIcon(this);
|
---|
58 | putValue(SHORT_DESCRIPTION, tooltip);
|
---|
59 | this.cursor = cursor;
|
---|
60 | }
|
---|
61 |
|
---|
62 | /**
|
---|
63 | * Constructor for mapmodes without a menu
|
---|
64 | * @param name the action's text
|
---|
65 | * @param iconName icon filename in {@code mapmode} directory
|
---|
66 | * @param tooltip a longer description of the action that will be displayed in the tooltip.
|
---|
67 | * @param shortcut a ready-created shortcut object or null if you don't want a shortcut.
|
---|
68 | * @param mapFrame unused but kept for plugin compatibility. Can be {@code null}
|
---|
69 | * @param cursor cursor displayed when map mode is active
|
---|
70 | * @deprecated use {@link #MapMode(String, String, String, Shortcut, Cursor) instead}
|
---|
71 | */
|
---|
72 | @Deprecated
|
---|
73 | public MapMode(String name, String iconName, String tooltip, Shortcut shortcut, MapFrame mapFrame, Cursor cursor) {
|
---|
74 | this(name, iconName, tooltip, shortcut, cursor);
|
---|
75 | }
|
---|
76 |
|
---|
77 | /**
|
---|
78 | * Constructor for mapmodes with a menu (no shortcut will be registered)
|
---|
79 | * @param name the action's text
|
---|
80 | * @param iconName icon filename in {@code mapmode} directory
|
---|
81 | * @param tooltip a longer description of the action that will be displayed in the tooltip.
|
---|
82 | * @param mapFrame unused but kept for plugin compatibility. Can be {@code null}
|
---|
83 | * @param cursor cursor displayed when map mode is active
|
---|
84 | * @deprecated use {@link #MapMode(String, String, String, Cursor) instead}
|
---|
85 | */
|
---|
86 | @Deprecated
|
---|
87 | public MapMode(String name, String iconName, String tooltip, MapFrame mapFrame, Cursor cursor) {
|
---|
88 | this(name, iconName, tooltip, cursor);
|
---|
89 | }
|
---|
90 |
|
---|
91 | /**
|
---|
92 | * Makes this map mode active.
|
---|
93 | */
|
---|
94 | public void enterMode() {
|
---|
95 | putValue("active", Boolean.TRUE);
|
---|
96 | Main.pref.addPreferenceChangeListener(this);
|
---|
97 | readPreferences();
|
---|
98 | Main.map.mapView.setNewCursor(cursor, this);
|
---|
99 | updateStatusLine();
|
---|
100 | }
|
---|
101 |
|
---|
102 | /**
|
---|
103 | * Makes this map mode inactive.
|
---|
104 | */
|
---|
105 | public void exitMode() {
|
---|
106 | putValue("active", Boolean.FALSE);
|
---|
107 | Main.pref.removePreferenceChangeListener(this);
|
---|
108 | Main.map.mapView.resetCursor(this);
|
---|
109 | }
|
---|
110 |
|
---|
111 | protected void updateStatusLine() {
|
---|
112 | Main.map.statusLine.setHelpText(getModeHelpText());
|
---|
113 | Main.map.statusLine.repaint();
|
---|
114 | }
|
---|
115 |
|
---|
116 | public String getModeHelpText() {
|
---|
117 | return "";
|
---|
118 | }
|
---|
119 |
|
---|
120 | protected void readPreferences() {}
|
---|
121 |
|
---|
122 | /**
|
---|
123 | * Call selectMapMode(this) on the parent mapFrame.
|
---|
124 | */
|
---|
125 | @Override
|
---|
126 | public void actionPerformed(ActionEvent e) {
|
---|
127 | if (Main.isDisplayingMapView()) {
|
---|
128 | Main.map.selectMapMode(this);
|
---|
129 | }
|
---|
130 | }
|
---|
131 |
|
---|
132 | /**
|
---|
133 | * Determines if layer {@code l} is supported by this map mode.
|
---|
134 | * By default, all tools will work with all layers.
|
---|
135 | * Can be overwritten to require a special type of layer
|
---|
136 | * @param l layer
|
---|
137 | * @return {@code true} if the layer is supported by this map mode
|
---|
138 | */
|
---|
139 | public boolean layerIsSupported(Layer l) {
|
---|
140 | return l != null;
|
---|
141 | }
|
---|
142 |
|
---|
143 | protected void updateKeyModifiers(InputEvent e) {
|
---|
144 | updateKeyModifiers(e.getModifiers());
|
---|
145 | }
|
---|
146 |
|
---|
147 | protected void updateKeyModifiers(MouseEvent e) {
|
---|
148 | updateKeyModifiers(e.getModifiers());
|
---|
149 | }
|
---|
150 |
|
---|
151 | protected void updateKeyModifiers(int modifiers) {
|
---|
152 | ctrl = (modifiers & ActionEvent.CTRL_MASK) != 0;
|
---|
153 | alt = (modifiers & (ActionEvent.ALT_MASK | InputEvent.ALT_GRAPH_MASK)) != 0;
|
---|
154 | shift = (modifiers & ActionEvent.SHIFT_MASK) != 0;
|
---|
155 | }
|
---|
156 |
|
---|
157 | protected void requestFocusInMapView() {
|
---|
158 | if (isEnabled()) {
|
---|
159 | // request focus in order to enable the expected keyboard shortcuts (see #8710)
|
---|
160 | Main.map.mapView.requestFocus();
|
---|
161 | }
|
---|
162 | }
|
---|
163 |
|
---|
164 | @Override
|
---|
165 | public void mouseReleased(MouseEvent e) {
|
---|
166 | requestFocusInMapView();
|
---|
167 | }
|
---|
168 |
|
---|
169 | @Override
|
---|
170 | public void mouseExited(MouseEvent e) {
|
---|
171 | // Do nothing
|
---|
172 | }
|
---|
173 |
|
---|
174 | @Override
|
---|
175 | public void mousePressed(MouseEvent e) {
|
---|
176 | requestFocusInMapView();
|
---|
177 | }
|
---|
178 |
|
---|
179 | @Override
|
---|
180 | public void mouseClicked(MouseEvent e) {
|
---|
181 | // Do nothing
|
---|
182 | }
|
---|
183 |
|
---|
184 | @Override
|
---|
185 | public void mouseEntered(MouseEvent e) {
|
---|
186 | // Do nothing
|
---|
187 | }
|
---|
188 |
|
---|
189 | @Override
|
---|
190 | public void mouseMoved(MouseEvent e) {
|
---|
191 | // Do nothing
|
---|
192 | }
|
---|
193 |
|
---|
194 | @Override
|
---|
195 | public void mouseDragged(MouseEvent e) {
|
---|
196 | // Do nothing
|
---|
197 | }
|
---|
198 |
|
---|
199 | @Override
|
---|
200 | public void preferenceChanged(PreferenceChangeEvent e) {
|
---|
201 | readPreferences();
|
---|
202 | }
|
---|
203 | }
|
---|