/[schmitzm]/trunk/src/skrueger/geotools/MapPaneToolBar.java
ViewVC logotype

Diff of /trunk/src/skrueger/geotools/MapPaneToolBar.java

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

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

Legend:
Removed from v.177  
changed lines
  Added in v.445

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26