/[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 117 by alfonx, Thu May 14 01:15:22 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    import javax.swing.JToolBar.Separator;
19    
20    import jj2000.j2k.NotImplementedError;
21    
22  import org.apache.log4j.Logger;  import org.apache.log4j.Logger;
23    
# Line 27  import schmitzm.swing.SwingUtil; Line 31  import schmitzm.swing.SwingUtil;
31  import com.vividsolutions.jts.geom.Envelope;  import com.vividsolutions.jts.geom.Envelope;
32    
33  /**  /**
34   * 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
35   * 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
36   * by {@link JToggleButton JToggleButtons}, where only one tool can be activated   * by {@link JToggleButton JToggleButtons}, where only one tool can be activated
37   * every time. And some (general) <i>actions</i>, represented by normal   * every time. And some (general) <i>actions</i>, represented by normal
38   * {@link JButton JButtons}.   * {@link JButton JButtons}.
39   * @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)
40     * @version 1.2 Stefan Krüger
41   */   */
42  public class MapPaneToolBar extends JToolBar {  public class MapPaneToolBar extends JToolBar {
43          private static final Logger LOGGER = Logger.getLogger(MapPaneToolBar.class.getName());          private static final Logger LOGGER = Logger.getLogger(MapPaneToolBar.class.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            /** Constant for the tool "Select" which sets the Selection to the selected features (210). */
61            public static final int TOOL_SELECTION_SET = 210;
62            /** Constant for the tool "Selection add" which adds the features to the Selection (220). */
63            public static final int TOOL_SELECTION_ADD = 220;
64            /** Constant for the tool "Selection subtract" which removes the selected features from the selection (230). */
65            public static final int TOOL_SELECTION_REMOVE = 230;
66            /** Constant for the tool "Selection Reset" which clears the selection (240). */
67            public static final int TOOL_SELECTION_CLEAR = 240;
68            public static final int SEPERATOR2 = 299;
69    
70            /** Constant for the action "Search Labels" (120). */
71            public static final int ACTION_SEARCH = 300;
72    
73          /** Tool currently selected */          /** Tool currently selected */
74      protected int selectedTool = TOOL_ZOOMIN;      protected int selectedTool = TOOL_ZOOMIN;
75        
76      /** Holds the tool buttons of the tool bar. */      /** Holds the tool buttons of the tool bar. */
77      protected SortedMap<Integer, JToggleButton> toolButtons = null;      protected SortedMap<Integer, JComponent> toolAndActionButtons = null;
78      /** Controls that only one tool button is activated. */      /** Controls that only one tool button is activated. */
79      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;  
80    
81      /** Holds the action buttons of the bar. */  // SK: Musste ich ändern damit man Tools und Actions in der Reihenfolge mischen kann.    
82      protected SortedMap<Integer, JButton> actionButtons = null;  //    /** Holds the action buttons of the bar. */
83    //    protected SortedMap<Integer, JButton> actionButtons = null;
84    
85          /** Holds the {@link JMapPane} this tool bar controls. */          /** Holds the {@link JMapPane} this tool bar controls. */
86          protected JMapPane mapPane = null;          protected JMapPane mapPane = null;
# Line 91  public class MapPaneToolBar extends JToo Line 112  public class MapPaneToolBar extends JToo
112           */           */
113          public MapPaneToolBar(JMapPane mapPane) {          public MapPaneToolBar(JMapPane mapPane) {
114            super("Control the map", JToolBar.HORIZONTAL);            super("Control the map", JToolBar.HORIZONTAL);
115        this.toolButtons     = new TreeMap<Integer,JToggleButton>();        this.toolAndActionButtons  = new TreeMap<Integer,JComponent>();
116        this.toolButtonGroup = new ButtonGroup();        this.toolButtonGroup = new ButtonGroup();
       this.actionButtons   = new TreeMap<Integer,JButton>();  
117        // Create a Listener to sniff the zooms on the JMapPane        // Create a Listener to sniff the zooms on the JMapPane
118        this.mapPaneListener = new JMapPaneListener() {        this.mapPaneListener = new JMapPaneListener() {
119            public void performMapPaneEvent(JMapPaneEvent e) {            public void performMapPaneEvent(JMapPaneEvent e) {
# Line 153  public class MapPaneToolBar extends JToo Line 173  public class MapPaneToolBar extends JToo
173          }          }
174                    
175          /**          /**
176           * Calls {@link #initTools()} and {@link #initActions()} and then puts           * Calls {@link #initToolsAndActions()} and {@link #initActions()} and then puts
177           * all tool buttons and all actions buttons to the tool bar.           * all tool buttons and all actions buttons to the tool bar.
178           */           */
179          protected void init() {          protected void init() {
180            initTools();            initToolsAndActions();
181            initActions();        
182              addSeparator(SEPERATOR0, new JToolBar.Separator());
183          addSeparator(SEPERATOR1, new JToolBar.Separator());
184          addSeparator(SEPERATOR2, new JToolBar.Separator());
185          
186            initToolBar();            initToolBar();
187          }          }
188    
189    
190          /**          /**
191           * Creates the tool buttons, adds them to {@link #toolButtons} and finally           * Creates the tool buttons and action buttons and seperators, adds them to
192           * creates a button group for all tools. So sub-classes which override this           * {@link #toolAndActionButtons} and finally creates a button group for all tools.
193           * method should FIRST add their new tool buttons to {@link #toolButtons}           * So sub-classes which override this method should FIRST add their new tool
194           * before calling {@code super.initTools()}.           * buttons to {@link #toolAndActionButtons} before calling {@code super.initTools()}.
195           */           */
196          protected void initTools() {          protected void initToolsAndActions() {
197        // Panning        // Panning
198        addTool( new MapPaneToolBarAction(        addTool( new MapPaneToolBarAction(
199            TOOL_PAN,            TOOL_PAN,
200            this,            this,
201            "",            "",
202            new ImageIcon(MapView.class.getResource("pan.png"))            new ImageIcon(MapView.class.getResource("resource/icons/pan.png"))
203        ), false );        ), false );
204        // Info        // Info
205        addTool( new MapPaneToolBarAction(        addTool( new MapPaneToolBarAction(
206            TOOL_INFO,            TOOL_INFO,
207            this,            this,
208            "",            "",
209            new ImageIcon(MapView.class.getResource("info.png"))            new ImageIcon(MapView.class.getResource("resource/icons/info.png"))
210        ), false );        ), false );
211          
212    //      // Set Selection
213    //      addTool( new MapPaneToolBarAction(
214    //          TOOL_SELECTION_SET,
215    //          this,
216    //          "",
217    //          new ImageIcon(MapView.class.getResource("resource/icons/selection_set.png"))
218    //      ), false );
219    //
220    //      // Add Selection
221    //      addTool( new MapPaneToolBarAction(
222    //                TOOL_SELECTION_ADD,
223    //                this,
224    //                "",
225    //                new ImageIcon(MapView.class.getResource("resource/icons/selection_add.png"))
226    //      ), false );
227    //
228    //      // Remove Selection
229    //      addTool( new MapPaneToolBarAction(
230    //                TOOL_SELECTION_REMOVE,
231    //                this,
232    //                "",
233    //                new ImageIcon(MapView.class.getResource("resource/icons/selection_remove.png"))
234    //      ), false );
235    //      
236    //      // ResetSelection
237    //      addAction( new MapPaneToolBarAction(
238    //                TOOL_SELECTION_CLEAR,
239    //                this,
240    //                "",
241    //                new ImageIcon(MapView.class.getResource("resource/icons/selection_clear.png"))
242    //      ), false );
243          
244        // Zoom in        // Zoom in
245        addTool( new MapPaneToolBarAction(        addTool( new MapPaneToolBarAction(
246            TOOL_ZOOMIN,            TOOL_ZOOMIN,
247            this,            this,
248            "",            "",
249            new ImageIcon(MapView.class.getResource("zoom_in.png"))            new ImageIcon(MapView.class.getResource("resource/icons/zoom_in.png"))
250        ), false );        ), false );
251        // Zoom out        // Zoom out
252        addTool( new MapPaneToolBarAction(        addTool( new MapPaneToolBarAction(
253            TOOL_ZOOMOUT,            TOOL_ZOOMOUT,
254            this,            this,
255            "",            "",
256            new ImageIcon(MapView.class.getResource("zoom_out.png"))            new ImageIcon(MapView.class.getResource("resource/icons/zoom_out.png"))
257        ), false );        ), false );
258                
           // set the selected tool enabled  
       setSelectedTool(selectedTool);  
         
         }  
   
     /**  
      * Creates the action buttons and adds them to {@link #actionButtons}.  
      */  
     protected void initActions() {  
259        // Action button to revert the last zoom        // Action button to revert the last zoom
260        addAction( new MapPaneToolBarAction(        addAction( new MapPaneToolBarAction(
261            ACTION_ZOOM_BACK,            ACTION_ZOOM_BACK,
262            this,            this,
263            "",            "",
264            new ImageIcon(MapView.class.getResource("zoom_back.png"))            new ImageIcon(MapView.class.getResource("resource/icons/zoom_back.png"))
265        ), false);        ), false);
266        setButtonEnabled( ACTION_ZOOM_BACK, false );        setButtonEnabled( ACTION_ZOOM_BACK, false );
267    
# Line 222  public class MapPaneToolBar extends JToo Line 270  public class MapPaneToolBar extends JToo
270            ACTION_ZOOM_FORWARD,            ACTION_ZOOM_FORWARD,
271            this,            this,
272            "",            "",
273            new ImageIcon(MapView.class.getResource("zoom_forward.png"))            new ImageIcon(MapView.class.getResource("resource/icons/zoom_forward.png"))
274        ), false);        ), false);
275        setButtonEnabled( ACTION_ZOOM_FORWARD, false );        setButtonEnabled( ACTION_ZOOM_FORWARD, false );
276          
277          
278              // set the selected tool enabled
279          setSelectedTool(selectedTool);
280          
281            }
282    
283      }  
284                /**
     /**  
285       * 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
286       * tool bar.       * tool bar.
287       */       */
# Line 240  public class MapPaneToolBar extends JToo Line 293  public class MapPaneToolBar extends JToo
293        Dimension dimension = new Dimension( 49,10);        Dimension dimension = new Dimension( 49,10);
294        addSeparator(dimension);        addSeparator(dimension);
295        // Tool buttons        // Tool buttons
296        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())  
297          add(b);          add(b);
298      }      }
299          // Space between tool buttons and action buttons
300    // SK: Seperators are now als manages like actions and tools      
301    //      Dimension dimension2 = new Dimension( 10,10);
302    //      this.addSeparator(dimension2);
303          
304    //      // Action buttons
305    //      for (JButton b : actionButtons.values())
306    //        add(b);
307    //    }
308            
309          /**          /**
310           * Performs the activation of a tool.           * Performs the activation of a tool.
# Line 272  public class MapPaneToolBar extends JToo Line 328  public class MapPaneToolBar extends JToo
328          case TOOL_INFO:          case TOOL_INFO:
329            // Set the mouse tool to "Info"            // Set the mouse tool to "Info"
330            mapPane.setWindowSelectionState(JMapPane.NONE);            mapPane.setWindowSelectionState(JMapPane.NONE);
331            mapPane.setState(JMapPane.SELECT_TOP);            mapPane.setState(JMapPane.SELECT_TOP); // Why not: JMapPane.SELECT_TOP_ONEONLY
332            mapPane.setHighlight(true);            mapPane.setHighlight(false);// SK: Was true, but since it not works properly removed it to save performance
333            mapPane.setNormalCursor(SwingUtil.CROSSHAIR_CURSOR);            mapPane.setNormalCursor(SwingUtil.CROSSHAIR_CURSOR);
334            break;            break;
335            case TOOL_SELECTION_SET:
336            case TOOL_SELECTION_ADD:
337            case TOOL_SELECTION_REMOVE:
338                    // Set the mouse tool to "Select"
339                    mapPane.setWindowSelectionState(JMapPane.SELECT_TOP);
340                    mapPane.setState(JMapPane.SELECT_TOP);
341                    mapPane.setHighlight(false);
342                    mapPane.setNormalCursor(SwingUtil.CROSSHAIR_CURSOR); // TODO Select Cursor
343                    break;
344          case TOOL_ZOOMIN:          case TOOL_ZOOMIN:
345            // Set the mouse tool to "Zoom in"            // Set the mouse tool to "Zoom in"
346            mapPane.setWindowSelectionState(JMapPane.ZOOM_IN);            mapPane.setWindowSelectionState(JMapPane.ZOOM_IN);
# Line 336  public class MapPaneToolBar extends JToo Line 401  public class MapPaneToolBar extends JToo
401            mapPane.refresh();            mapPane.refresh();
402          }          }
403        }        }
404          
405          /**
406           * Clear the selection by calling the selection model
407           */
408          if (action == TOOL_SELECTION_CLEAR) {
409              // TODO
410          }
411          }          }
412                    
413                    
# Line 355  public class MapPaneToolBar extends JToo Line 427  public class MapPaneToolBar extends JToo
427            JToggleButton button = new JToggleButton(buttonAction);            JToggleButton button = new JToggleButton(buttonAction);
428            button.setBorder( BorderFactory.createRaisedBevelBorder() );            button.setBorder( BorderFactory.createRaisedBevelBorder() );
429            toolButtonGroup.add(button);            toolButtonGroup.add(button);
430            toolButtons.put(buttonAction.getID(), button);            toolAndActionButtons.put(buttonAction.getID(), button);
431            if ( resetToolBar )            if ( resetToolBar )
432              initToolBar();              initToolBar();
433          }          }
# Line 382  public class MapPaneToolBar extends JToo Line 454  public class MapPaneToolBar extends JToo
454          return;          return;
455        }        }
456        JButton button = new JButton(buttonAction);        JButton button = new JButton(buttonAction);
457        actionButtons.put( buttonAction.getID(), button );        button.setBorder( BorderFactory.createRaisedBevelBorder() );
458          toolAndActionButtons.put( buttonAction.getID(), button );
459        if ( resetToolBar )        if ( resetToolBar )
460          initToolBar();          initToolBar();
461      }      }
462        
463    
464        private void addSeparator(int id, Separator separator) {
465            if ( isButtonIDUsed(id) ) {
466                LOGGER.warn("addSeparator(.) ignored because ID already used for tool or action. ");
467                return;
468              }
469              toolAndActionButtons.put( id, separator);
470            }
471    
472      /**      /**
473       * 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 479  public class MapPaneToolBar extends JToo
479            
480      /**      /**
481       * Returns the button for a specific tool or action.       * Returns the button for a specific tool or action.
482       * @param id the constant for a tool       * @param id the constant for any button in the {@link MapPaneToolBar}
483       * @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}
484       *         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}
485       */       */
486      public AbstractButton getButton(int id) {      public AbstractButton getButton(int id) {
487        AbstractButton button = toolButtons.get(id);        AbstractButton button = (AbstractButton)toolAndActionButtons.get(id);
       if ( button == null )  
         button = actionButtons.get(id);  
488        if ( button == null )        if ( button == null )
489          LOGGER.warn("Unknown tool or action ID: "+id);          LOGGER.warn("Unknown tool or action ID: "+id);
490        return button;        return button;
# Line 500  public class MapPaneToolBar extends JToo Line 580  public class MapPaneToolBar extends JToo
580       * @param tool tool ID       * @param tool tool ID
581       */       */
582      public boolean isButtonIDUsed(int id) {      public boolean isButtonIDUsed(int id) {
583        return toolButtons.get(id) != null || actionButtons.get(id) != null;        return toolAndActionButtons.get(id) != null;
584      }      }
585    
586      /**      /**
# Line 522  public class MapPaneToolBar extends JToo Line 602  public class MapPaneToolBar extends JToo
602       *                      {@code enabled} is {@code false}       *                      {@code enabled} is {@code false}
603       */       */
604      public void setAllToolsEnabled(boolean enabled, boolean hideOnDisable) {      public void setAllToolsEnabled(boolean enabled, boolean hideOnDisable) {
605        for (int tool : toolButtons.keySet())        for (int tool : toolAndActionButtons.keySet())
606          setButtonEnabled(tool,enabled,hideOnDisable);          setButtonEnabled(tool,enabled,hideOnDisable);
607      }        }  
608    
# Line 533  public class MapPaneToolBar extends JToo Line 613  public class MapPaneToolBar extends JToo
613       *                      {@code enabled} is {@code false}       *                      {@code enabled} is {@code false}
614       */       */
615      public void setAllActionsEnabled(boolean enabled, boolean hideOnDisable) {      public void setAllActionsEnabled(boolean enabled, boolean hideOnDisable) {
616        for (int tool : actionButtons.keySet())        for (int id : toolAndActionButtons.keySet()){
617          setButtonEnabled(tool,enabled,hideOnDisable);            if (toolAndActionButtons.get(id) instanceof JButton){
618                      setButtonEnabled(id,enabled,hideOnDisable);
619              }
620          }
621            
622      }        }  
623            
624      /**      /**
625       * Returns the maximum ID of tools.       * Returns the maximum ID of tools.
626       */       */
627      public int getMaxToolID() {      public int getMaxToolID() {
628        return toolButtons.lastKey();        return toolAndActionButtons.lastKey();
629      }      }
630    
631      /**      /**
632       * Returns the minimum ID of tools.       * Returns the minimum ID of tools.
633       */       */
634      public int getMinToolID() {      public int getMinToolID() {
635        return toolButtons.firstKey();        return toolAndActionButtons.firstKey();
     }  
   
     /**  
      * 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      }      }
637    //
638    //    /**
639    //     * Returns the maximum ID of actions.
640    //     */
641    //    public int getMaxActionID() {
642    //      return actionButtons.lastKey();
643    //    }
644    //
645    //    /**
646    //     * Returns the minimum ID of actions.
647    //     */
648    //    public int getMinActionID() {
649    //      return actionButtons.firstKey();
650    //    }
651            
652      /**      /**
653       * Extends the {@link AbstractAction} with maintaining an ID and       * Extends the {@link AbstractAction} with maintaining an ID and
# Line 620  public class MapPaneToolBar extends JToo Line 704  public class MapPaneToolBar extends JToo
704         * or {@link MapPaneToolBar#addAction(MapPaneToolBarAction)}.         * or {@link MapPaneToolBar#addAction(MapPaneToolBarAction)}.
705         */         */
706        public void actionPerformed(ActionEvent e) {        public void actionPerformed(ActionEvent e) {
707          if ( toolBar.toolButtons.get(id) != null )          if ( toolBar.toolAndActionButtons.get(id) instanceof JToggleButton)
708            toolBar.performToolButton(id, e);            toolBar.performToolButton(id, e);
709          if ( toolBar.actionButtons.get(id) != null )          else if ( toolBar.toolAndActionButtons.get(id) instanceof JButton )
710            toolBar.performActionButton(id, e);            toolBar.performActionButton(id, e);
711        }        }
712                

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26