30 |
package skrueger.geotools; |
package skrueger.geotools; |
31 |
|
|
32 |
import java.awt.Color; |
import java.awt.Color; |
|
import java.awt.Dimension; |
|
33 |
import java.awt.Graphics; |
import java.awt.Graphics; |
34 |
import java.awt.event.ActionEvent; |
import java.awt.event.ActionEvent; |
35 |
|
import java.awt.event.ActionListener; |
36 |
import java.util.ArrayList; |
import java.util.ArrayList; |
37 |
|
import java.util.HashSet; |
38 |
import java.util.Locale; |
import java.util.Locale; |
39 |
|
import java.util.Set; |
40 |
import java.util.SortedMap; |
import java.util.SortedMap; |
41 |
import java.util.TreeMap; |
import java.util.TreeMap; |
42 |
|
|
124 |
*/ |
*/ |
125 |
public static final int TOOL_SELECTION_REMOVE = 230; |
public static final int TOOL_SELECTION_REMOVE = 230; |
126 |
|
|
127 |
|
public static final int ACTION_CHARTS = 401; |
128 |
|
|
129 |
/** Tool currently selected */ |
/** Tool currently selected */ |
130 |
protected int selectedTool = TOOL_ZOOMIN; |
protected int selectedTool = TOOL_ZOOMIN; |
131 |
|
|
132 |
/** Holds the tool buttons of the tool bar. */ |
/** Holds the tool buttons of the tool bar. */ |
133 |
protected SortedMap<Integer, JComponent> toolAndActionButtons = null; |
final protected SortedMap<Integer, JComponent> toolAndActionButtons = new TreeMap<Integer, JComponent>(); |
134 |
/** Controls that only one tool button is activated. */ |
/** Controls that only one tool button is activated. */ |
135 |
protected ButtonGroup toolButtonGroup = null; |
protected ButtonGroup toolButtonGroup = null; |
136 |
|
|
155 |
|
|
156 |
protected boolean zoomBackForwardButtonInAction; |
protected boolean zoomBackForwardButtonInAction; |
157 |
|
|
158 |
|
/** Listeners what want to be informed about a change of the selected tool **/ |
159 |
|
protected Set<MapPaneToolSelectedListener> toolSelectionListeners = new HashSet<MapPaneToolSelectedListener>(); |
160 |
|
|
161 |
|
/** This listener is added to all {@link JToggleButton} **/ |
162 |
|
private final ActionListener toolSelectedListener = new ActionListener(){ |
163 |
|
|
164 |
|
@Override |
165 |
|
public void actionPerformed(ActionEvent e) { |
166 |
|
JToggleButton tb = (JToggleButton) e.getSource(); |
167 |
|
|
168 |
|
// Inform the listeners about a newly selected tool |
169 |
|
for (MapPaneToolSelectedListener l : toolSelectionListeners) { |
170 |
|
l.toolSelected(Integer.valueOf( tb.getName() ) ) ; |
171 |
|
} |
172 |
|
} |
173 |
|
|
174 |
|
}; |
175 |
|
|
176 |
/** |
/** |
177 |
* Creates a new toolbar. Notice: This toolbar does nothing until |
* Creates a new toolbar. Notice: This toolbar does nothing until |
178 |
* {@link #setMapPane(JMapPane)} is called! |
* {@link #setMapPane(JMapPane)} is called! |
180 |
public MapPaneToolBar() { |
public MapPaneToolBar() { |
181 |
this(null); |
this(null); |
182 |
} |
} |
183 |
|
|
184 |
|
public void addButtonSelectedListener(MapPaneToolSelectedListener listener ) { |
185 |
|
toolSelectionListeners.add(listener); |
186 |
|
} |
187 |
|
|
188 |
|
public void removeButtonSelectedListener(MapPaneToolSelectedListener listener ) { |
189 |
|
toolSelectionListeners .remove(listener); |
190 |
|
} |
191 |
|
|
192 |
/** |
/** |
193 |
* Creates a new tool bar. |
* Creates a new tool bar. |
197 |
*/ |
*/ |
198 |
public MapPaneToolBar(JMapPane mapPane) { |
public MapPaneToolBar(JMapPane mapPane) { |
199 |
super("Control the map", JToolBar.HORIZONTAL); |
super("Control the map", JToolBar.HORIZONTAL); |
200 |
this.toolAndActionButtons = new TreeMap<Integer, JComponent>(); |
|
201 |
|
// I want to see nothing on the background |
202 |
|
setOpaque(false); |
203 |
|
setBorder(null); |
204 |
|
|
205 |
this.toolButtonGroup = new ButtonGroup(); |
this.toolButtonGroup = new ButtonGroup(); |
206 |
|
|
207 |
// Create a Listener to listen to the zooms on the JMapPane |
// Create a Listener to listen to the zooms on the JMapPane |
276 |
* puts all tool buttons and all actions buttons to the tool bar. |
* puts all tool buttons and all actions buttons to the tool bar. |
277 |
*/ |
*/ |
278 |
protected void init() { |
protected void init() { |
279 |
|
|
280 |
initToolsAndActions(); |
initToolsAndActions(); |
281 |
|
|
282 |
addSeparator(SEPERATOR0, new JToolBar.Separator()); |
addSeparator(SEPERATOR0, new JToolBar.Separator()); |
326 |
setSelectedTool(selectedTool); |
setSelectedTool(selectedTool); |
327 |
|
|
328 |
} |
} |
329 |
|
|
330 |
|
@Override |
331 |
|
public void paint(Graphics g) { |
332 |
|
super.paint(g); |
333 |
|
} |
334 |
|
|
335 |
/** |
/** |
336 |
* Clears the GUI of all components and adds all tool and action buttons to |
* Clears the GUI of all components and adds all tool and action buttons to |
337 |
* the tool bar. |
* the tool bar. |
338 |
*/ |
*/ |
339 |
public void initToolBar() { |
public void initToolBar() { |
340 |
setAlignmentY(1f); |
// setOpaque(true); |
341 |
|
// |
342 |
|
// setAlignmentY(1f); |
343 |
|
// setAlignmentX(0.5f); |
344 |
removeAll(); |
removeAll(); |
345 |
// Separator to the left of the tool actions to start |
|
346 |
// the tool buttons with the map (not with the coordinate grid) |
// // Separator to the left of the tool actions to start |
347 |
Dimension dimension = new Dimension(49, 10); |
// // the tool buttons with the map (not with the coordinate grid) |
348 |
addSeparator(dimension); |
// Dimension dimension = new Dimension(49, 10); |
349 |
|
// addSeparator(dimension); |
350 |
|
|
351 |
|
|
352 |
// Tool buttons |
// Tool buttons |
353 |
for (JComponent b : toolAndActionButtons.values()) |
for (Integer bKey : toolAndActionButtons.keySet()) { |
354 |
|
|
355 |
|
JComponent b = toolAndActionButtons.get(bKey); |
356 |
|
|
357 |
|
if (b instanceof JToggleButton) { |
358 |
|
JToggleButton tb = (JToggleButton) b; |
359 |
|
tb.setName(bKey.toString()); |
360 |
|
tb.addActionListener( toolSelectedListener ); |
361 |
|
} |
362 |
|
|
363 |
add(b); |
add(b); |
364 |
|
} |
365 |
|
|
366 |
if (!toolAndActionButtons.containsKey(selectedTool)) { |
if (!toolAndActionButtons.containsKey(selectedTool)) { |
367 |
/** |
/** |
413 |
// Set the mouse tool to "Panning" |
// Set the mouse tool to "Panning" |
414 |
mapPane.setWindowSelectionState(JMapPane.NONE); |
mapPane.setWindowSelectionState(JMapPane.NONE); |
415 |
mapPane.setState(JMapPane.PAN); |
mapPane.setState(JMapPane.PAN); |
|
mapPane.setHighlight(false); |
|
416 |
mapPane.setNormalCursor(SwingUtil.PAN_CURSOR); |
mapPane.setNormalCursor(SwingUtil.PAN_CURSOR); |
417 |
break; |
break; |
418 |
case TOOL_INFO: |
case TOOL_INFO: |
420 |
mapPane.setWindowSelectionState(JMapPane.NONE); |
mapPane.setWindowSelectionState(JMapPane.NONE); |
421 |
mapPane.setState(JMapPane.SELECT_TOP); // Why not: |
mapPane.setState(JMapPane.SELECT_TOP); // Why not: |
422 |
// JMapPane.SELECT_TOP_ONEONLY |
// JMapPane.SELECT_TOP_ONEONLY |
|
mapPane.setHighlight(false);// SK: Was true, but since it not works |
|
423 |
// properly removed it to save |
// properly removed it to save |
424 |
// performance |
// performance |
425 |
mapPane.setNormalCursor(SwingUtil.CROSSHAIR_CURSOR); |
mapPane.setNormalCursor(SwingUtil.CROSSHAIR_CURSOR); |
428 |
// Set the mouse tool to "Zoom in" |
// Set the mouse tool to "Zoom in" |
429 |
mapPane.setWindowSelectionState(JMapPane.ZOOM_IN); |
mapPane.setWindowSelectionState(JMapPane.ZOOM_IN); |
430 |
mapPane.setState(JMapPane.ZOOM_IN); |
mapPane.setState(JMapPane.ZOOM_IN); |
|
mapPane.setHighlight(false); |
|
431 |
mapPane.setNormalCursor(SwingUtil.ZOOMIN_CURSOR); |
mapPane.setNormalCursor(SwingUtil.ZOOMIN_CURSOR); |
432 |
break; |
break; |
433 |
case TOOL_ZOOMOUT: |
case TOOL_ZOOMOUT: |
434 |
// Set the mouse tool to "Zoom out" |
// Set the mouse tool to "Zoom out" |
435 |
mapPane.setWindowSelectionState(JMapPane.NONE); |
mapPane.setWindowSelectionState(JMapPane.NONE); |
436 |
mapPane.setState(JMapPane.ZOOM_OUT); |
mapPane.setState(JMapPane.ZOOM_OUT); |
|
mapPane.setHighlight(false); |
|
437 |
mapPane.setNormalCursor(SwingUtil.ZOOMOUT_CURSOR); |
mapPane.setNormalCursor(SwingUtil.ZOOMOUT_CURSOR); |
438 |
break; |
break; |
439 |
default: |
default: |
440 |
// Set map actions to default |
// Set map actions to default |
441 |
mapPane.setWindowSelectionState(JMapPane.NONE); |
mapPane.setWindowSelectionState(JMapPane.NONE); |
442 |
mapPane.setState(JMapPane.NONE); |
mapPane.setState(JMapPane.NONE); |
|
mapPane.setHighlight(false); |
|
443 |
mapPane.setNormalCursor(null); |
mapPane.setNormalCursor(null); |
444 |
break; |
break; |
445 |
} |
} |
523 |
initToolBar(); |
initToolBar(); |
524 |
} |
} |
525 |
|
|
526 |
|
|
527 |
|
|
528 |
/** |
/** |
529 |
* Adds a tool to the tool bar and resets the toolbar GUI. |
* Adds a tool to the tool bar and resets the toolbar GUI. |
530 |
* |
* |
560 |
if (resetToolBar) |
if (resetToolBar) |
561 |
initToolBar(); |
initToolBar(); |
562 |
} |
} |
563 |
|
|
564 |
|
/** |
565 |
|
* Adds any JComponent to the tool bar. Does nothing if a tool or action with the |
566 |
|
* specified ID already exists! |
567 |
|
* |
568 |
|
* @param component A {@link JComponent} that shall be added |
569 |
|
* @param id The ID associaded with the {@link JComponent} |
570 |
|
* @param resetToolBar |
571 |
|
* indicates whether the toolbar GUI is reset after adding the |
572 |
|
* button (if adding several actions it useful only to reset the |
573 |
|
* GUI for the last added tool) |
574 |
|
*/ |
575 |
|
public void addJComponent(JComponent component, int id, |
576 |
|
boolean resetToolBar) { |
577 |
|
|
578 |
|
if (isButtonIDUsed(id)) { |
579 |
|
LOGGER |
580 |
|
.warn("addAction(.) ignored because ID already used for tool or action: " |
581 |
|
+ id); |
582 |
|
return; |
583 |
|
} |
584 |
|
|
585 |
|
toolAndActionButtons.put(id, component); |
586 |
|
if (resetToolBar) |
587 |
|
initToolBar(); |
588 |
|
} |
589 |
|
|
590 |
public void addSeparator(int id, Separator separator) { |
public void addSeparator(int id, Separator separator) { |
591 |
if (isButtonIDUsed(id)) { |
if (isButtonIDUsed(id)) { |
924 |
setBackground(orig); |
setBackground(orig); |
925 |
} |
} |
926 |
} |
} |
927 |
|
|
928 |
} |
} |