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

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

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

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

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26