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