/[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

branches/1.0-gt2-2.6/src/skrueger/geotools/MapPaneToolBar.java revision 486 by alfonx, Tue Oct 20 16:25:26 2009 UTC trunk/src/skrueger/geotools/MapPaneToolBar.java revision 1220 by alfonx, Wed Nov 3 13:09:32 2010 UTC
# Line 25  Line 25 
25   *   *
26   * Contributors:   * Contributors:
27   *     Martin O. J. Schmitz - initial API and implementation   *     Martin O. J. Schmitz - initial API and implementation
28   *     Stefan A. Krüger - additional utility classes   *     Stefan A. Tzeggai - additional utility classes
29   ******************************************************************************/   ******************************************************************************/
30  package skrueger.geotools;  package skrueger.geotools;
31    
# Line 35  import java.awt.event.ActionEvent; Line 35  import java.awt.event.ActionEvent;
35  import java.awt.event.ActionListener;  import java.awt.event.ActionListener;
36  import java.util.ArrayList;  import java.util.ArrayList;
37  import java.util.HashSet;  import java.util.HashSet;
 import java.util.Locale;  
38  import java.util.Set;  import java.util.Set;
39  import java.util.SortedMap;  import java.util.SortedMap;
40  import java.util.TreeMap;  import java.util.TreeMap;
# Line 52  import javax.swing.JToolBar; Line 51  import javax.swing.JToolBar;
51    
52  import org.apache.log4j.Logger;  import org.apache.log4j.Logger;
53    
54  import schmitzm.geotools.gui.JMapPane;  import schmitzm.geotools.gui.GeotoolsGUIUtil;
55  import schmitzm.geotools.map.event.JMapPaneEvent;  import schmitzm.geotools.gui.SelectableXMapPane;
56    import schmitzm.geotools.gui.XMapPaneEvent;
57    import schmitzm.geotools.gui.XMapPaneTool;
58  import schmitzm.geotools.map.event.JMapPaneListener;  import schmitzm.geotools.map.event.JMapPaneListener;
59  import schmitzm.geotools.map.event.MapAreaChangedEvent;  import schmitzm.geotools.map.event.MapAreaChangedEvent;
 import schmitzm.lang.LangUtil;  
 import schmitzm.lang.ResourceProvider;  
60  import schmitzm.swing.ButtonGroup;  import schmitzm.swing.ButtonGroup;
 import schmitzm.swing.SwingUtil;  
61  import skrueger.swing.SmallButton;  import skrueger.swing.SmallButton;
62  import skrueger.swing.SmallToggleButton;  import skrueger.swing.SmallToggleButton;
63    
64  import com.vividsolutions.jts.geom.Envelope;  import com.vividsolutions.jts.geom.Envelope;
65    
66  /**  /**
67   * A toolbar to control an {@link JMapPane} (Atlas visualization). This contains   * A toolbar to control an {@link SelectableXMapPane} (Atlas visualization).
68   * two types of buttons. A group of <i>tools</i> for the mouse actions on the   * This contains two types of buttons. A group of <i>tools</i> for the mouse
69   * map represented by {@link JToggleButton JToggleButtons}, where only one tool   * actions on the map represented by {@link JToggleButton JToggleButtons}, where
70   * can be activated every time. And some (general) <i>actions</i>, represented   * only one tool can be activated every time. And some (general) <i>actions</i>,
71   * by normal {@link JButton JButtons}.   * represented by normal {@link JButton JButtons}.
72   *   *
73   * @author <a href="mailto:[email protected]">Martin Schmitz</a>   * @author <a href="mailto:[email protected]">Martin Schmitz</a>
74   *         (University of Bonn/Germany)   *         (University of Bonn/Germany)
75   * @version 1.2 Stefan Krüger   * @version 1.2 Stefan Tzeggai
76   */   */
77  public class MapPaneToolBar extends JToolBar {  public class MapPaneToolBar extends JToolBar {
78          private static final Logger LOGGER = Logger.getLogger(MapPaneToolBar.class          private static final Logger LOGGER = Logger.getLogger(MapPaneToolBar.class
79                          .getName());                          .getName());
80    
         public static ResourceProvider RESOURCE = new ResourceProvider(LangUtil  
                         .extendPackagePath(MapPaneToolBar.class,  
                                         "resource.locales.mapPaneToolbar"), Locale.ENGLISH);  
   
81          public static String R(String key, Object... values) {          public static String R(String key, Object... values) {
82                  return RESOURCE.getString(key, values);                  return GeotoolsGUIUtil.R(key, values);
83          }          }
84    
85          /** Constant for the tool "Panning" (10). */          /** Constant for the tool "Panning" (10). */
# Line 96  public class MapPaneToolBar extends JToo Line 90  public class MapPaneToolBar extends JToo
90    
91          /** Constant for the tool "Zoom In" (110). */          /** Constant for the tool "Zoom In" (110). */
92          public static final int TOOL_ZOOMIN = 110;          public static final int TOOL_ZOOMIN = 110;
93            public static final int ACTION_ZOOM_DEFAULT = 150;
94          /** Constant for the tool "Zoom Out" (120). */          /** Constant for the tool "Zoom Out" (120). */
95          public static final int TOOL_ZOOMOUT = 120;          public static final int TOOL_ZOOMOUT = 120;
96          /** Constant for the action "Zoom back" (130). */          /** Constant for the action "Zoom back" (130). */
# Line 140  public class MapPaneToolBar extends JToo Line 135  public class MapPaneToolBar extends JToo
135          // /** Holds the action buttons of the bar. */          // /** Holds the action buttons of the bar. */
136          // protected SortedMap<Integer, JButton> actionButtons = null;          // protected SortedMap<Integer, JButton> actionButtons = null;
137    
138          /** Holds the {@link JMapPane} this tool bar controls. */          /** Holds the {@link SelectableXMapPane} this tool bar controls. */
139          protected JMapPane mapPane = null;          protected SelectableXMapPane mapPane = null;
140    
141          /**          /**
142           * A List to remember the last Envelopes that have been watched. Used for           * A List to remember the last Envelopes that have been watched. Used for
# Line 173  public class MapPaneToolBar extends JToo Line 168  public class MapPaneToolBar extends JToo
168                  }                  }
169    
170          };          };
171            private CrsLabel crsViewer;
172    
173          /**          /**
174           * Creates a new toolbar. Notice: This toolbar does nothing until           * Creates a new toolbar. Notice: This toolbar does nothing until
175           * {@link #setMapPane(JMapPane)} is called!           * {@link #setMapPane(SelectableXMapPane)} is called!
176           */           */
177          public MapPaneToolBar() {          public MapPaneToolBar() {
178                  this(null);                  this(null);
# Line 195  public class MapPaneToolBar extends JToo Line 191  public class MapPaneToolBar extends JToo
191           * Creates a new tool bar.           * Creates a new tool bar.
192           *           *
193           * @param mapPane           * @param mapPane
194           *            {@link JMapPane} the tool bar controls           *            {@link SelectableXMapPane} the tool bar controls
195           */           */
196          public MapPaneToolBar(JMapPane mapPane) {          public MapPaneToolBar(SelectableXMapPane mapPane) {
197                  super("Control the map", JToolBar.HORIZONTAL);                  super("Control the map", JToolBar.HORIZONTAL);
198    
199                  // I want to see nothing on the background                  // I want to see nothing on the background
# Line 208  public class MapPaneToolBar extends JToo Line 204  public class MapPaneToolBar extends JToo
204    
205                  // Create a Listener to listen to the zooms on the JMapPane                  // Create a Listener to listen to the zooms on the JMapPane
206                  this.mapPaneListener = new JMapPaneListener() {                  this.mapPaneListener = new JMapPaneListener() {
207                          public void performMapPaneEvent(JMapPaneEvent e) {                          public void performMapPaneEvent(XMapPaneEvent e) {
208    
209                                  if (!(e instanceof MapAreaChangedEvent))                                  if (!(e instanceof MapAreaChangedEvent))
210                                          return;                                          return;
211    
# Line 259  public class MapPaneToolBar extends JToo Line 256  public class MapPaneToolBar extends JToo
256                                  setButtonEnabled(ACTION_ZOOM_BACK, lastZooms.size() > 1);                                  setButtonEnabled(ACTION_ZOOM_BACK, lastZooms.size() > 1);
257                                  setButtonEnabled(ACTION_ZOOM_FORWARD, false);                                  setButtonEnabled(ACTION_ZOOM_FORWARD, false);
258                          }                          }
259    
260                  };                  };
261    
262                  setMapPane(mapPane);                  setMapPane(mapPane);
# Line 268  public class MapPaneToolBar extends JToo Line 266  public class MapPaneToolBar extends JToo
266                  init();                  init();
267          }          }
268    
269            private CrsLabel getCrsComponent() {
270                    if (crsViewer == null) {
271                            crsViewer = new CrsLabel(this.mapPane);
272                    }
273                    return crsViewer;
274            }
275    
276          /**          /**
277           * Sets the {@link JMapPane} controlled by this tool bar.           * Sets the {@link SelectableXMapPane} controlled by this tool bar.
278           *           *
279           * @param mapPane           * @param mapPane
280           *            {@link JMapPane} to control (if {@code null} this tool bar           *            {@link SelectableXMapPane} to control (if {@code null} this
281           *            controls NOTHING!)           *            tool bar controls NOTHING!)
282           */           */
283          public void setMapPane(JMapPane mapPane) {          public void setMapPane(SelectableXMapPane mapPane) {
284                  // Remove listener from old MapPane                  // Remove listener from old MapPane
285                  if (this.mapPane != null)                  if (this.mapPane != null)
286                          this.mapPane.removeMapPaneListener(mapPaneListener);                          this.mapPane.removeMapPaneListener(mapPaneListener);
# Line 302  public class MapPaneToolBar extends JToo Line 307  public class MapPaneToolBar extends JToo
307           * Creates the tool buttons and action buttons and seperators, adds them to           * Creates the tool buttons and action buttons and seperators, adds them to
308           * {@link #toolAndActionButtons} and finally creates a button group for all           * {@link #toolAndActionButtons} and finally creates a button group for all
309           * tools. So sub-classes which override this method should FIRST add their           * tools. So sub-classes which override this method should FIRST add their
310           * new tool buttons to {@link #toolAndActionButtons} before calling {@code           * new tool buttons to {@link #toolAndActionButtons} before calling
311           * super.initTools()}.           * {@code super.initTools()}.
312           */           */
313          protected void initToolsAndActions() {          protected void initToolsAndActions() {
314                  // Panning                  // Pan
315                  addTool(new MapPaneToolBarAction(TOOL_PAN, this, "", new ImageIcon(                  addTool(new MapPaneToolBarAction(TOOL_PAN, this, XMapPaneTool.PAN),
316                                  MapView.class.getResource("resource/icons/pan.png")),                                  false);
317                                  R("MapPaneButtons.Pan.TT")), false);  
318                  // Info                  // Info
319                  addTool(new MapPaneToolBarAction(TOOL_INFO, this, "", new ImageIcon(                  addTool(new MapPaneToolBarAction(TOOL_INFO, this, XMapPaneTool.INFO),
320                                  MapView.class.getResource("resource/icons/info.png")),                                  false);
                                 R("MapPaneButtons.Info.TT")), false);  
321    
322                  // Zoom in                  // Zoom in
323                  addTool(new MapPaneToolBarAction(TOOL_ZOOMIN, this, "", new ImageIcon(                  addTool(new MapPaneToolBarAction(TOOL_ZOOMIN, this,
324                                  MapView.class.getResource("resource/icons/zoom_in.png")),                                  XMapPaneTool.ZOOM_IN), false);
325                                  R("MapPaneButtons.ZoomIn.TT")), false);  
326                  // Zoom out                  // Zoom out
327                  addTool(new MapPaneToolBarAction(TOOL_ZOOMOUT, this, "", new ImageIcon(                  addTool(new MapPaneToolBarAction(TOOL_ZOOMOUT, this,
328                                  MapView.class.getResource("resource/icons/zoom_out.png")),                                  XMapPaneTool.ZOOM_OUT), false);
                                 R("MapPaneButtons.ZoomOut.TT")), false);  
329    
330                  // Action button to revert the last zoom                  // Action button to revert the last zoom
331                  addAction(new MapPaneToolBarAction(ACTION_ZOOM_BACK, this, "",                  addAction(
332                                  new ImageIcon(MapView.class                                  new MapPaneToolBarAction(ACTION_ZOOM_BACK, this, "",
333                                                  .getResource("resource/icons/zoom_back.png")),                                                  new ImageIcon(MapView.class
334                                  R("MapPaneButtons.LastZoom.TT")), false);                                                                  .getResource("resource/icons/zoom_back.png")),
335                                                    R("MapPaneButtons.LastZoom.TT")), false);
336                  setButtonEnabled(ACTION_ZOOM_BACK, false);                  setButtonEnabled(ACTION_ZOOM_BACK, false);
337    
338                  // Action button to redo the last zoom                  // Action button to redo the last zoom
339                  addAction(new MapPaneToolBarAction(ACTION_ZOOM_FORWARD, this, "",                  addAction(
340                                  new ImageIcon(MapView.class                                  new MapPaneToolBarAction(
341                                                  .getResource("resource/icons/zoom_forward.png")),                                                  ACTION_ZOOM_FORWARD,
342                                  R("MapPaneButtons.NextZoom.TT")), false);                                                  this,
343                                                    "",
344                                                    new ImageIcon(MapView.class
345                                                                    .getResource("resource/icons/zoom_forward.png")),
346                                                    R("MapPaneButtons.NextZoom.TT")), false);
347                  setButtonEnabled(ACTION_ZOOM_FORWARD, false);                  setButtonEnabled(ACTION_ZOOM_FORWARD, false);
348    
349                  // set the selected tool enabled                  // set the selected tool enabled
350                  setSelectedTool(selectedTool);                  setSelectedTool(selectedTool);
351    
352                    add(getCrsComponent());
353    
354          }          }
355    
356          @Override          @Override
# Line 353  public class MapPaneToolBar extends JToo Line 363  public class MapPaneToolBar extends JToo
363           * the tool bar.           * the tool bar.
364           */           */
365          public void initToolBar() {          public void initToolBar() {
                 // setOpaque(true);  
                 //                
                 // setAlignmentY(1f);  
                 // setAlignmentX(0.5f);  
366                  removeAll();                  removeAll();
367    
368                  // // Separator to the left of the tool actions to start                  // // Separator to the left of the tool actions to start
# Line 399  public class MapPaneToolBar extends JToo Line 405  public class MapPaneToolBar extends JToo
405                  repaint();                  repaint();
406          }          }
407    
         // Space between tool buttons and action buttons  
         // SK: Seperators are now als manages like actions and tools  
         // Dimension dimension2 = new Dimension( 10,10);  
         // this.addSeparator(dimension2);  
   
         // // Action buttons  
         // for (JButton b : actionButtons.values())  
         // add(b);  
         // }  
   
408          /**          /**
409           * Performs the activation of a tool.           * Performs the activation of a tool.
410           *           *
# Line 425  public class MapPaneToolBar extends JToo Line 421  public class MapPaneToolBar extends JToo
421    
422                  switch (tool) {                  switch (tool) {
423                  case TOOL_PAN:                  case TOOL_PAN:
424                          // Set the mouse tool to "Panning"                          mapPane.setTool(XMapPaneTool.PAN);
                         mapPane.setWindowSelectionState(JMapPane.NONE);  
                         mapPane.setState(JMapPane.PAN);  
                         mapPane.setNormalCursor(SwingUtil.PAN_CURSOR);  
425                          break;                          break;
426                  case TOOL_INFO:                  case TOOL_INFO:
427                          // Set the mouse tool to "Info"                          mapPane.setTool(XMapPaneTool.INFO);
                         mapPane.setWindowSelectionState(JMapPane.NONE);  
                         mapPane.setState(JMapPane.SELECT_TOP); // Why not:  
                         // JMapPane.SELECT_TOP_ONEONLY  
                         // properly removed it to save  
                         // performance  
                         mapPane.setNormalCursor(SwingUtil.CROSSHAIR_CURSOR);  
428                          break;                          break;
429                  case TOOL_ZOOMIN:                  case TOOL_ZOOMIN:
430                          // Set the mouse tool to "Zoom in"                          mapPane.setTool(XMapPaneTool.ZOOM_IN);
                         mapPane.setWindowSelectionState(JMapPane.ZOOM_IN);  
                         mapPane.setState(JMapPane.ZOOM_IN);  
                         mapPane.setNormalCursor(SwingUtil.ZOOMIN_CURSOR);  
431                          break;                          break;
432                  case TOOL_ZOOMOUT:                  case TOOL_ZOOMOUT:
433                          // Set the mouse tool to "Zoom out"                          mapPane.setTool(XMapPaneTool.ZOOM_OUT);
                         mapPane.setWindowSelectionState(JMapPane.NONE);  
                         mapPane.setState(JMapPane.ZOOM_OUT);  
                         mapPane.setNormalCursor(SwingUtil.ZOOMOUT_CURSOR);  
                         break;  
                 default:  
                         // Set map actions to default  
                         mapPane.setWindowSelectionState(JMapPane.NONE);  
                         mapPane.setState(JMapPane.NONE);  
                         mapPane.setNormalCursor(null);  
434                          break;                          break;
435                  }                  }
                 mapPane.updateCursor();  
436          }          }
437    
438          /**          /**
# Line 525  public class MapPaneToolBar extends JToo Line 499  public class MapPaneToolBar extends JToo
499           */           */
500          public void addTool(MapPaneToolBarAction buttonAction, boolean resetToolBar) {          public void addTool(MapPaneToolBarAction buttonAction, boolean resetToolBar) {
501                  if (isButtonIDUsed(buttonAction.getID())) {                  if (isButtonIDUsed(buttonAction.getID())) {
502                          LOGGER                          LOGGER.warn("addTool(.) ignored because ID already used for tool or action: "
503                                          .warn("addTool(.) ignored because ID already used for tool or action: "                                          + buttonAction.getID());
                                                         + buttonAction.getID());  
504                          return;                          return;
505                  }                  }
506                  JToggleButton button = new SmallToggleButton(buttonAction);                  JToggleButton button = new SmallToggleButton(buttonAction);
507                    
 //              button.setBorder(BorderFactory.createRaisedBevelBorder());  
508                  toolButtonGroup.add(button);                  toolButtonGroup.add(button);
509                  toolAndActionButtons.put(buttonAction.getID(), button);                  toolAndActionButtons.put(buttonAction.getID(), button);
510                  if (resetToolBar)                  if (resetToolBar)
# Line 563  public class MapPaneToolBar extends JToo Line 535  public class MapPaneToolBar extends JToo
535          public void addAction(MapPaneToolBarAction buttonAction,          public void addAction(MapPaneToolBarAction buttonAction,
536                          boolean resetToolBar) {                          boolean resetToolBar) {
537                  if (isButtonIDUsed(buttonAction.getID())) {                  if (isButtonIDUsed(buttonAction.getID())) {
538                          LOGGER                          LOGGER.warn("addAction(.) ignored because ID already used for tool or action: "
539                                          .warn("addAction(.) ignored because ID already used for tool or action: "                                          + buttonAction.getID());
                                                         + buttonAction.getID());  
540                          return;                          return;
541                  }                  }
542                  JButton button = new SmallButton(buttonAction);                  JButton button = new SmallButton(buttonAction);
 //              button.setBorder(BorderFactory.createRaisedBevelBorder());  
543                  toolAndActionButtons.put(buttonAction.getID(), button);                  toolAndActionButtons.put(buttonAction.getID(), button);
544                  if (resetToolBar)                  if (resetToolBar)
545                          initToolBar();                          initToolBar();
# Line 591  public class MapPaneToolBar extends JToo Line 561  public class MapPaneToolBar extends JToo
561          public void addJComponent(JComponent component, int id, boolean resetToolBar) {          public void addJComponent(JComponent component, int id, boolean resetToolBar) {
562    
563                  if (isButtonIDUsed(id)) {                  if (isButtonIDUsed(id)) {
564                          LOGGER                          LOGGER.warn("addAction(.) ignored because ID already used for tool or action: "
565                                          .warn("addAction(.) ignored because ID already used for tool or action: "                                          + id);
                                                         + id);  
566                          return;                          return;
567                  }                  }
568    
# Line 604  public class MapPaneToolBar extends JToo Line 573  public class MapPaneToolBar extends JToo
573    
574          public void addSeparator(int id, Separator separator) {          public void addSeparator(int id, Separator separator) {
575                  if (isButtonIDUsed(id)) {                  if (isButtonIDUsed(id)) {
576                          LOGGER                          LOGGER.warn("addSeparator(.) ignored because ID already used for tool or action. ");
                                         .warn("addSeparator(.) ignored because ID already used for tool or action. ");  
577                          return;                          return;
578                  }                  }
579                  toolAndActionButtons.put(id, separator);                  toolAndActionButtons.put(id, separator);
# Line 632  public class MapPaneToolBar extends JToo Line 600  public class MapPaneToolBar extends JToo
600           *         {@linkplain #getToolButton(int) tool button}           *         {@linkplain #getToolButton(int) tool button}
601           */           */
602          public AbstractButton getButton(int id) {          public AbstractButton getButton(int id) {
603    
604                    // ACHUTNG: Das ist ein SK QUICK FIX! TODO
605                    if (!(toolAndActionButtons.get(id) instanceof AbstractButton))
606                            return null;
607    
608                  AbstractButton button = (AbstractButton) toolAndActionButtons.get(id);                  AbstractButton button = (AbstractButton) toolAndActionButtons.get(id);
609                  if (button == null)                  if (button == null)
610                          LOGGER.warn("Unknown tool or action ID: " + id);                          LOGGER.warn("Unknown tool or action ID: " + id);
# Line 773  public class MapPaneToolBar extends JToo Line 746  public class MapPaneToolBar extends JToo
746           *            is {@code false}           *            is {@code false}
747           */           */
748          public void setAllToolsEnabled(boolean enabled, boolean hideOnDisable) {          public void setAllToolsEnabled(boolean enabled, boolean hideOnDisable) {
749                  for (int tool : toolAndActionButtons.keySet())                  for (int id : toolAndActionButtons.keySet()) {
750                          setButtonEnabled(tool, enabled, hideOnDisable);                          if (toolAndActionButtons.get(id) instanceof JToggleButton) {
751                                    setButtonEnabled(id, enabled, hideOnDisable);
752                            }
753                    }
754          }          }
755    
756          /**          /**
# Line 897  public class MapPaneToolBar extends JToo Line 873  public class MapPaneToolBar extends JToo
873                          this.toolBar = toolBar;                          this.toolBar = toolBar;
874                  }                  }
875    
876                    public MapPaneToolBarAction(int id, MapPaneToolBar toolBar,
877                                    XMapPaneTool tool) {
878                            this(id, toolBar, "", tool.getIcon(), tool.getToolTip());
879                    }
880    
881                  /**                  /**
882                   * Calls {@link MapPaneToolBar#performToolButton(int, ActionEvent)} or                   * Calls {@link MapPaneToolBar#performToolButton(int, ActionEvent)} or
883                   * {@link MapPaneToolBar#performActionButton(int, ActionEvent)}                   * {@link MapPaneToolBar#performActionButton(int, ActionEvent)}
# Line 925  public class MapPaneToolBar extends JToo Line 906  public class MapPaneToolBar extends JToo
906           * Nuetzlich wenn die Componente gedruckt (z.B. wenn ein Screenshot gemacht           * Nuetzlich wenn die Componente gedruckt (z.B. wenn ein Screenshot gemacht
907           * wird) wird. Dann werden wird der Hintergrund auf WEISS gesetzt.           * wird) wird. Dann werden wird der Hintergrund auf WEISS gesetzt.
908           *           *
909           * @author <a href="mailto:[email protected]">Stefan Alfons           * @author <a href="mailto:[email protected]">Stefan Alfons Tzeggai</a>
          *         Kr&uuml;ger</a>  
910           */           */
911          @Override          @Override
912          public void print(Graphics g) {          public void print(Graphics g) {
# Line 940  public class MapPaneToolBar extends JToo Line 920  public class MapPaneToolBar extends JToo
920                  }                  }
921          }          }
922    
923            @Override
924            protected void finalize() throws Throwable {
925                    super.finalize();
926    
927                    if (mapPane != null && mapPaneListener != null)
928                            mapPane.removeMapPaneListener(mapPaneListener);
929            }
930    
931  }  }

Legend:
Removed from v.486  
changed lines
  Added in v.1220

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26