/[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 1220 - (hide annotations)
Wed Nov 3 13:09:32 2010 UTC (14 years, 4 months ago) by alfonx
Original Path: trunk/src/skrueger/geotools/MapPaneToolBar.java
File size: 27101 byte(s)


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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26