/[schmitzm]/branches/2.0-RC1/src/skrueger/geotools/MapPaneToolBar.java
ViewVC logotype

Diff of /branches/2.0-RC1/src/skrueger/geotools/MapPaneToolBar.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

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

Legend:
Removed from v.111  
changed lines
  Added in v.414

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26