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