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

Diff of /branches/2.0-RC1/src/skrueger/geotools/MapPaneToolBar.java

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

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

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26