/[schmitzm]/branches/2.3.x/src/skrueger/geotools/MapPaneToolBar.java
ViewVC logotype

Annotation of /branches/2.3.x/src/skrueger/geotools/MapPaneToolBar.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1355 - (hide annotations)
Sat Dec 18 22:36:54 2010 UTC (14 years, 2 months ago) by alfonx
File size: 27057 byte(s)
The last commit was ment for trunk, not 1.6! Un-did the last commit!
1 alfonx 244 /*******************************************************************************
2     * Copyright (c) 2009 Martin O. J. Schmitz.
3     *
4     * This file is part of the SCHMITZM library - a collection of utility
5 alfonx 256 * classes based on Java 1.6, focusing (not only) on Java Swing
6 alfonx 244 * and the Geotools library.
7     *
8     * The SCHMITZM project is hosted at:
9     * http://wald.intevation.org/projects/schmitzm/
10     *
11     * This program is free software; you can redistribute it and/or
12     * modify it under the terms of the GNU Lesser General Public License
13     * as published by the Free Software Foundation; either version 3
14     * of the License, or (at your option) any later version.
15     *
16     * This program is distributed in the hope that it will be useful,
17     * but WITHOUT ANY WARRANTY; without even the implied warranty of
18     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19     * GNU General Public License for more details.
20     *
21     * You should have received a copy of the GNU Lesser General Public License (license.txt)
22     * along with this program; if not, write to the Free Software
23     * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24     * or try this link: http://www.gnu.org/licenses/lgpl.html
25     *
26     * Contributors:
27     * Martin O. J. Schmitz - initial API and implementation
28 alfonx 862 * Stefan A. Tzeggai - additional utility classes
29 alfonx 244 ******************************************************************************/
30     package skrueger.geotools;
31    
32     import java.awt.Color;
33     import java.awt.Graphics;
34     import java.awt.event.ActionEvent;
35 alfonx 414 import java.awt.event.ActionListener;
36 alfonx 244 import java.util.ArrayList;
37 alfonx 414 import java.util.HashSet;
38     import java.util.Set;
39 alfonx 244 import java.util.SortedMap;
40     import java.util.TreeMap;
41    
42     import javax.swing.AbstractAction;
43     import javax.swing.AbstractButton;
44     import javax.swing.Action;
45     import javax.swing.Icon;
46     import javax.swing.ImageIcon;
47     import javax.swing.JButton;
48     import javax.swing.JComponent;
49     import javax.swing.JToggleButton;
50     import javax.swing.JToolBar;
51    
52     import org.apache.log4j.Logger;
53    
54 alfonx 886 import schmitzm.geotools.gui.GeotoolsGUIUtil;
55 alfonx 509 import schmitzm.geotools.gui.SelectableXMapPane;
56 alfonx 740 import schmitzm.geotools.gui.XMapPaneEvent;
57     import schmitzm.geotools.gui.XMapPaneTool;
58 alfonx 244 import schmitzm.geotools.map.event.JMapPaneListener;
59     import schmitzm.geotools.map.event.MapAreaChangedEvent;
60     import schmitzm.swing.ButtonGroup;
61 alfonx 486 import skrueger.swing.SmallButton;
62     import skrueger.swing.SmallToggleButton;
63 alfonx 244
64     import com.vividsolutions.jts.geom.Envelope;
65    
66     /**
67 alfonx 1220 * A toolbar to control an {@link SelectableXMapPane} (Atlas visualization).
68     * This contains two types of buttons. A group of <i>tools</i> for the mouse
69     * actions on the map represented by {@link JToggleButton JToggleButtons}, where
70     * only one tool can be activated every time. And some (general) <i>actions</i>,
71     * represented by normal {@link JButton JButtons}.
72 alfonx 244 *
73     * @author <a href="mailto:[email protected]">Martin Schmitz</a>
74     * (University of Bonn/Germany)
75 alfonx 862 * @version 1.2 Stefan Tzeggai
76 alfonx 244 */
77     public class MapPaneToolBar extends JToolBar {
78     private static final Logger LOGGER = Logger.getLogger(MapPaneToolBar.class
79     .getName());
80 alfonx 445
81 alfonx 244 public static String R(String key, Object... values) {
82 alfonx 886 return GeotoolsGUIUtil.R(key, values);
83 alfonx 244 }
84    
85     /** Constant for the tool "Panning" (10). */
86     public static final int TOOL_PAN = 10;
87     /** Constant for the tool "Info" (20). */
88     public static final int TOOL_INFO = 20;
89     public static final int SEPERATOR0 = 99;
90    
91     /** Constant for the tool "Zoom In" (110). */
92     public static final int TOOL_ZOOMIN = 110;
93 alfonx 680 public static final int ACTION_ZOOM_DEFAULT = 150;
94 alfonx 244 /** Constant for the tool "Zoom Out" (120). */
95     public static final int TOOL_ZOOMOUT = 120;
96     /** Constant for the action "Zoom back" (130). */
97     public static final int ACTION_ZOOM_BACK = 130;
98     /** Constant for the action "Zoom forward" (140). */
99     public static final int ACTION_ZOOM_FORWARD = 140;
100     public static final int SEPERATOR1 = 199;
101    
102     /**
103     * Constant for the tool "Selection Reset" which clears the selection (240).
104     */
105     public static final int TOOL_SELECTION_CLEAR = 240;
106    
107     /**
108     * Constant for the tool "Select" which sets the Selection to the selected
109     * features (210).
110     */
111     public static final int TOOL_SELECTION_SET = 210;
112     /**
113     * Constant for the tool "Selection add" which adds the features to the
114     * Selection (220).
115     */
116     public static final int TOOL_SELECTION_ADD = 220;
117     /**
118     * Constant for the tool "Selection subtract" which removes the selected
119     * features from the selection (230).
120     */
121     public static final int TOOL_SELECTION_REMOVE = 230;
122    
123 alfonx 297 public static final int ACTION_CHARTS = 401;
124    
125 alfonx 244 /** Tool currently selected */
126     protected int selectedTool = TOOL_ZOOMIN;
127    
128     /** Holds the tool buttons of the tool bar. */
129 alfonx 414 final protected SortedMap<Integer, JComponent> toolAndActionButtons = new TreeMap<Integer, JComponent>();
130 alfonx 244 /** Controls that only one tool button is activated. */
131     protected ButtonGroup toolButtonGroup = null;
132    
133     // SK: Musste ich ändern damit man Tools und Actions in der Reihenfolge
134     // mischen kann.
135     // /** Holds the action buttons of the bar. */
136     // protected SortedMap<Integer, JButton> actionButtons = null;
137    
138 alfonx 509 /** Holds the {@link SelectableXMapPane} this tool bar controls. */
139     protected SelectableXMapPane mapPane = null;
140 alfonx 244
141     /**
142     * A List to remember the last Envelopes that have been watched. Used for
143     * the zoomBack- and zoomForwardButtons *
144     */
145     protected ArrayList<Envelope> lastZooms = new ArrayList<Envelope>();
146     /** Holds the index to the current element in {@link #lastZooms}. */
147     protected int zoomBackIndex = 0;
148    
149     /** Listener to sniff the zoom actions on the map. */
150     protected JMapPaneListener mapPaneListener = null;
151    
152     protected boolean zoomBackForwardButtonInAction;
153    
154 alfonx 414 /** Listeners what want to be informed about a change of the selected tool **/
155     protected Set<MapPaneToolSelectedListener> toolSelectionListeners = new HashSet<MapPaneToolSelectedListener>();
156    
157 alfonx 445 /** This listener is added to all {@link JToggleButton} **/
158     private final ActionListener toolSelectedListener = new ActionListener() {
159 alfonx 414
160     @Override
161     public void actionPerformed(ActionEvent e) {
162     JToggleButton tb = (JToggleButton) e.getSource();
163 alfonx 445
164 alfonx 414 // Inform the listeners about a newly selected tool
165     for (MapPaneToolSelectedListener l : toolSelectionListeners) {
166 alfonx 445 l.toolSelected(Integer.valueOf(tb.getName()));
167 alfonx 414 }
168     }
169 alfonx 445
170 alfonx 414 };
171    
172 alfonx 244 /**
173     * Creates a new toolbar. Notice: This toolbar does nothing until
174 alfonx 509 * {@link #setMapPane(SelectableXMapPane)} is called!
175 alfonx 244 */
176     public MapPaneToolBar() {
177     this(null);
178     }
179 alfonx 445
180     public void addButtonSelectedListener(MapPaneToolSelectedListener listener) {
181 alfonx 414 toolSelectionListeners.add(listener);
182     }
183 alfonx 244
184 alfonx 445 public void removeButtonSelectedListener(
185     MapPaneToolSelectedListener listener) {
186     toolSelectionListeners.remove(listener);
187 alfonx 414 }
188    
189 alfonx 244 /**
190     * Creates a new tool bar.
191     *
192     * @param mapPane
193 alfonx 509 * {@link SelectableXMapPane} the tool bar controls
194 alfonx 244 */
195 alfonx 509 public MapPaneToolBar(SelectableXMapPane mapPane) {
196 alfonx 244 super("Control the map", JToolBar.HORIZONTAL);
197 alfonx 445
198     // I want to see nothing on the background
199 alfonx 417 setOpaque(false);
200     setBorder(null);
201 alfonx 445
202 alfonx 244 this.toolButtonGroup = new ButtonGroup();
203 alfonx 445
204 alfonx 244 // Create a Listener to listen to the zooms on the JMapPane
205     this.mapPaneListener = new JMapPaneListener() {
206 alfonx 740 public void performMapPaneEvent(XMapPaneEvent e) {
207 alfonx 1220
208 alfonx 244 if (!(e instanceof MapAreaChangedEvent))
209     return;
210    
211     if (zoomBackForwardButtonInAction) {
212     zoomBackForwardButtonInAction = false;
213     return;
214     }
215    
216     final MapAreaChangedEvent mapAreaChangedEvent = (MapAreaChangedEvent) e;
217     Envelope oldMapArea = mapAreaChangedEvent.getOldMapArea();
218 alfonx 445
219 alfonx 244 final Envelope mapArea = mapAreaChangedEvent.getNewMapArea();
220 alfonx 445 if (mapArea == null || mapArea.equals(oldMapArea)
221     || Double.isNaN(mapArea.getMinX())
222     || Double.isNaN(mapArea.getMaxX())
223     || Double.isNaN(mapArea.getMinY())
224     || Double.isNaN(mapArea.getMaxY())) {
225     // If the MapArea didn't change... we don't want to register
226     // it as a zoom action.
227 alfonx 244 return;
228     }
229 alfonx 445
230     if (lastZooms.size() == 0
231     && oldMapArea != null
232     && !oldMapArea.isNull()
233     && !(Double.isNaN(oldMapArea.getMinX())
234     || Double.isNaN(oldMapArea.getMaxX())
235     || Double.isNaN(oldMapArea.getMinY()) || Double
236     .isNaN(oldMapArea.getMaxY()))) {
237 alfonx 244 lastZooms.add(oldMapArea);
238     zoomBackIndex = 1;
239     }
240     if (mapArea == null)
241     return;
242    
243     if (lastZooms.size() > 0
244     && mapArea.equals(lastZooms.get(lastZooms.size() - 1))) {
245     // LOGGER.debug("MapAreaChangedEvent ausgelassen bei der Zaehlung der Zoomschritt weil identisch");
246     return;
247     }
248    
249     if (lastZooms.size() > 0)
250     while (zoomBackIndex < lastZooms.size())
251     lastZooms.remove(lastZooms.size() - 1);
252    
253     lastZooms.add(mapArea);
254     zoomBackIndex = lastZooms.size();
255     setButtonEnabled(ACTION_ZOOM_BACK, lastZooms.size() > 1);
256     setButtonEnabled(ACTION_ZOOM_FORWARD, false);
257     }
258 alfonx 1220
259 alfonx 244 };
260    
261     setMapPane(mapPane);
262     setFloatable(false);
263     setRollover(true);
264    
265     init();
266     }
267    
268 alfonx 1355 // private CrsLabel getCrsComponent() {
269     // if (crsViewer == null) {
270     // crsViewer = new CrsLabel(this.mapPane);
271     // }
272     // return crsViewer;
273     // }
274    
275 alfonx 244 /**
276 alfonx 509 * Sets the {@link SelectableXMapPane} controlled by this tool bar.
277 alfonx 244 *
278     * @param mapPane
279 alfonx 1220 * {@link SelectableXMapPane} to control (if {@code null} this
280     * tool bar controls NOTHING!)
281 alfonx 244 */
282 alfonx 509 public void setMapPane(SelectableXMapPane mapPane) {
283 alfonx 244 // Remove listener from old MapPane
284     if (this.mapPane != null)
285     this.mapPane.removeMapPaneListener(mapPaneListener);
286     this.mapPane = mapPane;
287     if (this.mapPane != null && mapPaneListener != null)
288 alfonx 1355 this.mapPane.addMapPaneListener(mapPaneListener);
289 alfonx 244 }
290    
291     /**
292     * Calls {@link #initToolsAndActions()} and {@link #initActions()} and then
293     * puts all tool buttons and all actions buttons to the tool bar.
294     */
295     protected void init() {
296 alfonx 445
297 alfonx 244 initToolsAndActions();
298    
299     addSeparator(SEPERATOR0, new JToolBar.Separator());
300     addSeparator(SEPERATOR1, new JToolBar.Separator());
301    
302     initToolBar();
303     }
304    
305     /**
306     * Creates the tool buttons and action buttons and seperators, adds them to
307     * {@link #toolAndActionButtons} and finally creates a button group for all
308     * tools. So sub-classes which override this method should FIRST add their
309 alfonx 1220 * new tool buttons to {@link #toolAndActionButtons} before calling
310     * {@code super.initTools()}.
311 alfonx 244 */
312     protected void initToolsAndActions() {
313 alfonx 891 // Pan
314 alfonx 1220 addTool(new MapPaneToolBarAction(TOOL_PAN, this, XMapPaneTool.PAN),
315     false);
316    
317 alfonx 244 // Info
318 alfonx 1220 addTool(new MapPaneToolBarAction(TOOL_INFO, this, XMapPaneTool.INFO),
319     false);
320 alfonx 680
321 alfonx 244 // Zoom in
322 alfonx 1220 addTool(new MapPaneToolBarAction(TOOL_ZOOMIN, this,
323     XMapPaneTool.ZOOM_IN), false);
324    
325 alfonx 244 // Zoom out
326 alfonx 1220 addTool(new MapPaneToolBarAction(TOOL_ZOOMOUT, this,
327     XMapPaneTool.ZOOM_OUT), false);
328    
329 alfonx 244 // Action button to revert the last zoom
330 alfonx 1220 addAction(
331     new MapPaneToolBarAction(ACTION_ZOOM_BACK, this, "",
332     new ImageIcon(MapView.class
333     .getResource("resource/icons/zoom_back.png")),
334     R("MapPaneButtons.LastZoom.TT")), false);
335 alfonx 244 setButtonEnabled(ACTION_ZOOM_BACK, false);
336    
337     // Action button to redo the last zoom
338 alfonx 1220 addAction(
339     new MapPaneToolBarAction(
340     ACTION_ZOOM_FORWARD,
341     this,
342     "",
343     new ImageIcon(MapView.class
344     .getResource("resource/icons/zoom_forward.png")),
345     R("MapPaneButtons.NextZoom.TT")), false);
346 alfonx 244 setButtonEnabled(ACTION_ZOOM_FORWARD, false);
347    
348     // set the selected tool enabled
349     setSelectedTool(selectedTool);
350    
351     }
352 alfonx 445
353 alfonx 417 @Override
354     public void paint(Graphics g) {
355     super.paint(g);
356     }
357 alfonx 244
358     /**
359     * Clears the GUI of all components and adds all tool and action buttons to
360     * the tool bar.
361     */
362     public void initToolBar() {
363     removeAll();
364 alfonx 445
365     // // Separator to the left of the tool actions to start
366     // // the tool buttons with the map (not with the coordinate grid)
367     // Dimension dimension = new Dimension(49, 10);
368     // addSeparator(dimension);
369    
370 alfonx 244 // Tool buttons
371 alfonx 414 for (Integer bKey : toolAndActionButtons.keySet()) {
372 alfonx 445
373 alfonx 414 JComponent b = toolAndActionButtons.get(bKey);
374 alfonx 445
375 alfonx 414 if (b instanceof JToggleButton) {
376     JToggleButton tb = (JToggleButton) b;
377     tb.setName(bKey.toString());
378 alfonx 445 tb.addActionListener(toolSelectedListener);
379 alfonx 414 }
380 alfonx 445
381 alfonx 244 add(b);
382 alfonx 414 }
383 alfonx 244
384     if (!toolAndActionButtons.containsKey(selectedTool)) {
385     /**
386     * This might be a bit specific, but IF selection buttons are
387     * available, select one of them.. if not, select the INFO tool.
388     */
389    
390     if (toolAndActionButtons.containsKey(TOOL_SELECTION_SET)) {
391     setSelectedTool(TOOL_SELECTION_SET);
392     } else if (toolAndActionButtons.containsKey(TOOL_INFO)) {
393     setSelectedTool(TOOL_INFO);
394     } else {
395     // TODO What to do now?!
396     setSelectedTool(null);
397     }
398    
399     }
400 alfonx 445
401 alfonx 244 revalidate();
402     repaint();
403     }
404    
405     /**
406     * Performs the activation of a tool.
407     *
408     * @param tool
409     * the tool to activate
410     * @param e
411     * the event of the button
412     */
413     public void performToolButton(int tool, ActionEvent e) {
414     if (mapPane == null)
415     return;
416    
417     selectedTool = tool;
418    
419     switch (tool) {
420     case TOOL_PAN:
421 alfonx 653 mapPane.setTool(XMapPaneTool.PAN);
422 alfonx 244 break;
423     case TOOL_INFO:
424 alfonx 653 mapPane.setTool(XMapPaneTool.INFO);
425 alfonx 244 break;
426     case TOOL_ZOOMIN:
427 alfonx 653 mapPane.setTool(XMapPaneTool.ZOOM_IN);
428 alfonx 244 break;
429     case TOOL_ZOOMOUT:
430 alfonx 653 mapPane.setTool(XMapPaneTool.ZOOM_OUT);
431 alfonx 244 break;
432     }
433     }
434    
435     /**
436     * @param id
437     * The ID of the Component to remove. The change will not be
438     * visible until {@link #initToolBar()} is called.
439     * @return <code>null</code> or the component that has been removed.
440     */
441     public JComponent removeId(int id) {
442     return toolAndActionButtons.remove(id);
443     }
444    
445     /**
446     * Performs the action of an action button.
447     *
448     * @param tool
449     * the action
450     * @param e
451     * the event of the button
452     */
453     protected void performActionButton(int action, ActionEvent e) {
454     if (mapPane == null)
455     return;
456 alfonx 1220
457 alfonx 244 // Perform the action "Zoom back": Revert the last zoom
458     if (action == ACTION_ZOOM_BACK) {
459     if (zoomBackIndex <= 1)
460     return;
461    
462     zoomBackForwardButtonInAction = true;
463     zoomBackIndex--;
464     getButton(ACTION_ZOOM_FORWARD).setEnabled(true);
465     getButton(ACTION_ZOOM_BACK).setEnabled(zoomBackIndex > 1);
466    
467     mapPane.setMapArea(lastZooms.get(zoomBackIndex - 1));
468     mapPane.refresh();
469     }
470    
471     // Perform the action "Zoom forward": Re-do the last zoom
472     if (action == ACTION_ZOOM_FORWARD) {
473     if (zoomBackIndex < lastZooms.size()) {
474     zoomBackForwardButtonInAction = true;
475     zoomBackIndex++;
476     getButton(ACTION_ZOOM_BACK).setEnabled(true);
477     getButton(ACTION_ZOOM_FORWARD).setEnabled(
478     zoomBackIndex < lastZooms.size());
479    
480     mapPane.setMapArea(lastZooms.get(zoomBackIndex - 1));
481     mapPane.refresh();
482     }
483     }
484     }
485    
486     /**
487     * Adds a tool to the tool bar. Does nothing if a tool or action with the
488     * specified ID already exists!
489     *
490     * @param buttonAction
491     * action for the toggle button
492     * @param resetToolBar
493     * indicates whether the toolbar GUI is reset after adding the
494     * button (if adding several actions it useful only to reset the
495     * GUI for the last added tool)
496     */
497     public void addTool(MapPaneToolBarAction buttonAction, boolean resetToolBar) {
498     if (isButtonIDUsed(buttonAction.getID())) {
499 alfonx 1220 LOGGER.warn("addTool(.) ignored because ID already used for tool or action: "
500     + buttonAction.getID());
501 alfonx 244 return;
502     }
503 alfonx 486 JToggleButton button = new SmallToggleButton(buttonAction);
504 alfonx 1220
505 alfonx 244 toolButtonGroup.add(button);
506     toolAndActionButtons.put(buttonAction.getID(), button);
507     if (resetToolBar)
508     initToolBar();
509     }
510    
511     /**
512     * Adds a tool to the tool bar and resets the toolbar GUI.
513     *
514     * @param buttonAction
515     * action for the toggle button
516     */
517     public void addTool(MapPaneToolBarAction buttonAction) {
518     addTool(buttonAction, true);
519     }
520    
521     /**
522     * Adds an action to the tool bar. Does nothing if a tool or action with the
523     * specified ID already exists!
524     *
525     * @param buttonAction
526     * action for the button
527     * @param resetToolBar
528     * indicates whether the toolbar GUI is reset after adding the
529     * button (if adding several actions it useful only to reset the
530     * GUI for the last added tool)
531     */
532     public void addAction(MapPaneToolBarAction buttonAction,
533     boolean resetToolBar) {
534     if (isButtonIDUsed(buttonAction.getID())) {
535 alfonx 1220 LOGGER.warn("addAction(.) ignored because ID already used for tool or action: "
536     + buttonAction.getID());
537 alfonx 244 return;
538     }
539 alfonx 486 JButton button = new SmallButton(buttonAction);
540 alfonx 244 toolAndActionButtons.put(buttonAction.getID(), button);
541     if (resetToolBar)
542     initToolBar();
543     }
544 alfonx 445
545 alfonx 297 /**
546 alfonx 445 * Adds any JComponent to the tool bar. Does nothing if a tool or action
547     * with the specified ID already exists!
548 alfonx 297 *
549 alfonx 445 * @param component
550     * A {@link JComponent} that shall be added
551     * @param id
552     * The ID associaded with the {@link JComponent}
553 alfonx 297 * @param resetToolBar
554     * indicates whether the toolbar GUI is reset after adding the
555     * button (if adding several actions it useful only to reset the
556     * GUI for the last added tool)
557     */
558 alfonx 445 public void addJComponent(JComponent component, int id, boolean resetToolBar) {
559 alfonx 244
560 alfonx 297 if (isButtonIDUsed(id)) {
561 alfonx 1220 LOGGER.warn("addAction(.) ignored because ID already used for tool or action: "
562     + id);
563 alfonx 297 return;
564     }
565 alfonx 445
566 alfonx 297 toolAndActionButtons.put(id, component);
567     if (resetToolBar)
568     initToolBar();
569     }
570    
571 alfonx 244 public void addSeparator(int id, Separator separator) {
572     if (isButtonIDUsed(id)) {
573 alfonx 1220 LOGGER.warn("addSeparator(.) ignored because ID already used for tool or action. ");
574 alfonx 244 return;
575     }
576     toolAndActionButtons.put(id, separator);
577     }
578    
579     /**
580     * Adds an action to the tool bar and resets the toolbar GUI.
581     *
582     * @param buttonAction
583     * action for the toggle button
584     */
585     public void addAction(MapPaneToolBarAction buttonAction) {
586     addAction(buttonAction, true);
587     }
588    
589     /**
590     * Returns the button for a specific tool or action.
591     *
592     * @param id
593     * the constant for any button in the {@link MapPaneToolBar}
594     * @return a {@link JButton} if {@code id} specifies an
595     * {@linkplain #getActionButton(int) action button} or
596     * {@link JToogleButton} if {@code id} specifies a
597     * {@linkplain #getToolButton(int) tool button}
598     */
599     public AbstractButton getButton(int id) {
600 alfonx 1220
601     // ACHUTNG: Das ist ein SK QUICK FIX! TODO
602     if (!(toolAndActionButtons.get(id) instanceof AbstractButton))
603     return null;
604    
605 alfonx 244 AbstractButton button = (AbstractButton) toolAndActionButtons.get(id);
606     if (button == null)
607     LOGGER.warn("Unknown tool or action ID: " + id);
608     return button;
609     }
610    
611     /**
612     * Returns the button for a specific tool.
613     *
614     * @param tool
615     * the constant for a tool
616     */
617     public JToggleButton getToolButton(int tool) {
618     AbstractButton button = getButton(tool);
619     if (button != null && !(button instanceof JToggleButton)) {
620     LOGGER.warn("ID specifies no tool: " + tool);
621     button = null;
622     }
623     return (JToggleButton) button;
624     }
625    
626     /**
627     * Returns the button for a specific action.
628     *
629     * @param action
630     * the constant an action
631     */
632     public JButton getActionButton(int action) {
633     AbstractButton button = getButton(action);
634     if (button != null && !(button instanceof JButton)) {
635     LOGGER.warn("ID specifies no action: " + action);
636     button = null;
637     }
638     return (JButton) button;
639    
640     }
641    
642     /**
643     * Sets the selected tool.
644     *
645     * @param tool
646     * ID of the tool
647     */
648     public void setSelectedTool(Integer tool) {
649     if (tool == null)
650     toolButtonGroup.setUnselected();
651    
652     JToggleButton button = getToolButton(tool);
653     if (button == null)
654     return;
655     button.setSelected(true);
656     button.getAction().actionPerformed(null);
657    
658     selectedTool = tool;
659     }
660    
661     /**
662     * Returns the selected tool.
663     *
664     * @return -1 if no tool is active
665     */
666     public int getSelectedTool() {
667     if (toolButtonGroup.getSelectedButton() == null)
668     return -1;
669     return selectedTool;
670     }
671 alfonx 1220
672 alfonx 244 /**
673     * Sets whether a tool or action is activated or not. The visible property
674     * of the button is not affected.
675     *
676     * @param id
677     * tool or actionID
678     * @param enabled
679     * if {@code true} the tool becomes available
680     */
681     public void setButtonEnabled(int id, boolean enabled) {
682     AbstractButton button = getButton(id);
683     if (button == null)
684     return;
685     button.setEnabled(enabled);
686     }
687    
688     /**
689     * Sets whether a tool or action is activated or not.
690     *
691     * @param id
692     * tool or actionID
693     * @param enabled
694     * if {@code true} the tool becomes available
695     * @param hideOnDisable
696     * if {@code true} the button is also hidden if {@code enabled}
697     * is {@code false}
698     */
699     public void setButtonEnabled(int id, boolean enabled, boolean hideOnDisable) {
700     AbstractButton button = getButton(id);
701     if (button == null)
702     return;
703     button.setEnabled(enabled);
704     // if button is enabled, it becomes visible anyway
705     // if button is disabled and the "hide" option is set, it is also hidden
706     if (enabled)
707     button.setVisible(true);
708     else
709     button.setVisible(!hideOnDisable);
710     }
711    
712     /**
713     * Checks whether a ID is already used for a tool or action.
714     *
715     * @param tool
716     * tool ID
717     */
718     public boolean isButtonIDUsed(int id) {
719     return toolAndActionButtons.get(id) != null;
720     }
721    
722     /**
723     * Checks whether a tool is activated.
724     *
725     * @param tool
726     * tool ID
727     * @return {@code false} if an unknown ID is specified
728     */
729     public boolean isButtonEnabled(int id) {
730     AbstractButton button = getButton(id);
731     if (button != null)
732     return button.isEnabled();
733     return false;
734     }
735    
736     /**
737     * Sets the activation for all tools.
738     *
739     * @param enabled
740     * if {@code true} all tools becomes available
741     * @param hideOnDisable
742     * if {@code true} the buttons are also hidden if {@code enabled}
743     * is {@code false}
744     */
745     public void setAllToolsEnabled(boolean enabled, boolean hideOnDisable) {
746 alfonx 655 for (int id : toolAndActionButtons.keySet()) {
747     if (toolAndActionButtons.get(id) instanceof JToggleButton) {
748 alfonx 1220 setButtonEnabled(id, enabled, hideOnDisable);
749 alfonx 655 }
750     }
751 alfonx 244 }
752 alfonx 1220
753 alfonx 244 /**
754     * Sets the activation for all actions.
755     *
756     * @param enabled
757     * if {@code true} all actions becomes available
758     * @param hideOnDisable
759     * if {@code true} the buttons are also hidden if {@code enabled}
760     * is {@code false}
761     */
762     public void setAllActionsEnabled(boolean enabled, boolean hideOnDisable) {
763     for (int id : toolAndActionButtons.keySet()) {
764     if (toolAndActionButtons.get(id) instanceof JButton) {
765     setButtonEnabled(id, enabled, hideOnDisable);
766     }
767     }
768    
769     }
770    
771     /**
772     * Returns the maximum ID of tools.
773     */
774     public int getMaxToolID() {
775     return toolAndActionButtons.lastKey();
776     }
777    
778     /**
779     * Returns the minimum ID of tools.
780     */
781     public int getMinToolID() {
782     return toolAndActionButtons.firstKey();
783     }
784    
785     /**
786     * Extends the {@link AbstractAction} with maintaining an ID and the
787     * {@link MapPaneToolBar} the actions controls. Additionally this class
788     * automatically calls
789     * {@link MapPaneToolBar#performToolButton(int, ActionEvent)} or
790     * {@link MapPaneToolBar#performActionButton(int, ActionEvent)} depending on
791     * whether the action is added via
792     * {@link MapPaneToolBar#addTool(MapPaneToolBarAction)} or
793     * {@link MapPaneToolBar#addAction(MapPaneToolBarAction)}.
794     *
795     * @author <a href="mailto:[email protected]">Martin Schmitz</a>
796     * (University of Bonn/Germany)
797     */
798     public static class MapPaneToolBarAction extends AbstractAction {
799     /** The ID of the action */
800     protected int id = -1;
801     /** The tool bar, this action is made for. */
802     protected MapPaneToolBar toolBar = null;
803    
804     /**
805     * Creates a new action with a dummy description and no icon.
806     *
807     * @param id
808     * unique ID for the action
809     * @param toolBar
810     * toolbar this action is made for
811     */
812     public MapPaneToolBarAction(int id, MapPaneToolBar toolBar) {
813     this(id, toolBar, "" + id);
814     }
815    
816     /**
817     * Creates a new action without an icon.
818     *
819     * @param id
820     * unique ID for the action
821     * @param toolBar
822     * toolbar this action is made for
823     * @param name
824     * description used for buttons or menus
825     */
826     public MapPaneToolBarAction(int id, MapPaneToolBar toolBar, String name) {
827     this(id, toolBar, name, null);
828     }
829    
830     /**
831     * Creates a new action.
832     *
833     * @param id
834     * unique ID for the action
835     * @param toolBar
836     * toolbar this action is made for
837     * @param name
838     * description used for buttons or menus
839     * @param icon
840     * icon used for buttons or menus
841     */
842     public MapPaneToolBarAction(int id, MapPaneToolBar toolBar,
843     String name, Icon icon) {
844 alfonx 445 this(id, toolBar, name, icon, null);
845 alfonx 244 }
846 alfonx 445
847 alfonx 244 /**
848     * Creates a new action.
849     *
850     * @param id
851     * unique ID for the action
852     * @param toolBar
853     * The {@link MapPaneToolBar} this action is made for
854     * @param name
855     * description used for buttons or menus
856     * @param icon
857     * icon used for buttons or menus
858     * @param toolTip
859     * Tooltip to use for the button or menu
860     */
861     public MapPaneToolBarAction(int id, MapPaneToolBar toolBar,
862     String name, Icon icon, String toolTip) {
863     super(name, icon);
864 alfonx 445
865     if (toolTip != null && !toolTip.trim().isEmpty()) {
866 alfonx 244 putValue(Action.SHORT_DESCRIPTION, toolTip);
867     }
868 alfonx 445
869 alfonx 244 this.id = id;
870     this.toolBar = toolBar;
871     }
872    
873 alfonx 1220 public MapPaneToolBarAction(int id, MapPaneToolBar toolBar,
874     XMapPaneTool tool) {
875 alfonx 656 this(id, toolBar, "", tool.getIcon(), tool.getToolTip());
876 alfonx 653 }
877    
878 alfonx 244 /**
879     * Calls {@link MapPaneToolBar#performToolButton(int, ActionEvent)} or
880     * {@link MapPaneToolBar#performActionButton(int, ActionEvent)}
881     * depending on whether the action is added to the toolbar via
882     * {@link MapPaneToolBar#addTool(MapPaneToolBarAction)} or
883     * {@link MapPaneToolBar#addAction(MapPaneToolBarAction)}.
884     */
885     public void actionPerformed(ActionEvent e) {
886     if (toolBar.toolAndActionButtons.get(id) instanceof JToggleButton)
887     toolBar.performToolButton(id, e);
888     else if (toolBar.toolAndActionButtons.get(id) instanceof JButton)
889     toolBar.performActionButton(id, e);
890     }
891    
892     /**
893     * Returns the (unique) id of this action.
894     *
895     * @return
896     */
897     public int getID() {
898     return id;
899     }
900     }
901 alfonx 414
902 alfonx 445 /**
903     * Nuetzlich wenn die Componente gedruckt (z.B. wenn ein Screenshot gemacht
904     * wird) wird. Dann werden wird der Hintergrund auf WEISS gesetzt.
905     *
906 alfonx 1220 * @author <a href="mailto:[email protected]">Stefan Alfons Tzeggai</a>
907 alfonx 445 */
908     @Override
909     public void print(Graphics g) {
910     Color orig = getBackground();
911     setBackground(Color.WHITE);
912     // wrap in try/finally so that we always restore the state
913     try {
914     super.print(g);
915     } finally {
916     setBackground(orig);
917     }
918     }
919    
920 alfonx 1220 @Override
921     protected void finalize() throws Throwable {
922     super.finalize();
923    
924     if (mapPane != null && mapPaneListener != null)
925     mapPane.removeMapPaneListener(mapPaneListener);
926     }
927    
928 alfonx 244 }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26