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