/[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 111 by alfonx, Tue May 12 23:33:14 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 );
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 );        ), 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 355  public class MapPaneToolBar extends JToo Line 420  public class MapPaneToolBar extends JToo
420            JToggleButton button = new JToggleButton(buttonAction);            JToggleButton button = new JToggleButton(buttonAction);
421            button.setBorder( BorderFactory.createRaisedBevelBorder() );            button.setBorder( BorderFactory.createRaisedBevelBorder() );
422            toolButtonGroup.add(button);            toolButtonGroup.add(button);
423            toolButtons.put(buttonAction.getID(), button);            toolAndActionButtons.put(buttonAction.getID(), button);
424            if ( resetToolBar )            if ( resetToolBar )
425              initToolBar();              initToolBar();
426          }          }
# Line 382  public class MapPaneToolBar extends JToo Line 447  public class MapPaneToolBar extends JToo
447          return;          return;
448        }        }
449        JButton button = new JButton(buttonAction);        JButton button = new JButton(buttonAction);
450        actionButtons.put( buttonAction.getID(), button );        button.setBorder( BorderFactory.createRaisedBevelBorder() );
451          toolAndActionButtons.put( buttonAction.getID(), button );
452        if ( resetToolBar )        if ( resetToolBar )
453          initToolBar();          initToolBar();
454      }      }
455        
456    
457        private void addSeparator(int id, Separator separator) {
458            if ( isButtonIDUsed(id) ) {
459                LOGGER.warn("addSeparator(.) ignored because ID already used for tool or action. ");
460                return;
461              }
462              toolAndActionButtons.put( id, separator);
463            }
464    
465      /**      /**
466       * 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 472  public class MapPaneToolBar extends JToo
472            
473      /**      /**
474       * Returns the button for a specific tool or action.       * Returns the button for a specific tool or action.
475       * @param id the constant for a tool       * @param id the constant for any button in the {@link MapPaneToolBar}
476       * @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}
477       *         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}
478       */       */
479      public AbstractButton getButton(int id) {      public AbstractButton getButton(int id) {
480        AbstractButton button = toolButtons.get(id);        AbstractButton button = (AbstractButton)toolAndActionButtons.get(id);
       if ( button == null )  
         button = actionButtons.get(id);  
481        if ( button == null )        if ( button == null )
482          LOGGER.warn("Unknown tool or action ID: "+id);          LOGGER.warn("Unknown tool or action ID: "+id);
483        return button;        return button;
# Line 500  public class MapPaneToolBar extends JToo Line 573  public class MapPaneToolBar extends JToo
573       * @param tool tool ID       * @param tool tool ID
574       */       */
575      public boolean isButtonIDUsed(int id) {      public boolean isButtonIDUsed(int id) {
576        return toolButtons.get(id) != null || actionButtons.get(id) != null;        return toolAndActionButtons.get(id) != null;
577      }      }
578    
579      /**      /**
# Line 522  public class MapPaneToolBar extends JToo Line 595  public class MapPaneToolBar extends JToo
595       *                      {@code enabled} is {@code false}       *                      {@code enabled} is {@code false}
596       */       */
597      public void setAllToolsEnabled(boolean enabled, boolean hideOnDisable) {      public void setAllToolsEnabled(boolean enabled, boolean hideOnDisable) {
598        for (int tool : toolButtons.keySet())        for (int tool : toolAndActionButtons.keySet())
599          setButtonEnabled(tool,enabled,hideOnDisable);          setButtonEnabled(tool,enabled,hideOnDisable);
600      }        }  
601    
# Line 533  public class MapPaneToolBar extends JToo Line 606  public class MapPaneToolBar extends JToo
606       *                      {@code enabled} is {@code false}       *                      {@code enabled} is {@code false}
607       */       */
608      public void setAllActionsEnabled(boolean enabled, boolean hideOnDisable) {      public void setAllActionsEnabled(boolean enabled, boolean hideOnDisable) {
609        for (int tool : actionButtons.keySet())        for (int id : toolAndActionButtons.keySet()){
610          setButtonEnabled(tool,enabled,hideOnDisable);            if (toolAndActionButtons.get(id) instanceof JButton){
611                      setButtonEnabled(id,enabled,hideOnDisable);
612              }
613          }
614            
615      }        }  
616            
617      /**      /**
618       * Returns the maximum ID of tools.       * Returns the maximum ID of tools.
619       */       */
620      public int getMaxToolID() {      public int getMaxToolID() {
621        return toolButtons.lastKey();        return toolAndActionButtons.lastKey();
622      }      }
623    
624      /**      /**
625       * Returns the minimum ID of tools.       * Returns the minimum ID of tools.
626       */       */
627      public int getMinToolID() {      public int getMinToolID() {
628        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();  
629      }      }
630    //
631    //    /**
632    //     * Returns the maximum ID of actions.
633    //     */
634    //    public int getMaxActionID() {
635    //      return actionButtons.lastKey();
636    //    }
637    //
638    //    /**
639    //     * Returns the minimum ID of actions.
640    //     */
641    //    public int getMinActionID() {
642    //      return actionButtons.firstKey();
643    //    }
644            
645      /**      /**
646       * 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 697  public class MapPaneToolBar extends JToo
697         * or {@link MapPaneToolBar#addAction(MapPaneToolBarAction)}.         * or {@link MapPaneToolBar#addAction(MapPaneToolBarAction)}.
698         */         */
699        public void actionPerformed(ActionEvent e) {        public void actionPerformed(ActionEvent e) {
700          if ( toolBar.toolButtons.get(id) != null )          if ( toolBar.toolAndActionButtons.get(id) instanceof JToggleButton)
701            toolBar.performToolButton(id, e);            toolBar.performToolButton(id, e);
702          if ( toolBar.actionButtons.get(id) != null )          else if ( toolBar.toolAndActionButtons.get(id) instanceof JButton )
703            toolBar.performActionButton(id, e);            toolBar.performActionButton(id, e);
704        }        }
705                

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26