/[schmitzm]/branches/2.0-RC1/src/skrueger/geotools/MapPaneToolBar.java
ViewVC logotype

Diff of /branches/2.0-RC1/src/skrueger/geotools/MapPaneToolBar.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

trunk/src/skrueger/geotools/MapPaneToolBar.java revision 256 by alfonx, Fri Jul 31 14:43:47 2009 UTC branches/1.0-gt2-2.6/src/skrueger/geotools/MapPaneToolBar.java revision 486 by alfonx, Tue Oct 20 16:25:26 2009 UTC
# Line 30  Line 30 
30  package skrueger.geotools;  package skrueger.geotools;
31    
32  import java.awt.Color;  import java.awt.Color;
 import java.awt.Dimension;  
33  import java.awt.Graphics;  import java.awt.Graphics;
34  import java.awt.event.ActionEvent;  import java.awt.event.ActionEvent;
35    import java.awt.event.ActionListener;
36  import java.util.ArrayList;  import java.util.ArrayList;
37    import java.util.HashSet;
38  import java.util.Locale;  import java.util.Locale;
39    import java.util.Set;
40  import java.util.SortedMap;  import java.util.SortedMap;
41  import java.util.TreeMap;  import java.util.TreeMap;
42    
43  import javax.swing.AbstractAction;  import javax.swing.AbstractAction;
44  import javax.swing.AbstractButton;  import javax.swing.AbstractButton;
45  import javax.swing.Action;  import javax.swing.Action;
 import javax.swing.BorderFactory;  
