1 |
package skrueger.geotools; |
2 |
|
3 |
import java.awt.Cursor; |
4 |
import java.awt.event.InputEvent; |
5 |
import java.awt.event.KeyEvent; |
6 |
import java.util.HashMap; |
7 |
import java.util.Locale; |
8 |
import java.util.Map; |
9 |
|
10 |
import javax.swing.Icon; |
11 |
import javax.swing.ImageIcon; |
12 |
import javax.swing.KeyStroke; |
13 |
|
14 |
import schmitzm.lang.LangUtil; |
15 |
import schmitzm.lang.ResourceProvider; |
16 |
import schmitzm.swing.SwingUtil; |
17 |
import schmitzm.swing.event.MouseInputType; |
18 |
|
19 |
/** |
20 |
* This class combines the mapping of mouse-inputs and keyboard inputs to |
21 |
* {@link XMapPaneAction}s and provides a tool icon and a default mouse cursor. |
22 |
*/ |
23 |
public class XMapPaneTool implements Copyable<XMapPaneTool> { |
24 |
public static ResourceProvider RESOURCE = new ResourceProvider( |
25 |
LangUtil.extendPackagePath(MapPaneToolBar.class, |
26 |
"resource.locales.mapPaneToolbar"), |
27 |
Locale.ENGLISH); |
28 |
public static String R(String key, Object... values) { |
29 |
return RESOURCE.getString(key, values); |
30 |
} |
31 |
|
32 |
/** The cursor of the mouse if the tool is active **/ |
33 |
private Cursor cursor = null; |
34 |
|
35 |
/** A tool-tip for the tool , optional **/ |
36 |
private String toolTip = null; |
37 |
|
38 |
/** The icon for the button **/ |
39 |
private Icon icon = null; |
40 |
|
41 |
/** |
42 |
* Defines which {@link XMapPaneAction} should be should be called when a |
43 |
* {@link MouseInputType} is triggered |
44 |
**/ |
45 |
Map<MouseInputType, XMapPaneAction> mouseActions = new HashMap<MouseInputType, XMapPaneAction>(); |
46 |
|
47 |
/** |
48 |
* Defines which {@link XMapPaneAction#performKeyboard(XMapPane, Object)} |
49 |
* should be called when a {@link KeyStroke} is triggered |
50 |
**/ |
51 |
Map<KeyStroke, XMapPaneAction> keyAction = new HashMap<KeyStroke, XMapPaneAction>(); |
52 |
|
53 |
/** |
54 |
* Defines which optional parameter should be passed to |
55 |
* {@link XMapPaneAction#performKeyboard(XMapPane, Object)} if a |
56 |
* {@link KeyStroke} is triggered |
57 |
**/ |
58 |
Map<KeyStroke, Object> keyActionParams = new HashMap<KeyStroke, Object>(); |
59 |
|
60 |
/** |
61 |
* The default constructor sets some default keyboard settings |
62 |
*/ |
63 |
public XMapPaneTool() { |
64 |
initTool(); |
65 |
} |
66 |
|
67 |
/** |
68 |
* Called by the constructor. Initializes the tool actions by defining default |
69 |
* keyboard actions. |
70 |
*/ |
71 |
protected void initTool() { |
72 |
// + and - keys zoom |
73 |
keyAction.put(KeyStroke.getKeyStroke(KeyEvent.VK_PLUS, 0), |
74 |
XMapPaneAction.ZOOM_IN); |
75 |
keyAction.put(KeyStroke.getKeyStroke(KeyEvent.VK_MINUS, 0), |
76 |
XMapPaneAction.ZOOM_OUT); |
77 |
|
78 |
KeyStroke keyStroke; |
79 |
for (int modifier : new int[] { InputEvent.ALT_DOWN_MASK, |
80 |
InputEvent.ALT_GRAPH_DOWN_MASK, |
81 |
InputEvent.CTRL_DOWN_MASK, |
82 |
InputEvent.SHIFT_DOWN_MASK, |
83 |
InputEvent.META_DOWN_MASK }) { |
84 |
// RIGHT button pan |
85 |
keyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, modifier); |
86 |
keyAction.put(keyStroke, XMapPaneAction.PAN); |
87 |
keyActionParams.put(keyStroke, XMapPaneAction_Pan.Direction.RIGHT); |
88 |
|
89 |
// LEFT button pan |
90 |
keyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, modifier); |
91 |
keyAction.put(keyStroke, XMapPaneAction.PAN); |
92 |
keyActionParams.put(keyStroke, XMapPaneAction_Pan.Direction.LEFT); |
93 |
|
94 |
// UP button pan |
95 |
keyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, modifier); |
96 |
keyAction.put(keyStroke, XMapPaneAction.PAN); |
97 |
keyActionParams.put(keyStroke, XMapPaneAction_Pan.Direction.UP); |
98 |
|
99 |
// DOWN button pan |
100 |
keyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_UP, modifier); |
101 |
keyAction.put(keyStroke, XMapPaneAction.PAN); |
102 |
keyActionParams.put(keyStroke, XMapPaneAction_Pan.Direction.DOWN); |
103 |
} |
104 |
} |
105 |
|
106 |
/** |
107 |
* Returns a copy of the tool to derive other tools. |
108 |
*/ |
109 |
public XMapPaneTool copy() { |
110 |
return copyTo(null); |
111 |
} |
112 |
|
113 |
/** |
114 |
* Copies all actions of this tool to another. |
115 |
* @param tool tool to copy the actions to (if {@code null} a new |
116 |
* tool is created) |
117 |
* @return the modified or created tool |
118 |
*/ |
119 |
public XMapPaneTool copyTo(XMapPaneTool tool) { |
120 |
if ( tool == null ) |
121 |
tool = new XMapPaneTool(); |
122 |
|
123 |
tool.setCursor( getCursor() ); |
124 |
tool.setIcon( getIcon() ); |
125 |
tool.setToolTip( getToolTip() ); |
126 |
for ( MouseInputType type : mouseActions.keySet() ) |
127 |
tool.setMouseAction(type, getMouseAction(type)); |
128 |
for ( KeyStroke stroke : keyAction.keySet() ) |
129 |
tool.setKeyAction(stroke, getKeyAction(stroke)); |
130 |
for ( KeyStroke stroke : keyActionParams.keySet() ) |
131 |
tool.setKeyActionParam(stroke, getKeyActionParam(stroke)); |
132 |
|
133 |
return tool; |
134 |
} |
135 |
|
136 |
/** |
137 |
* @return the {@link Cursor} that shall be set as the default mouse cursor |
138 |
* (when no button is clicked) |
139 |
*/ |
140 |
public Cursor getCursor() { |
141 |
return cursor; |
142 |
} |
143 |
|
144 |
/** |
145 |
* @return the {@link Cursor} that shall be set as the default mouse cursor |
146 |
* (when no button is clicked) |
147 |
*/ |
148 |
public void setCursor(Cursor cursor) { |
149 |
this.cursor = cursor; |
150 |
} |
151 |
|
152 |
public String getToolTip() { |
153 |
return toolTip; |
154 |
} |
155 |
|
156 |
public void setToolTip(String toolTip) { |
157 |
this.toolTip = toolTip; |
158 |
} |
159 |
|
160 |
/** |
161 |
* An icon to use if the tool is associated with a button. May be |
162 |
* <code>null</code>. |
163 |
*/ |
164 |
public Icon getIcon() { |
165 |
return icon; |
166 |
} |
167 |
|
168 |
/** |
169 |
* An icon to use if the tool is associated with a button. May be |
170 |
* <code>null</code>. |
171 |
*/ |
172 |
public void setIcon(Icon icon) { |
173 |
this.icon = icon; |
174 |
} |
175 |
|
176 |
/** |
177 |
* @return The {@link XMapPaneAction} associated with a given |
178 |
* {@link MouseInputType} |
179 |
*/ |
180 |
public XMapPaneAction getMouseAction(MouseInputType type) { |
181 |
return mouseActions.get(type); |
182 |
} |
183 |
|
184 |
/** |
185 |
* Sets the {@link XMapPaneAction} for a given {@link MouseInputType} |
186 |
*/ |
187 |
public void setMouseAction(MouseInputType type, XMapPaneAction mouseAction) { |
188 |
this.mouseActions.put(type, mouseAction); |
189 |
} |
190 |
|
191 |
/** |
192 |
* @return The {@link XMapPaneAction} associated with a {@link KeyStroke} |
193 |
*/ |
194 |
public XMapPaneAction getKeyAction(KeyStroke keyStroke) { |
195 |
return keyAction.get(keyStroke); |
196 |
} |
197 |
|
198 |
/** |
199 |
* Set the {@link XMapPaneAction} for a {@link KeyStroke} |
200 |
*/ |
201 |
public void setKeyAction(KeyStroke keyStroke, XMapPaneAction keyAction) { |
202 |
this.keyAction.put(keyStroke, keyAction); |
203 |
} |
204 |
|
205 |
/** |
206 |
* Get the optional parameter for a{@link XMapPaneAction} when triggered by |
207 |
* {@link KeyStroke} |
208 |
*/ |
209 |
public Object getKeyActionParam(KeyStroke keyStroke) { |
210 |
return keyActionParams.get(keyStroke); |
211 |
} |
212 |
|
213 |
/** |
214 |
* Set the optional parameter for a{@link XMapPaneAction} when triggered by |
215 |
* {@link KeyStroke} |
216 |
*/ |
217 |
public void setKeyActionParam(KeyStroke keyStroke, Object param) { |
218 |
this.keyActionParams.put(keyStroke, param); |
219 |
} |
220 |
|
221 |
/** This {@link XMapPaneTool} does nothing **/ |
222 |
public static XMapPaneTool NO_ACTION = new XMapPaneTool(); |
223 |
static { |
224 |
// Remove the keyboard mapping that are defined by the default |
225 |
// constructor |
226 |
NO_ACTION.keyAction.clear(); |
227 |
NO_ACTION.keyActionParams.clear(); |
228 |
} |
229 |
|
230 |
/** The configuration of the default ZOOM IN {@link XMapPaneTool} **/ |
231 |
public static XMapPaneTool ZOOM_IN = new XMapPaneTool(); |
232 |
static { |
233 |
ZOOM_IN.icon = new ImageIcon( |
234 |
MapView.class.getResource("resource/icons/zoom_in.png")); |
235 |
ZOOM_IN.toolTip = R("MapPaneButtons.ZoomIn.TT"); |
236 |
ZOOM_IN.cursor = SwingUtil.ZOOMIN_CURSOR; |
237 |
|
238 |
// Left mouse click & drag zoom in |
239 |
ZOOM_IN.mouseActions.put(MouseInputType.LClick, XMapPaneAction.ZOOM_IN); |
240 |
ZOOM_IN.mouseActions.put(MouseInputType.LDrag, XMapPaneAction.ZOOM_IN); |
241 |
|
242 |
// Right mouse click & drag zoom out |
243 |
ZOOM_IN.mouseActions.put(MouseInputType.RClick, XMapPaneAction.ZOOM_OUT); |
244 |
ZOOM_IN.mouseActions.put(MouseInputType.RDrag, XMapPaneAction.PAN); |
245 |
|
246 |
// Mousewheel can zoom too |
247 |
ZOOM_IN.mouseActions.put(MouseInputType.Wheel, XMapPaneAction.ZOOM_IN); |
248 |
} |
249 |
|
250 |
/** The configuration of the default ZOOM IN {@link XMapPaneTool} **/ |
251 |
public static XMapPaneTool ZOOM_OUT = new XMapPaneTool(); |
252 |
static { |
253 |
ZOOM_OUT.icon = new ImageIcon( |
254 |
MapView.class.getResource("resource/icons/zoom_out.png")); |
255 |
ZOOM_OUT.toolTip = R("MapPaneButtons.ZoomOut.TT"); |
256 |
ZOOM_OUT.cursor = SwingUtil.ZOOMOUT_CURSOR; |
257 |
|
258 |
// Left mouse click & drag zoom in |
259 |
ZOOM_OUT.mouseActions.put(MouseInputType.LClick, XMapPaneAction.ZOOM_OUT); |
260 |
ZOOM_OUT.mouseActions.put(MouseInputType.LDrag, XMapPaneAction.ZOOM_OUT); |
261 |
|
262 |
// Right mouse click & drag zoom out |
263 |
ZOOM_OUT.mouseActions.put(MouseInputType.RClick, XMapPaneAction.ZOOM_IN); |
264 |
ZOOM_OUT.mouseActions.put(MouseInputType.RDrag, XMapPaneAction.PAN); |
265 |
|
266 |
// Mousewheel can zoom |
267 |
ZOOM_OUT.mouseActions.put(MouseInputType.Wheel, XMapPaneAction.ZOOM_IN); |
268 |
|
269 |
} |
270 |
|
271 |
/** The configuration of the PAN {@link XMapPaneTool} **/ |
272 |
public static final XMapPaneTool PAN = new XMapPaneTool(); |
273 |
static { |
274 |
PAN.icon = new ImageIcon( |
275 |
MapView.class.getResource("resource/icons/pan.png")); |
276 |
PAN.toolTip = R("MapPaneButtons.Pan.TT"); |
277 |
PAN.cursor = SwingUtil.PAN_CURSOR; |
278 |
|
279 |
// Left mouse click & drag zoom in |
280 |
PAN.mouseActions.put(MouseInputType.LClick, XMapPaneAction.PAN); |
281 |
PAN.mouseActions.put(MouseInputType.LDrag, XMapPaneAction.PAN); |
282 |
|
283 |
// Right mouse click & drag zoom out |
284 |
PAN.mouseActions.put(MouseInputType.RClick, XMapPaneAction.PAN); |
285 |
PAN.mouseActions.put(MouseInputType.RDrag, XMapPaneAction.PAN); |
286 |
|
287 |
// Mousewheel can zoom |
288 |
PAN.mouseActions.put(MouseInputType.Wheel, XMapPaneAction.ZOOM_IN); |
289 |
|
290 |
} |
291 |
|
292 |
/** A tool that will do a select_top action on the left mouse button **/ |
293 |
public static final XMapPaneTool SELECTION_TOP_LAYER = new XMapPaneTool(); |
294 |
static { |
295 |
SELECTION_TOP_LAYER.icon = new ImageIcon( |
296 |
MapView.class.getResource("resource/icons/selection_set.png")); |
297 |
|
298 |
SELECTION_TOP_LAYER.cursor = SwingUtil.SELECTION_SET_CURSOR; |
299 |
|
300 |
// Left mouse click & drag zoom in |
301 |
SELECTION_TOP_LAYER.mouseActions.put(MouseInputType.LClick, |
302 |
XMapPaneAction.SELECT_TOP); |
303 |
SELECTION_TOP_LAYER.mouseActions.put(MouseInputType.LDrag, |
304 |
XMapPaneAction.SELECT_TOP); |
305 |
|
306 |
SELECTION_TOP_LAYER.mouseActions.put(MouseInputType.RDrag, |
307 |
XMapPaneAction.PAN); |
308 |
|
309 |
// Mousewheel can zoom |
310 |
SELECTION_TOP_LAYER.mouseActions.put(MouseInputType.Wheel, |
311 |
XMapPaneAction.ZOOM_IN); |
312 |
|
313 |
// TODO Strg-A shoud select all |
314 |
} |
315 |
|
316 |
/** A tool that will do a select_top action on the left mouse button **/ |
317 |
public static final XMapPaneTool SELECTION_ONE_FROM_TOP_LAYER = new XMapPaneTool(); |
318 |
static { |
319 |
SELECTION_ONE_FROM_TOP_LAYER.icon = new ImageIcon( |
320 |
MapView.class.getResource("resource/icons/selection_set.png")); |
321 |
|
322 |
SELECTION_ONE_FROM_TOP_LAYER.cursor = SwingUtil.SELECTION_SET_CURSOR; |
323 |
|
324 |
// Left mouse click & drag zoom in |
325 |
SELECTION_ONE_FROM_TOP_LAYER.mouseActions.put( |
326 |
MouseInputType.LClick, |
327 |
XMapPaneAction.SELECT_ONE_FROM_TOP); |
328 |
SELECTION_ONE_FROM_TOP_LAYER.mouseActions.put( |
329 |
MouseInputType.LDrag, |
330 |
XMapPaneAction.SELECT_ONE_FROM_TOP); |
331 |
|
332 |
SELECTION_ONE_FROM_TOP_LAYER.mouseActions.put(MouseInputType.RDrag, |
333 |
XMapPaneAction.PAN); |
334 |
|
335 |
// Mousewheel can zoom |
336 |
SELECTION_ONE_FROM_TOP_LAYER.mouseActions.put(MouseInputType.Wheel, |
337 |
XMapPaneAction.ZOOM_IN); |
338 |
|
339 |
// TODO Strg-A shoud select all |
340 |
} |
341 |
|
342 |
/** A tool that will do a select_top action on the left mouse button **/ |
343 |
public static final XMapPaneTool SELECTION_ALL_LAYERS = new XMapPaneTool(); |
344 |
static { |
345 |
SELECTION_ALL_LAYERS.icon = new ImageIcon( |
346 |
MapView.class.getResource("resource/icons/selection_set.png")); |
347 |
|
348 |
SELECTION_ALL_LAYERS.cursor = SwingUtil.SELECTION_SET_CURSOR; |
349 |
|
350 |
// Left mouse click & drag zoom in |
351 |
SELECTION_ALL_LAYERS.mouseActions.put(MouseInputType.LClick, |
352 |
XMapPaneAction.SELECT_ALL); |
353 |
SELECTION_ALL_LAYERS.mouseActions.put(MouseInputType.LDrag, |
354 |
XMapPaneAction.SELECT_ALL); |
355 |
|
356 |
SELECTION_ALL_LAYERS.mouseActions.put(MouseInputType.RDrag, |
357 |
XMapPaneAction.PAN); |
358 |
|
359 |
// Mousewheel can zoom |
360 |
SELECTION_ALL_LAYERS.mouseActions.put(MouseInputType.Wheel, |
361 |
XMapPaneAction.ZOOM_IN); |
362 |
|
363 |
// TODO Strg-A shoud select all |
364 |
} |
365 |
|
366 |
/** The configuration of the INFO {@link XMapPaneTool} **/ |
367 |
public static final XMapPaneTool INFO = new XMapPaneTool(); |
368 |
|
369 |
static { |
370 |
INFO.icon = new ImageIcon( |
371 |
MapView.class.getResource("resource/icons/info.png")); |
372 |
INFO.toolTip = R("MapPaneButtons.Info.TT"); |
373 |
INFO.cursor = SwingUtil.INFO_CURSOR; |
374 |
|
375 |
// Left mouse click & drag zoom in |
376 |
INFO.mouseActions.put(MouseInputType.LClick, |
377 |
XMapPaneAction.SELECT_ONE_FROM_TOP); |
378 |
// INFO.mouseActions.put(MouseInputType.LDrag, |
379 |
// XMapPaneAction.SELECT_ONE_FROM_TOP); |
380 |
|
381 |
INFO.mouseActions.put(MouseInputType.RDrag, XMapPaneAction.PAN); |
382 |
|
383 |
// Mousewheel can zoom |
384 |
INFO.mouseActions.put(MouseInputType.Wheel, XMapPaneAction.ZOOM_IN); |
385 |
|
386 |
} |
387 |
|
388 |
public static final XMapPaneTool SELECTION_ADD = new XMapPaneTool(); |
389 |
static { |
390 |
SELECTION_ADD.icon = new ImageIcon( |
391 |
MapView.class.getResource("resource/icons/selection_add.png")); |
392 |
SELECTION_ADD.toolTip = R("MapPaneButtons.Selection.AddSelection.TT"); // TODO |
393 |
// move |
394 |
// to |
395 |
// schmitzm |
396 |
|
397 |
SELECTION_ADD.cursor = SwingUtil.SELECTION_ADD_CURSOR; |
398 |
|
399 |
// Left mouse click & drag zoom in |
400 |
SELECTION_ADD.mouseActions.put(MouseInputType.LClick, |
401 |
XMapPaneAction.SELECT_ALL); |
402 |
SELECTION_ADD.mouseActions.put(MouseInputType.LDrag, |
403 |
XMapPaneAction.SELECT_ALL); |
404 |
|
405 |
SELECTION_ADD.mouseActions.put(MouseInputType.RDrag, XMapPaneAction.PAN); |
406 |
|
407 |
// Mousewheel can zoom |
408 |
SELECTION_ADD.mouseActions.put(MouseInputType.Wheel, XMapPaneAction.ZOOM_IN); |
409 |
|
410 |
// TODO Strg-A shoud select all |
411 |
} |
412 |
|
413 |
public static final XMapPaneTool SELECTION_REMOVE = new XMapPaneTool(); |
414 |
static { |
415 |
SELECTION_REMOVE.icon = new ImageIcon( |
416 |
MapView.class.getResource("resource/icons/selection_remove.png")); |
417 |
SELECTION_REMOVE.toolTip = R("MapPaneButtons.Selection.RemoveSelection.TT"); // TODO |
418 |
// move |
419 |
// to |
420 |
// schmitzm |
421 |
|
422 |
SELECTION_REMOVE.cursor = SwingUtil.SELECTION_REMOVE_CURSOR; |
423 |
|
424 |
// Left mouse click & drag zoom in |
425 |
SELECTION_REMOVE.mouseActions.put(MouseInputType.LClick, |
426 |
XMapPaneAction.SELECT_ALL); |
427 |
SELECTION_REMOVE.mouseActions.put(MouseInputType.LDrag, |
428 |
XMapPaneAction.SELECT_ALL); |
429 |
|
430 |
SELECTION_REMOVE.mouseActions.put(MouseInputType.RDrag, XMapPaneAction.PAN); |
431 |
|
432 |
// Mousewheel can zoom |
433 |
SELECTION_REMOVE.mouseActions.put(MouseInputType.Wheel, |
434 |
XMapPaneAction.ZOOM_IN); |
435 |
|
436 |
// TODO Strg-A shoud select all |
437 |
} |
438 |
|
439 |
public static final XMapPaneTool SELECTION_SET = new XMapPaneTool(); |
440 |
static { |
441 |
SELECTION_SET.icon = new ImageIcon( |
442 |
MapView.class.getResource("resource/icons/selection_set.png")); |
443 |
SELECTION_SET.toolTip = R("MapPaneButtons.Selection.SetSelection.TT"); // TODO |
444 |
// move |
445 |
// to |
446 |
// schmitzm |
447 |
|
448 |
SELECTION_SET.cursor = SwingUtil.SELECTION_SET_CURSOR; |
449 |
|
450 |
// Left mouse click & drag zoom in |
451 |
SELECTION_SET.mouseActions.put(MouseInputType.LClick, |
452 |
XMapPaneAction.SELECT_ALL); |
453 |
SELECTION_SET.mouseActions.put(MouseInputType.LDrag, |
454 |
XMapPaneAction.SELECT_ALL); |
455 |
|
456 |
SELECTION_SET.mouseActions.put(MouseInputType.RDrag, XMapPaneAction.PAN); |
457 |
|
458 |
// Mousewheel can zoom |
459 |
SELECTION_SET.mouseActions.put(MouseInputType.Wheel, XMapPaneAction.ZOOM_IN); |
460 |
|
461 |
// TODO Strg-A shoud select all |
462 |
} |
463 |
|
464 |
} |