/[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 1354 - (hide annotations)
Sat Dec 18 21:58:10 2010 UTC (14 years, 2 months ago) by alfonx
File size: 26912 byte(s)
The XMapPaneListeners of XMapPane are now stored in a WeakHashSet! Every class registering a listener has to keep a reference to the listener, otherwise it will be removed directly! 
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     /**
269 alfonx 509 * Sets the {@link SelectableXMapPane} controlled by this tool bar.
270 alfonx 244 *
271     * @param mapPane
272 alfonx 1220 * {@link SelectableXMapPane} to control (if {@code null} this
273     * tool bar controls NOTHING!)
274 alfonx 244 */
275 alfonx 509 public void setMapPane(SelectableXMapPane mapPane) {
276 alfonx 244 // Remove listener from old MapPane
277     if (this.mapPane != null)
278     this.mapPane.removeMapPaneListener(mapPaneListener);
279     this.mapPane = mapPane;
280     if (this.mapPane != null && mapPaneListener != null)
281 alfonx 1354 this.mapPane.addMapPaneListenerNew(mapPaneListener);
282 alfonx 244 }
283    
284     /**
285     * Calls {@link #initToolsAndActions()} and {@link #initActions()} and then
286     * puts all tool buttons and all actions buttons to the tool bar.
287     */
288     protected void init() {
289 alfonx 445
290 alfonx 244 initToolsAndActions();
291    
292     addSeparator(SEPERATOR0, new JToolBar.Separator());
293     addSeparator(SEPERATOR1, new JToolBar.Separator());
294    
295     initToolBar();
296     }
297    
298     /**
299     * Creates the tool buttons and action buttons and seperators, adds them to
300     * {@link #toolAndActionButtons} and finally creates a button group for all
301     * tools. So sub-classes which override this method should FIRST add their
302 alfonx 1220 * new tool buttons to {@link #toolAndActionButtons} before calling
303     * {@code super.initTools()}.
304 alfonx 244 */
305     protected void initToolsAndActions() {
306 alfonx 891 // Pan
307 alfonx 1220 addTool(new MapPaneToolBarAction(TOOL_PAN, this, XMapPaneTool.PAN),
308     false);
309    
310 alfonx 244 // Info
311 alfonx 1220 addTool(new MapPaneToolBarAction(TOOL_INFO, this, XMapPaneTool.INFO),
312     false);
313 alfonx 680
314 alfonx 244 // Zoom in
315 alfonx 1220 addTool(new MapPaneToolBarAction(TOOL_ZOOMIN, this,
316     XMapPaneTool.ZOOM_IN), false);
317    
318 alfonx 244 // Zoom out
319 alfonx 1220 addTool(new MapPaneToolBarAction(TOOL_ZOOMOUT, this,
320     XMapPaneTool.ZOOM_OUT), false);
321    
322 alfonx 244 // Action button to revert the last zoom
323 alfonx 1220 addAction(
324     new MapPaneToolBarAction(ACTION_ZOOM_BACK, this, "",
325     new ImageIcon(MapView.class
326     .getResource("resource/icons/zoom_back.png")),
327     R("MapPaneButtons.LastZoom.TT")), false);
328 alfonx 244 setButtonEnabled(ACTION_ZOOM_BACK, false);
329    
330     // Action button to redo the last zoom
331 alfonx 1220 addAction(
332     new MapPaneToolBarAction(
333     ACTION_ZOOM_FORWARD,
334     this,
335     "",
336     new ImageIcon(MapView.class
337     .getResource("resource/icons/zoom_forward.png")),
338     R("MapPaneButtons.NextZoom.TT")), false);
339 alfonx 244 setButtonEnabled(ACTION_ZOOM_FORWARD, false);
340    
341     // set the selected tool enabled
342     setSelectedTool(selectedTool);
343    
344     }
345 alfonx 445
346 alfonx 417 @Override
347     public void paint(Graphics g) {
348     super.paint(g);
349     }
350 alfonx 244
351     /**
352     * Clears the GUI of all components and adds all tool and action buttons to
353     * the tool bar.
354     */
355     public void initToolBar() {
356     removeAll();
357 alfonx 445
358     // // Separator to the left of the tool actions to start
359     // // the tool buttons with the map (not with the coordinate grid)
360     // Dimension dimension = new Dimension(49, 10);
361     // addSeparator(dimension);
362    
363 alfonx 244 // Tool buttons
364 alfonx 414 for (Integer bKey : toolAndActionButtons.keySet()) {
365 alfonx 445
366 alfonx 414 JComponent b = toolAndActionButtons.get(bKey);
367 alfonx 445
368 alfonx 414 if (b instanceof JToggleButton) {
369     JToggleButton tb = (JToggleButton) b;
370     tb.setName(bKey.toString());
371 alfonx 445 tb.addActionListener(toolSelectedListener);
372 alfonx 414 }
373 alfonx 445
374 alfonx 244 add(b);
375 alfonx 414 }
376 alfonx 244
377     if (!toolAndActionButtons.containsKey(selectedTool)) {
378     /**
379     * This might be a bit specific, but IF selection buttons are
380     * available, select one of them.. if not, select the INFO tool.
381     */
382    
383     if (toolAndActionButtons.containsKey(TOOL_SELECTION_SET)) {
384     setSelectedTool(TOOL_SELECTION_SET);
385     } else if (toolAndActionButtons.containsKey(TOOL_INFO)) {
386     setSelectedTool(TOOL_INFO);
387     } else {
388     // TODO What to do now?!
389     setSelectedTool(null);
390     }
391    
392     }
393 alfonx 445
394 alfonx 244 revalidate();
395     repaint();
396     }
397    
398     /**
399     * Performs the activation of a tool.
400     *
401     * @param tool
402     * the tool to activate
403     * @param e
404     * the event of the button
405     */
406     public void performToolButton(int tool, ActionEvent e) {
407     if (mapPane == null)
408     return;
409    
410     selectedTool = tool;
411    
412     switch (tool) {
413     case TOOL_PAN:
414 alfonx 653 mapPane.setTool(XMapPaneTool.PAN);
415 alfonx 244 break;
416     case TOOL_INFO:
417 alfonx 653 mapPane.setTool(XMapPaneTool.INFO);
418 alfonx 244 break;
419     case TOOL_ZOOMIN:
420 alfonx 653 mapPane.setTool(XMapPaneTool.ZOOM_IN);
421 alfonx 244 break;
422     case TOOL_ZOOMOUT:
423 alfonx 653 mapPane.setTool(XMapPaneTool.ZOOM_OUT);
424 alfonx 244 break;
425     }
426     }
427    
428     /**
429     * @param id
430     * The ID of the Component to remove. The change will not be
431     * visible until {@link #initToolBar()} is called.
432     * @return <code>null</code> or the component that has been removed.
433     */
434     public JComponent removeId(int id) {
435     return toolAndActionButtons.remove(id);
436     }
437    
438     /**
439     * Performs the action of an action button.
440     *
441     * @param tool
442     * the action
443     * @param e
444     * the event of the button
445     */
446     protected void performActionButton(int action, ActionEvent e) {
447     if (mapPane == null)
448     return;
449 alfonx 1220
450 alfonx 244 // Perform the action "Zoom back": Revert the last zoom
451     if (action == ACTION_ZOOM_BACK) {
452     if (zoomBackIndex <= 1)
453     return;
454    
455     zoomBackForwardButtonInAction = true;
456     zoomBackIndex--;
457     getButton(ACTION_ZOOM_FORWARD).setEnabled(true);
458     getButton(ACTION_ZOOM_BACK).setEnabled(zoomBackIndex > 1);
459    
460     mapPane.setMapArea(lastZooms.get(zoomBackIndex - 1));
461     mapPane.refresh();
462     }
463    
464     // Perform the action "Zoom forward": Re-do the last zoom
465     if (action == ACTION_ZOOM_FORWARD) {
466     if (zoomBackIndex < lastZooms.size()) {
467     zoomBackForwardButtonInAction = true;
468     zoomBackIndex++;
469     getButton(ACTION_ZOOM_BACK).setEnabled(true);
470     getButton(ACTION_ZOOM_FORWARD).setEnabled(
471     zoomBackIndex < lastZooms.size());
472    
473     mapPane.setMapArea(lastZooms.get(zoomBackIndex - 1));
474     mapPane.refresh();
475     }
476     }
477     }
478    
479     /**
480     * Adds a tool to the tool bar. Does nothing if a tool or action with the
481     * specified ID already exists!
482     *
483     * @param buttonAction
484     * action for the toggle button
485     * @param resetToolBar
486     * indicates whether the toolbar GUI is reset after adding the
487     * button (if adding several actions it useful only to reset the
488     * GUI for the last added tool)
489     */
490     public void addTool(MapPaneToolBarAction buttonAction, boolean resetToolBar) {
491     if (isButtonIDUsed(buttonAction.getID())) {
492 alfonx 1220 LOGGER.warn("addTool(.) ignored because ID already used for tool or action: "
493     + buttonAction.getID());
494 alfonx 244 return;
495     }
496 alfonx 486 JToggleButton button = new SmallToggleButton(buttonAction);
497 alfonx 1220
498 alfonx 244 toolButtonGroup.add(button);
499     toolAndActionButtons.put(buttonAction.getID(), button);
500     if (resetToolBar)
501     initToolBar();
502     }
503    
504     /**
505     * Adds a tool to the tool bar and resets the toolbar GUI.
506     *
507     * @param buttonAction
508     * action for the toggle button
509     */
510     public void addTool(MapPaneToolBarAction buttonAction) {
511     addTool(buttonAction, true);
512     }
513    
514     /**
515     * Adds an action to the tool bar. Does nothing if a tool or action with the
516     * specified ID already exists!
517     *
518     * @param buttonAction
519     * action for the button
520     * @param resetToolBar
521     * indicates whether the toolbar GUI is reset after adding the
522     * button (if adding several actions it useful only to reset the
523     * GUI for the last added tool)
524     */
525     public void addAction(MapPaneToolBarAction buttonAction,
526     boolean resetToolBar) {
527     if (isButtonIDUsed(buttonAction.getID())) {
528 alfonx 1220 LOGGER.warn("addAction(.) ignored because ID already used for tool or action: "
529     + buttonAction.getID());
530 alfonx 244 return;
531     }
532 alfonx 486 JButton button = new SmallButton(buttonAction);
533 alfonx 244 toolAndActionButtons.put(buttonAction.getID(), button);
534     if (resetToolBar)
535     initToolBar();
536     }
537 alfonx 445
538 alfonx 297 /**
539 alfonx 445 * Adds any JComponent to the tool bar. Does nothing if a tool or action
540     * with the specified ID already exists!
541 alfonx 297 *
542 alfonx 445 * @param component
543     * A {@link JComponent} that shall be added
544     * @param id
545     * The ID associaded with the {@link JComponent}
546 alfonx 297 * @param resetToolBar
547     * indicates whether the toolbar GUI is reset after adding the
548     * button (if adding several actions it useful only to reset the
549     * GUI for the last added tool)
550     */
551 alfonx 445 public void addJComponent(JComponent component, int id, boolean resetToolBar) {
552 alfonx 244
553 alfonx 297 if (isButtonIDUsed(id)) {
554 alfonx 1220 LOGGER.warn("addAction(.) ignored because ID already used for tool or action: "
555     + id);
556 alfonx 297 return;
557     }
558 alfonx 445
559 alfonx 297 toolAndActionButtons.put(id, component);
560     if (resetToolBar)
561     initToolBar();
562     }
563    
564 alfonx 244 public void addSeparator(int id, Separator separator) {
565     if (isButtonIDUsed(id)) {
566 alfonx 1220 LOGGER.warn("addSeparator(.) ignored because ID already used for tool or action. ");
567 alfonx 244 return;
568     }
569     toolAndActionButtons.put(id, separator);
570     }
571    
572     /**
573     * Adds an action to the tool bar and resets the toolbar GUI.
574     *
575     * @param buttonAction
576     * action for the toggle button
577     */
578     public void addAction(MapPaneToolBarAction buttonAction) {
579     addAction(buttonAction, true);
580     }
581    
582     /**
583     * Returns the button for a specific tool or action.
584     *
585     * @param id
586     * the constant for any button in the {@link MapPaneToolBar}
587     * @return a {@link JButton} if {@code id} specifies an
588     * {@linkplain #getActionButton(int) action button} or
589     * {@link JToogleButton} if {@code id} specifies a
590     * {@linkplain #getToolButton(int) tool button}
591     */
592     public AbstractButton getButton(int id) {
593 alfonx 1220
594     // ACHUTNG: Das ist ein SK QUICK FIX! TODO
595     if (!(toolAndActionButtons.get(id) instanceof AbstractButton))
596     return null;
597    
598 alfonx 244 AbstractButton button = (AbstractButton) toolAndActionButtons.get(id);
599     if (button == null)
600     LOGGER.warn("Unknown tool or action ID: " + id);
601     return button;
602     }
603    
604     /**
605     * Returns the button for a specific tool.
606     *
607     * @param tool
608     * the constant for a tool
609     */
610     public JToggleButton getToolButton(int tool) {
611     AbstractButton button = getButton(tool);
612     if (button != null && !(button instanceof JToggleButton)) {
613     LOGGER.warn("ID specifies no tool: " + tool);
614     button = null;
615     }
616     return (JToggleButton) button;
617     }
618    
619     /**
620     * Returns the button for a specific action.
621     *
622     * @param action
623     * the constant an action
624     */
625     public JButton getActionButton(int action) {
626     AbstractButton button = getButton(action);
627     if (button != null && !(button instanceof JButton)) {
628     LOGGER.warn("ID specifies no action: " + action);
629     button = null;
630     }
631     return (JButton) button;
632    
633     }
634    
635     /**
636     * Sets the selected tool.
637     *
638     * @param tool
639     * ID of the tool
640     */
641     public void setSelectedTool(Integer tool) {
642     if (tool == null)
643     toolButtonGroup.setUnselected();
644    
645     JToggleButton button = getToolButton(tool);
646     if (button == null)
647     return;
648     button.setSelected(true);
649     button.getAction().actionPerformed(null);
650    
651     selectedTool = tool;
652     }
653    
654     /**
655     * Returns the selected tool.
656     *
657     * @return -1 if no tool is active
658     */
659     public int getSelectedTool() {
660     if (toolButtonGroup.getSelectedButton() == null)
661     return -1;
662     return selectedTool;
663     }
664 alfonx 1220
665 alfonx 244 /**
666     * Sets whether a tool or action is activated or not. The visible property
667     * of the button is not affected.
668     *
669     * @param id
670     * tool or actionID
671     * @param enabled
672     * if {@code true} the tool becomes available
673     */
674     public void setButtonEnabled(int id, boolean enabled) {
675     AbstractButton button = getButton(id);
676     if (button == null)
677     return;
678     button.setEnabled(enabled);
679     }
680    
681     /**
682     * Sets whether a tool or action is activated or not.
683     *
684     * @param id
685     * tool or actionID
686     * @param enabled
687     * if {@code true} the tool becomes available
688     * @param hideOnDisable
689     * if {@code true} the button is also hidden if {@code enabled}
690     * is {@code false}
691     */
692     public void setButtonEnabled(int id, boolean enabled, boolean hideOnDisable) {
693     AbstractButton button = getButton(id);
694     if (button == null)
695     return;
696     button.setEnabled(enabled);
697     // if button is enabled, it becomes visible anyway
698     // if button is disabled and the "hide" option is set, it is also hidden
699     if (enabled)
700     button.setVisible(true);
701     else
702     button.setVisible(!hideOnDisable);
703     }
704    
705     /**
706     * Checks whether a ID is already used for a tool or action.
707     *
708     * @param tool
709     * tool ID
710     */
711     public boolean isButtonIDUsed(int id) {
712     return toolAndActionButtons.get(id) != null;
713     }
714    
715     /**
716     * Checks whether a tool is activated.
717     *
718     * @param tool
719     * tool ID
720     * @return {@code false} if an unknown ID is specified
721     */
722     public boolean isButtonEnabled(int id) {
723     AbstractButton button = getButton(id);
724     if (button != null)
725     return button.isEnabled();
726     return false;
727     }
728    
729     /**
730     * Sets the activation for all tools.
731     *
732     * @param enabled
733     * if {@code true} all tools becomes available
734     * @param hideOnDisable
735     * if {@code true} the buttons are also hidden if {@code enabled}
736     * is {@code false}
737     */
738     public void setAllToolsEnabled(boolean enabled, boolean hideOnDisable) {
739 alfonx 655 for (int id : toolAndActionButtons.keySet()) {
740     if (toolAndActionButtons.get(id) instanceof JToggleButton) {
741 alfonx 1220 setButtonEnabled(id, enabled, hideOnDisable);
742 alfonx 655 }
743     }
744 alfonx 244 }
745 alfonx 1220
746 alfonx 244 /**
747     * Sets the activation for all actions.
748     *
749     * @param enabled
750     * if {@code true} all actions becomes available
751     * @param hideOnDisable
752     * if {@code true} the buttons are also hidden if {@code enabled}
753     * is {@code false}
754     */
755     public void setAllActionsEnabled(boolean enabled, boolean hideOnDisable) {
756     for (int id : toolAndActionButtons.keySet()) {
757     if (toolAndActionButtons.get(id) instanceof JButton) {
758     setButtonEnabled(id, enabled, hideOnDisable);
759     }
760     }
761    
762     }
763    
764     /**
765     * Returns the maximum ID of tools.
766     */
767     public int getMaxToolID() {
768     return toolAndActionButtons.lastKey();
769     }
770    
771     /**
772     * Returns the minimum ID of tools.
773     */
774     public int getMinToolID() {
775     return toolAndActionButtons.firstKey();
776     }
777    
778     /**
779     * Extends the {@link AbstractAction} with maintaining an ID and the
780     * {@link MapPaneToolBar} the actions controls. Additionally this class
781     * automatically calls
782     * {@link MapPaneToolBar#performToolButton(int, ActionEvent)} or
783     * {@link MapPaneToolBar#performActionButton(int, ActionEvent)} depending on
784     * whether the action is added via
785     * {@link MapPaneToolBar#addTool(MapPaneToolBarAction)} or
786     * {@link MapPaneToolBar#addAction(MapPaneToolBarAction)}.
787     *
788     * @author <a href="mailto:[email protected]">Martin Schmitz</a>
789     * (University of Bonn/Germany)
790     */
791     public static class MapPaneToolBarAction extends AbstractAction {
792     /** The ID of the action */
793     protected int id = -1;
794     /** The tool bar, this action is made for. */
795     protected MapPaneToolBar toolBar = null;
796    
797     /**
798     * Creates a new action with a dummy description and no icon.
799     *
800     * @param id
801     * unique ID for the action
802     * @param toolBar
803     * toolbar this action is made for
804     */
805     public MapPaneToolBarAction(int id, MapPaneToolBar toolBar) {
806     this(id, toolBar, "" + id);
807     }
808    
809     /**
810     * Creates a new action without an icon.
811     *
812     * @param id
813     * unique ID for the action
814     * @param toolBar
815     * toolbar this action is made for
816     * @param name
817     * description used for buttons or menus
818     */
819     public MapPaneToolBarAction(int id, MapPaneToolBar toolBar, String name) {
820     this(id, toolBar, name, null);
821     }
822    
823     /**
824     * Creates a new action.
825     *
826     * @param id
827     * unique ID for the action
828     * @param toolBar
829     * toolbar this action is made for
830     * @param name
831     * description used for buttons or menus
832     * @param icon
833     * icon used for buttons or menus
834     */
835     public MapPaneToolBarAction(int id, MapPaneToolBar toolBar,
836     String name, Icon icon) {
837 alfonx 445 this(id, toolBar, name, icon, null);
838 alfonx 244 }
839 alfonx 445
840 alfonx 244 /**
841     * Creates a new action.
842     *
843     * @param id
844     * unique ID for the action
845     * @param toolBar
846     * The {@link MapPaneToolBar} this action is made for
847     * @param name
848     * description used for buttons or menus
849     * @param icon
850     * icon used for buttons or menus
851     * @param toolTip
852     * Tooltip to use for the button or menu
853     */
854     public MapPaneToolBarAction(int id, MapPaneToolBar toolBar,
855     String name, Icon icon, String toolTip) {
856     super(name, icon);
857 alfonx 445
858     if (toolTip != null && !toolTip.trim().isEmpty()) {
859 alfonx 244 putValue(Action.SHORT_DESCRIPTION, toolTip);
860     }
861 alfonx 445
862 alfonx 244 this.id = id;
863     this.toolBar = toolBar;
864     }
865    
866 alfonx 1220 public MapPaneToolBarAction(int id, MapPaneToolBar toolBar,
867     XMapPaneTool tool) {
868 alfonx 656 this(id, toolBar, "", tool.getIcon(), tool.getToolTip());
869 alfonx 653 }
870    
871 alfonx 244 /**
872     * Calls {@link MapPaneToolBar#performToolButton(int, ActionEvent)} or
873     * {@link MapPaneToolBar#performActionButton(int, ActionEvent)}
874     * depending on whether the action is added to the toolbar via
875     * {@link MapPaneToolBar#addTool(MapPaneToolBarAction)} or
876     * {@link MapPaneToolBar#addAction(MapPaneToolBarAction)}.
877     */
878     public void actionPerformed(ActionEvent e) {
879     if (toolBar.toolAndActionButtons.get(id) instanceof JToggleButton)
880     toolBar.performToolButton(id, e);
881     else if (toolBar.toolAndActionButtons.get(id) instanceof JButton)
882     toolBar.performActionButton(id, e);
883     }
884    
885     /**
886     * Returns the (unique) id of this action.
887     *
888     * @return
889     */
890     public int getID() {
891     return id;
892     }
893     }
894 alfonx 414
895 alfonx 445 /**
896     * Nuetzlich wenn die Componente gedruckt (z.B. wenn ein Screenshot gemacht
897     * wird) wird. Dann werden wird der Hintergrund auf WEISS gesetzt.
898     *
899 alfonx 1220 * @author <a href="mailto:[email protected]">Stefan Alfons Tzeggai</a>
900 alfonx 445 */
901     @Override
902     public void print(Graphics g) {
903     Color orig = getBackground();
904     setBackground(Color.WHITE);
905     // wrap in try/finally so that we always restore the state
906     try {
907     super.print(g);
908     } finally {
909     setBackground(orig);
910     }
911     }
912    
913 alfonx 1220 @Override
914     protected void finalize() throws Throwable {
915     super.finalize();
916    
917     if (mapPane != null && mapPaneListener != null)
918     mapPane.removeMapPaneListener(mapPaneListener);
919     }
920    
921 alfonx 244 }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26