/[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

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

Legend:
Removed from v.140  
changed lines
  Added in v.1220

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26