/[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 139 by alfonx, Thu Jun 11 12:52:58 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 two types
32   * of buttons. A group of <i>tools</i> for the mouse actions on the map represented   * of buttons. A group of <i>tools</i> for the mouse actions on the map represented
33   * by {@link JToggleButton JToggleButtons}, where only one tool can be activated   * by {@link JToggleButton JToggleButtons}, where only one tool can be activated
34   * every time. And some (general) <i>actions</i>, represented by normal   * every time. And some (general) <i>actions</i>, represented by normal
35   * {@link JButton JButtons}.   * {@link JButton JButtons}.
36   * @author <a href="mailto:[email protected]">Martin Schmitz</a> (University of Bonn/Germany)   * @author <a href="mailto:[email protected]">Martin Schmitz</a> (University of Bonn/Germany)
37     * @version 1.2 Stefan Krüger
38   */   */
39  public class MapPaneToolBar extends JToolBar {  public class MapPaneToolBar extends JToolBar {
40          private static final Logger LOGGER = Logger.getLogger(MapPaneToolBar.class.getName());          private static final Logger LOGGER = Logger.getLogger(MapPaneToolBar.class.getName());
41          /** Constant for the tool "Panning" (10). */          /** Constant for the tool "Panning" (10). */
42          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;  
43          /** Constant for the tool "Info" (20). */          /** Constant for the tool "Info" (20). */
44          public static final int TOOL_INFO = 20;          public static final int TOOL_INFO = 20;
45            public static final int SEPERATOR0 = 99;
46            
47            /** Constant for the tool "Zoom In" (110). */
48            public static final int TOOL_ZOOMIN = 110;
49            /** Constant for the tool "Zoom Out" (120). */
50            public static final int TOOL_ZOOMOUT = 120;
51            /** Constant for the action "Zoom back" (130). */
52            public static final int ACTION_ZOOM_BACK = 130;
53            /** Constant for the action "Zoom forward" (140). */
54            public static final int ACTION_ZOOM_FORWARD = 140;
55            public static final int SEPERATOR1 = 199;
56            
57            /** Constant for the tool "Select" which sets the Selection to the selected features (210). */
58            public static final int TOOL_SELECTION_SET = 210;
59            /** Constant for the tool "Selection add" which adds the features to the Selection (220). */
60            public static final int TOOL_SELECTION_ADD = 220;
61            /** Constant for the tool "Selection subtract" which removes the selected features from the selection (230). */
62            public static final int TOOL_SELECTION_REMOVE = 230;
63            /** Constant for the tool "Selection Reset" which clears the selection (240). */
64            public static final int TOOL_SELECTION_CLEAR = 240;
65            public static final int SEPERATOR2 = 299;
66    
67            /** Constant for the action "Search Labels" (120). */
68            public static final int ACTION_SEARCH = 300;
69    
70          /** Tool currently selected */          /** Tool currently selected */
71      protected int selectedTool = TOOL_ZOOMIN;      protected int selectedTool = TOOL_ZOOMIN;
72        
73      /** Holds the tool buttons of the tool bar. */      /** Holds the tool buttons of the tool bar. */
74      protected SortedMap<Integer, JToggleButton> toolButtons = null;      protected SortedMap<Integer, JComponent> toolAndActionButtons = null;
75      /** Controls that only one tool button is activated. */      /** Controls that only one tool button is activated. */
76      protected ButtonGroup toolButtonGroup = null;      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;  
77    
78      /** Holds the action buttons of the bar. */  // SK: Musste ich ändern damit man Tools und Actions in der Reihenfolge mischen kann.    
79      protected SortedMap<Integer, JButton> actionButtons = null;  //    /** Holds the action buttons of the bar. */
80    //    protected SortedMap<Integer, JButton> actionButtons = null;
81    
82          /** Holds the {@link JMapPane} this tool bar controls. */          /** Holds the {@link JMapPane} this tool bar controls. */
83          protected JMapPane mapPane = null;          protected JMapPane mapPane = null;
# Line 91  public class MapPaneToolBar extends JToo Line 109  public class MapPaneToolBar extends JToo
109           */           */
110          public MapPaneToolBar(JMapPane mapPane) {          public MapPaneToolBar(JMapPane mapPane) {
111            super("Control the map", JToolBar.HORIZONTAL);            super("Control the map", JToolBar.HORIZONTAL);
112        this.toolButtons     = new TreeMap<Integer,JToggleButton>();        this.toolAndActionButtons  = new TreeMap<Integer,JComponent>();
113        this.toolButtonGroup = new ButtonGroup();        this.toolButtonGroup = new ButtonGroup();
       this.actionButtons   = new TreeMap<Integer,JButton>();  
114        // Create a Listener to sniff the zooms on the JMapPane        // Create a Listener to sniff the zooms on the JMapPane
115        this.mapPaneListener = new JMapPaneListener() {        this.mapPaneListener = new JMapPaneListener() {
116            public void performMapPaneEvent(JMapPaneEvent e) {            public void performMapPaneEvent(JMapPaneEvent e) {
# Line 153  public class MapPaneToolBar extends JToo Line 170  public class MapPaneToolBar extends JToo
170          }          }
171                    
172          /**          /**
173           * Calls {@link #initTools()} and {@link #initActions()} and then puts           * Calls {@link #initToolsAndActions()} and {@link #initActions()} and then puts
174           * all tool buttons and all actions buttons to the tool bar.           * all tool buttons and all actions buttons to the tool bar.
175           */           */
176          protected void init() {          protected void init() {
177            initTools();            initToolsAndActions();
178            initActions();        
179              addSeparator(SEPERATOR0, new JToolBar.Separator());
180          addSeparator(SEPERATOR1, new JToolBar.Separator());
181          addSeparator(SEPERATOR2, new JToolBar.Separator());
182          
183            initToolBar();            initToolBar();
184          }          }
185    
186    
187          /**          /**
188           * Creates the tool buttons, adds them to {@link #toolButtons} and finally           * Creates the tool buttons and action buttons and seperators, adds them to
189           * creates a button group for all tools. So sub-classes which override this           * {@link #toolAndActionButtons} and finally creates a button group for all tools.
190           * method should FIRST add their new tool buttons to {@link #toolButtons}           * So sub-classes which override this method should FIRST add their new tool
191           * before calling {@code super.initTools()}.           * buttons to {@link #toolAndActionButtons} before calling {@code super.initTools()}.
192           */           */
193          protected void initTools() {          protected void initToolsAndActions() {
194        // Panning        // Panning
195        addTool( new MapPaneToolBarAction(        addTool( new MapPaneToolBarAction(
196            TOOL_PAN,            TOOL_PAN,
197            this,            this,
198            "",            "",
199            new ImageIcon(MapView.class.getResource("pan.png"))            new ImageIcon(MapView.class.getResource("resource/icons/pan.png"))
200        ), false );        ), false );
201        // Info        // Info
202        addTool( new MapPaneToolBarAction(        addTool( new MapPaneToolBarAction(
203            TOOL_INFO,            TOOL_INFO,
204            this,            this,
205            "",            "",
206            new ImageIcon(MapView.class.getResource("info.png"))            new ImageIcon(MapView.class.getResource("resource/icons/info.png"))
207          ), false );
208          
209          // Set Selection
210          addTool( new MapPaneToolBarAction(
211              TOOL_SELECTION_SET,
212              this,
213              "",
214              new ImageIcon(MapView.class.getResource("resource/icons/selection_set.png"))
215          ), false );
216    
217          // Add Selection
218          addTool( new MapPaneToolBarAction(
219                      TOOL_SELECTION_ADD,
220                      this,
221                      "",
222                      new ImageIcon(MapView.class.getResource("resource/icons/selection_add.png"))
223        ), false );        ), false );
224    
225          // Remove Selection
226          addTool( new MapPaneToolBarAction(
227                      TOOL_SELECTION_REMOVE,
228                      this,
229                      "",
230                      new ImageIcon(MapView.class.getResource("resource/icons/selection_remove.png"))
231          ), false );
232          
233          // ResetSelection
234          addAction( new MapPaneToolBarAction(
235                      TOOL_SELECTION_CLEAR,
236                      this,
237                      "",
238                      new ImageIcon(MapView.class.getResource("resource/icons/selection_clear.png"))
239          ), false );
240          
241        // Zoom in        // Zoom in
242        addTool( new MapPaneToolBarAction(        addTool( new MapPaneToolBarAction(
243            TOOL_ZOOMIN,            TOOL_ZOOMIN,
244            this,            this,
245            "",            "",
246            new ImageIcon(MapView.class.getResource("zoom_in.png"))            new ImageIcon(MapView.class.getResource("resource/icons/zoom_in.png"))
247        ), false );        ), false );
248        // Zoom out        // Zoom out
249        addTool( new MapPaneToolBarAction(        addTool( new MapPaneToolBarAction(
250            TOOL_ZOOMOUT,            TOOL_ZOOMOUT,
251            this,            this,
252            "",            "",
253            new ImageIcon(MapView.class.getResource("zoom_out.png"))            new ImageIcon(MapView.class.getResource("resource/icons/zoom_out.png"))
254        ), false );        ), false );
255                
           // set the selected tool enabled  
       setSelectedTool(selectedTool);  
         
         }  
   
     /**  
      * Creates the action buttons and adds them to {@link #actionButtons}.  
      */  
     protected void initActions() {  
256        // Action button to revert the last zoom        // Action button to revert the last zoom
257        addAction( new MapPaneToolBarAction(        addAction( new MapPaneToolBarAction(
258            ACTION_ZOOM_BACK,            ACTION_ZOOM_BACK,
259            this,            this,
260            "",            "",
261            new ImageIcon(MapView.class.getResource("zoom_back.png"))            new ImageIcon(MapView.class.getResource("resource/icons/zoom_back.png"))
262        ), false);        ), false);
263        setButtonEnabled( ACTION_ZOOM_BACK, false );        setButtonEnabled( ACTION_ZOOM_BACK, false );
264    
# Line 222  public class MapPaneToolBar extends JToo Line 267  public class MapPaneToolBar extends JToo
267            ACTION_ZOOM_FORWARD,            ACTION_ZOOM_FORWARD,
268            this,            this,
269            "",            "",
270            new ImageIcon(MapView.class.getResource("zoom_forward.png"))            new ImageIcon(MapView.class.getResource("resource/icons/zoom_forward.png"))
271        ), false);        ), false);
272        setButtonEnabled( ACTION_ZOOM_FORWARD, false );        setButtonEnabled( ACTION_ZOOM_FORWARD, false );
273          
274          
275              // set the selected tool enabled
276          setSelectedTool(selectedTool);
277          
278            }
279    
280      }  
281                /**
     /**  
282       * Clears the GUI of all components and adds all tool and action buttons to the       * Clears the GUI of all components and adds all tool and action buttons to the
283       * tool bar.       * tool bar.
284       */       */
# Line 240  public class MapPaneToolBar extends JToo Line 290  public class MapPaneToolBar extends JToo
290        Dimension dimension = new Dimension( 49,10);        Dimension dimension = new Dimension( 49,10);
291        addSeparator(dimension);        addSeparator(dimension);
292        // Tool buttons        // Tool buttons
293        for (JToggleButton b : toolButtons.values())        for (JComponent b : toolAndActionButtons.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())  
294          add(b);          add(b);
295      }      }
296          // Space between tool buttons and action buttons
297    // SK: Seperators are now als manages like actions and tools      
298    //      Dimension dimension2 = new Dimension( 10,10);
299    //      this.addSeparator(dimension2);
300          
301    //      // Action buttons
302    //      for (JButton b : actionButtons.values())
303    //        add(b);
304    //    }
305            
306          /**          /**
307           * Performs the activation of a tool.           * Performs the activation of a tool.
# Line 272  public class MapPaneToolBar extends JToo Line 325  public class MapPaneToolBar extends JToo
325          case TOOL_INFO:          case TOOL_INFO:
326            // Set the mouse tool to "Info"            // Set the mouse tool to "Info"
327            mapPane.setWindowSelectionState(JMapPane.NONE);            mapPane.setWindowSelectionState(JMapPane.NONE);
328            mapPane.setState(JMapPane.SELECT_TOP);            mapPane.setState(JMapPane.SELECT_TOP); // Why not: JMapPane.SELECT_TOP_ONEONLY
329            mapPane.setHighlight(true);            mapPane.setHighlight(false);// SK: Was true, but since it not works properly removed it to save performance
330            mapPane.setNormalCursor(SwingUtil.CROSSHAIR_CURSOR);            mapPane.setNormalCursor(SwingUtil.CROSSHAIR_CURSOR);
331            break;            break;
332            case TOOL_SELECTION_SET:
333            case TOOL_SELECTION_ADD:
334            case TOOL_SELECTION_REMOVE:
335                    // Set the mouse tool to "Select"
336                    mapPane.setWindowSelectionState(JMapPane.SELECT_TOP);
337                    mapPane.setState(JMapPane.SELECT_TOP);
338                    mapPane.setHighlight(false);
339                    mapPane.setNormalCursor(SwingUtil.CROSSHAIR_CURSOR); // TODO Select Cursor
340                    break;
341          case TOOL_ZOOMIN:          case TOOL_ZOOMIN:
342            // Set the mouse tool to "Zoom in"            // Set the mouse tool to "Zoom in"
343            mapPane.setWindowSelectionState(JMapPane.ZOOM_IN);            mapPane.setWindowSelectionState(JMapPane.ZOOM_IN);
# Line 336  public class MapPaneToolBar extends JToo Line 398  public class MapPaneToolBar extends JToo
398            mapPane.refresh();            mapPane.refresh();
399          }          }
400        }        }
401          
402          /**
403           * Clear the selection by calling the selection model
404           */
405          if (action == TOOL_SELECTION_CLEAR) {
406              // TODO
407          }
408          }          }
409                    
410                    
# Line 355  public class MapPaneToolBar extends JToo Line 424  public class MapPaneToolBar extends JToo
424            JToggleButton button = new JToggleButton(buttonAction);            JToggleButton button = new JToggleButton(buttonAction);
425            button.setBorder( BorderFactory.createRaisedBevelBorder() );            button.setBorder( BorderFactory.createRaisedBevelBorder() );
426            toolButtonGroup.add(button);            toolButtonGroup.add(button);
427            toolButtons.put(buttonAction.getID(), button);            toolAndActionButtons.put(buttonAction.getID(), button);
428            if ( resetToolBar )            if ( resetToolBar )
429              initToolBar();              initToolBar();
430          }          }
# Line 382  public class MapPaneToolBar extends JToo Line 451  public class MapPaneToolBar extends JToo
451          return;          return;
452        }        }
453        JButton button = new JButton(buttonAction);        JButton button = new JButton(buttonAction);
454        actionButtons.put( buttonAction.getID(), button );        button.setBorder( BorderFactory.createRaisedBevelBorder() );
455          toolAndActionButtons.put( buttonAction.getID(), button );
456        if ( resetToolBar )        if ( resetToolBar )
457          initToolBar();          initToolBar();
458      }      }
459        
460    
461        private void addSeparator(int id, Separator separator) {
462            if ( isButtonIDUsed(id) ) {
463                LOGGER.warn("addSeparator(.) ignored because ID already used for tool or action. ");
464                return;
465              }
466              toolAndActionButtons.put( id, separator);
467            }
468    
469      /**      /**
470       * Adds an action to the tool bar and resets the toolbar GUI.       * Adds an action to the tool bar and resets the toolbar GUI.
# Line 397  public class MapPaneToolBar extends JToo Line 476  public class MapPaneToolBar extends JToo
476            
477      /**      /**
478       * Returns the button for a specific tool or action.       * Returns the button for a specific tool or action.
479       * @param id the constant for a tool       * @param id the constant for any button in the {@link MapPaneToolBar}
480       * @return a {@link JButton} if {@code id} specifies an {@linkplain #getActionButton(int) action button}       * @return a {@link JButton} if {@code id} specifies an {@linkplain #getActionButton(int) action button}
481       *         or {@link JToogleButton} if {@code id} specifies a {@linkplain #getToolButton(int) tool button}       *         or {@link JToogleButton} if {@code id} specifies a {@linkplain #getToolButton(int) tool button}
482       */       */
483      public AbstractButton getButton(int id) {      public AbstractButton getButton(int id) {
484        AbstractButton button = toolButtons.get(id);        AbstractButton button = (AbstractButton)toolAndActionButtons.get(id);
       if ( button == null )  
         button = actionButtons.get(id);  
485        if ( button == null )        if ( button == null )
486          LOGGER.warn("Unknown tool or action ID: "+id);          LOGGER.warn("Unknown tool or action ID: "+id);
487        return button;        return button;
# Line 500  public class MapPaneToolBar extends JToo Line 577  public class MapPaneToolBar extends JToo
577       * @param tool tool ID       * @param tool tool ID
578       */       */
579      public boolean isButtonIDUsed(int id) {      public boolean isButtonIDUsed(int id) {
580        return toolButtons.get(id) != null || actionButtons.get(id) != null;        return toolAndActionButtons.get(id) != null;
581      }      }
582    
583      /**      /**
# Line 522  public class MapPaneToolBar extends JToo Line 599  public class MapPaneToolBar extends JToo
599       *                      {@code enabled} is {@code false}       *                      {@code enabled} is {@code false}
600       */       */
601      public void setAllToolsEnabled(boolean enabled, boolean hideOnDisable) {      public void setAllToolsEnabled(boolean enabled, boolean hideOnDisable) {
602        for (int tool : toolButtons.keySet())        for (int tool : toolAndActionButtons.keySet())
603          setButtonEnabled(tool,enabled,hideOnDisable);          setButtonEnabled(tool,enabled,hideOnDisable);
604      }        }  
605    
# Line 533  public class MapPaneToolBar extends JToo Line 610  public class MapPaneToolBar extends JToo
610       *                      {@code enabled} is {@code false}       *                      {@code enabled} is {@code false}
611       */       */
612      public void setAllActionsEnabled(boolean enabled, boolean hideOnDisable) {      public void setAllActionsEnabled(boolean enabled, boolean hideOnDisable) {
613        for (int tool : actionButtons.keySet())        for (int id : toolAndActionButtons.keySet()){
614          setButtonEnabled(tool,enabled,hideOnDisable);            if (toolAndActionButtons.get(id) instanceof JButton){
615                      setButtonEnabled(id,enabled,hideOnDisable);
616              }
617          }
618            
619      }        }  
620            
621      /**      /**
622       * Returns the maximum ID of tools.       * Returns the maximum ID of tools.
623       */       */
624      public int getMaxToolID() {      public int getMaxToolID() {
625        return toolButtons.lastKey();        return toolAndActionButtons.lastKey();
626      }      }
627    
628      /**      /**
629       * Returns the minimum ID of tools.       * Returns the minimum ID of tools.
630       */       */
631      public int getMinToolID() {      public int getMinToolID() {
632        return toolButtons.firstKey();        return toolAndActionButtons.firstKey();
633      }      }
634    
635      /**      /**
      * Returns the maximum ID of actions.  
      */  
     public int getMaxActionID() {  
       return actionButtons.lastKey();  
     }  
   
     /**  
      * Returns the minimum ID of actions.  
      */  
     public int getMinActionID() {  
       return actionButtons.firstKey();  
     }  
       
     /**  
636       * Extends the {@link AbstractAction} with maintaining an ID and       * Extends the {@link AbstractAction} with maintaining an ID and
637       * the {@link MapPaneToolBar} the actions controls.       * the {@link MapPaneToolBar} the actions controls.
638       * Additionally this class automatically calls {@link MapPaneToolBar#performToolButton(int, ActionEvent)}       * Additionally this class automatically calls {@link MapPaneToolBar#performToolButton(int, ActionEvent)}
# Line 620  public class MapPaneToolBar extends JToo Line 687  public class MapPaneToolBar extends JToo
687         * or {@link MapPaneToolBar#addAction(MapPaneToolBarAction)}.         * or {@link MapPaneToolBar#addAction(MapPaneToolBarAction)}.
688         */         */
689        public void actionPerformed(ActionEvent e) {        public void actionPerformed(ActionEvent e) {
690          if ( toolBar.toolButtons.get(id) != null )          if ( toolBar.toolAndActionButtons.get(id) instanceof JToggleButton)
691            toolBar.performToolButton(id, e);            toolBar.performToolButton(id, e);
692          if ( toolBar.actionButtons.get(id) != null )          else if ( toolBar.toolAndActionButtons.get(id) instanceof JButton )
693            toolBar.performActionButton(id, e);            toolBar.performActionButton(id, e);
694        }        }
695                

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26