/[schmitzm]/branches/1.0-gt2-2.6/src/skrueger/geotools/MapPaneToolBar.java
ViewVC logotype

Annotation of /branches/1.0-gt2-2.6/src/skrueger/geotools/MapPaneToolBar.java

Parent Directory Parent Directory | Revision Log Revision Log


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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26