/[schmitzm]/branches/2.0-RC1/src/skrueger/geotools/MapPaneToolBar.java
ViewVC logotype

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

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26