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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26