1 |
package skrueger.geotools; |
2 |
|
3 |
import java.awt.Dimension; |
4 |
import java.awt.event.ActionEvent; |
5 |
import java.util.ArrayList; |
6 |
import java.util.Locale; |
7 |
import java.util.SortedMap; |
8 |
import java.util.TreeMap; |
9 |
|
10 |
import javax.swing.AbstractAction; |
11 |
import javax.swing.AbstractButton; |
12 |
import javax.swing.Action; |
13 |
import javax.swing.BorderFactory; |
14 |
import javax.swing.Icon; |
15 |
import javax.swing.ImageIcon; |
16 |
import javax.swing.JButton; |
17 |
import javax.swing.JComponent; |
18 |
import javax.swing.JToggleButton; |
19 |
import javax.swing.JToolBar; |
20 |
|
21 |
import org.apache.log4j.Logger; |
22 |
|
23 |
import schmitzm.geotools.gui.JMapPane; |
24 |
import schmitzm.geotools.map.event.JMapPaneEvent; |
25 |
import schmitzm.geotools.map.event.JMapPaneListener; |
26 |
import schmitzm.geotools.map.event.MapAreaChangedEvent; |
27 |
import schmitzm.lang.LangUtil; |
28 |
import schmitzm.lang.ResourceProvider; |
29 |
import schmitzm.swing.ButtonGroup; |
30 |
import schmitzm.swing.SwingUtil; |
31 |
|
32 |
import com.vividsolutions.jts.geom.Envelope; |
33 |
|
34 |
/** |
35 |
* A toolbar to control an {@link JMapPane} (Atlas visualization). This contains |
36 |
* two types of buttons. A group of <i>tools</i> for the mouse actions on the |
37 |
* map represented by {@link JToggleButton JToggleButtons}, where only one tool |
38 |
* can be activated every time. And some (general) <i>actions</i>, represented |
39 |
* by normal {@link JButton JButtons}. |
40 |
* |
41 |
* @author <a href="mailto:[email protected]">Martin Schmitz</a> |
42 |
* (University of Bonn/Germany) |
43 |
* @version 1.2 Stefan Krüger |
44 |
*/ |
45 |
public class MapPaneToolBar extends JToolBar { |
46 |
private static final Logger LOGGER = Logger.getLogger(MapPaneToolBar.class |
47 |
.getName()); |
48 |
|
49 |
public static ResourceProvider RESOURCE = new ResourceProvider(LangUtil |
50 |
.extendPackagePath(MapPaneToolBar.class, |
51 |
"resource.locales.mapPaneToolbar"), Locale.ENGLISH); |
52 |
|
53 |
public static String R(String key, Object... values) { |
54 |
return RESOURCE.getString(key, values); |
55 |
} |
56 |
|
57 |
/** Constant for the tool "Panning" (10). */ |
58 |
public static final int TOOL_PAN = 10; |
59 |
/** Constant for the tool "Info" (20). */ |
60 |
public static final int TOOL_INFO = 20; |
61 |
public static final int SEPERATOR0 = 99; |
62 |
|
63 |
/** Constant for the tool "Zoom In" (110). */ |
64 |
public static final int TOOL_ZOOMIN = 110; |
65 |
/** Constant for the tool "Zoom Out" (120). */ |
66 |
public static final int TOOL_ZOOMOUT = 120; |
67 |
/** Constant for the action "Zoom back" (130). */ |
68 |
public static final int ACTION_ZOOM_BACK = 130; |
69 |
/** Constant for the action "Zoom forward" (140). */ |
70 |
public static final int ACTION_ZOOM_FORWARD = 140; |
71 |
public static final int SEPERATOR1 = 199; |
72 |
|
73 |
/** |
74 |
* Constant for the tool "Selection Reset" which clears the selection (240). |
75 |
*/ |
76 |
public static final int TOOL_SELECTION_CLEAR = 240; |
77 |
|
78 |
/** |
79 |
* Constant for the tool "Select" which sets the Selection to the selected |
80 |
* features (210). |
81 |
*/ |
82 |
public static final int TOOL_SELECTION_SET = 210; |
83 |
/** |
84 |
* Constant for the tool "Selection add" which adds the features to the |
85 |
* Selection (220). |
86 |
*/ |
87 |
public static final int TOOL_SELECTION_ADD = 220; |
88 |
/** |
89 |
* Constant for the tool "Selection subtract" which removes the selected |
90 |
* features from the selection (230). |
91 |
*/ |
92 |
public static final int TOOL_SELECTION_REMOVE = 230; |
93 |
|
94 |
/** Tool currently selected */ |
95 |
protected int selectedTool = TOOL_ZOOMIN; |
96 |
|
97 |
/** Holds the tool buttons of the tool bar. */ |
98 |
protected SortedMap<Integer, JComponent> toolAndActionButtons = null; |
99 |
/** Controls that only one tool button is activated. */ |
100 |
protected ButtonGroup toolButtonGroup = null; |
101 |
|
102 |
// SK: Musste ich ändern damit man Tools und Actions in der Reihenfolge |
103 |
// mischen kann. |
104 |
// /** Holds the action buttons of the bar. */ |
105 |
// protected SortedMap<Integer, JButton> actionButtons = null; |
106 |
|
107 |
/** Holds the {@link JMapPane} this tool bar controls. */ |
108 |
protected JMapPane mapPane = null; |
109 |
|
110 |
/** |
111 |
* A List to remember the last Envelopes that have been watched. Used for |
112 |
* the zoomBack- and zoomForwardButtons * |
113 |
*/ |
114 |
protected ArrayList<Envelope> lastZooms = new ArrayList<Envelope>(); |
115 |
/** Holds the index to the current element in {@link #lastZooms}. */ |
116 |
protected int zoomBackIndex = 0; |
117 |
|
118 |
/** Listener to sniff the zoom actions on the map. */ |
119 |
protected JMapPaneListener mapPaneListener = null; |
120 |
|
121 |
protected boolean zoomBackForwardButtonInAction; |
122 |
|
123 |
/** |
124 |
* Creates a new toolbar. Notice: This toolbar does nothing until |
125 |
* {@link #setMapPane(JMapPane)} is called! |
126 |
*/ |
127 |
public MapPaneToolBar() { |
128 |
this(null); |
129 |
} |
130 |
|
131 |
/** |
132 |
* Creates a new tool bar. |
133 |
* |
134 |
* @param mapPane |
135 |
* {@link JMapPane} the tool bar controls |
136 |
*/ |
137 |
public MapPaneToolBar(JMapPane mapPane) { |
138 |
super("Control the map", JToolBar.HORIZONTAL); |
139 |
this.toolAndActionButtons = new TreeMap<Integer, JComponent>(); |
140 |
this.toolButtonGroup = new ButtonGroup(); |
141 |
|
142 |
// Create a Listener to listen to the zooms on the JMapPane |
143 |
this.mapPaneListener = new JMapPaneListener() { |
144 |
public void performMapPaneEvent(JMapPaneEvent e) { |
145 |
if (!(e instanceof MapAreaChangedEvent)) |
146 |
return; |
147 |
|
148 |
if (zoomBackForwardButtonInAction) { |
149 |
zoomBackForwardButtonInAction = false; |
150 |
return; |
151 |
} |
152 |
|
153 |
final MapAreaChangedEvent mapAreaChangedEvent = (MapAreaChangedEvent) e; |
154 |
Envelope oldMapArea = mapAreaChangedEvent.getOldMapArea(); |
155 |
|
156 |
final Envelope mapArea = mapAreaChangedEvent.getNewMapArea(); |
157 |
if (mapArea == null || mapArea.equals(oldMapArea) ) { |
158 |
// If the MapArea didn't change... we don't want to register it as a zoom action. |
159 |
return; |
160 |
} |
161 |
|
162 |
if (lastZooms.size() == 0 && oldMapArea != null) { |
163 |
lastZooms.add(oldMapArea); |
164 |
zoomBackIndex = 1; |
165 |
} |
166 |
if (mapArea == null) |
167 |
return; |
168 |
|
169 |
if (lastZooms.size() > 0 |
170 |
&& mapArea.equals(lastZooms.get(lastZooms.size() - 1))) { |
171 |
// LOGGER.debug("MapAreaChangedEvent ausgelassen bei der Zaehlung der Zoomschritt weil identisch"); |
172 |
return; |
173 |
} |
174 |
|
175 |
if (lastZooms.size() > 0) |
176 |
while (zoomBackIndex < lastZooms.size()) |
177 |
lastZooms.remove(lastZooms.size() - 1); |
178 |
|
179 |
lastZooms.add(mapArea); |
180 |
zoomBackIndex = lastZooms.size(); |
181 |
setButtonEnabled(ACTION_ZOOM_BACK, lastZooms.size() > 1); |
182 |
setButtonEnabled(ACTION_ZOOM_FORWARD, false); |
183 |
} |
184 |
}; |
185 |
|
186 |
setMapPane(mapPane); |
187 |
setFloatable(false); |
188 |
setRollover(true); |
189 |
|
190 |
init(); |
191 |
} |
192 |
|
193 |
/** |
194 |
* Sets the {@link JMapPane} controlled by this tool bar. |
195 |
* |
196 |
* @param mapPane |
197 |
* {@link JMapPane} to control (if {@code null} this tool bar |
198 |
* controls NOTHING!) |
199 |
*/ |
200 |
public void setMapPane(JMapPane mapPane) { |
201 |
// Remove listener from old MapPane |
202 |
if (this.mapPane != null) |
203 |
this.mapPane.removeMapPaneListener(mapPaneListener); |
204 |
this.mapPane = mapPane; |
205 |
if (this.mapPane != null && mapPaneListener != null) |
206 |
this.mapPane.addMapPaneListener(mapPaneListener); |
207 |
} |
208 |
|
209 |
/** |
210 |
* Calls {@link #initToolsAndActions()} and {@link #initActions()} and then |
211 |
* puts all tool buttons and all actions buttons to the tool bar. |
212 |
*/ |
213 |
protected void init() { |
214 |
initToolsAndActions(); |
215 |
|
216 |
addSeparator(SEPERATOR0, new JToolBar.Separator()); |
217 |
addSeparator(SEPERATOR1, new JToolBar.Separator()); |
218 |
|
219 |
initToolBar(); |
220 |
} |
221 |
|
222 |
/** |
223 |
* Creates the tool buttons and action buttons and seperators, adds them to |
224 |
* {@link #toolAndActionButtons} and finally creates a button group for all |
225 |
* tools. So sub-classes which override this method should FIRST add their |
226 |
* new tool buttons to {@link #toolAndActionButtons} before calling {@code |
227 |
* super.initTools()}. |
228 |
*/ |
229 |
protected void initToolsAndActions() { |
230 |
// Panning |
231 |
addTool(new MapPaneToolBarAction(TOOL_PAN, this, "", new ImageIcon( |
232 |
MapView.class.getResource("resource/icons/pan.png")), R("MapPaneButtons.Pan.TT")), false); |
233 |
// Info |
234 |
addTool(new MapPaneToolBarAction(TOOL_INFO, this, "", new ImageIcon( |
235 |
MapView.class.getResource("resource/icons/info.png")),R("MapPaneButtons.Info.TT")), false); |
236 |
|
237 |
// Zoom in |
238 |
addTool(new MapPaneToolBarAction(TOOL_ZOOMIN, this, "", new ImageIcon( |
239 |
MapView.class.getResource("resource/icons/zoom_in.png")), R("MapPaneButtons.ZoomIn.TT")), |
240 |
false); |
241 |
// Zoom out |
242 |
addTool(new MapPaneToolBarAction(TOOL_ZOOMOUT, this, "", new ImageIcon( |
243 |
MapView.class.getResource("resource/icons/zoom_out.png")), R("MapPaneButtons.ZoomOut.TT")), |
244 |
false); |
245 |
|
246 |
// Action button to revert the last zoom |
247 |
addAction(new MapPaneToolBarAction(ACTION_ZOOM_BACK, this, "", |
248 |
new ImageIcon(MapView.class |
249 |
.getResource("resource/icons/zoom_back.png")), R("MapPaneButtons.LastZoom.TT")), false); |
250 |
setButtonEnabled(ACTION_ZOOM_BACK, false); |
251 |
|
252 |
// Action button to redo the last zoom |
253 |
addAction(new MapPaneToolBarAction(ACTION_ZOOM_FORWARD, this, "", |
254 |
new ImageIcon(MapView.class |
255 |
.getResource("resource/icons/zoom_forward.png")), R("MapPaneButtons.NextZoom.TT")), |
256 |
false); |
257 |
setButtonEnabled(ACTION_ZOOM_FORWARD, false); |
258 |
|
259 |
// set the selected tool enabled |
260 |
setSelectedTool(selectedTool); |
261 |
|
262 |
} |
263 |
|
264 |
/** |
265 |
* Clears the GUI of all components and adds all tool and action buttons to |
266 |
* the tool bar. |
267 |
*/ |
268 |
public void initToolBar() { |
269 |
setAlignmentY(1f); |
270 |
removeAll(); |
271 |
// Separator to the left of the tool actions to start |
272 |
// the tool buttons with the map (not with the coordinate grid) |
273 |
Dimension dimension = new Dimension(49, 10); |
274 |
addSeparator(dimension); |
275 |
// Tool buttons |
276 |
for (JComponent b : toolAndActionButtons.values()) |
277 |
add(b); |
278 |
|
279 |
repaint(); |
280 |
|
281 |
if (!toolAndActionButtons.containsKey(selectedTool)) { |
282 |
/** |
283 |
* This might be a bit specific, but IF selection buttons are |
284 |
* available, select one of them.. if not, select the INFO tool. |
285 |
*/ |
286 |
|
287 |
if (toolAndActionButtons.containsKey(TOOL_SELECTION_SET)) { |
288 |
setSelectedTool(TOOL_SELECTION_SET); |
289 |
} else if (toolAndActionButtons.containsKey(TOOL_INFO)) { |
290 |
setSelectedTool(TOOL_INFO); |
291 |
} else { |
292 |
// TODO What to do now?! |
293 |
setSelectedTool(null); |
294 |
} |
295 |
|
296 |
} |
297 |
} |
298 |
|
299 |
// Space between tool buttons and action buttons |
300 |
// SK: Seperators are now als manages like actions and tools |
301 |
// Dimension dimension2 = new Dimension( 10,10); |
302 |
// this.addSeparator(dimension2); |
303 |
|
304 |
// // Action buttons |
305 |
// for (JButton b : actionButtons.values()) |
306 |
// add(b); |
307 |
// } |
308 |
|
309 |
/** |
310 |
* Performs the activation of a tool. |
311 |
* |
312 |
* @param tool |
313 |
* the tool to activate |
314 |
* @param e |
315 |
* the event of the button |
316 |
*/ |
317 |
public void performToolButton(int tool, ActionEvent e) { |
318 |
if (mapPane == null) |
319 |
return; |
320 |
|
321 |
selectedTool = tool; |
322 |
|
323 |
switch (tool) { |
324 |
case TOOL_PAN: |
325 |
// Set the mouse tool to "Panning" |
326 |
mapPane.setWindowSelectionState(JMapPane.NONE); |
327 |
mapPane.setState(JMapPane.PAN); |
328 |
mapPane.setHighlight(false); |
329 |
mapPane.setNormalCursor(SwingUtil.PAN_CURSOR); |
330 |
break; |
331 |
case TOOL_INFO: |
332 |
// Set the mouse tool to "Info" |
333 |
mapPane.setWindowSelectionState(JMapPane.NONE); |
334 |
mapPane.setState(JMapPane.SELECT_TOP); // Why not: |
335 |
// JMapPane.SELECT_TOP_ONEONLY |
336 |
mapPane.setHighlight(false);// SK: Was true, but since it not works |
337 |
// properly removed it to save |
338 |
// performance |
339 |
mapPane.setNormalCursor(SwingUtil.CROSSHAIR_CURSOR); |
340 |
break; |
341 |
case TOOL_ZOOMIN: |
342 |
// Set the mouse tool to "Zoom in" |
343 |
mapPane.setWindowSelectionState(JMapPane.ZOOM_IN); |
344 |
mapPane.setState(JMapPane.ZOOM_IN); |
345 |
mapPane.setHighlight(false); |
346 |
mapPane.setNormalCursor(SwingUtil.ZOOMIN_CURSOR); |
347 |
break; |
348 |
case TOOL_ZOOMOUT: |
349 |
// Set the mouse tool to "Zoom out" |
350 |
mapPane.setWindowSelectionState(JMapPane.NONE); |
351 |
mapPane.setState(JMapPane.ZOOM_OUT); |
352 |
mapPane.setHighlight(false); |
353 |
mapPane.setNormalCursor(SwingUtil.ZOOMOUT_CURSOR); |
354 |
break; |
355 |
default: |
356 |
// Set map actions to default |
357 |
mapPane.setWindowSelectionState(JMapPane.NONE); |
358 |
mapPane.setState(JMapPane.NONE); |
359 |
mapPane.setHighlight(false); |
360 |
mapPane.setNormalCursor(null); |
361 |
break; |
362 |
} |
363 |
mapPane.updateCursor(); |
364 |
} |
365 |
|
366 |
/** |
367 |
* @param id |
368 |
* The ID of the Component to remove. The change will not be |
369 |
* visible until {@link #initToolBar()} is called. |
370 |
* @return <code>null</code> or the component that has been removed. |
371 |
*/ |
372 |
public JComponent removeId(int id) { |
373 |
return toolAndActionButtons.remove(id); |
374 |
} |
375 |
|
376 |
/** |
377 |
* Performs the action of an action button. |
378 |
* |
379 |
* @param tool |
380 |
* the action |
381 |
* @param e |
382 |
* the event of the button |
383 |
*/ |
384 |
protected void performActionButton(int action, ActionEvent e) { |
385 |
if (mapPane == null) |
386 |
return; |
387 |
|
388 |
// Perform the action "Zoom back": Revert the last zoom |
389 |
if (action == ACTION_ZOOM_BACK) { |
390 |
if (zoomBackIndex <= 1) |
391 |
return; |
392 |
|
393 |
zoomBackForwardButtonInAction = true; |
394 |
zoomBackIndex--; |
395 |
getButton(ACTION_ZOOM_FORWARD).setEnabled(true); |
396 |
getButton(ACTION_ZOOM_BACK).setEnabled(zoomBackIndex > 1); |
397 |
|
398 |
mapPane.setMapArea(lastZooms.get(zoomBackIndex - 1)); |
399 |
mapPane.refresh(); |
400 |
} |
401 |
|
402 |
// Perform the action "Zoom forward": Re-do the last zoom |
403 |
if (action == ACTION_ZOOM_FORWARD) { |
404 |
if (zoomBackIndex < lastZooms.size()) { |
405 |
zoomBackForwardButtonInAction = true; |
406 |
zoomBackIndex++; |
407 |
getButton(ACTION_ZOOM_BACK).setEnabled(true); |
408 |
getButton(ACTION_ZOOM_FORWARD).setEnabled( |
409 |
zoomBackIndex < lastZooms.size()); |
410 |
|
411 |
mapPane.setMapArea(lastZooms.get(zoomBackIndex - 1)); |
412 |
mapPane.refresh(); |
413 |
} |
414 |
} |
415 |
} |
416 |
|
417 |
/** |
418 |
* Adds a tool to the tool bar. Does nothing if a tool or action with the |
419 |
* specified ID already exists! |
420 |
* |
421 |
* @param buttonAction |
422 |
* action for the toggle button |
423 |
* @param resetToolBar |
424 |
* indicates whether the toolbar GUI is reset after adding the |
425 |
* button (if adding several actions it useful only to reset the |
426 |
* GUI for the last added tool) |
427 |
*/ |
428 |
public void addTool(MapPaneToolBarAction buttonAction, boolean resetToolBar) { |
429 |
if (isButtonIDUsed(buttonAction.getID())) { |
430 |
LOGGER |
431 |
.warn("addTool(.) ignored because ID already used for tool or action: " |
432 |
+ buttonAction.getID()); |
433 |
return; |
434 |
} |
435 |
JToggleButton button = new JToggleButton(buttonAction); |
436 |
button.setBorder(BorderFactory.createRaisedBevelBorder()); |
437 |
toolButtonGroup.add(button); |
438 |
toolAndActionButtons.put(buttonAction.getID(), button); |
439 |
if (resetToolBar) |
440 |
initToolBar(); |
441 |
} |
442 |
|
443 |
/** |
444 |
* Adds a tool to the tool bar and resets the toolbar GUI. |
445 |
* |
446 |
* @param buttonAction |
447 |
* action for the toggle button |
448 |
*/ |
449 |
public void addTool(MapPaneToolBarAction buttonAction) { |
450 |
addTool(buttonAction, true); |
451 |
} |
452 |
|
453 |
/** |
454 |
* Adds an action to the tool bar. Does nothing if a tool or action with the |
455 |
* specified ID already exists! |
456 |
* |
457 |
* @param buttonAction |
458 |
* action for the button |
459 |
* @param resetToolBar |
460 |
* indicates whether the toolbar GUI is reset after adding the |
461 |
* button (if adding several actions it useful only to reset the |
462 |
* GUI for the last added tool) |
463 |
*/ |
464 |
public void addAction(MapPaneToolBarAction buttonAction, |
465 |
boolean resetToolBar) { |
466 |
if (isButtonIDUsed(buttonAction.getID())) { |
467 |
LOGGER |
468 |
.warn("addAction(.) ignored because ID already used for tool or action: " |
469 |
+ buttonAction.getID()); |
470 |
return; |
471 |
} |
472 |
JButton button = new JButton(buttonAction); |
473 |
button.setBorder(BorderFactory.createRaisedBevelBorder()); |
474 |
toolAndActionButtons.put(buttonAction.getID(), button); |
475 |
if (resetToolBar) |
476 |
initToolBar(); |
477 |
} |
478 |
|
479 |
public void addSeparator(int id, Separator separator) { |
480 |
if (isButtonIDUsed(id)) { |
481 |
LOGGER |
482 |
.warn("addSeparator(.) ignored because ID already used for tool or action. "); |
483 |
return; |
484 |
} |
485 |
toolAndActionButtons.put(id, separator); |
486 |
} |
487 |
|
488 |
/** |
489 |
* Adds an action to the tool bar and resets the toolbar GUI. |
490 |
* |
491 |
* @param buttonAction |
492 |
* action for the toggle button |
493 |
*/ |
494 |
public void addAction(MapPaneToolBarAction buttonAction) { |
495 |
addAction(buttonAction, true); |
496 |
} |
497 |
|
498 |
/** |
499 |
* Returns the button for a specific tool or action. |
500 |
* |
501 |
* @param id |
502 |
* the constant for any button in the {@link MapPaneToolBar} |
503 |
* @return a {@link JButton} if {@code id} specifies an |
504 |
* {@linkplain #getActionButton(int) action button} or |
505 |
* {@link JToogleButton} if {@code id} specifies a |
506 |
* {@linkplain #getToolButton(int) tool button} |
507 |
*/ |
508 |
public AbstractButton getButton(int id) { |
509 |
AbstractButton button = (AbstractButton) toolAndActionButtons.get(id); |
510 |
if (button == null) |
511 |
LOGGER.warn("Unknown tool or action ID: " + id); |
512 |
return button; |
513 |
} |
514 |
|
515 |
/** |
516 |
* Returns the button for a specific tool. |
517 |
* |
518 |
* @param tool |
519 |
* the constant for a tool |
520 |
*/ |
521 |
public JToggleButton getToolButton(int tool) { |
522 |
AbstractButton button = getButton(tool); |
523 |
if (button != null && !(button instanceof JToggleButton)) { |
524 |
LOGGER.warn("ID specifies no tool: " + tool); |
525 |
button = null; |
526 |
} |
527 |
return (JToggleButton) button; |
528 |
} |
529 |
|
530 |
/** |
531 |
* Returns the button for a specific action. |
532 |
* |
533 |
* @param action |
534 |
* the constant an action |
535 |
*/ |
536 |
public JButton getActionButton(int action) { |
537 |
AbstractButton button = getButton(action); |
538 |
if (button != null && !(button instanceof JButton)) { |
539 |
LOGGER.warn("ID specifies no action: " + action); |
540 |
button = null; |
541 |
} |
542 |
return (JButton) button; |
543 |
|
544 |
} |
545 |
|
546 |
/** |
547 |
* Sets the selected tool. |
548 |
* |
549 |
* @param tool |
550 |
* ID of the tool |
551 |
*/ |
552 |
public void setSelectedTool(Integer tool) { |
553 |
if (tool == null) |
554 |
toolButtonGroup.setUnselected(); |
555 |
|
556 |
JToggleButton button = getToolButton(tool); |
557 |
if (button == null) |
558 |
return; |
559 |
button.setSelected(true); |
560 |
button.getAction().actionPerformed(null); |
561 |
|
562 |
selectedTool = tool; |
563 |
} |
564 |
|
565 |
/** |
566 |
* Returns the selected tool. |
567 |
* |
568 |
* @return -1 if no tool is active |
569 |
*/ |
570 |
public int getSelectedTool() { |
571 |
if (toolButtonGroup.getSelectedButton() == null) |
572 |
return -1; |
573 |
return selectedTool; |
574 |
} |
575 |
|
576 |
/** |
577 |
* Sets whether a tool or action is activated or not. The visible property |
578 |
* of the button is not affected. |
579 |
* |
580 |
* @param id |
581 |
* tool or actionID |
582 |
* @param enabled |
583 |
* if {@code true} the tool becomes available |
584 |
*/ |
585 |
public void setButtonEnabled(int id, boolean enabled) { |
586 |
AbstractButton button = getButton(id); |
587 |
if (button == null) |
588 |
return; |
589 |
button.setEnabled(enabled); |
590 |
} |
591 |
|
592 |
/** |
593 |
* Sets whether a tool or action is activated or not. |
594 |
* |
595 |
* @param id |
596 |
* tool or actionID |
597 |
* @param enabled |
598 |
* if {@code true} the tool becomes available |
599 |
* @param hideOnDisable |
600 |
* if {@code true} the button is also hidden if {@code enabled} |
601 |
* is {@code false} |
602 |
*/ |
603 |
public void setButtonEnabled(int id, boolean enabled, boolean hideOnDisable) { |
604 |
AbstractButton button = getButton(id); |
605 |
if (button == null) |
606 |
return; |
607 |
button.setEnabled(enabled); |
608 |
// if button is enabled, it becomes visible anyway |
609 |
// if button is disabled and the "hide" option is set, it is also hidden |
610 |
if (enabled) |
611 |
button.setVisible(true); |
612 |
else |
613 |
button.setVisible(!hideOnDisable); |
614 |
} |
615 |
|
616 |
/** |
617 |
* Checks whether a ID is already used for a tool or action. |
618 |
* |
619 |
* @param tool |
620 |
* tool ID |
621 |
*/ |
622 |
public boolean isButtonIDUsed(int id) { |
623 |
return toolAndActionButtons.get(id) != null; |
624 |
} |
625 |
|
626 |
/** |
627 |
* Checks whether a tool is activated. |
628 |
* |
629 |
* @param tool |
630 |
* tool ID |
631 |
* @return {@code false} if an unknown ID is specified |
632 |
*/ |
633 |
public boolean isButtonEnabled(int id) { |
634 |
AbstractButton button = getButton(id); |
635 |
if (button != null) |
636 |
return button.isEnabled(); |
637 |
return false; |
638 |
} |
639 |
|
640 |
/** |
641 |
* Sets the activation for all tools. |
642 |
* |
643 |
* @param enabled |
644 |
* if {@code true} all tools becomes available |
645 |
* @param hideOnDisable |
646 |
* if {@code true} the buttons are also hidden if {@code enabled} |
647 |
* is {@code false} |
648 |
*/ |
649 |
public void setAllToolsEnabled(boolean enabled, boolean hideOnDisable) { |
650 |
for (int tool : toolAndActionButtons.keySet()) |
651 |
setButtonEnabled(tool, enabled, hideOnDisable); |
652 |
} |
653 |
|
654 |
/** |
655 |
* Sets the activation for all actions. |
656 |
* |
657 |
* @param enabled |
658 |
* if {@code true} all actions becomes available |
659 |
* @param hideOnDisable |
660 |
* if {@code true} the buttons are also hidden if {@code enabled} |
661 |
* is {@code false} |
662 |
*/ |
663 |
public void setAllActionsEnabled(boolean enabled, boolean hideOnDisable) { |
664 |
for (int id : toolAndActionButtons.keySet()) { |
665 |
if (toolAndActionButtons.get(id) instanceof JButton) { |
666 |
setButtonEnabled(id, enabled, hideOnDisable); |
667 |
} |
668 |
} |
669 |
|
670 |
} |
671 |
|
672 |
/** |
673 |
* Returns the maximum ID of tools. |
674 |
*/ |
675 |
public int getMaxToolID() { |
676 |
return toolAndActionButtons.lastKey(); |
677 |
} |
678 |
|
679 |
/** |
680 |
* Returns the minimum ID of tools. |
681 |
*/ |
682 |
public int getMinToolID() { |
683 |
return toolAndActionButtons.firstKey(); |
684 |
} |
685 |
|
686 |
/** |
687 |
* Extends the {@link AbstractAction} with maintaining an ID and the |
688 |
* {@link MapPaneToolBar} the actions controls. Additionally this class |
689 |
* automatically calls |
690 |
* {@link MapPaneToolBar#performToolButton(int, ActionEvent)} or |
691 |
* {@link MapPaneToolBar#performActionButton(int, ActionEvent)} depending on |
692 |
* whether the action is added via |
693 |
* {@link MapPaneToolBar#addTool(MapPaneToolBarAction)} or |
694 |
* {@link MapPaneToolBar#addAction(MapPaneToolBarAction)}. |
695 |
* |
696 |
* @author <a href="mailto:[email protected]">Martin Schmitz</a> |
697 |
* (University of Bonn/Germany) |
698 |
*/ |
699 |
public static class MapPaneToolBarAction extends AbstractAction { |
700 |
/** The ID of the action */ |
701 |
protected int id = -1; |
702 |
/** The tool bar, this action is made for. */ |
703 |
protected MapPaneToolBar toolBar = null; |
704 |
|
705 |
/** |
706 |
* Creates a new action with a dummy description and no icon. |
707 |
* |
708 |
* @param id |
709 |
* unique ID for the action |
710 |
* @param toolBar |
711 |
* toolbar this action is made for |
712 |
*/ |
713 |
public MapPaneToolBarAction(int id, MapPaneToolBar toolBar) { |
714 |
this(id, toolBar, "" + id); |
715 |
} |
716 |
|
717 |
/** |
718 |
* Creates a new action without an icon. |
719 |
* |
720 |
* @param id |
721 |
* unique ID for the action |
722 |
* @param toolBar |
723 |
* toolbar this action is made for |
724 |
* @param name |
725 |
* description used for buttons or menus |
726 |
*/ |
727 |
public MapPaneToolBarAction(int id, MapPaneToolBar toolBar, String name) { |
728 |
this(id, toolBar, name, null); |
729 |
} |
730 |
|
731 |
/** |
732 |
* Creates a new action. |
733 |
* |
734 |
* @param id |
735 |
* unique ID for the action |
736 |
* @param toolBar |
737 |
* toolbar this action is made for |
738 |
* @param name |
739 |
* description used for buttons or menus |
740 |
* @param icon |
741 |
* icon used for buttons or menus |
742 |
*/ |
743 |
public MapPaneToolBarAction(int id, MapPaneToolBar toolBar, |
744 |
String name, Icon icon) { |
745 |
this (id, toolBar, name, icon, null); |
746 |
} |
747 |
|
748 |
/** |
749 |
* Creates a new action. |
750 |
* |
751 |
* @param id |
752 |
* unique ID for the action |
753 |
* @param toolBar |
754 |
* The {@link MapPaneToolBar} this action is made for |
755 |
* @param name |
756 |
* description used for buttons or menus |
757 |
* @param icon |
758 |
* icon used for buttons or menus |
759 |
* @param toolTip |
760 |
* Tooltip to use for the button or menu |
761 |
*/ |
762 |
public MapPaneToolBarAction(int id, MapPaneToolBar toolBar, |
763 |
String name, Icon icon, String toolTip) { |
764 |
super(name, icon); |
765 |
|
766 |
if (toolTip != null && !toolTip.trim().isEmpty()){ |
767 |
putValue(Action.SHORT_DESCRIPTION, toolTip); |
768 |
} |
769 |
|
770 |
this.id = id; |
771 |
this.toolBar = toolBar; |
772 |
} |
773 |
|
774 |
/** |
775 |
* Calls {@link MapPaneToolBar#performToolButton(int, ActionEvent)} or |
776 |
* {@link MapPaneToolBar#performActionButton(int, ActionEvent)} |
777 |
* depending on whether the action is added to the toolbar via |
778 |
* {@link MapPaneToolBar#addTool(MapPaneToolBarAction)} or |
779 |
* {@link MapPaneToolBar#addAction(MapPaneToolBarAction)}. |
780 |
*/ |
781 |
public void actionPerformed(ActionEvent e) { |
782 |
if (toolBar.toolAndActionButtons.get(id) instanceof JToggleButton) |
783 |
toolBar.performToolButton(id, e); |
784 |
else if (toolBar.toolAndActionButtons.get(id) instanceof JButton) |
785 |
toolBar.performActionButton(id, e); |
786 |
} |
787 |
|
788 |
/** |
789 |
* Returns the (unique) id of this action. |
790 |
* |
791 |
* @return |
792 |
*/ |
793 |
public int getID() { |
794 |
return id; |
795 |
} |
796 |
} |
797 |
} |