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

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

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

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

Legend:
Removed from v.152  
changed lines
  Added in v.888

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26