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

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

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

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26