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