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

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

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

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

Legend:
Removed from v.180  
changed lines
  Added in v.685

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26