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