46  import javax.swing.Icon;  import javax.swing.Icon;
47  import javax.swing.ImageIcon;  import javax.swing.ImageIcon;
48  import javax.swing.JButton;  import javax.swing.JButton;
# Line 59  import schmitzm.lang.LangUtil; Line 60  import schmitzm.lang.LangUtil;
60  import schmitzm.lang.ResourceProvider;  import schmitzm.lang.ResourceProvider;
61  import schmitzm.swing.ButtonGroup;  import schmitzm.swing.ButtonGroup;
62  import schmitzm.swing.SwingUtil;  import schmitzm.swing.SwingUtil;
63    import skrueger.swing.SmallButton;
64    import skrueger.swing.SmallToggleButton;
65    
66  import com.vividsolutions.jts.geom.Envelope;  import com.vividsolutions.jts.geom.Envelope;
67    
# Line 76  import com.vividsolutions.jts.geom.Envel Line 79  import com.vividsolutions.jts.geom.Envel
79  public class MapPaneToolBar extends JToolBar {  public class MapPaneToolBar extends JToolBar {
80          private static final Logger LOGGER = Logger.getLogger(MapPaneToolBar.class          private static final Logger LOGGER = Logger.getLogger(MapPaneToolBar.class
81                          .getName());                          .getName());
82            
83          public static ResourceProvider RESOURCE = new ResourceProvider(LangUtil          public static ResourceProvider RESOURCE = new ResourceProvider(LangUtil
84                          .extendPackagePath(MapPaneToolBar.class,                          .extendPackagePath(MapPaneToolBar.class,
85                                          "resource.locales.mapPaneToolbar"), Locale.ENGLISH);                                          "resource.locales.mapPaneToolbar"), Locale.ENGLISH);
86            
87          public static String R(String key, Object... values) {          public static String R(String key, Object... values) {
88                  return RESOURCE.getString(key, values);                  return RESOURCE.getString(key, values);
89          }          }
# Line 122  public class MapPaneToolBar extends JToo Line 125  public class MapPaneToolBar extends JToo
125           */           */
126          public static final int TOOL_SELECTION_REMOVE = 230;          public static final int TOOL_SELECTION_REMOVE = 230;
127    
128            public static final int ACTION_CHARTS = 401;
129    
130          /** Tool currently selected */          /** Tool currently selected */
131          protected int selectedTool = TOOL_ZOOMIN;          protected int selectedTool = TOOL_ZOOMIN;
132    
133          /** Holds the tool buttons of the tool bar. */          /** Holds the tool buttons of the tool bar. */
134          protected SortedMap<Integer, JComponent> toolAndActionButtons = null;          final protected SortedMap<Integer, JComponent> toolAndActionButtons = new TreeMap<Integer, JComponent>();
135          /** Controls that only one tool button is activated. */          /** Controls that only one tool button is activated. */
136          protected ButtonGroup toolButtonGroup = null;          protected ButtonGroup toolButtonGroup = null;
137    
# Line 151  public class MapPaneToolBar extends JToo Line 156  public class MapPaneToolBar extends JToo
156    
157          protected boolean zoomBackForwardButtonInAction;          protected boolean zoomBackForwardButtonInAction;
158    
159            /** Listeners what want to be informed about a change of the selected tool **/
160            protected Set<MapPaneToolSelectedListener> toolSelectionListeners = new HashSet<MapPaneToolSelectedListener>();
161    
162            /** This listener is added to all {@link JToggleButton} **/
163            private final ActionListener toolSelectedListener = new ActionListener() {
164    
165                    @Override
166                    public void actionPerformed(ActionEvent e) {
167                            JToggleButton tb = (JToggleButton) e.getSource();
168    
169                            // Inform the listeners about a newly selected tool
170                            for (MapPaneToolSelectedListener l : toolSelectionListeners) {
171                                    l.toolSelected(Integer.valueOf(tb.getName()));
172                            }
173                    }
174    
175            };
176    
177          /**          /**
178           * Creates a new toolbar. Notice: This toolbar does nothing until           * Creates a new toolbar. Notice: This toolbar does nothing until
179           * {@link #setMapPane(JMapPane)} is called!           * {@link #setMapPane(JMapPane)} is called!
# Line 159  public class MapPaneToolBar extends JToo Line 182  public class MapPaneToolBar extends JToo
182                  this(null);                  this(null);
183          }          }
184    
185            public void addButtonSelectedListener(MapPaneToolSelectedListener listener) {
186                    toolSelectionListeners.add(listener);
187            }
188    
189            public void removeButtonSelectedListener(
190                            MapPaneToolSelectedListener listener) {
191                    toolSelectionListeners.remove(listener);
192            }
193    
194          /**          /**
195           * Creates a new tool bar.           * Creates a new tool bar.
196           *           *
# Line 167  public class MapPaneToolBar extends JToo Line 199  public class MapPaneToolBar extends JToo
199           */           */
200          public MapPaneToolBar(JMapPane mapPane) {          public MapPaneToolBar(JMapPane mapPane) {
201                  super("Control the map", JToolBar.HORIZONTAL);                  super("Control the map", JToolBar.HORIZONTAL);
202                  this.toolAndActionButtons = new TreeMap<Integer, JComponent>();  
203                    // I want to see nothing on the background
204                    setOpaque(false);
205                    setBorder(null);
206    
207                  this.toolButtonGroup = new ButtonGroup();                  this.toolButtonGroup = new ButtonGroup();
208                    
209                  // Create a Listener to listen to the zooms on the JMapPane                  // Create a Listener to listen to the zooms on the JMapPane
210                  this.mapPaneListener = new JMapPaneListener() {                  this.mapPaneListener = new JMapPaneListener() {
211                          public void performMapPaneEvent(JMapPaneEvent e) {                          public void performMapPaneEvent(JMapPaneEvent e) {
# Line 183  public class MapPaneToolBar extends JToo Line 219  public class MapPaneToolBar extends JToo
219    
220                                  final MapAreaChangedEvent mapAreaChangedEvent = (MapAreaChangedEvent) e;                                  final MapAreaChangedEvent mapAreaChangedEvent = (MapAreaChangedEvent) e;
221                                  Envelope oldMapArea = mapAreaChangedEvent.getOldMapArea();                                  Envelope oldMapArea = mapAreaChangedEvent.getOldMapArea();
222                                    
223                                  final Envelope mapArea = mapAreaChangedEvent.getNewMapArea();                                  final Envelope mapArea = mapAreaChangedEvent.getNewMapArea();
224                                  if (mapArea == null || mapArea.equals(oldMapArea) ) {                                  if (mapArea == null || mapArea.equals(oldMapArea)
225                                          // If the MapArea didn't change... we don't want to register it as a zoom action.                                                  || Double.isNaN(mapArea.getMinX())
226                                                    || Double.isNaN(mapArea.getMaxX())
227                                                    || Double.isNaN(mapArea.getMinY())
228                                                    || Double.isNaN(mapArea.getMaxY())) {
229                                            // If the MapArea didn't change... we don't want to register
230                                            // it as a zoom action.
231                                          return;                                          return;
232                                  }                                  }
233                                    
234                                  if (lastZooms.size() == 0 && oldMapArea != null) {                                  if (lastZooms.size() == 0
235                                                    && oldMapArea != null
236                                                    && !oldMapArea.isNull()
237                                                    && !(Double.isNaN(oldMapArea.getMinX())
238                                                                    || Double.isNaN(oldMapArea.getMaxX())
239                                                                    || Double.isNaN(oldMapArea.getMinY()) || Double
240                                                                    .isNaN(oldMapArea.getMaxY()))) {
241                                          lastZooms.add(oldMapArea);                                          lastZooms.add(oldMapArea);
242                                          zoomBackIndex = 1;                                          zoomBackIndex = 1;
243                                  }                                  }
# Line 242  public class MapPaneToolBar extends JToo Line 289  public class MapPaneToolBar extends JToo
289           * puts all tool buttons and all actions buttons to the tool bar.           * puts all tool buttons and all actions buttons to the tool bar.
290           */           */
291          protected void init() {          protected void init() {
292    
293                  initToolsAndActions();                  initToolsAndActions();
294    
295                  addSeparator(SEPERATOR0, new JToolBar.Separator());                  addSeparator(SEPERATOR0, new JToolBar.Separator());
# Line 260  public class MapPaneToolBar extends JToo Line 308  public class MapPaneToolBar extends JToo
308          protected void initToolsAndActions() {          protected void initToolsAndActions() {
309                  // Panning                  // Panning
310                  addTool(new MapPaneToolBarAction(TOOL_PAN, this, "", new ImageIcon(                  addTool(new MapPaneToolBarAction(TOOL_PAN, this, "", new ImageIcon(
311                                  MapView.class.getResource("resource/icons/pan.png")), R("MapPaneButtons.Pan.TT")), false);                                  MapView.class.getResource("resource/icons/pan.png")),
312                                    R("MapPaneButtons.Pan.TT")), false);
313                  // Info                  // Info
314                  addTool(new MapPaneToolBarAction(TOOL_INFO, this, "", new ImageIcon(                  addTool(new MapPaneToolBarAction(TOOL_INFO, this, "", new ImageIcon(
315                                  MapView.class.getResource("resource/icons/info.png")),R("MapPaneButtons.Info.TT")), false);                                  MapView.class.getResource("resource/icons/info.png")),
316                                    R("MapPaneButtons.Info.TT")), false);
317    
318                  // Zoom in                  // Zoom in
319                  addTool(new MapPaneToolBarAction(TOOL_ZOOMIN, this, "", new ImageIcon(                  addTool(new MapPaneToolBarAction(TOOL_ZOOMIN, this, "", new ImageIcon(
320                                  MapView.class.getResource("resource/icons/zoom_in.png")), R("MapPaneButtons.ZoomIn.TT")),                                  MapView.class.getResource("resource/icons/zoom_in.png")),
321                                  false);                                  R("MapPaneButtons.ZoomIn.TT")), false);
322                  // Zoom out                  // Zoom out
323                  addTool(new MapPaneToolBarAction(TOOL_ZOOMOUT, this, "", new ImageIcon(                  addTool(new MapPaneToolBarAction(TOOL_ZOOMOUT, this, "", new ImageIcon(
324                                  MapView.class.getResource("resource/icons/zoom_out.png")), R("MapPaneButtons.ZoomOut.TT")),                                  MapView.class.getResource("resource/icons/zoom_out.png")),
325                                  false);                                  R("MapPaneButtons.ZoomOut.TT")), false);
326    
327                  // Action button to revert the last zoom                  // Action button to revert the last zoom
328                  addAction(new MapPaneToolBarAction(ACTION_ZOOM_BACK, this, "",                  addAction(new MapPaneToolBarAction(ACTION_ZOOM_BACK, this, "",
329                                  new ImageIcon(MapView.class                                  new ImageIcon(MapView.class
330                                                  .getResource("resource/icons/zoom_back.png")), R("MapPaneButtons.LastZoom.TT")), false);                                                  .getResource("resource/icons/zoom_back.png")),
331                                    R("MapPaneButtons.LastZoom.TT")), false);
332                  setButtonEnabled(ACTION_ZOOM_BACK, false);                  setButtonEnabled(ACTION_ZOOM_BACK, false);
333    
334                  // Action button to redo the last zoom                  // Action button to redo the last zoom
335                  addAction(new MapPaneToolBarAction(ACTION_ZOOM_FORWARD, this, "",                  addAction(new MapPaneToolBarAction(ACTION_ZOOM_FORWARD, this, "",
336                                  new ImageIcon(MapView.class                                  new ImageIcon(MapView.class
337                                                  .getResource("resource/icons/zoom_forward.png")), R("MapPaneButtons.NextZoom.TT")),                                                  .getResource("resource/icons/zoom_forward.png")),
338                                  false);                                  R("MapPaneButtons.NextZoom.TT")), false);
339                  setButtonEnabled(ACTION_ZOOM_FORWARD, false);                  setButtonEnabled(ACTION_ZOOM_FORWARD, false);
340    
341                  // set the selected tool enabled                  // set the selected tool enabled
# Line 292  public class MapPaneToolBar extends JToo Line 343  public class MapPaneToolBar extends JToo
343    
344          }          }
345    
346            @Override
347            public void paint(Graphics g) {
348                    super.paint(g);
349            }
350    
351          /**          /**
352           * Clears the GUI of all components and adds all tool and action buttons to           * Clears the GUI of all components and adds all tool and action buttons to
353           * the tool bar.           * the tool bar.
354           */           */
355          public void initToolBar() {          public void initToolBar() {
356                  setAlignmentY(1f);                  // setOpaque(true);
357                    //              
358                    // setAlignmentY(1f);
359                    // setAlignmentX(0.5f);
360                  removeAll();                  removeAll();
361                  // Separator to the left of the tool actions to start  
362                  // the tool buttons with the map (not with the coordinate grid)                  // // Separator to the left of the tool actions to start
363                  Dimension dimension = new Dimension(49, 10);                  // // the tool buttons with the map (not with the coordinate grid)
364                  addSeparator(dimension);                  // Dimension dimension = new Dimension(49, 10);
365                    // addSeparator(dimension);
366    
367                  // Tool buttons                  // Tool buttons
368                  for (JComponent b : toolAndActionButtons.values())                  for (Integer bKey : toolAndActionButtons.keySet()) {
369    
370                            JComponent b = toolAndActionButtons.get(bKey);
371    
372                            if (b instanceof JToggleButton) {
373                                    JToggleButton tb = (JToggleButton) b;
374                                    tb.setName(bKey.toString());
375                                    tb.addActionListener(toolSelectedListener);
376                            }
377    
378                          add(b);                          add(b);
379                    }
380    
381                  if (!toolAndActionButtons.containsKey(selectedTool)) {                  if (!toolAndActionButtons.containsKey(selectedTool)) {
382                          /**                          /**
# Line 323  public class MapPaneToolBar extends JToo Line 394  public class MapPaneToolBar extends JToo
394                          }                          }
395    
396                  }                  }
397                    
398                  revalidate();                  revalidate();
399                  repaint();                  repaint();
400          }          }
# Line 357  public class MapPaneToolBar extends JToo Line 428  public class MapPaneToolBar extends JToo
428                          // Set the mouse tool to "Panning"                          // Set the mouse tool to "Panning"
429                          mapPane.setWindowSelectionState(JMapPane.NONE);                          mapPane.setWindowSelectionState(JMapPane.NONE);
430                          mapPane.setState(JMapPane.PAN);                          mapPane.setState(JMapPane.PAN);
                         mapPane.setHighlight(false);  
431                          mapPane.setNormalCursor(SwingUtil.PAN_CURSOR);                          mapPane.setNormalCursor(SwingUtil.PAN_CURSOR);
432                          break;                          break;
433                  case TOOL_INFO:                  case TOOL_INFO:
# Line 365  public class MapPaneToolBar extends JToo Line 435  public class MapPaneToolBar extends JToo
435                          mapPane.setWindowSelectionState(JMapPane.NONE);                          mapPane.setWindowSelectionState(JMapPane.NONE);
436                          mapPane.setState(JMapPane.SELECT_TOP); // Why not:                          mapPane.setState(JMapPane.SELECT_TOP); // Why not:
437                          // JMapPane.SELECT_TOP_ONEONLY                          // JMapPane.SELECT_TOP_ONEONLY
                         mapPane.setHighlight(false);// SK: Was true, but since it not works  
438                          // properly removed it to save                          // properly removed it to save
439                          // performance                          // performance
440                          mapPane.setNormalCursor(SwingUtil.CROSSHAIR_CURSOR);                          mapPane.setNormalCursor(SwingUtil.CROSSHAIR_CURSOR);
# Line 374  public class MapPaneToolBar extends JToo Line 443  public class MapPaneToolBar extends JToo
443                          // Set the mouse tool to "Zoom in"                          // Set the mouse tool to "Zoom in"
444                          mapPane.setWindowSelectionState(JMapPane.ZOOM_IN);                          mapPane.setWindowSelectionState(JMapPane.ZOOM_IN);
445                          mapPane.setState(JMapPane.ZOOM_IN);                          mapPane.setState(JMapPane.ZOOM_IN);
                         mapPane.setHighlight(false);  
446                          mapPane.setNormalCursor(SwingUtil.ZOOMIN_CURSOR);                          mapPane.setNormalCursor(SwingUtil.ZOOMIN_CURSOR);
447                          break;                          break;
448                  case TOOL_ZOOMOUT:                  case TOOL_ZOOMOUT:
449                          // Set the mouse tool to "Zoom out"                          // Set the mouse tool to "Zoom out"
450                          mapPane.setWindowSelectionState(JMapPane.NONE);                          mapPane.setWindowSelectionState(JMapPane.NONE);
451                          mapPane.setState(JMapPane.ZOOM_OUT);                          mapPane.setState(JMapPane.ZOOM_OUT);
                         mapPane.setHighlight(false);  
452                          mapPane.setNormalCursor(SwingUtil.ZOOMOUT_CURSOR);                          mapPane.setNormalCursor(SwingUtil.ZOOMOUT_CURSOR);
453                          break;                          break;
454                  default:                  default:
455                          // Set map actions to default                          // Set map actions to default
456                          mapPane.setWindowSelectionState(JMapPane.NONE);                          mapPane.setWindowSelectionState(JMapPane.NONE);
457                          mapPane.setState(JMapPane.NONE);                          mapPane.setState(JMapPane.NONE);
                         mapPane.setHighlight(false);  
458                          mapPane.setNormalCursor(null);                          mapPane.setNormalCursor(null);
459                          break;                          break;
460                  }                  }
# Line 464  public class MapPaneToolBar extends JToo Line 530  public class MapPaneToolBar extends JToo
530                                                          + buttonAction.getID());                                                          + buttonAction.getID());
531                          return;                          return;
532                  }                  }
533                  JToggleButton button = new JToggleButton(buttonAction);                  JToggleButton button = new SmallToggleButton(buttonAction);
534                  button.setBorder(BorderFactory.createRaisedBevelBorder());                  
535    //              button.setBorder(BorderFactory.createRaisedBevelBorder());
536                  toolButtonGroup.add(button);                  toolButtonGroup.add(button);
537                  toolAndActionButtons.put(buttonAction.getID(), button);                  toolAndActionButtons.put(buttonAction.getID(), button);
538                  if (resetToolBar)                  if (resetToolBar)
# Line 501  public class MapPaneToolBar extends JToo Line 568  public class MapPaneToolBar extends JToo
568                                                          + buttonAction.getID());                                                          + buttonAction.getID());
569                          return;                          return;
570                  }                  }
571                  JButton button = new JButton(buttonAction);                  JButton button = new SmallButton(buttonAction);
572                  button.setBorder(BorderFactory.createRaisedBevelBorder());  //              button.setBorder(BorderFactory.createRaisedBevelBorder());
573                  toolAndActionButtons.put(buttonAction.getID(), button);                  toolAndActionButtons.put(buttonAction.getID(), button);
574                  if (resetToolBar)                  if (resetToolBar)
575                          initToolBar();                          initToolBar();
576          }          }
577    
578            /**
579             * Adds any JComponent to the tool bar. Does nothing if a tool or action
580             * with the specified ID already exists!
581             *
582             * @param component
583             *            A {@link JComponent} that shall be added
584             * @param id
585             *            The ID associaded with the {@link JComponent}
586             * @param resetToolBar
587             *            indicates whether the toolbar GUI is reset after adding the
588             *            button (if adding several actions it useful only to reset the
589             *            GUI for the last added tool)
590             */
591            public void addJComponent(JComponent component, int id, boolean resetToolBar) {
592    
593                    if (isButtonIDUsed(id)) {
594                            LOGGER
595                                            .warn("addAction(.) ignored because ID already used for tool or action: "
596                                                            + id);
597                            return;
598                    }
599    
600                    toolAndActionButtons.put(id, component);
601                    if (resetToolBar)
602                            initToolBar();
603            }
604    
605          public void addSeparator(int id, Separator separator) {          public void addSeparator(int id, Separator separator) {
606                  if (isButtonIDUsed(id)) {                  if (isButtonIDUsed(id)) {
607                          LOGGER                          LOGGER
# Line 774  public class MapPaneToolBar extends JToo Line 868  public class MapPaneToolBar extends JToo
868                   */                   */
869                  public MapPaneToolBarAction(int id, MapPaneToolBar toolBar,                  public MapPaneToolBarAction(int id, MapPaneToolBar toolBar,
870                                  String name, Icon icon) {                                  String name, Icon icon) {
871                          this (id, toolBar, name, icon, null);                          this(id, toolBar, name, icon, null);
872                  }                  }
873                    
874                  /**                  /**
875                   * Creates a new action.                   * Creates a new action.
876                   *                   *
# Line 794  public class MapPaneToolBar extends JToo Line 888  public class MapPaneToolBar extends JToo
888                  public MapPaneToolBarAction(int id, MapPaneToolBar toolBar,                  public MapPaneToolBarAction(int id, MapPaneToolBar toolBar,
889                                  String name, Icon icon, String toolTip) {                                  String name, Icon icon, String toolTip) {
890                          super(name, icon);                          super(name, icon);
891                            
892                          if (toolTip != null && !toolTip.trim().isEmpty()){                          if (toolTip != null && !toolTip.trim().isEmpty()) {
893                                  putValue(Action.SHORT_DESCRIPTION, toolTip);                                  putValue(Action.SHORT_DESCRIPTION, toolTip);
894                          }                          }
895                            
896                          this.id = id;                          this.id = id;
897                          this.toolBar = toolBar;                          this.toolBar = toolBar;
898                  }                  }
# Line 826  public class MapPaneToolBar extends JToo Line 920  public class MapPaneToolBar extends JToo
920                          return id;                          return id;
921                  }                  }
922          }          }
923            
924            /**          /**
925             * Nuetzlich wenn die Componente gedruckt (z.B. wenn ein Screenshot gemacht wird) wird. Dann werden wird der           * Nuetzlich wenn die Componente gedruckt (z.B. wenn ein Screenshot gemacht
926             * Hintergrund auf WEISS gesetzt.           * wird) wird. Dann werden wird der Hintergrund auf WEISS gesetzt.
927             *           *
928             * @author <a href="mailto:[email protected]">Stefan Alfons           * @author <a href="mailto:[email protected]">Stefan Alfons
929             *         Kr&uuml;ger</a>           *         Kr&uuml;ger</a>
930             */           */
931            @Override          @Override
932            public void print(Graphics g) {          public void print(Graphics g) {
933                Color orig = getBackground();                  Color orig = getBackground();
934                setBackground(Color.WHITE);                  setBackground(Color.WHITE);
935                // wrap in try/finally so that we always restore the state                  // wrap in try/finally so that we always restore the state
936                try {                  try {
937                    super.print(g);                          super.print(g);
938                } finally {                  } finally {
939                    setBackground(orig);                          setBackground(orig);
940                }                  }
941            }          }
942    
943  }  }

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26