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