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

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

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

revision 48 by alfonx, Fri Apr 17 12:49:33 2009 UTC revision 140 by alfonx, Sun Jun 14 17:24:44 2009 UTC
# Line 12  import javax.swing.BorderFactory; Line 12  import javax.swing.BorderFactory;
12  import javax.swing.Icon;  import javax.swing.Icon;
13  import javax.swing.ImageIcon;  import javax.swing.ImageIcon;
14  import javax.swing.JButton;  import javax.swing.JButton;
15    import javax.swing.JComponent;
16  import javax.swing.JToggleButton;  import javax.swing.JToggleButton;
17  import javax.swing.JToolBar;  import javax.swing.JToolBar;
18    
# Line 27  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 controll a {@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
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;
         /** Constant for the tool "Zoom In" (30). */  
         public static final int TOOL_ZOOMIN = 30;  
         /** Constant for the tool "Zoom Out" (40). */  
         public static final int TOOL_ZOOMOUT = 40;  
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;
49    
50            /** Constant for the tool "Zoom In" (110). */
51            public static final int TOOL_ZOOMIN = 110;
52            /** Constant for the tool "Zoom Out" (120). */
53            public static final int TOOL_ZOOMOUT = 120;
54            /** Constant for the action "Zoom back" (130). */
55            public static final int ACTION_ZOOM_BACK = 130;
56            /** Constant for the action "Zoom forward" (140). */
57            public static final int ACTION_ZOOM_FORWARD = 140;
58            public static final int SEPERATOR1 = 199;
59    
60            /**
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;
70            /**
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;
75            /**
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;
80    
81          /** Tool currently selected */          /** Tool currently selected */
82      protected int selectedTool = TOOL_ZOOMIN;          protected int selectedTool = TOOL_ZOOMIN;
     /** Holds the tool buttons of the tool bar. */  
     protected SortedMap<Integer, JToggleButton> toolButtons = null;  
     /** Controls that only one tool button is activated. */  
     protected ButtonGroup toolButtonGroup = null;  
     /** Constant for the action "Zoom back" (100). */  
     public static final int ACTION_ZOOM_BACK = 100;  
     /** Constant for the action "Zoom forward" (110). */  
     public static final int ACTION_ZOOM_FORWARD = 110;  
     /** Constant for the action "Search Labels" (120). */  
     public static final int ACTION_SEARCH = 120;  
