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

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

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

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

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26