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

Legend:
Removed from v.160  
changed lines
  Added in v.680

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26