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

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

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

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26