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

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26