83    
84      /** Holds the action buttons of the bar. */          /** Holds the tool buttons of the tool bar. */
85      protected SortedMap<Integer, JButton> actionButtons = null;          protected SortedMap<Integer, JComponent> toolAndActionButtons = null;
86            /** Controls that only one tool button is activated. */
87            protected ButtonGroup toolButtonGroup = null;
88    
89            // SK: Musste ich ändern damit man Tools und Actions in der Reihenfolge
90            // mischen kann.
91            // /** 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 74  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.toolButtons     = new TreeMap<Integer,JToggleButton>();                  this.toolAndActionButtons = new TreeMap<Integer, JComponent>();
127        this.toolButtonGroup = new ButtonGroup();                  this.toolButtonGroup = new ButtonGroup();
128        this.actionButtons   = new TreeMap<Integer,JButton>();                  // Create a Listener to sniff the zooms on the JMapPane
129        // Create a Listener to sniff the zooms on the JMapPane                  this.mapPaneListener = new JMapPaneListener() {
130        this.mapPaneListener = new JMapPaneListener() {                          public void performMapPaneEvent(JMapPaneEvent e) {
131            public void performMapPaneEvent(JMapPaneEvent e) {                                  if (!(e instanceof MapAreaChangedEvent))
132                if ( !(e instanceof MapAreaChangedEvent) )                                          return;
133                  return;  
134                                                    if (zoomBackForwardButtonInAction) {
135                if ( zoomBackForwardButtonInAction ) {                                          zoomBackForwardButtonInAction = false;
136                  zoomBackForwardButtonInAction = false;                                          return;
137                  return;                                  }
138                }  
139                                                Envelope oldMapArea = ((MapAreaChangedEvent) e).getOldMapArea();
140                Envelope oldMapArea = ((MapAreaChangedEvent)e).getOldMapArea();                                  if (lastZooms.size() == 0 && oldMapArea != null) {
141                if (lastZooms.size() == 0 && oldMapArea != null ) {                                          lastZooms.add(oldMapArea);
142                  lastZooms.add(oldMapArea);                                          zoomBackIndex = 1;
143                  zoomBackIndex = 1;                                  }
144                }  
145                                    final Envelope mapArea = ((MapAreaChangedEvent) e)
146                final Envelope mapArea = ((MapAreaChangedEvent)e).getNewMapArea();                                                  .getNewMapArea();
147                if (mapArea == null)                                  if (mapArea == null)
148                  return;                                          return;
149                  
150                if (lastZooms.size() > 0 && mapArea.equals(lastZooms.get(lastZooms.size() - 1))) {                                  if (lastZooms.size() > 0
151                  // LOGGER.debug("MapAreaChangedEvent ausgelassen bei der Zaehlung der Zoomschritt weil identisch");                                                  && mapArea.equals(lastZooms.get(lastZooms.size() - 1))) {
152                  return;                                          // LOGGER.debug("MapAreaChangedEvent ausgelassen bei der Zaehlung der Zoomschritt weil identisch");
153                }                                          return;
154                                    }
155                if (lastZooms.size() > 0)  
156                  while (zoomBackIndex < lastZooms.size())                                  if (lastZooms.size() > 0)
157                    lastZooms.remove(lastZooms.size() - 1);                                          while (zoomBackIndex < lastZooms.size())
158                                                    lastZooms.remove(lastZooms.size() - 1);
159                lastZooms.add(mapArea);  
160                zoomBackIndex = lastZooms.size();                                  lastZooms.add(mapArea);
161                setButtonEnabled(ACTION_ZOOM_BACK, lastZooms.size() > 1);                                  zoomBackIndex = lastZooms.size();
162                setButtonEnabled(ACTION_ZOOM_FORWARD, false);                                  setButtonEnabled(ACTION_ZOOM_BACK, lastZooms.size() > 1);
163            }                                  setButtonEnabled(ACTION_ZOOM_FORWARD, false);
164        };                              }
165                        };
166        setMapPane(mapPane);  
167            setFloatable(false);                  setMapPane(mapPane);
168            setRollover(true);                  setFloatable(false);
169                              setRollover(true);
170            init();  
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 #initTools()} 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            initTools();                  initToolsAndActions();
196            initActions();  
197            initToolBar();                  addSeparator(SEPERATOR0, new JToolBar.Separator());
198          }                  addSeparator(SEPERATOR1, new JToolBar.Separator());
199    
200                    initToolBar();
201          /**          }
202           * Creates the tool buttons, adds them to {@link #toolButtons} and finally  
203           * creates a button group for all tools. So sub-classes which override this          /**
204           * method should FIRST add their new tool buttons to {@link #toolButtons}           * Creates the tool buttons and action buttons and seperators, adds them to
205           * before calling {@code super.initTools()}.           * {@link #toolAndActionButtons} and finally creates a button group for all
206           */           * tools. So sub-classes which override this method should FIRST add their
207          protected void initTools() {           * new tool buttons to {@link #toolAndActionButtons} before calling {@code
208        // Panning           * super.initTools()}.
209        addTool( new MapPaneToolBarAction(           */
210            TOOL_PAN,          protected void initToolsAndActions() {
211            this,                  // Panning
212            "",                  addTool(new MapPaneToolBarAction(TOOL_PAN, this, "", new ImageIcon(
213            new ImageIcon(MapView.class.getResource("pan.png"))                                  MapView.class.getResource("resource/icons/pan.png"))), false);
214        ), false );                  // Info
215        // Info                  addTool(new MapPaneToolBarAction(TOOL_INFO, this, "", new ImageIcon(
216        addTool( new MapPaneToolBarAction(                                  MapView.class.getResource("resource/icons/info.png"))), false);
217            TOOL_INFO,  
218            this,                  // Zoom in
219            "",                  addTool(new MapPaneToolBarAction(TOOL_ZOOMIN, this, "", new ImageIcon(
220            new ImageIcon(MapView.class.getResource("info.png"))                                  MapView.class.getResource("resource/icons/zoom_in.png"))),
221        ), false );                                  false);
222        // Zoom in                  // Zoom out
223        addTool( new MapPaneToolBarAction(                  addTool(new MapPaneToolBarAction(TOOL_ZOOMOUT, this, "", new ImageIcon(
224            TOOL_ZOOMIN,                                  MapView.class.getResource("resource/icons/zoom_out.png"))),
225            this,                                  false);
226            "",  
227            new ImageIcon(MapView.class.getResource("zoom_in.png"))                  // Action button to revert the last zoom
228        ), false );                  addAction(new MapPaneToolBarAction(ACTION_ZOOM_BACK, this, "",
229        // Zoom out                                  new ImageIcon(MapView.class
230        addTool( new MapPaneToolBarAction(                                                  .getResource("resource/icons/zoom_back.png"))), false);
231            TOOL_ZOOMOUT,                  setButtonEnabled(ACTION_ZOOM_BACK, false);
232            this,  
233            "",                  // Action button to redo the last zoom
234            new ImageIcon(MapView.class.getResource("zoom_out.png"))                  addAction(new MapPaneToolBarAction(ACTION_ZOOM_FORWARD, this, "",
235        ), false );                                  new ImageIcon(MapView.class
236                                                          .getResource("resource/icons/zoom_forward.png"))),
237            // set the selected tool enabled                                  false);
238        setSelectedTool(selectedTool);                  setButtonEnabled(ACTION_ZOOM_FORWARD, false);
239          
240          }                  // set the selected tool enabled
241                    setSelectedTool(selectedTool);
242      /**  
243       * Creates the action buttons and adds them to {@link #actionButtons}.          }
244       */  
245      protected void initActions() {          /**
246        // Action button to revert the last zoom           * Clears the GUI of all components and adds all tool and action buttons to
247        addAction( new MapPaneToolBarAction(           * the tool bar.
248            ACTION_ZOOM_BACK,           */
249            this,          protected void initToolBar() {
250            "",                  setAlignmentY(1f);
251            new ImageIcon(MapView.class.getResource("zoom_back.png"))                  removeAll();
252        ), false);                  // Separator to the left of the tool actions to start
253        setButtonEnabled( ACTION_ZOOM_BACK, false );                  // the tool buttons with the map (not with the coordinate grid)
254                    Dimension dimension = new Dimension(49, 10);
255        // Action button to redo the last zoom                  addSeparator(dimension);
256        addAction( new MapPaneToolBarAction(                  // Tool buttons
257            ACTION_ZOOM_FORWARD,                  for (JComponent b : toolAndActionButtons.values())
258            this,                          add(b);
259            "",          }
260            new ImageIcon(MapView.class.getResource("zoom_forward.png"))  
261        ), false);          // Space between tool buttons and action buttons
262        setButtonEnabled( ACTION_ZOOM_FORWARD, false );          // SK: Seperators are now als manages like actions and tools
263            // Dimension dimension2 = new Dimension( 10,10);
264      }          // this.addSeparator(dimension2);
265        
266      /**          // // Action buttons
267       * Clears the GUI of all components and adds all tool and action buttons to the          // for (JButton b : actionButtons.values())
268       * tool bar.          // add(b);
269       */          // }
270      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 (JToggleButton b : toolButtons.values())  
         add(b);  
       // Space between tool buttons and action buttons  
       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);                          mapPane.setState(JMapPane.SELECT_TOP); // Why not:
