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

Legend:
Removed from v.128  
changed lines
  Added in v.417

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26