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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26