297            mapPane.setHighlight(true);                                                                                                          // 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_ZOOMIN:                                                                                  // performance
301            // Set the mouse tool to "Zoom in"                          mapPane.setNormalCursor(SwingUtil.CROSSHAIR_CURSOR);
302            mapPane.setWindowSelectionState(JMapPane.ZOOM_IN);                          break;
303            mapPane.setState(JMapPane.ZOOM_IN);                  case TOOL_ZOOMIN:
304            mapPane.setHighlight(false);                          // Set the mouse tool to "Zoom in"
305            mapPane.setNormalCursor(SwingUtil.ZOOMIN_CURSOR);                          mapPane.setWindowSelectionState(JMapPane.ZOOM_IN);
306            break;                          mapPane.setState(JMapPane.ZOOM_IN);
307          case TOOL_ZOOMOUT:                          mapPane.setHighlight(false);
308            // Set the mouse tool to "Zoom out"                          mapPane.setNormalCursor(SwingUtil.ZOOMIN_CURSOR);
309            mapPane.setWindowSelectionState(JMapPane.NONE);                          break;
310            mapPane.setState(JMapPane.ZOOM_OUT);                  case TOOL_ZOOMOUT:
311            mapPane.setHighlight(false);                          // Set the mouse tool to "Zoom out"
312            mapPane.setNormalCursor(SwingUtil.ZOOMOUT_CURSOR);                          mapPane.setWindowSelectionState(JMapPane.NONE);
313            break;                          mapPane.setState(JMapPane.ZOOM_OUT);
314          default:                          mapPane.setHighlight(false);
315            // Set map actions to default                          mapPane.setNormalCursor(SwingUtil.ZOOMOUT_CURSOR);
316            mapPane.setWindowSelectionState(JMapPane.NONE);                          break;
317            mapPane.setState(JMapPane.NONE);  //              default:
318            mapPane.setHighlight(false);  //                      // Set map actions to default
319            mapPane.setNormalCursor(null);  //                      mapPane.setWindowSelectionState(JMapPane.NONE);
320            break;  //                      mapPane.setState(JMapPane.NONE);
321        }  //                      mapPane.setHighlight(false);
322        mapPane.updateCursor();  //                      mapPane.setNormalCursor(null);
323    //                      break;
324                    }
325                    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();  
         }  
       }  
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            toolButtons.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        actionButtons.put( buttonAction.getID(), button );           */
425        if ( resetToolBar )          public void addAction(MapPaneToolBarAction buttonAction,
426          initToolBar();                          boolean resetToolBar) {
427      }                  if (isButtonIDUsed(buttonAction.getID())) {
428                            LOGGER
429      /**                                          .warn("addAction(.) ignored because ID already used for tool or action: "
430       * Adds an action to the tool bar and resets the toolbar GUI.                                                          + buttonAction.getID());
431       * @param buttonAction action for the toggle button                          return;
432       */                  }
433      public void addAction(MapPaneToolBarAction buttonAction) {                  JButton button = new JButton(buttonAction);
434        addAction(buttonAction, true);                  button.setBorder(BorderFactory.createRaisedBevelBorder());
435      }                  toolAndActionButtons.put(buttonAction.getID(), button);
436                        if (resetToolBar)
437      /**                          initToolBar();
438       * Returns the button for a specific tool or action.          }
439       * @param id the constant for a tool  
440       * @return a {@link JButton} if {@code id} specifies an {@linkplain #getActionButton(int) action button}          public void addSeparator(int id, Separator separator) {
441       *         or {@link JToogleButton} if {@code id} specifies a {@linkplain #getToolButton(int) tool button}                  if (isButtonIDUsed(id)) {
442       */                          LOGGER
443      public AbstractButton getButton(int id) {                                          .warn("addSeparator(.) ignored because ID already used for tool or action. ");
444        AbstractButton button = toolButtons.get(id);                          return;
445        if ( button == null )                  }
446          button = actionButtons.get(id);                  toolAndActionButtons.put(id, separator);
447        if ( button == null )          }
448          LOGGER.warn("Unknown tool or action ID: "+id);  
449        return button;          /**
450      }           * Adds an action to the tool bar and resets the toolbar GUI.
451             *
452      /**           * @param buttonAction
453       * Returns the button for a specific tool.           *            action for the toggle button
454       * @param tool the constant for a tool           */
455       */          public void addAction(MapPaneToolBarAction buttonAction) {
456                    addAction(buttonAction, true);
457            }
458    
459            /**
460             * Returns the button for a specific tool or action.
461             *
462             * @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 toolButtons.get(id) != null || actionButtons.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 : toolButtons.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 tool : actionButtons.keySet())          /**
614          setButtonEnabled(tool,enabled,hideOnDisable);           * Sets the activation for all actions.
615      }             *
616                 * @param enabled
617      /**           *            if {@code true} all actions becomes available
618       * Returns the maximum ID of tools.           * @param hideOnDisable
619       */           *            if {@code true} the buttons are also hidden if {@code enabled}
620      public int getMaxToolID() {           *            is {@code false}
621        return toolButtons.lastKey();           */
622      }          public void setAllActionsEnabled(boolean enabled, boolean hideOnDisable) {
623                    for (int id : toolAndActionButtons.keySet()) {
624      /**                          if (toolAndActionButtons.get(id) instanceof JButton) {
625       * Returns the minimum ID of tools.                                  setButtonEnabled(id, enabled, hideOnDisable);
626       */                          }
627      public int getMinToolID() {                  }
628        return toolButtons.firstKey();  
629      }          }
630    
631      /**          /**
632       * Returns the maximum ID of actions.           * Returns the maximum ID of tools.
633       */           */
634      public int getMaxActionID() {          public int getMaxToolID() {
635        return actionButtons.lastKey();                  return toolAndActionButtons.lastKey();
636      }          }
637    
638      /**          /**
639       * Returns the minimum ID of actions.           * Returns the minimum ID of tools.
640       */           */
641      public int getMinActionID() {          public int getMinToolID() {
642        return actionButtons.firstKey();                  return toolAndActionButtons.firstKey();
643      }          }
644        
645      /**          /**
646       * Extends the {@link AbstractAction} with maintaining an ID and           * Extends the {@link AbstractAction} with maintaining an ID and the
647       * the {@link MapPaneToolBar} the actions controls.           * {@link MapPaneToolBar} the actions controls. Additionally this class
648       * Additionally this class automatically calls {@link MapPaneToolBar#performToolButton(int, ActionEvent)}           * automatically calls
649       * or {@link MapPaneToolBar#performActionButton(int, ActionEvent)}           * {@link MapPaneToolBar#performToolButton(int, ActionEvent)} or
650       * depending on whether the action is added via {@link MapPaneToolBar#addTool(MapPaneToolBarAction)}           * {@link MapPaneToolBar#performActionButton(int, ActionEvent)} depending on
651       * or {@link MapPaneToolBar#addAction(MapPaneToolBarAction)}.           * whether the action is added via
652       * @author <a href="mailto:[email protected]">Martin Schmitz</a> (University of Bonn/Germany)           * {@link MapPaneToolBar#addTool(MapPaneToolBarAction)} or
653       */           * {@link MapPaneToolBar#addAction(MapPaneToolBarAction)}.
654      public static class MapPaneToolBarAction extends AbstractAction {           *
655        /** The ID of the action */           * @author <a href="mailto:[email protected]">Martin Schmitz</a>
656        protected int id = -1;           *         (University of Bonn/Germany)
657        /** The tool bar, this action is made for. */           */
658        protected MapPaneToolBar toolBar = null;          public static class MapPaneToolBarAction extends AbstractAction {
659                    /** The ID of the action */
660        /**                  protected int id = -1;
661         * Creates a new action with a dummy description and no icon.                  /** The tool bar, this action is made for. */
662         * @param id      unique ID for the action                  protected MapPaneToolBar toolBar = null;
663         * @param toolBar toolbar this action is made for  
664         */                  /**
665        public MapPaneToolBarAction(int id, MapPaneToolBar toolBar) {                   * Creates a new action with a dummy description and no icon.
666          this(id,toolBar,""+id);                   *
667        }                   * @param id
668                     *            unique ID for the action
669        /**                   * @param toolBar
670         * Creates a new action without an icon.                   *            toolbar this action is made for
671         * @param id      unique ID for the action                   */
672         * @param toolBar toolbar this action is made for                  public MapPaneToolBarAction(int id, MapPaneToolBar toolBar) {
673         * @param name    description used for buttons or menus                          this(id, toolBar, "" + id);
674         */                  }
675        public MapPaneToolBarAction(int id, MapPaneToolBar toolBar, String name) {  
676          this(id,toolBar,name,null);                  /**
677        }                   * Creates a new action without an icon.
678                     *
679        /**                   * @param id
680         * Creates a new action.                   *            unique ID for the action
681         * @param id      unique ID for the action                   * @param toolBar
682         * @param toolBar toolbar this action is made for                   *            toolbar this action is made for
683         * @param name    description used for buttons or menus                   * @param name
684         * @param icon    icon used for buttons or menus                   *            description used for buttons or menus
685         */                   */
686        public MapPaneToolBarAction(int id, MapPaneToolBar toolBar, String name, Icon icon) {                  public MapPaneToolBarAction(int id, MapPaneToolBar toolBar, String name) {
687          super(name,icon);                          this(id, toolBar, name, null);
688          this.id      = id;                  }
689          this.toolBar = toolBar;  
690        }                  /**
691                           * Creates a new action.
692        /**                   *
693         * Calls {@link MapPaneToolBar#performToolButton(int, ActionEvent)}                   * @param id
694         * or {@link MapPaneToolBar#performActionButton(int, ActionEvent)}                   *            unique ID for the action
695         * depending on whether the action is added to the toolbar via                   * @param toolBar
696         * {@link MapPaneToolBar#addTool(MapPaneToolBarAction)}                   *            toolbar this action is made for
697         * or {@link MapPaneToolBar#addAction(MapPaneToolBarAction)}.                   * @param name
698         */                   *            description used for buttons or menus
699        public void actionPerformed(ActionEvent e) {                   * @param icon
700          if ( toolBar.toolButtons.get(id) != null )                   *            icon used for buttons or menus
701            toolBar.performToolButton(id, e);                   */
702          if ( toolBar.actionButtons.get(id) != null )                  public MapPaneToolBarAction(int id, MapPaneToolBar toolBar,
703            toolBar.performActionButton(id, e);                                  String name, Icon icon) {
704        }                          super(name, icon);
705                                  this.id = id;
706        /**                          this.toolBar = toolBar;
707         * Returns the (unique) id of this action.                  }
708         * @return  
709         */                  /**
710        public int getID() {                   * Calls {@link MapPaneToolBar#performToolButton(int, ActionEvent)} or
711          return id;                   * {@link MapPaneToolBar#performActionButton(int, ActionEvent)}
712        }                   * depending on whether the action is added to the toolbar via
713      }                   * {@link MapPaneToolBar#addTool(MapPaneToolBarAction)} or
714                     * {@link MapPaneToolBar#addAction(MapPaneToolBarAction)}.
715                     */
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.48  
changed lines
  Added in v.140

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26