25 |
* |
* |
26 |
* Contributors: |
* Contributors: |
27 |
* Martin O. J. Schmitz - initial API and implementation |
* Martin O. J. Schmitz - initial API and implementation |
28 |
* Stefan A. Krüger - additional utility classes |
* Stefan A. Tzeggai - additional utility classes |
29 |
******************************************************************************/ |
******************************************************************************/ |
30 |
package skrueger.geotools; |
package skrueger.geotools; |
31 |
|
|
35 |
import java.awt.event.ActionListener; |
import java.awt.event.ActionListener; |
36 |
import java.util.ArrayList; |
import java.util.ArrayList; |
37 |
import java.util.HashSet; |
import java.util.HashSet; |
|
import java.util.Locale; |
|
38 |
import java.util.Set; |
import java.util.Set; |
39 |
import java.util.SortedMap; |
import java.util.SortedMap; |
40 |
import java.util.TreeMap; |
import java.util.TreeMap; |
51 |
|
|
52 |
import org.apache.log4j.Logger; |
import org.apache.log4j.Logger; |
53 |
|
|
54 |
|
import schmitzm.geotools.gui.GeotoolsGUIUtil; |
55 |
import schmitzm.geotools.gui.SelectableXMapPane; |
import schmitzm.geotools.gui.SelectableXMapPane; |
56 |
|
import schmitzm.geotools.gui.XMapPaneEvent; |
57 |
|
import schmitzm.geotools.gui.XMapPaneTool; |
58 |
import schmitzm.geotools.map.event.JMapPaneListener; |
import schmitzm.geotools.map.event.JMapPaneListener; |
59 |
import schmitzm.geotools.map.event.MapAreaChangedEvent; |
import schmitzm.geotools.map.event.MapAreaChangedEvent; |
|
import schmitzm.geotools.map.event.MapPaneEvent; |
|
|
import schmitzm.lang.LangUtil; |
|
|
import schmitzm.lang.ResourceProvider; |
|
60 |
import schmitzm.swing.ButtonGroup; |
import schmitzm.swing.ButtonGroup; |
|
import schmitzm.swing.SwingUtil; |
|
61 |
import skrueger.swing.SmallButton; |
import skrueger.swing.SmallButton; |
62 |
import skrueger.swing.SmallToggleButton; |
import skrueger.swing.SmallToggleButton; |
63 |
|
|
64 |
import com.vividsolutions.jts.geom.Envelope; |
import com.vividsolutions.jts.geom.Envelope; |
65 |
|
|
66 |
/** |
/** |
67 |
* A toolbar to control an {@link SelectableXMapPane} (Atlas visualization). This contains |
* A toolbar to control an {@link SelectableXMapPane} (Atlas visualization). |
68 |
* two types of buttons. A group of <i>tools</i> for the mouse actions on the |
* This contains two types of buttons. A group of <i>tools</i> for the mouse |
69 |
* map represented by {@link JToggleButton JToggleButtons}, where only one tool |
* actions on the map represented by {@link JToggleButton JToggleButtons}, where |
70 |
* can be activated every time. And some (general) <i>actions</i>, represented |
* only one tool can be activated every time. And some (general) <i>actions</i>, |
71 |
* by normal {@link JButton JButtons}. |
* represented by normal {@link JButton JButtons}. |
72 |
* |
* |
73 |
* @author <a href="mailto:[email protected]">Martin Schmitz</a> |
* @author <a href="mailto:[email protected]">Martin Schmitz</a> |
74 |
* (University of Bonn/Germany) |
* (University of Bonn/Germany) |
75 |
* @version 1.2 Stefan Krüger |
* @version 1.2 Stefan Tzeggai |
76 |
*/ |
*/ |
77 |
public class MapPaneToolBar extends JToolBar { |
public class MapPaneToolBar extends JToolBar { |
78 |
private static final Logger LOGGER = Logger.getLogger(MapPaneToolBar.class |
private static final Logger LOGGER = Logger.getLogger(MapPaneToolBar.class |
79 |
.getName()); |
.getName()); |
80 |
|
|
|
public static ResourceProvider RESOURCE = new ResourceProvider(LangUtil |
|
|
.extendPackagePath(MapPaneToolBar.class, |
|
|
"resource.locales.mapPaneToolbar"), Locale.ENGLISH); |
|
|
|
|
81 |
public static String R(String key, Object... values) { |
public static String R(String key, Object... values) { |
82 |
return RESOURCE.getString(key, values); |
return GeotoolsGUIUtil.R(key, values); |
83 |
} |
} |
84 |
|
|
85 |
/** Constant for the tool "Panning" (10). */ |
/** Constant for the tool "Panning" (10). */ |
168 |
} |
} |
169 |
|
|
170 |
}; |
}; |
171 |
|
private CrsLabel crsViewer; |
172 |
|
|
173 |
/** |
/** |
174 |
* Creates a new toolbar. Notice: This toolbar does nothing until |
* Creates a new toolbar. Notice: This toolbar does nothing until |
204 |
|
|
205 |
// Create a Listener to listen to the zooms on the JMapPane |
// Create a Listener to listen to the zooms on the JMapPane |
206 |
this.mapPaneListener = new JMapPaneListener() { |
this.mapPaneListener = new JMapPaneListener() { |
207 |
public void performMapPaneEvent(MapPaneEvent e) { |
public void performMapPaneEvent(XMapPaneEvent e) { |
208 |
|
|
209 |
if (!(e instanceof MapAreaChangedEvent)) |
if (!(e instanceof MapAreaChangedEvent)) |
210 |
return; |
return; |
211 |
|
|
256 |
setButtonEnabled(ACTION_ZOOM_BACK, lastZooms.size() > 1); |
setButtonEnabled(ACTION_ZOOM_BACK, lastZooms.size() > 1); |
257 |
setButtonEnabled(ACTION_ZOOM_FORWARD, false); |
setButtonEnabled(ACTION_ZOOM_FORWARD, false); |
258 |
} |
} |
259 |
|
|
260 |
}; |
}; |
261 |
|
|
262 |
setMapPane(mapPane); |
setMapPane(mapPane); |
266 |
init(); |
init(); |
267 |
} |
} |
268 |
|
|
269 |
|
private CrsLabel getCrsComponent() { |
270 |
|
if (crsViewer == null) { |
271 |
|
crsViewer = new CrsLabel(this.mapPane); |
272 |
|
} |
273 |
|
return crsViewer; |
274 |
|
} |
275 |
|
|
276 |
/** |
/** |
277 |
* Sets the {@link SelectableXMapPane} controlled by this tool bar. |
* Sets the {@link SelectableXMapPane} controlled by this tool bar. |
278 |
* |
* |
279 |
* @param mapPane |
* @param mapPane |
280 |
* {@link SelectableXMapPane} to control (if {@code null} this tool bar |
* {@link SelectableXMapPane} to control (if {@code null} this |
281 |
* controls NOTHING!) |
* tool bar controls NOTHING!) |
282 |
*/ |
*/ |
283 |
public void setMapPane(SelectableXMapPane mapPane) { |
public void setMapPane(SelectableXMapPane mapPane) { |
284 |
// Remove listener from old MapPane |
// Remove listener from old MapPane |
307 |
* Creates the tool buttons and action buttons and seperators, adds them to |
* Creates the tool buttons and action buttons and seperators, adds them to |
308 |
* {@link #toolAndActionButtons} and finally creates a button group for all |
* {@link #toolAndActionButtons} and finally creates a button group for all |
309 |
* tools. So sub-classes which override this method should FIRST add their |
* tools. So sub-classes which override this method should FIRST add their |
310 |
* new tool buttons to {@link #toolAndActionButtons} before calling {@code |
* new tool buttons to {@link #toolAndActionButtons} before calling |
311 |
* super.initTools()}. |
* {@code super.initTools()}. |
312 |
*/ |
*/ |
313 |
protected void initToolsAndActions() { |
protected void initToolsAndActions() { |
314 |
// Panning |
// Pan |
315 |
// addTool(new MapPaneToolBarAction(TOOL_PAN, this, "", new ImageIcon( |
addTool(new MapPaneToolBarAction(TOOL_PAN, this, XMapPaneTool.PAN), |
316 |
// MapView.class.getResource("resource/icons/pan.png")), |
false); |
|
// R("MapPaneButtons.Pan.TT")), false); |
|
317 |
|
|
|
addTool(new MapPaneToolBarAction(TOOL_PAN, this, XMapPaneTool.PAN), false); |
|
|
|
|
318 |
// Info |
// Info |
319 |
// addTool(new MapPaneToolBarAction(TOOL_INFO, this, "", new ImageIcon( |
addTool(new MapPaneToolBarAction(TOOL_INFO, this, XMapPaneTool.INFO), |
320 |
// MapView.class.getResource("resource/icons/info.png")), |
false); |
|
// R("MapPaneButtons.Info.TT")), false); |
|
|
addTool(new MapPaneToolBarAction(TOOL_INFO, this, XMapPaneTool.INFO), false); |
|
321 |
|
|
322 |
// Zoom in |
// Zoom in |
323 |
addTool(new MapPaneToolBarAction(TOOL_ZOOMIN, this, XMapPaneTool.ZOOM_IN), false); |
addTool(new MapPaneToolBarAction(TOOL_ZOOMIN, this, |
324 |
|
XMapPaneTool.ZOOM_IN), false); |
325 |
|
|
326 |
// Zoom out |
// Zoom out |
327 |
addTool(new MapPaneToolBarAction(TOOL_ZOOMOUT, this, XMapPaneTool.ZOOM_OUT), false); |
addTool(new MapPaneToolBarAction(TOOL_ZOOMOUT, this, |
328 |
|
XMapPaneTool.ZOOM_OUT), false); |
|
|
|
|
// // Zoom in |
|
|
// addTool(new MapPaneToolBarAction(TOOL_ZOOMIN, this, "", new ImageIcon( |
|
|
// MapView.class.getResource("resource/icons/zoom_in.png")), |
|
|
// R("MapPaneButtons.ZoomIn.TT")), false); |
|
|
// |
|
|
// // Zoom out |
|
|
// addTool(new MapPaneToolBarAction(TOOL_ZOOMOUT, this, "", new ImageIcon( |
|
|
// MapView.class.getResource("resource/icons/zoom_out.png")), |
|
|
// R("MapPaneButtons.ZoomOut.TT")), false); |
|
|
|
|
|
|
|
329 |
|
|
330 |
// Action button to revert the last zoom |
// Action button to revert the last zoom |
331 |
addAction(new MapPaneToolBarAction(ACTION_ZOOM_BACK, this, "", |
addAction( |
332 |
new ImageIcon(MapView.class |
new MapPaneToolBarAction(ACTION_ZOOM_BACK, this, "", |
333 |
.getResource("resource/icons/zoom_back.png")), |
new ImageIcon(MapView.class |
334 |
R("MapPaneButtons.LastZoom.TT")), false); |
.getResource("resource/icons/zoom_back.png")), |
335 |
|
R("MapPaneButtons.LastZoom.TT")), false); |
336 |
setButtonEnabled(ACTION_ZOOM_BACK, false); |
setButtonEnabled(ACTION_ZOOM_BACK, false); |
337 |
|
|
338 |
// Action button to redo the last zoom |
// Action button to redo the last zoom |
339 |
addAction(new MapPaneToolBarAction(ACTION_ZOOM_FORWARD, this, "", |
addAction( |
340 |
new ImageIcon(MapView.class |
new MapPaneToolBarAction( |
341 |
.getResource("resource/icons/zoom_forward.png")), |
ACTION_ZOOM_FORWARD, |
342 |
R("MapPaneButtons.NextZoom.TT")), false); |
this, |
343 |
|
"", |
344 |
|
new ImageIcon(MapView.class |
345 |
|
.getResource("resource/icons/zoom_forward.png")), |
346 |
|
R("MapPaneButtons.NextZoom.TT")), false); |
347 |
setButtonEnabled(ACTION_ZOOM_FORWARD, false); |
setButtonEnabled(ACTION_ZOOM_FORWARD, false); |
348 |
|
|
349 |
// set the selected tool enabled |
// set the selected tool enabled |
350 |
setSelectedTool(selectedTool); |
setSelectedTool(selectedTool); |
351 |
|
|
352 |
|
add(getCrsComponent()); |
353 |
|
|
354 |
} |
} |
355 |
|
|
356 |
@Override |
@Override |
363 |
* the tool bar. |
* the tool bar. |
364 |
*/ |
*/ |
365 |
public void initToolBar() { |
public void initToolBar() { |
|
// setOpaque(true); |
|
|
// |
|
|
// setAlignmentY(1f); |
|
|
// setAlignmentX(0.5f); |
|
366 |
removeAll(); |
removeAll(); |
367 |
|
|
368 |
// // Separator to the left of the tool actions to start |
// // Separator to the left of the tool actions to start |
405 |
repaint(); |
repaint(); |
406 |
} |
} |
407 |
|
|
|
// Space between tool buttons and action buttons |
|
|
// SK: Seperators are now als manages like actions and tools |
|
|
// Dimension dimension2 = new Dimension( 10,10); |
|
|
// this.addSeparator(dimension2); |
|
|
|
|
|
// // Action buttons |
|
|
// for (JButton b : actionButtons.values()) |
|
|
// add(b); |
|
|
// } |
|
|
|
|
408 |
/** |
/** |
409 |
* Performs the activation of a tool. |
* Performs the activation of a tool. |
410 |
* |
* |
421 |
|
|
422 |
switch (tool) { |
switch (tool) { |
423 |
case TOOL_PAN: |
case TOOL_PAN: |
|
// Set the mouse tool to "Panning" |
|
|
// mapPane.setState(SelectableXMapPane.PAN); |
|
424 |
mapPane.setTool(XMapPaneTool.PAN); |
mapPane.setTool(XMapPaneTool.PAN); |
|
// mapPane.setNormalCursor(SwingUtil.PAN_CURSOR); |
|
425 |
break; |
break; |
426 |
case TOOL_INFO: |
case TOOL_INFO: |
|
// Set the mouse tool to "Info" |
|
427 |
mapPane.setTool(XMapPaneTool.INFO); |
mapPane.setTool(XMapPaneTool.INFO); |
|
// mapPane.setState(SelectableXMapPane.SELECT_ONE_FROM_TOP); |
|
|
// mapPane.setNormalCursor(SwingUtil.CROSSHAIR_CURSOR); |
|
428 |
break; |
break; |
429 |
case TOOL_ZOOMIN: |
case TOOL_ZOOMIN: |
|
// Set the mouse tool to "Zoom in" |
|
430 |
mapPane.setTool(XMapPaneTool.ZOOM_IN); |
mapPane.setTool(XMapPaneTool.ZOOM_IN); |
|
// mapPane.setNormalCursor(SwingUtil.ZOOMIN_CURSOR); |
|
431 |
break; |
break; |
432 |
case TOOL_ZOOMOUT: |
case TOOL_ZOOMOUT: |
|
// Set the mouse tool to "Zoom out" |
|
433 |
mapPane.setTool(XMapPaneTool.ZOOM_OUT); |
mapPane.setTool(XMapPaneTool.ZOOM_OUT); |
|
// mapPane.setState(SelectableXMapPane.ZOOM_OUT); |
|
|
// mapPane.setNormalCursor(SwingUtil.ZOOMOUT_CURSOR); |
|
434 |
break; |
break; |
|
// default: |
|
|
// Set map actions to default |
|
|
// mapPane.setState(SelectableXMapPane.NONE); |
|
|
// mapPane.setNormalCursor(null); |
|
|
// break; |
|
435 |
} |
} |
|
// mapPane.updateCursorAndRepaintTimer(); |
|
436 |
} |
} |
437 |
|
|
438 |
/** |
/** |
456 |
protected void performActionButton(int action, ActionEvent e) { |
protected void performActionButton(int action, ActionEvent e) { |
457 |
if (mapPane == null) |
if (mapPane == null) |
458 |
return; |
return; |
459 |
|
|
460 |
// Perform the action "Zoom back": Revert the last zoom |
// Perform the action "Zoom back": Revert the last zoom |
461 |
if (action == ACTION_ZOOM_BACK) { |
if (action == ACTION_ZOOM_BACK) { |
462 |
if (zoomBackIndex <= 1) |
if (zoomBackIndex <= 1) |
499 |
*/ |
*/ |
500 |
public void addTool(MapPaneToolBarAction buttonAction, boolean resetToolBar) { |
public void addTool(MapPaneToolBarAction buttonAction, boolean resetToolBar) { |
501 |
if (isButtonIDUsed(buttonAction.getID())) { |
if (isButtonIDUsed(buttonAction.getID())) { |
502 |
LOGGER |
LOGGER.warn("addTool(.) ignored because ID already used for tool or action: " |
503 |
.warn("addTool(.) ignored because ID already used for tool or action: " |
+ buttonAction.getID()); |
|
+ buttonAction.getID()); |
|
504 |
return; |
return; |
505 |
} |
} |
506 |
JToggleButton button = new SmallToggleButton(buttonAction); |
JToggleButton button = new SmallToggleButton(buttonAction); |
507 |
|
|
|
// button.setBorder(BorderFactory.createRaisedBevelBorder()); |
|
508 |
toolButtonGroup.add(button); |
toolButtonGroup.add(button); |
509 |
toolAndActionButtons.put(buttonAction.getID(), button); |
toolAndActionButtons.put(buttonAction.getID(), button); |
510 |
if (resetToolBar) |
if (resetToolBar) |
535 |
public void addAction(MapPaneToolBarAction buttonAction, |
public void addAction(MapPaneToolBarAction buttonAction, |
536 |
boolean resetToolBar) { |
boolean resetToolBar) { |
537 |
if (isButtonIDUsed(buttonAction.getID())) { |
if (isButtonIDUsed(buttonAction.getID())) { |
538 |
LOGGER |
LOGGER.warn("addAction(.) ignored because ID already used for tool or action: " |
539 |
.warn("addAction(.) ignored because ID already used for tool or action: " |
+ buttonAction.getID()); |
|
+ buttonAction.getID()); |
|
540 |
return; |
return; |
541 |
} |
} |
542 |
JButton button = new SmallButton(buttonAction); |
JButton button = new SmallButton(buttonAction); |
|
// button.setBorder(BorderFactory.createRaisedBevelBorder()); |
|
543 |
toolAndActionButtons.put(buttonAction.getID(), button); |
toolAndActionButtons.put(buttonAction.getID(), button); |
544 |
if (resetToolBar) |
if (resetToolBar) |
545 |
initToolBar(); |
initToolBar(); |
561 |
public void addJComponent(JComponent component, int id, boolean resetToolBar) { |
public void addJComponent(JComponent component, int id, boolean resetToolBar) { |
562 |
|
|
563 |
if (isButtonIDUsed(id)) { |
if (isButtonIDUsed(id)) { |
564 |
LOGGER |
LOGGER.warn("addAction(.) ignored because ID already used for tool or action: " |
565 |
.warn("addAction(.) ignored because ID already used for tool or action: " |
+ id); |
|
+ id); |
|
566 |
return; |
return; |
567 |
} |
} |
568 |
|
|
573 |
|
|
574 |
public void addSeparator(int id, Separator separator) { |
public void addSeparator(int id, Separator separator) { |
575 |
if (isButtonIDUsed(id)) { |
if (isButtonIDUsed(id)) { |
576 |
LOGGER |
LOGGER.warn("addSeparator(.) ignored because ID already used for tool or action. "); |
|
.warn("addSeparator(.) ignored because ID already used for tool or action. "); |
|
577 |
return; |
return; |
578 |
} |
} |
579 |
toolAndActionButtons.put(id, separator); |
toolAndActionButtons.put(id, separator); |
600 |
* {@linkplain #getToolButton(int) tool button} |
* {@linkplain #getToolButton(int) tool button} |
601 |
*/ |
*/ |
602 |
public AbstractButton getButton(int id) { |
public AbstractButton getButton(int id) { |
603 |
|
|
604 |
//ACHUTNG: Das ist ein SK QUICK FIX! TODO |
// ACHUTNG: Das ist ein SK QUICK FIX! TODO |
605 |
if (!(toolAndActionButtons.get(id) instanceof AbstractButton)) return null; |
if (!(toolAndActionButtons.get(id) instanceof AbstractButton)) |
606 |
|
return null; |
607 |
|
|
608 |
AbstractButton button = (AbstractButton) toolAndActionButtons.get(id); |
AbstractButton button = (AbstractButton) toolAndActionButtons.get(id); |
609 |
if (button == null) |
if (button == null) |
610 |
LOGGER.warn("Unknown tool or action ID: " + id); |
LOGGER.warn("Unknown tool or action ID: " + id); |
671 |
return -1; |
return -1; |
672 |
return selectedTool; |
return selectedTool; |
673 |
} |
} |
674 |
|
|
675 |
/** |
/** |
676 |
* Sets whether a tool or action is activated or not. The visible property |
* Sets whether a tool or action is activated or not. The visible property |
677 |
* of the button is not affected. |
* of the button is not affected. |
748 |
public void setAllToolsEnabled(boolean enabled, boolean hideOnDisable) { |
public void setAllToolsEnabled(boolean enabled, boolean hideOnDisable) { |
749 |
for (int id : toolAndActionButtons.keySet()) { |
for (int id : toolAndActionButtons.keySet()) { |
750 |
if (toolAndActionButtons.get(id) instanceof JToggleButton) { |
if (toolAndActionButtons.get(id) instanceof JToggleButton) { |
751 |
setButtonEnabled(id, enabled, hideOnDisable); |
setButtonEnabled(id, enabled, hideOnDisable); |
752 |
} |
} |
753 |
} |
} |
754 |
} |
} |
755 |
|
|
756 |
/** |
/** |
757 |
* Sets the activation for all actions. |
* Sets the activation for all actions. |
758 |
* |
* |
873 |
this.toolBar = toolBar; |
this.toolBar = toolBar; |
874 |
} |
} |
875 |
|
|
876 |
public MapPaneToolBarAction(int id, MapPaneToolBar toolBar, XMapPaneTool tool) { |
public MapPaneToolBarAction(int id, MapPaneToolBar toolBar, |
877 |
|
XMapPaneTool tool) { |
878 |
this(id, toolBar, "", tool.getIcon(), tool.getToolTip()); |
this(id, toolBar, "", tool.getIcon(), tool.getToolTip()); |
879 |
} |
} |
880 |
|
|
906 |
* Nuetzlich wenn die Componente gedruckt (z.B. wenn ein Screenshot gemacht |
* Nuetzlich wenn die Componente gedruckt (z.B. wenn ein Screenshot gemacht |
907 |
* wird) wird. Dann werden wird der Hintergrund auf WEISS gesetzt. |
* wird) wird. Dann werden wird der Hintergrund auf WEISS gesetzt. |
908 |
* |
* |
909 |
* @author <a href="mailto:[email protected]">Stefan Alfons |
* @author <a href="mailto:[email protected]">Stefan Alfons Tzeggai</a> |
|
* Krüger</a> |
|
910 |
*/ |
*/ |
911 |
@Override |
@Override |
912 |
public void print(Graphics g) { |
public void print(Graphics g) { |
920 |
} |
} |
921 |
} |
} |
922 |
|
|
923 |
|
@Override |
924 |
|
protected void finalize() throws Throwable { |
925 |
|
super.finalize(); |
926 |
|
|
927 |
|
if (mapPane != null && mapPaneListener != null) |
928 |
|
mapPane.removeMapPaneListener(mapPaneListener); |
929 |
|
} |
930 |
|
|
931 |
} |
} |