/[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 127 by mojays, Sat May 30 14:29:58 2009 UTC revision 141 by alfonx, Sun Jun 14 18:23:36 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  //                public 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,                  repaint();
261            this,  
262            "",                  if (!toolAndActionButtons.containsKey(selectedTool)) {
263            new ImageIcon(MapView.class.getResource("resource/icons/zoom_in.png"))                          /**
264        ), false );                           * This might be a bit specific, but IF selection buttons are
265        // Zoom out                           * available, select one of them.. if not, select the INFO tool.
266        addTool( new MapPaneToolBarAction(                           */
267            TOOL_ZOOMOUT,  
268            this,                          if (toolAndActionButtons.containsKey(TOOL_SELECTION_SET)) {
269            "",                                  setSelectedTool(TOOL_SELECTION_SET);
270            new ImageIcon(MapView.class.getResource("resource/icons/zoom_out.png"))                          } else if (toolAndActionButtons.containsKey(TOOL_INFO)) {
271        ), false );                                  setSelectedTool(TOOL_INFO);
272                                  } else {
273        // Action button to revert the last zoom                                  // TODO What to do now?!
274        addAction( new MapPaneToolBarAction(                                  setSelectedTool(null);
275            ACTION_ZOOM_BACK,                          }
276            this,  
277            "",                  }
278            new ImageIcon(MapView.class.getResource("resource/icons/zoom_back.png"))          }
279        ), false);  
280        setButtonEnabled( ACTION_ZOOM_BACK, false );          // Space between tool buttons and action buttons
281            // SK: Seperators are now als manages like actions and tools
282        // Action button to redo the last zoom          // Dimension dimension2 = new Dimension( 10,10);
283        addAction( new MapPaneToolBarAction(          // this.addSeparator(dimension2);
284            ACTION_ZOOM_FORWARD,  
285            this,          // // Action buttons
286            "",          // for (JButton b : actionButtons.values())
287            new ImageIcon(MapView.class.getResource("resource/icons/zoom_forward.png"))          // add(b);
288        ), false);          // }
289        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);  
 //    }  
       
