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

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26