/[schmitzm]/trunk/src/skrueger/geotools/MapPaneToolBar.java
ViewVC logotype

Diff of /trunk/src/skrueger/geotools/MapPaneToolBar.java

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

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

Legend:
Removed from v.127  
changed lines
  Added in v.680

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26