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

Legend:
Removed from v.151  
changed lines
  Added in v.417

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26