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

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

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

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

Legend:
Removed from v.48  
changed lines
  Added in v.160

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26