2 |
* Copyright (c) 2009 Martin O. J. Schmitz. |
* Copyright (c) 2009 Martin O. J. Schmitz. |
3 |
* |
* |
4 |
* This file is part of the SCHMITZM library - a collection of utility |
* This file is part of the SCHMITZM library - a collection of utility |
5 |
* classes based on Java 1.6, focussing (not only) on Java Swing |
* classes based on Java 1.6, focusing (not only) on Java Swing |
6 |
* and the Geotools library. |
* and the Geotools library. |
7 |
* |
* |
8 |
* The SCHMITZM project is hosted at: |
* The SCHMITZM project is hosted at: |
33 |
import java.awt.Dimension; |
import java.awt.Dimension; |
34 |
import java.awt.Graphics; |
import java.awt.Graphics; |
35 |
import java.awt.event.ActionEvent; |
import java.awt.event.ActionEvent; |
36 |
|
import java.awt.event.ActionListener; |
37 |
import java.util.ArrayList; |
import java.util.ArrayList; |
38 |
|
import java.util.HashSet; |
39 |
import java.util.Locale; |
import java.util.Locale; |
40 |
|
import java.util.Set; |
41 |
import java.util.SortedMap; |
import java.util.SortedMap; |
42 |
import java.util.TreeMap; |
import java.util.TreeMap; |
43 |
|
|
125 |
*/ |
*/ |
126 |
public static final int TOOL_SELECTION_REMOVE = 230; |
public static final int TOOL_SELECTION_REMOVE = 230; |
127 |
|
|
128 |
|
public static final int ACTION_CHARTS = 401; |
129 |
|
|
130 |
/** Tool currently selected */ |
/** Tool currently selected */ |
131 |
protected int selectedTool = TOOL_ZOOMIN; |
protected int selectedTool = TOOL_ZOOMIN; |
132 |
|
|
133 |
/** Holds the tool buttons of the tool bar. */ |
/** Holds the tool buttons of the tool bar. */ |
134 |
protected SortedMap<Integer, JComponent> toolAndActionButtons = null; |
final protected SortedMap<Integer, JComponent> toolAndActionButtons = new TreeMap<Integer, JComponent>(); |
135 |
/** Controls that only one tool button is activated. */ |
/** Controls that only one tool button is activated. */ |
136 |
protected ButtonGroup toolButtonGroup = null; |
protected ButtonGroup toolButtonGroup = null; |
137 |
|
|
156 |
|
|
157 |
protected boolean zoomBackForwardButtonInAction; |
protected boolean zoomBackForwardButtonInAction; |
158 |
|
|
159 |
|
/** Listeners what want to be informed about a change of the selected tool **/ |
160 |
|
protected Set<MapPaneToolSelectedListener> toolSelectionListeners = new HashSet<MapPaneToolSelectedListener>(); |
161 |
|
|
162 |
|
/** This listener is added to all {@link JToggleButton} **/ |
163 |
|
private final ActionListener toolSelectedListener = new ActionListener(){ |
164 |
|
|
165 |
|
@Override |
166 |
|
public void actionPerformed(ActionEvent e) { |
167 |
|
JToggleButton tb = (JToggleButton) e.getSource(); |
168 |
|
|
169 |
|
// Inform the listeners about a newly selected tool |
170 |
|
for (MapPaneToolSelectedListener l : toolSelectionListeners) { |
171 |
|
l.toolSelected(Integer.valueOf( tb.getName() ) ) ; |
172 |
|
} |
173 |
|
} |
174 |
|
|
175 |
|
}; |
176 |
|
|
177 |
/** |
/** |
178 |
* Creates a new toolbar. Notice: This toolbar does nothing until |
* Creates a new toolbar. Notice: This toolbar does nothing until |
179 |
* {@link #setMapPane(JMapPane)} is called! |
* {@link #setMapPane(JMapPane)} is called! |
181 |
public MapPaneToolBar() { |
public MapPaneToolBar() { |
182 |
this(null); |
this(null); |
183 |
} |
} |
184 |
|
|
185 |
|
public void addButtonSelectedListener(MapPaneToolSelectedListener listener ) { |
186 |
|
toolSelectionListeners.add(listener); |
187 |
|
} |
188 |
|
|
189 |
|
public void removeButtonSelectedListener(MapPaneToolSelectedListener listener ) { |
190 |
|
toolSelectionListeners .remove(listener); |
191 |
|
} |
192 |
|
|
193 |
/** |
/** |
194 |
* Creates a new tool bar. |
* Creates a new tool bar. |
198 |
*/ |
*/ |
199 |
public MapPaneToolBar(JMapPane mapPane) { |
public MapPaneToolBar(JMapPane mapPane) { |
200 |
super("Control the map", JToolBar.HORIZONTAL); |
super("Control the map", JToolBar.HORIZONTAL); |
|
this.toolAndActionButtons = new TreeMap<Integer, JComponent>(); |
|
201 |
this.toolButtonGroup = new ButtonGroup(); |
this.toolButtonGroup = new ButtonGroup(); |
202 |
|
|
203 |
// Create a Listener to listen to the zooms on the JMapPane |
// Create a Listener to listen to the zooms on the JMapPane |
334 |
Dimension dimension = new Dimension(49, 10); |
Dimension dimension = new Dimension(49, 10); |
335 |
addSeparator(dimension); |
addSeparator(dimension); |
336 |
// Tool buttons |
// Tool buttons |
337 |
for (JComponent b : toolAndActionButtons.values()) |
for (Integer bKey : toolAndActionButtons.keySet()) { |
338 |
|
|
339 |
|
JComponent b = toolAndActionButtons.get(bKey); |
340 |
|
|
341 |
|
if (b instanceof JToggleButton) { |
342 |
|
JToggleButton tb = (JToggleButton) b; |
343 |
|
tb.setName(bKey.toString()); |
344 |
|
tb.addActionListener( toolSelectedListener ); |
345 |
|
} |
346 |
|
|
347 |
add(b); |
add(b); |
348 |
|
} |
349 |
|
|
350 |
if (!toolAndActionButtons.containsKey(selectedTool)) { |
if (!toolAndActionButtons.containsKey(selectedTool)) { |
351 |
/** |
/** |
397 |
// Set the mouse tool to "Panning" |
// Set the mouse tool to "Panning" |
398 |
mapPane.setWindowSelectionState(JMapPane.NONE); |
mapPane.setWindowSelectionState(JMapPane.NONE); |
399 |
mapPane.setState(JMapPane.PAN); |
mapPane.setState(JMapPane.PAN); |
|
mapPane.setHighlight(false); |
|
400 |
mapPane.setNormalCursor(SwingUtil.PAN_CURSOR); |
mapPane.setNormalCursor(SwingUtil.PAN_CURSOR); |
401 |
break; |
break; |
402 |
case TOOL_INFO: |
case TOOL_INFO: |
404 |
mapPane.setWindowSelectionState(JMapPane.NONE); |
mapPane.setWindowSelectionState(JMapPane.NONE); |
405 |
mapPane.setState(JMapPane.SELECT_TOP); // Why not: |
mapPane.setState(JMapPane.SELECT_TOP); // Why not: |
406 |
// JMapPane.SELECT_TOP_ONEONLY |
// JMapPane.SELECT_TOP_ONEONLY |
|
mapPane.setHighlight(false);// SK: Was true, but since it not works |
|
407 |
// properly removed it to save |
// properly removed it to save |
408 |
// performance |
// performance |
409 |
mapPane.setNormalCursor(SwingUtil.CROSSHAIR_CURSOR); |
mapPane.setNormalCursor(SwingUtil.CROSSHAIR_CURSOR); |
412 |
// Set the mouse tool to "Zoom in" |
// Set the mouse tool to "Zoom in" |
413 |
mapPane.setWindowSelectionState(JMapPane.ZOOM_IN); |
mapPane.setWindowSelectionState(JMapPane.ZOOM_IN); |
414 |
mapPane.setState(JMapPane.ZOOM_IN); |
mapPane.setState(JMapPane.ZOOM_IN); |
|
mapPane.setHighlight(false); |
|
415 |
mapPane.setNormalCursor(SwingUtil.ZOOMIN_CURSOR); |
mapPane.setNormalCursor(SwingUtil.ZOOMIN_CURSOR); |
416 |
break; |
break; |
417 |
case TOOL_ZOOMOUT: |
case TOOL_ZOOMOUT: |
418 |
// Set the mouse tool to "Zoom out" |
// Set the mouse tool to "Zoom out" |
419 |
mapPane.setWindowSelectionState(JMapPane.NONE); |
mapPane.setWindowSelectionState(JMapPane.NONE); |
420 |
mapPane.setState(JMapPane.ZOOM_OUT); |
mapPane.setState(JMapPane.ZOOM_OUT); |
|
mapPane.setHighlight(false); |
|
421 |
mapPane.setNormalCursor(SwingUtil.ZOOMOUT_CURSOR); |
mapPane.setNormalCursor(SwingUtil.ZOOMOUT_CURSOR); |
422 |
break; |
break; |
423 |
default: |
default: |
424 |
// Set map actions to default |
// Set map actions to default |
425 |
mapPane.setWindowSelectionState(JMapPane.NONE); |
mapPane.setWindowSelectionState(JMapPane.NONE); |
426 |
mapPane.setState(JMapPane.NONE); |
mapPane.setState(JMapPane.NONE); |
|
mapPane.setHighlight(false); |
|
427 |
mapPane.setNormalCursor(null); |
mapPane.setNormalCursor(null); |
428 |
break; |
break; |
429 |
} |
} |
507 |
initToolBar(); |
initToolBar(); |
508 |
} |
} |
509 |
|
|
510 |
|
|
511 |
|
|
512 |
/** |
/** |
513 |
* Adds a tool to the tool bar and resets the toolbar GUI. |
* Adds a tool to the tool bar and resets the toolbar GUI. |
514 |
* |
* |
544 |
if (resetToolBar) |
if (resetToolBar) |
545 |
initToolBar(); |
initToolBar(); |
546 |
} |
} |
547 |
|
|
548 |
|
/** |
549 |
|
* Adds any JComponent to the tool bar. Does nothing if a tool or action with the |
550 |
|
* specified ID already exists! |
551 |
|
* |
552 |
|
* @param component A {@link JComponent} that shall be added |
553 |
|
* @param id The ID associaded with the {@link JComponent} |
554 |
|
* @param resetToolBar |
555 |
|
* indicates whether the toolbar GUI is reset after adding the |
556 |
|
* button (if adding several actions it useful only to reset the |
557 |
|
* GUI for the last added tool) |
558 |
|
*/ |
559 |
|
public void addJComponent(JComponent component, int id, |
560 |
|
boolean resetToolBar) { |
561 |
|
|
562 |
|
if (isButtonIDUsed(id)) { |
563 |
|
LOGGER |
564 |
|
.warn("addAction(.) ignored because ID already used for tool or action: " |
565 |
|
+ id); |
566 |
|
return; |
567 |
|
} |
568 |
|
|
569 |
|
toolAndActionButtons.put(id, component); |
570 |
|
if (resetToolBar) |
571 |
|
initToolBar(); |
572 |
|
} |
573 |
|
|
574 |
public void addSeparator(int id, Separator separator) { |
public void addSeparator(int id, Separator separator) { |
575 |
if (isButtonIDUsed(id)) { |
if (isButtonIDUsed(id)) { |
908 |
setBackground(orig); |
setBackground(orig); |
909 |
} |
} |
910 |
} |
} |
911 |
|
|
912 |
} |
} |