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

Legend:
Removed from v.2  
changed lines
  Added in v.680

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26