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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26