290          /**          /**
291           * Performs the activation of a tool.           * Performs the activation of a tool.
292           * @param tool the tool to activate           *
293           * @param e    the event of the button           * @param tool
294             *            the tool to activate
295             * @param e
296             *            the event of the button
297           */           */
298          public void performToolButton(int tool, ActionEvent e) {          public void performToolButton(int tool, ActionEvent e) {
299            if ( mapPane == null )                  if (mapPane == null)
300              return;                          return;
301              
302            selectedTool = tool;                  selectedTool = tool;
303              
304        switch( tool ) {                  switch (tool) {
305          case TOOL_PAN:                  case TOOL_PAN:
306            // Set the mouse tool to "Panning"                          // Set the mouse tool to "Panning"
307            mapPane.setWindowSelectionState(JMapPane.NONE);                          mapPane.setWindowSelectionState(JMapPane.NONE);
308            mapPane.setState(JMapPane.PAN);                          mapPane.setState(JMapPane.PAN);
309            mapPane.setHighlight(false);                          mapPane.setHighlight(false);
310            mapPane.setNormalCursor(SwingUtil.PAN_CURSOR);                          mapPane.setNormalCursor(SwingUtil.PAN_CURSOR);
311            break;                          break;
312          case TOOL_INFO:                  case TOOL_INFO:
313            // Set the mouse tool to "Info"                          // Set the mouse tool to "Info"
314            mapPane.setWindowSelectionState(JMapPane.NONE);                          mapPane.setWindowSelectionState(JMapPane.NONE);
315            mapPane.setState(JMapPane.SELECT_TOP); // Why not: JMapPane.SELECT_TOP_ONEONLY                          mapPane.setState(JMapPane.SELECT_TOP); // Why not:
316            mapPane.setHighlight(false);// SK: Was true, but since it not works properly removed it to save performance                          // JMapPane.SELECT_TOP_ONEONLY
317            mapPane.setNormalCursor(SwingUtil.CROSSHAIR_CURSOR);                          mapPane.setHighlight(false);// SK: Was true, but since it not works
318            break;                          // properly removed it to save
319          case TOOL_SELECTION_SET:                          // performance
320          case TOOL_SELECTION_ADD:                          mapPane.setNormalCursor(SwingUtil.CROSSHAIR_CURSOR);
321          case TOOL_SELECTION_REMOVE:                          break;
322                  // Set the mouse tool to "Select"                  case TOOL_ZOOMIN:
323                  mapPane.setWindowSelectionState(JMapPane.SELECT_TOP);                          // Set the mouse tool to "Zoom in"
324                  mapPane.setState(JMapPane.SELECT_TOP);                          mapPane.setWindowSelectionState(JMapPane.ZOOM_IN);
325                  mapPane.setHighlight(false);                          mapPane.setState(JMapPane.ZOOM_IN);
326                  mapPane.setNormalCursor(SwingUtil.CROSSHAIR_CURSOR); // TODO Select Cursor                          mapPane.setHighlight(false);
327                  break;                          mapPane.setNormalCursor(SwingUtil.ZOOMIN_CURSOR);
328          case TOOL_ZOOMIN:                          break;
329            // Set the mouse tool to "Zoom in"                  case TOOL_ZOOMOUT:
330            mapPane.setWindowSelectionState(JMapPane.ZOOM_IN);                          // Set the mouse tool to "Zoom out"
331            mapPane.setState(JMapPane.ZOOM_IN);                          mapPane.setWindowSelectionState(JMapPane.NONE);
332            mapPane.setHighlight(false);                          mapPane.setState(JMapPane.ZOOM_OUT);
333            mapPane.setNormalCursor(SwingUtil.ZOOMIN_CURSOR);                          mapPane.setHighlight(false);
334            break;                          mapPane.setNormalCursor(SwingUtil.ZOOMOUT_CURSOR);
335          case TOOL_ZOOMOUT:                          break;
336            // Set the mouse tool to "Zoom out"                  default:
337            mapPane.setWindowSelectionState(JMapPane.NONE);                          // Set map actions to default
338            mapPane.setState(JMapPane.ZOOM_OUT);                          mapPane.setWindowSelectionState(JMapPane.NONE);
339            mapPane.setHighlight(false);                          mapPane.setState(JMapPane.NONE);
340            mapPane.setNormalCursor(SwingUtil.ZOOMOUT_CURSOR);                          mapPane.setHighlight(false);
341            break;                          mapPane.setNormalCursor(null);
342          default:                          break;
343            // Set map actions to default                  }
344            mapPane.setWindowSelectionState(JMapPane.NONE);                  mapPane.updateCursor();
345            mapPane.setState(JMapPane.NONE);          }
346            mapPane.setHighlight(false);  
347            mapPane.setNormalCursor(null);          /**
348            break;           * @param id
349        }           *            The ID of the Component to remove. The change will not be
350        mapPane.updateCursor();           *            visible until {@link #initToolBar()} is called.
351          }           * @return <code>null</code> or the component that has been removed.
352                     */
353      /**          public JComponent removeId(int id) {
354       * Performs the action of an action button.                  return toolAndActionButtons.remove(id);
355       * @param tool the action          }
356       * @param e    the event of the button  
357       */          /**
358             * Performs the action of an action button.
359             *
360             * @param tool
361             *            the action
362             * @param e
363             *            the event of the button
364             */
365          protected void performActionButton(int action, ActionEvent e) {          protected void performActionButton(int action, ActionEvent e) {
366        if ( mapPane == null )                  if (mapPane == null)
367          return;                          return;
368    
369                    // Perform the action "Zoom back": Revert the last zoom
370                    if (action == ACTION_ZOOM_BACK) {
371                            if (zoomBackIndex <= 1)
372                                    return;
373    
374                            zoomBackForwardButtonInAction = true;
375                            zoomBackIndex--;
376                            getButton(ACTION_ZOOM_FORWARD).setEnabled(true);
377                            getButton(ACTION_ZOOM_BACK).setEnabled(zoomBackIndex > 1);
378    
379                            mapPane.setMapArea(lastZooms.get(zoomBackIndex - 1));
380                            mapPane.refresh();
381                    }
382    
383                    // Perform the action "Zoom forward": Re-do the last zoom
384                    if (action == ACTION_ZOOM_FORWARD) {
385                            if (zoomBackIndex < lastZooms.size()) {
386                                    zoomBackForwardButtonInAction = true;
387                                    zoomBackIndex++;
388                                    getButton(ACTION_ZOOM_BACK).setEnabled(true);
389                                    getButton(ACTION_ZOOM_FORWARD).setEnabled(
390                                                    zoomBackIndex < lastZooms.size());
391    
392                                    mapPane.setMapArea(lastZooms.get(zoomBackIndex - 1));
393                                    mapPane.refresh();
394                            }
395                    }
396    
       // 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  
       }  
397          }          }
398            
           
399          /**          /**
400           * 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
401           * specified ID already exists!           * specified ID already exists!
402           * @param buttonAction action for the toggle button           *
403           * @param resetToolBar indicates whether the toolbar GUI is reset after adding           * @param buttonAction
404           *                     the button (if adding several actions it useful only to           *            action for the toggle button
405           *                     reset the GUI for the last added tool)           * @param resetToolBar
406             *            indicates whether the toolbar GUI is reset after adding the
407             *            button (if adding several actions it useful only to reset the
408             *            GUI for the last added tool)
409           */           */
410          public void addTool(MapPaneToolBarAction buttonAction, boolean resetToolBar) {          public void addTool(MapPaneToolBarAction buttonAction, boolean resetToolBar) {
411            if ( isButtonIDUsed(buttonAction.getID()) ) {                  if (isButtonIDUsed(buttonAction.getID())) {
412              LOGGER.warn("addTool(.) ignored because ID already used for tool or action: "+buttonAction.getID());                          LOGGER
413              return;                                          .warn("addTool(.) ignored because ID already used for tool or action: "
414            }                                                          + buttonAction.getID());
415            JToggleButton button = new JToggleButton(buttonAction);                          return;
416            button.setBorder( BorderFactory.createRaisedBevelBorder() );                  }
417            toolButtonGroup.add(button);                  JToggleButton button = new JToggleButton(buttonAction);
418            toolAndActionButtons.put(buttonAction.getID(), button);                  button.setBorder(BorderFactory.createRaisedBevelBorder());
419            if ( resetToolBar )                  toolButtonGroup.add(button);
420              initToolBar();                  toolAndActionButtons.put(buttonAction.getID(), button);
421          }                  if (resetToolBar)
422                            initToolBar();
423      /**          }
424       * Adds a tool to the tool bar and resets the toolbar GUI.  
425       * @param buttonAction action for the toggle button          /**
426       */           * Adds a tool to the tool bar and resets the toolbar GUI.
427      public void addTool(MapPaneToolBarAction buttonAction) {           *
428        addTool(buttonAction, true);           * @param buttonAction
429      }           *            action for the toggle button
430             */
431      /**          public void addTool(MapPaneToolBarAction buttonAction) {
432       * Adds an action to the tool bar. Does nothing if a tool or action with the                  addTool(buttonAction, true);
433       * specified ID already exists!          }
434       * @param buttonAction action for the button  
435       * @param resetToolBar indicates whether the toolbar GUI is reset after adding          /**
436       *                     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
437       *                     reset the GUI for the last added tool)           * specified ID already exists!
438       */           *
439      public void addAction(MapPaneToolBarAction buttonAction, boolean resetToolBar) {           * @param buttonAction
440        if ( isButtonIDUsed(buttonAction.getID()) ) {           *            action for the button
441          LOGGER.warn("addAction(.) ignored because ID already used for tool or action: "+buttonAction.getID());           * @param resetToolBar
442          return;           *            indicates whether the toolbar GUI is reset after adding the
443        }           *            button (if adding several actions it useful only to reset the
444        JButton button = new JButton(buttonAction);           *            GUI for the last added tool)
445        button.setBorder( BorderFactory.createRaisedBevelBorder() );           */
446        toolAndActionButtons.put( buttonAction.getID(), button );          public void addAction(MapPaneToolBarAction buttonAction,
447        if ( resetToolBar )                          boolean resetToolBar) {
448          initToolBar();                  if (isButtonIDUsed(buttonAction.getID())) {
449      }                          LOGGER
450                                                .warn("addAction(.) ignored because ID already used for tool or action: "
451                                                            + buttonAction.getID());
452      private void addSeparator(int id, Separator separator) {                          return;
453          if ( isButtonIDUsed(id) ) {                  }
454              LOGGER.warn("addSeparator(.) ignored because ID already used for tool or action. ");                  JButton button = new JButton(buttonAction);
455              return;                  button.setBorder(BorderFactory.createRaisedBevelBorder());
456            }                  toolAndActionButtons.put(buttonAction.getID(), button);
457            toolAndActionButtons.put( id, separator);                  if (resetToolBar)
458          }                          initToolBar();
459            }
460      /**  
461       * Adds an action to the tool bar and resets the toolbar GUI.          public void addSeparator(int id, Separator separator) {
462       * @param buttonAction action for the toggle button                  if (isButtonIDUsed(id)) {
463       */                          LOGGER
464      public void addAction(MapPaneToolBarAction buttonAction) {                                          .warn("addSeparator(.) ignored because ID already used for tool or action. ");
465        addAction(buttonAction, true);                          return;
466      }                  }
467                        toolAndActionButtons.put(id, separator);
468      /**          }
469       * Returns the button for a specific tool or action.  
470       * @param id the constant for any button in the {@link MapPaneToolBar}          /**
471       * @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.
472       *         or {@link JToogleButton} if {@code id} specifies a {@linkplain #getToolButton(int) tool button}           *
473       */           * @param buttonAction
474      public AbstractButton getButton(int id) {           *            action for the toggle button
475        AbstractButton button = (AbstractButton)toolAndActionButtons.get(id);           */
476        if ( button == null )          public void addAction(MapPaneToolBarAction buttonAction) {
477          LOGGER.warn("Unknown tool or action ID: "+id);                  addAction(buttonAction, true);
478        return button;          }
479      }  
480            /**
481      /**           * Returns the button for a specific tool or action.
482       * Returns the button for a specific tool.           *
483       * @param tool the constant for a tool           * @param id
484       */           *            the constant for any button in the {@link MapPaneToolBar}
485             * @return a {@link JButton} if {@code id} specifies an
486             *         {@linkplain #getActionButton(int) action button} or
487             *         {@link JToogleButton} if {@code id} specifies a
488             *         {@linkplain #getToolButton(int) tool button}
489             */
490            public AbstractButton getButton(int id) {
491                    AbstractButton button = (AbstractButton) toolAndActionButtons.get(id);
492                    if (button == null)
493                            LOGGER.warn("Unknown tool or action ID: " + id);
494                    return button;
495            }
496    
497            /**
498             * Returns the button for a specific tool.
499             *
500             * @param tool
501             *            the constant for a tool
502             */
503          public JToggleButton getToolButton(int tool) {          public JToggleButton getToolButton(int tool) {
504        AbstractButton button = getButton(tool);                  AbstractButton button = getButton(tool);
505        if ( button != null && !(button instanceof JToggleButton) ) {                  if (button != null && !(button instanceof JToggleButton)) {
506          LOGGER.warn("ID specifies no tool: "+tool);                          LOGGER.warn("ID specifies no tool: " + tool);
507          button = null;                          button = null;
508        }                  }
509        return (JToggleButton)button;                  return (JToggleButton) button;
510      }          }
511    
512      /**          /**
513       * Returns the button for a specific action.           * Returns the button for a specific action.
514       * @param action the constant an action           *
515       */           * @param action
516      public JButton getActionButton(int action) {           *            the constant an action
517        AbstractButton button = getButton(action);           */
518        if ( button != null && !(button instanceof JButton) ) {          public JButton getActionButton(int action) {
519          LOGGER.warn("ID specifies no action: "+action);                  AbstractButton button = getButton(action);
520          button = null;                  if (button != null && !(button instanceof JButton)) {
521        }                          LOGGER.warn("ID specifies no action: " + action);
522        return (JButton)button;                          button = null;
523                    }
524                    return (JButton) button;
525    
526      }          }
527    
528          /**          /**
529           * Sets the selected tool.           * Sets the selected tool.
530           * @param tool ID of the tool           *
531             * @param tool
532             *            ID of the tool
533           */           */
534          public void setSelectedTool(Integer tool) {          public void setSelectedTool(Integer tool) {
535            if ( tool == null )                  if (tool == null)
536              toolButtonGroup.setUnselected();                          toolButtonGroup.setUnselected();
537              
538            JToggleButton button = getToolButton(tool);                  JToggleButton button = getToolButton(tool);
539            if ( button == null )                  if (button == null)
540              return;                          return;
541            button.setSelected( true );                  button.setSelected(true);
542            button.getAction().actionPerformed(null);                  button.getAction().actionPerformed(null);
543    
544                    selectedTool = tool;
545          }          }
546            
547          /**          /**
548           * Returns the selected tool.           * Returns the selected tool.
549             *
550           * @return -1 if no tool is active           * @return -1 if no tool is active
551           */           */
552          public int getSelectedTool() {          public int getSelectedTool() {
553            if ( toolButtonGroup.getSelectedButton() == null )                  if (toolButtonGroup.getSelectedButton() == null)
554              return -1;                          return -1;
555            return selectedTool;                  return selectedTool;
556          }          }
557            
558      /**          /**
559       * 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
560       * of the button is not affected.           * of the button is not affected.
561       * @param id tool or actionID           *
562       * @param enabled if {@code true} the tool becomes available           * @param id
563       */           *            tool or actionID
564      public void setButtonEnabled(int id, boolean enabled) {           * @param enabled
565        AbstractButton button = getButton(id);           *            if {@code true} the tool becomes available
566        if ( button == null )           */
567          return;          public void setButtonEnabled(int id, boolean enabled) {
568        button.setEnabled( enabled );                  AbstractButton button = getButton(id);
569      }                  if (button == null)
570                            return;
571      /**                  button.setEnabled(enabled);
572       * Sets whether a tool or action is activated or not.          }
573       * @param id tool or actionID  
574       * @param enabled if {@code true} the tool becomes available          /**
575       * @param hideOnDisable if {@code true} the button is also hidden if           * Sets whether a tool or action is activated or not.
576       *                      {@code enabled} is {@code false}           *
577       */           * @param id
578             *            tool or actionID
579             * @param enabled
580             *            if {@code true} the tool becomes available
581             * @param hideOnDisable
582             *            if {@code true} the button is also hidden if {@code enabled}
583             *            is {@code false}
584             */
585          public void setButtonEnabled(int id, boolean enabled, boolean hideOnDisable) {          public void setButtonEnabled(int id, boolean enabled, boolean hideOnDisable) {
586            AbstractButton button = getButton(id);                  AbstractButton button = getButton(id);
587            if ( button == null )                  if (button == null)
588              return;                          return;
589            button.setEnabled( enabled );                  button.setEnabled(enabled);
590            // if button is enabled, it becomes visible anyway                  // if button is enabled, it becomes visible anyway
591            // 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
592            if ( enabled )                  if (enabled)
593              button.setVisible( true );                          button.setVisible(true);
594            else                  else
595              button.setVisible( !hideOnDisable );                          button.setVisible(!hideOnDisable);
596          }          }
597    
598      /**          /**
599       * Checks whether a ID is already used for a tool or action.           * Checks whether a ID is already used for a tool or action.
600       * @param tool tool ID           *
601       */           * @param tool
602      public boolean isButtonIDUsed(int id) {           *            tool ID
603        return toolAndActionButtons.get(id) != null;           */
604      }          public boolean isButtonIDUsed(int id) {
605                    return toolAndActionButtons.get(id) != null;
606      /**          }
607       * Checks whether a tool is activated.  
608       * @param tool tool ID          /**
609       * @return {@code false} if an unknown ID is specified           * Checks whether a tool is activated.
610       */           *
611      public boolean isButtonEnabled(int id) {           * @param tool
612        AbstractButton button = getButton(id);           *            tool ID
613        if ( button != null )           * @return {@code false} if an unknown ID is specified
614          return button.isEnabled();           */
615        return false;          public boolean isButtonEnabled(int id) {
616      }                  AbstractButton button = getButton(id);
617                    if (button != null)
618      /**                          return button.isEnabled();
619       * Sets the activation for all tools.                  return false;
620       * @param enabled if {@code true} all tools becomes available          }
621       * @param hideOnDisable if {@code true} the buttons are also hidden if  
622       *                      {@code enabled} is {@code false}          /**
623       */           * Sets the activation for all tools.
624      public void setAllToolsEnabled(boolean enabled, boolean hideOnDisable) {           *
625        for (int tool : toolAndActionButtons.keySet())           * @param enabled
626          setButtonEnabled(tool,enabled,hideOnDisable);           *            if {@code true} all tools becomes available
627      }             * @param hideOnDisable
628             *            if {@code true} the buttons are also hidden if {@code enabled}
629      /**           *            is {@code false}
630       * Sets the activation for all actions.           */
631       * @param enabled if {@code true} all actions becomes available          public void setAllToolsEnabled(boolean enabled, boolean hideOnDisable) {
632       * @param hideOnDisable if {@code true} the buttons are also hidden if                  for (int tool : toolAndActionButtons.keySet())
633       *                      {@code enabled} is {@code false}                          setButtonEnabled(tool, enabled, hideOnDisable);
634       */          }
635      public void setAllActionsEnabled(boolean enabled, boolean hideOnDisable) {  
636        for (int id : toolAndActionButtons.keySet()){          /**
637            if (toolAndActionButtons.get(id) instanceof JButton){           * Sets the activation for all actions.
638                    setButtonEnabled(id,enabled,hideOnDisable);           *
639            }           * @param enabled
640        }           *            if {@code true} all actions becomes available
641                     * @param hideOnDisable
642      }             *            if {@code true} the buttons are also hidden if {@code enabled}
643                 *            is {@code false}
644      /**           */
645       * Returns the maximum ID of tools.          public void setAllActionsEnabled(boolean enabled, boolean hideOnDisable) {
646       */                  for (int id : toolAndActionButtons.keySet()) {
647      public int getMaxToolID() {                          if (toolAndActionButtons.get(id) instanceof JButton) {
648        return toolAndActionButtons.lastKey();                                  setButtonEnabled(id, enabled, hideOnDisable);
649      }                          }
650                    }
651      /**  
652       * Returns the minimum ID of tools.          }
653       */  
654      public int getMinToolID() {          /**
655        return toolAndActionButtons.firstKey();           * Returns the maximum ID of tools.
656      }           */
657  //          public int getMaxToolID() {
658  //    /**                  return toolAndActionButtons.lastKey();
659  //     * Returns the maximum ID of actions.          }
660  //     */  
661  //    public int getMaxActionID() {          /**
662  //      return actionButtons.lastKey();           * Returns the minimum ID of tools.
663  //    }           */
664  //          public int getMinToolID() {
665  //    /**                  return toolAndActionButtons.firstKey();
666  //     * Returns the minimum ID of actions.          }
667  //     */  
668  //    public int getMinActionID() {          /**
669  //      return actionButtons.firstKey();           * Extends the {@link AbstractAction} with maintaining an ID and the
670  //    }           * {@link MapPaneToolBar} the actions controls. Additionally this class
671                 * automatically calls
672      /**           * {@link MapPaneToolBar#performToolButton(int, ActionEvent)} or
673       * Extends the {@link AbstractAction} with maintaining an ID and           * {@link MapPaneToolBar#performActionButton(int, ActionEvent)} depending on
674       * the {@link MapPaneToolBar} the actions controls.           * whether the action is added via
675       * Additionally this class automatically calls {@link MapPaneToolBar#performToolButton(int, ActionEvent)}           * {@link MapPaneToolBar#addTool(MapPaneToolBarAction)} or
676       * or {@link MapPaneToolBar#performActionButton(int, ActionEvent)}           * {@link MapPaneToolBar#addAction(MapPaneToolBarAction)}.
677       * depending on whether the action is added via {@link MapPaneToolBar#addTool(MapPaneToolBarAction)}           *
678       * or {@link MapPaneToolBar#addAction(MapPaneToolBarAction)}.           * @author <a href="mailto:[email protected]">Martin Schmitz</a>
679       * @author <a href="mailto:[email protected]">Martin Schmitz</a> (University of Bonn/Germany)           *         (University of Bonn/Germany)
680       */           */
681      public static class MapPaneToolBarAction extends AbstractAction {          public static class MapPaneToolBarAction extends AbstractAction {
682        /** The ID of the action */                  /** The ID of the action */
683        protected int id = -1;                  protected int id = -1;
684        /** The tool bar, this action is made for. */                  /** The tool bar, this action is made for. */
685        protected MapPaneToolBar toolBar = null;                  protected MapPaneToolBar toolBar = null;
686    
687        /**                  /**
688         * Creates a new action with a dummy description and no icon.                   * Creates a new action with a dummy description and no icon.
689         * @param id      unique ID for the action                   *
690         * @param toolBar toolbar this action is made for                   * @param id
691         */                   *            unique ID for the action
692        public MapPaneToolBarAction(int id, MapPaneToolBar toolBar) {                   * @param toolBar
693          this(id,toolBar,""+id);                   *            toolbar this action is made for
694        }                   */
695                    public MapPaneToolBarAction(int id, MapPaneToolBar toolBar) {
696        /**                          this(id, toolBar, "" + id);
697         * Creates a new action without an icon.                  }
698         * @param id      unique ID for the action  
699         * @param toolBar toolbar this action is made for                  /**
700         * @param name    description used for buttons or menus                   * Creates a new action without an icon.
701         */                   *
702        public MapPaneToolBarAction(int id, MapPaneToolBar toolBar, String name) {                   * @param id
703          this(id,toolBar,name,null);                   *            unique ID for the action
704        }                   * @param toolBar
705                     *            toolbar this action is made for
706        /**                   * @param name
707         * Creates a new action.                   *            description used for buttons or menus
708         * @param id      unique ID for the action                   */
709         * @param toolBar toolbar this action is made for                  public MapPaneToolBarAction(int id, MapPaneToolBar toolBar, String name) {
710         * @param name    description used for buttons or menus                          this(id, toolBar, name, null);
711         * @param icon    icon used for buttons or menus                  }
712         */  
713        public MapPaneToolBarAction(int id, MapPaneToolBar toolBar, String name, Icon icon) {                  /**
714          super(name,icon);                   * Creates a new action.
715          this.id      = id;                   *
716          this.toolBar = toolBar;                   * @param id
717        }                   *            unique ID for the action
718                           * @param toolBar
719        /**                   *            toolbar this action is made for
720         * Calls {@link MapPaneToolBar#performToolButton(int, ActionEvent)}                   * @param name
721         * or {@link MapPaneToolBar#performActionButton(int, ActionEvent)}                   *            description used for buttons or menus
722         * depending on whether the action is added to the toolbar via                   * @param icon
723         * {@link MapPaneToolBar#addTool(MapPaneToolBarAction)}                   *            icon used for buttons or menus
724         * or {@link MapPaneToolBar#addAction(MapPaneToolBarAction)}.                   */
725         */                  public MapPaneToolBarAction(int id, MapPaneToolBar toolBar,
726        public void actionPerformed(ActionEvent e) {                                  String name, Icon icon) {
727          if ( toolBar.toolAndActionButtons.get(id) instanceof JToggleButton)                          super(name, icon);
728            toolBar.performToolButton(id, e);                          this.id = id;
729          else if ( toolBar.toolAndActionButtons.get(id) instanceof JButton )                          this.toolBar = toolBar;
730            toolBar.performActionButton(id, e);                  }
731        }  
732                          /**
733        /**                   * Calls {@link MapPaneToolBar#performToolButton(int, ActionEvent)} or
734         * Returns the (unique) id of this action.                   * {@link MapPaneToolBar#performActionButton(int, ActionEvent)}
735         * @return                   * depending on whether the action is added to the toolbar via
736         */                   * {@link MapPaneToolBar#addTool(MapPaneToolBarAction)} or
737        public int getID() {                   * {@link MapPaneToolBar#addAction(MapPaneToolBarAction)}.
738          return id;                   */
739        }                  public void actionPerformed(ActionEvent e) {
740      }                          if (toolBar.toolAndActionButtons.get(id) instanceof JToggleButton)
741                                    toolBar.performToolButton(id, e);
742                            else if (toolBar.toolAndActionButtons.get(id) instanceof JButton)
743                                    toolBar.performActionButton(id, e);
744                    }
745    
746                    /**
747                     * Returns the (unique) id of this action.
748                     *
749                     * @return
750                     */
751                    public int getID() {
752                            return id;
753                    }
754            }
755  }  }

Legend:
Removed from v.127  
changed lines
  Added in v.141

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26