/[schmitzm]/branches/2.0-RC1/src/skrueger/geotools/MapPaneToolBar.java
ViewVC logotype

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

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26