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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26