/[schmitzm]/branches/1.0-gt2-2.6/src/skrueger/geotools/MapPaneToolBar.java
ViewVC logotype

Diff of /branches/1.0-gt2-2.6/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 177 by alfonx, Wed Jul 1 15:02:39 2009 UTC
# Line 3  package skrueger.geotools; Line 3  package skrueger.geotools;
3  import java.awt.Dimension;  import java.awt.Dimension;
4  import java.awt.event.ActionEvent;  import java.awt.event.ActionEvent;
5  import java.util.ArrayList;  import java.util.ArrayList;
6    import java.util.Locale;
7  import java.util.SortedMap;  import java.util.SortedMap;
8  import java.util.TreeMap;  import java.util.TreeMap;
9    
10  import javax.swing.AbstractAction;  import javax.swing.AbstractAction;
11  import javax.swing.AbstractButton;  import javax.swing.AbstractButton;
12    import javax.swing.Action;
13  import javax.swing.BorderFactory;  import javax.swing.BorderFactory;
14  import javax.swing.Icon;  import javax.swing.Icon;
15  import javax.swing.ImageIcon;  import javax.swing.ImageIcon;
16  import javax.swing.JButton;  import javax.swing.JButton;
17    import javax.swing.JComponent;
18  import javax.swing.JToggleButton;  import javax.swing.JToggleButton;
19  import javax.swing.JToolBar;  import javax.swing.JToolBar;
20    
# Line 21  import schmitzm.geotools.gui.JMapPane; Line 24  import schmitzm.geotools.gui.JMapPane;
24  import schmitzm.geotools.map.event.JMapPaneEvent;  import schmitzm.geotools.map.event.JMapPaneEvent;
25  import schmitzm.geotools.map.event.JMapPaneListener;  import schmitzm.geotools.map.event.JMapPaneListener;
26  import schmitzm.geotools.map.event.MapAreaChangedEvent;  import schmitzm.geotools.map.event.MapAreaChangedEvent;
27    import schmitzm.lang.LangUtil;
28    import schmitzm.lang.ResourceProvider;
29  import schmitzm.swing.ButtonGroup;  import schmitzm.swing.ButtonGroup;
30  import schmitzm.swing.SwingUtil;  import schmitzm.swing.SwingUtil;
31    
32  import com.vividsolutions.jts.geom.Envelope;  import com.vividsolutions.jts.geom.Envelope;
33    
34  /**  /**
35   * A toolbar to controll a {@link JMapPane} (Atlas visualization). This contains two types   * A toolbar to control an {@link JMapPane} (Atlas visualization). This contains
36   * 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
37   * by {@link JToggleButton JToggleButtons}, where only one tool can be activated   * map represented by {@link JToggleButton JToggleButtons}, where only one tool
38   * every time. And some (general) <i>actions</i>, represented by normal   * can be activated every time. And some (general) <i>actions</i>, represented
39   * {@link JButton JButtons}.   * by normal {@link JButton JButtons}.
40   * @author <a href="mailto:[email protected]">Martin Schmitz</a> (University of Bonn/Germany)   *
41     * @author <a href="mailto:[email protected]">Martin Schmitz</a>
42     *         (University of Bonn/Germany)
43     * @version 1.2 Stefan Krüger
44   */   */
45  public class MapPaneToolBar extends JToolBar {  public class MapPaneToolBar extends JToolBar {
46          private static final Logger LOGGER = Logger.getLogger(MapPaneToolBar.class.getName());          private static final Logger LOGGER = Logger.getLogger(MapPaneToolBar.class
47                            .getName());
48            
49            public static ResourceProvider RESOURCE = new ResourceProvider(LangUtil
50                            .extendPackagePath(MapPaneToolBar.class,
51                                            "resource.locales.mapPaneToolbar"), Locale.ENGLISH);
52            
53            public static String R(String key, Object... values) {
54                    return RESOURCE.getString(key, values);
55            }
56    
57          /** Constant for the tool "Panning" (10). */          /** Constant for the tool "Panning" (10). */
58          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;  
59          /** Constant for the tool "Info" (20). */          /** Constant for the tool "Info" (20). */
60          public static final int TOOL_INFO = 20;          public static final int TOOL_INFO = 20;
61            public static final int SEPERATOR0 = 99;
62    
63            /** Constant for the tool "Zoom In" (110). */
64            public static final int TOOL_ZOOMIN = 110;
65            /** Constant for the tool "Zoom Out" (120). */
66            public static final int TOOL_ZOOMOUT = 120;
67            /** Constant for the action "Zoom back" (130). */
68            public static final int ACTION_ZOOM_BACK = 130;
69            /** Constant for the action "Zoom forward" (140). */
70            public static final int ACTION_ZOOM_FORWARD = 140;
71            public static final int SEPERATOR1 = 199;
72    
73            /**
74             * Constant for the tool "Selection Reset" which clears the selection (240).
75             */
76            public static final int TOOL_SELECTION_CLEAR = 240;
77    
78            /**
79             * Constant for the tool "Select" which sets the Selection to the selected
80             * features (210).
81             */
82            public static final int TOOL_SELECTION_SET = 210;
83            /**
84             * Constant for the tool "Selection add" which adds the features to the
85             * Selection (220).
86             */
87            public static final int TOOL_SELECTION_ADD = 220;
88            /**
89             * Constant for the tool "Selection subtract" which removes the selected
90             * features from the selection (230).
91             */
92            public static final int TOOL_SELECTION_REMOVE = 230;
93    
94          /** Tool currently selected */          /** Tool currently selected */
95      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;  
96    
97      /** Holds the action buttons of the bar. */          /** Holds the tool buttons of the tool bar. */
98      protected SortedMap<Integer, JButton> actionButtons = null;          protected SortedMap<Integer, JComponent> toolAndActionButtons = null;
99            /** Controls that only one tool button is activated. */
100            protected ButtonGroup toolButtonGroup = null;
101    
102            // SK: Musste ich ändern damit man Tools und Actions in der Reihenfolge
103            // mischen kann.
104            // /** Holds the action buttons of the bar. */
105            // protected SortedMap<Integer, JButton> actionButtons = null;
106    
107          /** Holds the {@link JMapPane} this tool bar controls. */          /** Holds the {@link JMapPane} this tool bar controls. */
108          protected JMapPane mapPane = null;          protected JMapPane mapPane = null;
# Line 74  public class MapPaneToolBar extends JToo Line 117  public class MapPaneToolBar extends JToo
117    
118          /** Listener to sniff the zoom actions on the map. */          /** Listener to sniff the zoom actions on the map. */
119          protected JMapPaneListener mapPaneListener = null;          protected JMapPaneListener mapPaneListener = null;
120            
121          protected boolean zoomBackForwardButtonInAction;          protected boolean zoomBackForwardButtonInAction;
122    
123      /**          /**
124       * Creates a new toolbar. Notice: This toolbar does nothing           * Creates a new toolbar. Notice: This toolbar does nothing until
125       * until {@link #setMapPane(JMapPane)} is called!           * {@link #setMapPane(JMapPane)} is called!
126       */           */
127      public MapPaneToolBar() {          public MapPaneToolBar() {
128        this(null);                  this(null);
129      }          }
130        
131      /**          /**
132           * Creates a new tool bar.           * Creates a new tool bar.
133           * @param mapPane {@link JMapPane} the tool bar controls           *
134             * @param mapPane
135             *            {@link JMapPane} the tool bar controls
136           */           */
137          public MapPaneToolBar(JMapPane mapPane) {          public MapPaneToolBar(JMapPane mapPane) {
138            super("Control the map", JToolBar.HORIZONTAL);                  super("Control the map", JToolBar.HORIZONTAL);
139        this.toolButtons     = new TreeMap<Integer,JToggleButton>();                  this.toolAndActionButtons = new TreeMap<Integer, JComponent>();
140        this.toolButtonGroup = new ButtonGroup();                  this.toolButtonGroup = new ButtonGroup();
141        this.actionButtons   = new TreeMap<Integer,JButton>();                  
142        // Create a Listener to sniff the zooms on the JMapPane                  // Create a Listener to listen to the zooms on the JMapPane
143        this.mapPaneListener = new JMapPaneListener() {                  this.mapPaneListener = new JMapPaneListener() {
144            public void performMapPaneEvent(JMapPaneEvent e) {                          public void performMapPaneEvent(JMapPaneEvent e) {
145                if ( !(e instanceof MapAreaChangedEvent) )                                  if (!(e instanceof MapAreaChangedEvent))
146                  return;                                          return;
147                    
148                if ( zoomBackForwardButtonInAction ) {                                  if (zoomBackForwardButtonInAction) {
149                  zoomBackForwardButtonInAction = false;                                          zoomBackForwardButtonInAction = false;
150                  return;                                          return;
151                }                                  }
152                
153                Envelope oldMapArea = ((MapAreaChangedEvent)e).getOldMapArea();                                  final MapAreaChangedEvent mapAreaChangedEvent = (MapAreaChangedEvent) e;
154                if (lastZooms.size() == 0 && oldMapArea != null ) {                                  Envelope oldMapArea = mapAreaChangedEvent.getOldMapArea();
155                  lastZooms.add(oldMapArea);                                  
156                  zoomBackIndex = 1;                                  final Envelope mapArea = mapAreaChangedEvent.getNewMapArea();
157                }                                  if (mapArea == null || mapArea.equals(oldMapArea) ) {
158                                            // If the MapArea didn't change... we don't want to register it as a zoom action.
159                final Envelope mapArea = ((MapAreaChangedEvent)e).getNewMapArea();                                          return;
160                if (mapArea == null)                                  }
161                  return;                                  
162                                                  if (lastZooms.size() == 0 && oldMapArea != null) {
163                if (lastZooms.size() > 0 && mapArea.equals(lastZooms.get(lastZooms.size() - 1))) {                                          lastZooms.add(oldMapArea);
164                  // LOGGER.debug("MapAreaChangedEvent ausgelassen bei der Zaehlung der Zoomschritt weil identisch");                                          zoomBackIndex = 1;
165                  return;                                  }
166                }                                  if (mapArea == null)
167                                            return;
168                if (lastZooms.size() > 0)  
169                  while (zoomBackIndex < lastZooms.size())                                  if (lastZooms.size() > 0
170                    lastZooms.remove(lastZooms.size() - 1);                                                  && mapArea.equals(lastZooms.get(lastZooms.size() - 1))) {
171                                            // LOGGER.debug("MapAreaChangedEvent ausgelassen bei der Zaehlung der Zoomschritt weil identisch");
172                lastZooms.add(mapArea);                                          return;
173                zoomBackIndex = lastZooms.size();                                  }
174                setButtonEnabled(ACTION_ZOOM_BACK, lastZooms.size() > 1);  
175                setButtonEnabled(ACTION_ZOOM_FORWARD, false);                                  if (lastZooms.size() > 0)
176            }                                          while (zoomBackIndex < lastZooms.size())
177        };                                                      lastZooms.remove(lastZooms.size() - 1);
178        
179        setMapPane(mapPane);                                  lastZooms.add(mapArea);
180            setFloatable(false);                                  zoomBackIndex = lastZooms.size();
181            setRollover(true);                                  setButtonEnabled(ACTION_ZOOM_BACK, lastZooms.size() > 1);
182                                              setButtonEnabled(ACTION_ZOOM_FORWARD, false);
183            init();                          }
184                    };
185    
186                    setMapPane(mapPane);
187                    setFloatable(false);
188                    setRollover(true);
189    
190                    init();
191          }          }
192            
193          /**          /**
194           * Sets the {@link JMapPane} controlled by this tool bar.           * Sets the {@link JMapPane} controlled by this tool bar.
195           * @param mapPane {@link JMapPane} to control (if {@code null} this           *
196           *                tool bar controls NOTHING!)           * @param mapPane
197             *            {@link JMapPane} to control (if {@code null} this tool bar
198             *            controls NOTHING!)
199           */           */
200          public void setMapPane(JMapPane mapPane) {          public void setMapPane(JMapPane mapPane) {
201            // Remove listener from old MapPane                  // Remove listener from old MapPane
202            if ( this.mapPane != null )                  if (this.mapPane != null)
203              this.mapPane.removeMapPaneListener( mapPaneListener );                          this.mapPane.removeMapPaneListener(mapPaneListener);
204        this.mapPane = mapPane;                  this.mapPane = mapPane;
205        if ( this.mapPane != null && mapPaneListener != null )                  if (this.mapPane != null && mapPaneListener != null)
206          this.mapPane.addMapPaneListener( mapPaneListener );                          this.mapPane.addMapPaneListener(mapPaneListener);
207          }          }
208            
209          /**          /**
210           * Calls {@link #initTools()} and {@link #initActions()} and then puts           * Calls {@link #initToolsAndActions()} and {@link #initActions()} and then
211           * all tool buttons and all actions buttons to the tool bar.           * puts all tool buttons and all actions buttons to the tool bar.
212           */           */
213          protected void init() {          protected void init() {
214            initTools();                  initToolsAndActions();
215            initActions();  
216            initToolBar();                  addSeparator(SEPERATOR0, new JToolBar.Separator());
217          }                  addSeparator(SEPERATOR1, new JToolBar.Separator());
218    
219                    initToolBar();
220          /**          }
221           * Creates the tool buttons, adds them to {@link #toolButtons} and finally  
222           * creates a button group for all tools. So sub-classes which override this          /**
223           * method should FIRST add their new tool buttons to {@link #toolButtons}           * Creates the tool buttons and action buttons and seperators, adds them to
224           * before calling {@code super.initTools()}.           * {@link #toolAndActionButtons} and finally creates a button group for all
225           */           * tools. So sub-classes which override this method should FIRST add their
226          protected void initTools() {           * new tool buttons to {@link #toolAndActionButtons} before calling {@code
227        // Panning           * super.initTools()}.
228        addTool( new MapPaneToolBarAction(           */
229            TOOL_PAN,          protected void initToolsAndActions() {
230            this,                  // Panning
231            "",                  addTool(new MapPaneToolBarAction(TOOL_PAN, this, "", new ImageIcon(
232            new ImageIcon(MapView.class.getResource("pan.png"))                                  MapView.class.getResource("resource/icons/pan.png")), R("MapPaneButtons.Pan.TT")), false);
233        ), false );                  // Info
234        // Info                  addTool(new MapPaneToolBarAction(TOOL_INFO, this, "", new ImageIcon(
235        addTool( new MapPaneToolBarAction(                                  MapView.class.getResource("resource/icons/info.png")),R("MapPaneButtons.Info.TT")), false);
236            TOOL_INFO,  
237            this,                  // Zoom in
238            "",                  addTool(new MapPaneToolBarAction(TOOL_ZOOMIN, this, "", new ImageIcon(
239            new ImageIcon(MapView.class.getResource("info.png"))                                  MapView.class.getResource("resource/icons/zoom_in.png")), R("MapPaneButtons.ZoomIn.TT")),
240        ), false );                                  false);
241        // Zoom in                  // Zoom out
242        addTool( new MapPaneToolBarAction(                  addTool(new MapPaneToolBarAction(TOOL_ZOOMOUT, this, "", new ImageIcon(
243            TOOL_ZOOMIN,                                  MapView.class.getResource("resource/icons/zoom_out.png")), R("MapPaneButtons.ZoomOut.TT")),
244            this,                                  false);
245            "",  
246            new ImageIcon(MapView.class.getResource("zoom_in.png"))                  // Action button to revert the last zoom
247        ), false );                  addAction(new MapPaneToolBarAction(ACTION_ZOOM_BACK, this, "",
248        // Zoom out                                  new ImageIcon(MapView.class
249        addTool( new MapPaneToolBarAction(                                                  .getResource("resource/icons/zoom_back.png")), R("MapPaneButtons.LastZoom.TT")), false);
250            TOOL_ZOOMOUT,                  setButtonEnabled(ACTION_ZOOM_BACK, false);
251            this,  
252            "",                  // Action button to redo the last zoom
253            new ImageIcon(MapView.class.getResource("zoom_out.png"))                  addAction(new MapPaneToolBarAction(ACTION_ZOOM_FORWARD, this, "",
254        ), false );                                  new ImageIcon(MapView.class
255                                                          .getResource("resource/icons/zoom_forward.png")), R("MapPaneButtons.NextZoom.TT")),
256            // set the selected tool enabled                                  false);
257        setSelectedTool(selectedTool);                  setButtonEnabled(ACTION_ZOOM_FORWARD, false);
258          
259          }                  // set the selected tool enabled
260                    setSelectedTool(selectedTool);
261      /**  
262       * Creates the action buttons and adds them to {@link #actionButtons}.          }
263       */  
264      protected void initActions() {          /**
265        // Action button to revert the last zoom           * Clears the GUI of all components and adds all tool and action buttons to
266        addAction( new MapPaneToolBarAction(           * the tool bar.
267            ACTION_ZOOM_BACK,           */
268            this,          public void initToolBar() {
269            "",                  setAlignmentY(1f);
270            new ImageIcon(MapView.class.getResource("zoom_back.png"))                  removeAll();
271        ), false);                  // Separator to the left of the tool actions to start
272        setButtonEnabled( ACTION_ZOOM_BACK, false );                  // the tool buttons with the map (not with the coordinate grid)
273                    Dimension dimension = new Dimension(49, 10);
274        // Action button to redo the last zoom                  addSeparator(dimension);
275        addAction( new MapPaneToolBarAction(                  // Tool buttons
276            ACTION_ZOOM_FORWARD,                  for (JComponent b : toolAndActionButtons.values())
277            this,                          add(b);
278            "",  
279            new ImageIcon(MapView.class.getResource("zoom_forward.png"))                  if (!toolAndActionButtons.containsKey(selectedTool)) {
280        ), false);                          /**
281        setButtonEnabled( ACTION_ZOOM_FORWARD, false );                           * This might be a bit specific, but IF selection buttons are
282                             * available, select one of them.. if not, select the INFO tool.
283      }                           */
284        
285      /**                          if (toolAndActionButtons.containsKey(TOOL_SELECTION_SET)) {
286       * Clears the GUI of all components and adds all tool and action buttons to the                                  setSelectedTool(TOOL_SELECTION_SET);
287       * tool bar.                          } else if (toolAndActionButtons.containsKey(TOOL_INFO)) {
288       */                                  setSelectedTool(TOOL_INFO);
289      protected void initToolBar() {                          } else {
290        setAlignmentY( 1f );                                  // TODO What to do now?!
291        removeAll();                                  setSelectedTool(null);
292        // Separator to the left of the tool actions to start                          }
293        // the tool buttons with the map (not with the coordinate grid)  
294        Dimension dimension = new Dimension( 49,10);                  }
295        addSeparator(dimension);                  
296        // Tool buttons                  revalidate();
297        for (JToggleButton b : toolButtons.values())                  repaint();
298          add(b);          }
299        // Space between tool buttons and action buttons  
300        Dimension dimension2 = new Dimension( 10,10);          // Space between tool buttons and action buttons
301        this.addSeparator(dimension2);          // SK: Seperators are now als manages like actions and tools
302        // Action buttons          // Dimension dimension2 = new Dimension( 10,10);
303        for (JButton b : actionButtons.values())          // this.addSeparator(dimension2);
304          add(b);  
305      }          // // Action buttons
306                // for (JButton b : actionButtons.values())
307            // add(b);
308            // }
309    
310          /**          /**
311           * Performs the activation of a tool.           * Performs the activation of a tool.
312           * @param tool the tool to activate           *
313           * @param e    the event of the button           * @param tool
314             *            the tool to activate
315             * @param e
316             *            the event of the button
317           */           */
318          public void performToolButton(int tool, ActionEvent e) {          public void performToolButton(int tool, ActionEvent e) {
319            if ( mapPane == null )                  if (mapPane == null)
320              return;                          return;
321              
322            selectedTool = tool;                  selectedTool = tool;
323              
324        switch( tool ) {                  switch (tool) {
325          case TOOL_PAN:                  case TOOL_PAN:
326            // Set the mouse tool to "Panning"                          // Set the mouse tool to "Panning"
327            mapPane.setWindowSelectionState(JMapPane.NONE);                          mapPane.setWindowSelectionState(JMapPane.NONE);
328            mapPane.setState(JMapPane.PAN);                          mapPane.setState(JMapPane.PAN);
329            mapPane.setHighlight(false);                          mapPane.setHighlight(false);
330            mapPane.setNormalCursor(SwingUtil.PAN_CURSOR);                          mapPane.setNormalCursor(SwingUtil.PAN_CURSOR);
331            break;                          break;
332          case TOOL_INFO:                  case TOOL_INFO:
333            // Set the mouse tool to "Info"                          // Set the mouse tool to "Info"
334            mapPane.setWindowSelectionState(JMapPane.NONE);                          mapPane.setWindowSelectionState(JMapPane.NONE);
335            mapPane.setState(JMapPane.SELECT_TOP);                          mapPane.setState(JMapPane.SELECT_TOP); // Why not:
336            mapPane.setHighlight(true);                          // JMapPane.SELECT_TOP_ONEONLY
337            mapPane.setNormalCursor(SwingUtil.CROSSHAIR_CURSOR);                          mapPane.setHighlight(false);// SK: Was true, but since it not works
338            break;                          // properly removed it to save
339          case TOOL_ZOOMIN:                          // performance
340            // Set the mouse tool to "Zoom in"                          mapPane.setNormalCursor(SwingUtil.CROSSHAIR_CURSOR);
341            mapPane.setWindowSelectionState(JMapPane.ZOOM_IN);                          break;
342            mapPane.setState(JMapPane.ZOOM_IN);                  case TOOL_ZOOMIN:
343            mapPane.setHighlight(false);                          // Set the mouse tool to "Zoom in"
344            mapPane.setNormalCursor(SwingUtil.ZOOMIN_CURSOR);                          mapPane.setWindowSelectionState(JMapPane.ZOOM_IN);
345            break;                          mapPane.setState(JMapPane.ZOOM_IN);
346          case TOOL_ZOOMOUT:                          mapPane.setHighlight(false);
347            // Set the mouse tool to "Zoom out"                          mapPane.setNormalCursor(SwingUtil.ZOOMIN_CURSOR);
348            mapPane.setWindowSelectionState(JMapPane.NONE);                          break;
349            mapPane.setState(JMapPane.ZOOM_OUT);                  case TOOL_ZOOMOUT:
350            mapPane.setHighlight(false);                          // Set the mouse tool to "Zoom out"
351            mapPane.setNormalCursor(SwingUtil.ZOOMOUT_CURSOR);                          mapPane.setWindowSelectionState(JMapPane.NONE);
352            break;                          mapPane.setState(JMapPane.ZOOM_OUT);
353          default:                          mapPane.setHighlight(false);
354            // Set map actions to default                          mapPane.setNormalCursor(SwingUtil.ZOOMOUT_CURSOR);
355            mapPane.setWindowSelectionState(JMapPane.NONE);                          break;
356            mapPane.setState(JMapPane.NONE);                  default:
357            mapPane.setHighlight(false);                          // Set map actions to default
358            mapPane.setNormalCursor(null);                          mapPane.setWindowSelectionState(JMapPane.NONE);
359            break;                          mapPane.setState(JMapPane.NONE);
360        }                          mapPane.setHighlight(false);
361        mapPane.updateCursor();                          mapPane.setNormalCursor(null);
362                            break;
363                    }
364                    mapPane.updateCursor();
365          }          }
366            
367      /**          /**
368       * Performs the action of an action button.           * @param id
369       * @param tool the action           *            The ID of the Component to remove. The change will not be
370       * @param e    the event of the button           *            visible until {@link #initToolBar()} is called.
371       */           * @return <code>null</code> or the component that has been removed.
372             */
373            public JComponent removeId(int id) {
374                    return toolAndActionButtons.remove(id);
375            }
376    
377            /**
378             * Performs the action of an action button.
379             *
380             * @param tool
381             *            the action
382             * @param e
383             *            the event of the button
384             */
385          protected void performActionButton(int action, ActionEvent e) {          protected void performActionButton(int action, ActionEvent e) {
386        if ( mapPane == null )                  if (mapPane == null)
387          return;                          return;
388    
389        // Perform the action "Zoom back": Revert the last zoom                  // Perform the action "Zoom back": Revert the last zoom
390        if ( action == ACTION_ZOOM_BACK ) {                  if (action == ACTION_ZOOM_BACK) {
391              if (zoomBackIndex <= 1)                          if (zoomBackIndex <= 1)
392            return;                                  return;
393      
394          zoomBackForwardButtonInAction = true;                          zoomBackForwardButtonInAction = true;
395          zoomBackIndex--;                          zoomBackIndex--;
396          getButton(ACTION_ZOOM_FORWARD).setEnabled(true);                          getButton(ACTION_ZOOM_FORWARD).setEnabled(true);
397          getButton(ACTION_ZOOM_BACK).setEnabled( zoomBackIndex > 1 );                          getButton(ACTION_ZOOM_BACK).setEnabled(zoomBackIndex > 1);
398      
399          mapPane.setMapArea( lastZooms.get(zoomBackIndex-1) );                          mapPane.setMapArea(lastZooms.get(zoomBackIndex - 1));
400          mapPane.refresh();                          mapPane.refresh();
401        }                  }
402    
403        // Perform the action "Zoom forward": Redo the last zoom                  // Perform the action "Zoom forward": Re-do the last zoom
404        if ( action == ACTION_ZOOM_FORWARD ) {                  if (action == ACTION_ZOOM_FORWARD) {
405          if (zoomBackIndex < lastZooms.size()) {                          if (zoomBackIndex < lastZooms.size()) {
406            zoomBackForwardButtonInAction = true;                                  zoomBackForwardButtonInAction = true;
407            zoomBackIndex++;                                  zoomBackIndex++;
408            getButton(ACTION_ZOOM_BACK).setEnabled(true);                                  getButton(ACTION_ZOOM_BACK).setEnabled(true);
409            getButton(ACTION_ZOOM_FORWARD).setEnabled(zoomBackIndex < lastZooms.size());                                  getButton(ACTION_ZOOM_FORWARD).setEnabled(
410                                                    zoomBackIndex < lastZooms.size());
411            mapPane.setMapArea( lastZooms.get(zoomBackIndex-1) );  
412            mapPane.refresh();                                  mapPane.setMapArea(lastZooms.get(zoomBackIndex - 1));
413          }                                  mapPane.refresh();
414        }                          }
415                    }
416          }          }
417            
           
418          /**          /**
419           * 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
420           * specified ID already exists!           * specified ID already exists!
421           * @param buttonAction action for the toggle button           *
422           * @param resetToolBar indicates whether the toolbar GUI is reset after adding           * @param buttonAction
423           *                     the button (if adding several actions it useful only to           *            action for the toggle button
424           *                     reset the GUI for the last added tool)           * @param resetToolBar
425             *            indicates whether the toolbar GUI is reset after adding the
426             *            button (if adding several actions it useful only to reset the
427             *            GUI for the last added tool)
428           */           */
429          public void addTool(MapPaneToolBarAction buttonAction, boolean resetToolBar) {          public void addTool(MapPaneToolBarAction buttonAction, boolean resetToolBar) {
430            if ( isButtonIDUsed(buttonAction.getID()) ) {                  if (isButtonIDUsed(buttonAction.getID())) {
431              LOGGER.warn("addTool(.) ignored because ID already used for tool or action: "+buttonAction.getID());                          LOGGER
432              return;                                          .warn("addTool(.) ignored because ID already used for tool or action: "
433            }                                                          + buttonAction.getID());
434            JToggleButton button = new JToggleButton(buttonAction);                          return;
435            button.setBorder( BorderFactory.createRaisedBevelBorder() );                  }
436            toolButtonGroup.add(button);                  JToggleButton button = new JToggleButton(buttonAction);
437            toolButtons.put(buttonAction.getID(), button);                  button.setBorder(BorderFactory.createRaisedBevelBorder());
438            if ( resetToolBar )                  toolButtonGroup.add(button);
439              initToolBar();                  toolAndActionButtons.put(buttonAction.getID(), button);
440          }                  if (resetToolBar)
441                            initToolBar();
442      /**          }
443       * Adds a tool to the tool bar and resets the toolbar GUI.  
444       * @param buttonAction action for the toggle button          /**
445       */           * Adds a tool to the tool bar and resets the toolbar GUI.
446      public void addTool(MapPaneToolBarAction buttonAction) {           *
447        addTool(buttonAction, true);           * @param buttonAction
448      }           *            action for the toggle button
449             */
450      /**          public void addTool(MapPaneToolBarAction buttonAction) {
451       * Adds an action to the tool bar. Does nothing if a tool or action with the                  addTool(buttonAction, true);
452       * specified ID already exists!          }
453       * @param buttonAction action for the button  
454       * @param resetToolBar indicates whether the toolbar GUI is reset after adding          /**
455       *                     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
456       *                     reset the GUI for the last added tool)           * specified ID already exists!
457       */           *
458      public void addAction(MapPaneToolBarAction buttonAction, boolean resetToolBar) {           * @param buttonAction
459        if ( isButtonIDUsed(buttonAction.getID()) ) {           *            action for the button
460          LOGGER.warn("addAction(.) ignored because ID already used for tool or action: "+buttonAction.getID());           * @param resetToolBar
461          return;           *            indicates whether the toolbar GUI is reset after adding the
462        }           *            button (if adding several actions it useful only to reset the
463        JButton button = new JButton(buttonAction);           *            GUI for the last added tool)
464        actionButtons.put( buttonAction.getID(), button );           */
465        if ( resetToolBar )          public void addAction(MapPaneToolBarAction buttonAction,
466          initToolBar();                          boolean resetToolBar) {
467      }                  if (isButtonIDUsed(buttonAction.getID())) {
468                            LOGGER
469      /**                                          .warn("addAction(.) ignored because ID already used for tool or action: "
470       * Adds an action to the tool bar and resets the toolbar GUI.                                                          + buttonAction.getID());
471       * @param buttonAction action for the toggle button                          return;
472       */                  }
473      public void addAction(MapPaneToolBarAction buttonAction) {                  JButton button = new JButton(buttonAction);
474        addAction(buttonAction, true);                  button.setBorder(BorderFactory.createRaisedBevelBorder());
475      }                  toolAndActionButtons.put(buttonAction.getID(), button);
476                        if (resetToolBar)
477      /**                          initToolBar();
478       * Returns the button for a specific tool or action.          }
479       * @param id the constant for a tool  
480       * @return a {@link JButton} if {@code id} specifies an {@linkplain #getActionButton(int) action button}          public void addSeparator(int id, Separator separator) {
481       *         or {@link JToogleButton} if {@code id} specifies a {@linkplain #getToolButton(int) tool button}                  if (isButtonIDUsed(id)) {
482       */                          LOGGER
483      public AbstractButton getButton(int id) {                                          .warn("addSeparator(.) ignored because ID already used for tool or action. ");
484        AbstractButton button = toolButtons.get(id);                          return;
485        if ( button == null )                  }
486          button = actionButtons.get(id);                  toolAndActionButtons.put(id, separator);
487        if ( button == null )          }
488          LOGGER.warn("Unknown tool or action ID: "+id);  
489        return button;          /**
490      }           * Adds an action to the tool bar and resets the toolbar GUI.
491             *
492      /**           * @param buttonAction
493       * Returns the button for a specific tool.           *            action for the toggle button
494       * @param tool the constant for a tool           */
495       */          public void addAction(MapPaneToolBarAction buttonAction) {
496                    addAction(buttonAction, true);
497            }
498    
499            /**
500             * Returns the button for a specific tool or action.
501             *
502             * @param id
503             *            the constant for any button in the {@link MapPaneToolBar}
504             * @return a {@link JButton} if {@code id} specifies an
505             *         {@linkplain #getActionButton(int) action button} or
506             *         {@link JToogleButton} if {@code id} specifies a
507             *         {@linkplain #getToolButton(int) tool button}
508             */
509            public AbstractButton getButton(int id) {
510                    AbstractButton button = (AbstractButton) toolAndActionButtons.get(id);
511                    if (button == null)
512                            LOGGER.warn("Unknown tool or action ID: " + id);
513                    return button;
514            }
515    
516            /**
517             * Returns the button for a specific tool.
518             *
519             * @param tool
520             *            the constant for a tool
521             */
522          public JToggleButton getToolButton(int tool) {          public JToggleButton getToolButton(int tool) {
523        AbstractButton button = getButton(tool);                  AbstractButton button = getButton(tool);
524        if ( button != null && !(button instanceof JToggleButton) ) {                  if (button != null && !(button instanceof JToggleButton)) {
525          LOGGER.warn("ID specifies no tool: "+tool);                          LOGGER.warn("ID specifies no tool: " + tool);
526          button = null;                          button = null;
527        }                  }
528        return (JToggleButton)button;                  return (JToggleButton) button;
529      }          }
   
     /**  
      * Returns the button for a specific action.  
      * @param action the constant an action  
      */  
     public JButton getActionButton(int action) {  
       AbstractButton button = getButton(action);  
       if ( button != null && !(button instanceof JButton) ) {  
         LOGGER.warn("ID specifies no action: "+action);  
         button = null;  
       }  
       return (JButton)button;  
530    
531      }          /**
532             * Returns the button for a specific action.
533             *
534             * @param action
535             *            the constant an action
536             */
537            public JButton getActionButton(int action) {
538                    AbstractButton button = getButton(action);
539                    if (button != null && !(button instanceof JButton)) {
540                            LOGGER.warn("ID specifies no action: " + action);
541                            button = null;
542                    }
543                    return (JButton) button;
544    
545            }
546    
547          /**          /**
548           * Sets the selected tool.           * Sets the selected tool.
549           * @param tool ID of the tool           *
550             * @param tool
551             *            ID of the tool
552           */           */
553          public void setSelectedTool(Integer tool) {          public void setSelectedTool(Integer tool) {
554            if ( tool == null )                  if (tool == null)
555              toolButtonGroup.setUnselected();                          toolButtonGroup.setUnselected();
556              
557            JToggleButton button = getToolButton(tool);                  JToggleButton button = getToolButton(tool);
558            if ( button == null )                  if (button == null)
559              return;                          return;
560            button.setSelected( true );                  button.setSelected(true);
561            button.getAction().actionPerformed(null);                  button.getAction().actionPerformed(null);
562    
563                    selectedTool = tool;
564          }          }
565            
566          /**          /**
567           * Returns the selected tool.           * Returns the selected tool.
568             *
569           * @return -1 if no tool is active           * @return -1 if no tool is active
570           */           */
571          public int getSelectedTool() {          public int getSelectedTool() {
572            if ( toolButtonGroup.getSelectedButton() == null )                  if (toolButtonGroup.getSelectedButton() == null)
573              return -1;                          return -1;
574            return selectedTool;                  return selectedTool;
575          }          }
576            
577      /**          /**
578       * 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
579       * of the button is not affected.           * of the button is not affected.
580       * @param id tool or actionID           *
581       * @param enabled if {@code true} the tool becomes available           * @param id
582       */           *            tool or actionID
583      public void setButtonEnabled(int id, boolean enabled) {           * @param enabled
584        AbstractButton button = getButton(id);           *            if {@code true} the tool becomes available
585        if ( button == null )           */
586          return;          public void setButtonEnabled(int id, boolean enabled) {
587        button.setEnabled( enabled );                  AbstractButton button = getButton(id);
588      }                  if (button == null)
589                            return;
590      /**                  button.setEnabled(enabled);
591       * Sets whether a tool or action is activated or not.          }
592       * @param id tool or actionID  
593       * @param enabled if {@code true} the tool becomes available          /**
594       * @param hideOnDisable if {@code true} the button is also hidden if           * Sets whether a tool or action is activated or not.
595       *                      {@code enabled} is {@code false}           *
596       */           * @param id
597             *            tool or actionID
598             * @param enabled
599             *            if {@code true} the tool becomes available
600             * @param hideOnDisable
601             *            if {@code true} the button is also hidden if {@code enabled}
602             *            is {@code false}
603             */
604          public void setButtonEnabled(int id, boolean enabled, boolean hideOnDisable) {          public void setButtonEnabled(int id, boolean enabled, boolean hideOnDisable) {
605            AbstractButton button = getButton(id);                  AbstractButton button = getButton(id);
606            if ( button == null )                  if (button == null)
607              return;                          return;
608            button.setEnabled( enabled );                  button.setEnabled(enabled);
609            // if button is enabled, it becomes visible anyway                  // if button is enabled, it becomes visible anyway
610            // 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
611            if ( enabled )                  if (enabled)
612              button.setVisible( true );                          button.setVisible(true);
613            else                  else
614              button.setVisible( !hideOnDisable );                          button.setVisible(!hideOnDisable);
615          }          }
616    
617      /**          /**
618       * Checks whether a ID is already used for a tool or action.           * Checks whether a ID is already used for a tool or action.
619       * @param tool tool ID           *
620       */           * @param tool
621      public boolean isButtonIDUsed(int id) {           *            tool ID
622        return toolButtons.get(id) != null || actionButtons.get(id) != null;           */
623      }          public boolean isButtonIDUsed(int id) {
624                    return toolAndActionButtons.get(id) != null;
625      /**          }
626       * Checks whether a tool is activated.  
627       * @param tool tool ID          /**
628       * @return {@code false} if an unknown ID is specified           * Checks whether a tool is activated.
629       */           *
630      public boolean isButtonEnabled(int id) {           * @param tool
631        AbstractButton button = getButton(id);           *            tool ID
632        if ( button != null )           * @return {@code false} if an unknown ID is specified
633          return button.isEnabled();           */
634        return false;          public boolean isButtonEnabled(int id) {
635      }                  AbstractButton button = getButton(id);
636                    if (button != null)
637      /**                          return button.isEnabled();
638       * Sets the activation for all tools.                  return false;
639       * @param enabled if {@code true} all tools becomes available          }
640       * @param hideOnDisable if {@code true} the buttons are also hidden if  
641       *                      {@code enabled} is {@code false}          /**
642       */           * Sets the activation for all tools.
643      public void setAllToolsEnabled(boolean enabled, boolean hideOnDisable) {           *
644        for (int tool : toolButtons.keySet())           * @param enabled
645          setButtonEnabled(tool,enabled,hideOnDisable);           *            if {@code true} all tools becomes available
646      }             * @param hideOnDisable
647             *            if {@code true} the buttons are also hidden if {@code enabled}
648      /**           *            is {@code false}
649       * Sets the activation for all actions.           */
650       * @param enabled if {@code true} all actions becomes available          public void setAllToolsEnabled(boolean enabled, boolean hideOnDisable) {
651       * @param hideOnDisable if {@code true} the buttons are also hidden if                  for (int tool : toolAndActionButtons.keySet())
652       *                      {@code enabled} is {@code false}                          setButtonEnabled(tool, enabled, hideOnDisable);
653       */          }
654      public void setAllActionsEnabled(boolean enabled, boolean hideOnDisable) {  
655        for (int tool : actionButtons.keySet())          /**
656          setButtonEnabled(tool,enabled,hideOnDisable);           * Sets the activation for all actions.
657      }             *
658                 * @param enabled
659      /**           *            if {@code true} all actions becomes available
660       * Returns the maximum ID of tools.           * @param hideOnDisable
661       */           *            if {@code true} the buttons are also hidden if {@code enabled}
662      public int getMaxToolID() {           *            is {@code false}
663        return toolButtons.lastKey();           */
664      }          public void setAllActionsEnabled(boolean enabled, boolean hideOnDisable) {
665                    for (int id : toolAndActionButtons.keySet()) {
666      /**                          if (toolAndActionButtons.get(id) instanceof JButton) {
667       * Returns the minimum ID of tools.                                  setButtonEnabled(id, enabled, hideOnDisable);
668       */                          }
669      public int getMinToolID() {                  }
670        return toolButtons.firstKey();  
671      }          }
672    
673      /**          /**
674       * Returns the maximum ID of actions.           * Returns the maximum ID of tools.
675       */           */
676      public int getMaxActionID() {          public int getMaxToolID() {
677        return actionButtons.lastKey();                  return toolAndActionButtons.lastKey();
678      }          }
679    
680      /**          /**
681       * Returns the minimum ID of actions.           * Returns the minimum ID of tools.
682       */           */
683      public int getMinActionID() {          public int getMinToolID() {
684        return actionButtons.firstKey();                  return toolAndActionButtons.firstKey();
685      }          }
686        
687      /**          /**
688       * Extends the {@link AbstractAction} with maintaining an ID and           * Extends the {@link AbstractAction} with maintaining an ID and the
689       * the {@link MapPaneToolBar} the actions controls.           * {@link MapPaneToolBar} the actions controls. Additionally this class
690       * Additionally this class automatically calls {@link MapPaneToolBar#performToolButton(int, ActionEvent)}           * automatically calls
691       * or {@link MapPaneToolBar#performActionButton(int, ActionEvent)}           * {@link MapPaneToolBar#performToolButton(int, ActionEvent)} or
692       * depending on whether the action is added via {@link MapPaneToolBar#addTool(MapPaneToolBarAction)}           * {@link MapPaneToolBar#performActionButton(int, ActionEvent)} depending on
693       * or {@link MapPaneToolBar#addAction(MapPaneToolBarAction)}.           * whether the action is added via
694       * @author <a href="mailto:[email protected]">Martin Schmitz</a> (University of Bonn/Germany)           * {@link MapPaneToolBar#addTool(MapPaneToolBarAction)} or
695       */           * {@link MapPaneToolBar#addAction(MapPaneToolBarAction)}.
696      public static class MapPaneToolBarAction extends AbstractAction {           *
697        /** The ID of the action */           * @author <a href="mailto:[email protected]">Martin Schmitz</a>
698        protected int id = -1;           *         (University of Bonn/Germany)
699        /** The tool bar, this action is made for. */           */
700        protected MapPaneToolBar toolBar = null;          public static class MapPaneToolBarAction extends AbstractAction {
701                    /** The ID of the action */
702        /**                  protected int id = -1;
703         * Creates a new action with a dummy description and no icon.                  /** The tool bar, this action is made for. */
704         * @param id      unique ID for the action                  protected MapPaneToolBar toolBar = null;
705         * @param toolBar toolbar this action is made for  
706         */                  /**
707        public MapPaneToolBarAction(int id, MapPaneToolBar toolBar) {                   * Creates a new action with a dummy description and no icon.
708          this(id,toolBar,""+id);                   *
709        }                   * @param id
710                     *            unique ID for the action
711        /**                   * @param toolBar
712         * Creates a new action without an icon.                   *            toolbar this action is made for
713         * @param id      unique ID for the action                   */
714         * @param toolBar toolbar this action is made for                  public MapPaneToolBarAction(int id, MapPaneToolBar toolBar) {
715         * @param name    description used for buttons or menus                          this(id, toolBar, "" + id);
716         */                  }
717        public MapPaneToolBarAction(int id, MapPaneToolBar toolBar, String name) {  
718          this(id,toolBar,name,null);                  /**
719        }                   * Creates a new action without an icon.
720                     *
721        /**                   * @param id
722         * Creates a new action.                   *            unique ID for the action
723         * @param id      unique ID for the action                   * @param toolBar
724         * @param toolBar toolbar this action is made for                   *            toolbar this action is made for
725         * @param name    description used for buttons or menus                   * @param name
726         * @param icon    icon used for buttons or menus                   *            description used for buttons or menus
727         */                   */
728        public MapPaneToolBarAction(int id, MapPaneToolBar toolBar, String name, Icon icon) {                  public MapPaneToolBarAction(int id, MapPaneToolBar toolBar, String name) {
729          super(name,icon);                          this(id, toolBar, name, null);
730          this.id      = id;                  }
731          this.toolBar = toolBar;  
732        }                  /**
733                           * Creates a new action.
734        /**                   *
735         * Calls {@link MapPaneToolBar#performToolButton(int, ActionEvent)}                   * @param id
736         * or {@link MapPaneToolBar#performActionButton(int, ActionEvent)}                   *            unique ID for the action
737         * depending on whether the action is added to the toolbar via                   * @param toolBar
738         * {@link MapPaneToolBar#addTool(MapPaneToolBarAction)}                   *            toolbar this action is made for
739         * or {@link MapPaneToolBar#addAction(MapPaneToolBarAction)}.                   * @param name
740         */                   *            description used for buttons or menus
741        public void actionPerformed(ActionEvent e) {                   * @param icon
742          if ( toolBar.toolButtons.get(id) != null )                   *            icon used for buttons or menus
743            toolBar.performToolButton(id, e);                   */
744          if ( toolBar.actionButtons.get(id) != null )                  public MapPaneToolBarAction(int id, MapPaneToolBar toolBar,
745            toolBar.performActionButton(id, e);                                  String name, Icon icon) {
746        }                          this (id, toolBar, name, icon, null);
747                          }
748        /**                  
749         * Returns the (unique) id of this action.                  /**
750         * @return                   * Creates a new action.
751         */                   *
752        public int getID() {                   * @param id
753          return id;                   *            unique ID for the action
754        }                   * @param toolBar
755      }                   *            The {@link MapPaneToolBar} this action is made for
756                     * @param name
757                     *            description used for buttons or menus
758                     * @param icon
759                     *            icon used for buttons or menus
760                     * @param toolTip
761                     *            Tooltip to use for the button or menu
762                     */
763                    public MapPaneToolBarAction(int id, MapPaneToolBar toolBar,
764                                    String name, Icon icon, String toolTip) {
765                            super(name, icon);
766                            
767                            if (toolTip != null && !toolTip.trim().isEmpty()){
768                                    putValue(Action.SHORT_DESCRIPTION, toolTip);
769                            }
770                            
771                            this.id = id;
772                            this.toolBar = toolBar;
773                    }
774    
775                    /**
776                     * Calls {@link MapPaneToolBar#performToolButton(int, ActionEvent)} or
777                     * {@link MapPaneToolBar#performActionButton(int, ActionEvent)}
778                     * depending on whether the action is added to the toolbar via
779                     * {@link MapPaneToolBar#addTool(MapPaneToolBarAction)} or
780                     * {@link MapPaneToolBar#addAction(MapPaneToolBarAction)}.
781                     */
782                    public void actionPerformed(ActionEvent e) {
783                            if (toolBar.toolAndActionButtons.get(id) instanceof JToggleButton)
784                                    toolBar.performToolButton(id, e);
785                            else if (toolBar.toolAndActionButtons.get(id) instanceof JButton)
786                                    toolBar.performActionButton(id, e);
787                    }
788    
789                    /**
790                     * Returns the (unique) id of this action.
791                     *
792                     * @return
793                     */
794                    public int getID() {
795                            return id;
796                    }
797            }
798  }  }

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26