/[schmitzm]/trunk/src/skrueger/geotools/MapPaneToolBar.java
ViewVC logotype

Diff of /trunk/src/skrueger/geotools/MapPaneToolBar.java

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

revision 315 by mojays, Wed Aug 26 11:03:27 2009 UTC revision 445 by alfonx, Wed Oct 7 13:17:02 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    
# Line 76  import com.vividsolutions.jts.geom.Envel Line 78  import com.vividsolutions.jts.geom.Envel
78  public class MapPaneToolBar extends JToolBar {  public class MapPaneToolBar extends JToolBar {
79          private static final Logger LOGGER = Logger.getLogger(MapPaneToolBar.class          private static final Logger LOGGER = Logger.getLogger(MapPaneToolBar.class
80                          .getName());                          .getName());
81            
82          public static ResourceProvider RESOURCE = new ResourceProvider(LangUtil          public static ResourceProvider RESOURCE = new ResourceProvider(LangUtil
83                          .extendPackagePath(MapPaneToolBar.class,                          .extendPackagePath(MapPaneToolBar.class,
84                                          "resource.locales.mapPaneToolbar"), Locale.ENGLISH);                                          "resource.locales.mapPaneToolbar"), Locale.ENGLISH);
85            
86          public static String R(String key, Object... values) {          public static String R(String key, Object... values) {
87                  return RESOURCE.getString(key, values);                  return RESOURCE.getString(key, values);
88          }          }
# Line 128  public class MapPaneToolBar extends JToo Line 130  public class MapPaneToolBar extends JToo
130          protected int selectedTool = TOOL_ZOOMIN;          protected int selectedTool = TOOL_ZOOMIN;
131    
132          /** Holds the tool buttons of the tool bar. */          /** Holds the tool buttons of the tool bar. */
133          protected SortedMap<Integer, JComponent> toolAndActionButtons = null;          final protected SortedMap<Integer, JComponent> toolAndActionButtons = new TreeMap<Integer, JComponent>();
134          /** Controls that only one tool button is activated. */          /** Controls that only one tool button is activated. */
135          protected ButtonGroup toolButtonGroup = null;          protected ButtonGroup toolButtonGroup = null;
136    
# Line 153  public class MapPaneToolBar extends JToo Line 155  public class MapPaneToolBar extends JToo
155    
156          protected boolean zoomBackForwardButtonInAction;          protected boolean zoomBackForwardButtonInAction;
157    
158            /** Listeners what want to be informed about a change of the selected tool **/
159            protected Set<MapPaneToolSelectedListener> toolSelectionListeners = new HashSet<MapPaneToolSelectedListener>();
160    
161            /** This listener is added to all {@link JToggleButton} **/
162            private final ActionListener toolSelectedListener = new ActionListener() {
163    
164                    @Override
165                    public void actionPerformed(ActionEvent e) {
166                            JToggleButton tb = (JToggleButton) e.getSource();
167    
168                            // Inform the listeners about a newly selected tool
169                            for (MapPaneToolSelectedListener l : toolSelectionListeners) {
170                                    l.toolSelected(Integer.valueOf(tb.getName()));
171                            }
172                    }
173    
174            };
175    
176          /**          /**
177           * Creates a new toolbar. Notice: This toolbar does nothing until           * Creates a new toolbar. Notice: This toolbar does nothing until
178           * {@link #setMapPane(JMapPane)} is called!           * {@link #setMapPane(JMapPane)} is called!
# Line 161  public class MapPaneToolBar extends JToo Line 181  public class MapPaneToolBar extends JToo
181                  this(null);                  this(null);
182          }          }
183    
184            public void addButtonSelectedListener(MapPaneToolSelectedListener listener) {
185                    toolSelectionListeners.add(listener);
186            }
187    
188            public void removeButtonSelectedListener(
189                            MapPaneToolSelectedListener listener) {
190                    toolSelectionListeners.remove(listener);
191            }
192    
193          /**          /**
194           * Creates a new tool bar.           * Creates a new tool bar.
195           *           *
# Line 169  public class MapPaneToolBar extends JToo Line 198  public class MapPaneToolBar extends JToo
198           */           */
199          public MapPaneToolBar(JMapPane mapPane) {          public MapPaneToolBar(JMapPane mapPane) {
200                  super("Control the map", JToolBar.HORIZONTAL);                  super("Control the map", JToolBar.HORIZONTAL);
201                  this.toolAndActionButtons = new TreeMap<Integer, JComponent>();  
202                    // I want to see nothing on the background
203                    setOpaque(false);
204                    setBorder(null);
205    
206                  this.toolButtonGroup = new ButtonGroup();                  this.toolButtonGroup = new ButtonGroup();
207                    
208                  // Create a Listener to listen to the zooms on the JMapPane                  // Create a Listener to listen to the zooms on the JMapPane
209                  this.mapPaneListener = new JMapPaneListener() {                  this.mapPaneListener = new JMapPaneListener() {
210                          public void performMapPaneEvent(JMapPaneEvent e) {                          public void performMapPaneEvent(JMapPaneEvent e) {
# Line 185  public class MapPaneToolBar extends JToo Line 218  public class MapPaneToolBar extends JToo
218    
219                                  final MapAreaChangedEvent mapAreaChangedEvent = (MapAreaChangedEvent) e;                                  final MapAreaChangedEvent mapAreaChangedEvent = (MapAreaChangedEvent) e;
220                                  Envelope oldMapArea = mapAreaChangedEvent.getOldMapArea();                                  Envelope oldMapArea = mapAreaChangedEvent.getOldMapArea();
221                                    
222                                  final Envelope mapArea = mapAreaChangedEvent.getNewMapArea();                                  final Envelope mapArea = mapAreaChangedEvent.getNewMapArea();
223                                  if (mapArea == null || mapArea.equals(oldMapArea) ) {                                  if (mapArea == null || mapArea.equals(oldMapArea)
224                                          // If the MapArea didn't change... we don't want to register it as a zoom action.                                                  || Double.isNaN(mapArea.getMinX())
225                                                    || Double.isNaN(mapArea.getMaxX())
226                                                    || Double.isNaN(mapArea.getMinY())
227                                                    || Double.isNaN(mapArea.getMaxY())) {
228                                            // If the MapArea didn't change... we don't want to register
229                                            // it as a zoom action.
230                                          return;                                          return;
231                                  }                                  }
232                                    
233                                  if (lastZooms.size() == 0 && oldMapArea != null) {                                  if (lastZooms.size() == 0
234                                                    && oldMapArea != null
235                                                    && !oldMapArea.isNull()
236                                                    && !(Double.isNaN(oldMapArea.getMinX())
237                                                                    || Double.isNaN(oldMapArea.getMaxX())
238                                                                    || Double.isNaN(oldMapArea.getMinY()) || Double
239                                                                    .isNaN(oldMapArea.getMaxY()))) {
240                                          lastZooms.add(oldMapArea);                                          lastZooms.add(oldMapArea);
241                                          zoomBackIndex = 1;                                          zoomBackIndex = 1;
242                                  }                                  }
# Line 244  public class MapPaneToolBar extends JToo Line 288  public class MapPaneToolBar extends JToo
288           * puts all tool buttons and all actions buttons to the tool bar.           * puts all tool buttons and all actions buttons to the tool bar.
289           */           */
290          protected void init() {          protected void init() {
291    
292                  initToolsAndActions();                  initToolsAndActions();
293    
294                  addSeparator(SEPERATOR0, new JToolBar.Separator());                  addSeparator(SEPERATOR0, new JToolBar.Separator());
# Line 262  public class MapPaneToolBar extends JToo Line 307  public class MapPaneToolBar extends JToo
307          protected void initToolsAndActions() {          protected void initToolsAndActions() {
308                  // Panning                  // Panning
309                  addTool(new MapPaneToolBarAction(TOOL_PAN, this, "", new ImageIcon(                  addTool(new MapPaneToolBarAction(TOOL_PAN, this, "", new ImageIcon(
310                                  MapView.class.getResource("resource/icons/pan.png")), R("MapPaneButtons.Pan.TT")), false);                                  MapView.class.getResource("resource/icons/pan.png")),
311                                    R("MapPaneButtons.Pan.TT")), false);
312                  // Info                  // Info
313                  addTool(new MapPaneToolBarAction(TOOL_INFO, this, "", new ImageIcon(                  addTool(new MapPaneToolBarAction(TOOL_INFO, this, "", new ImageIcon(
314                                  MapView.class.getResource("resource/icons/info.png")),R("MapPaneButtons.Info.TT")), false);                                  MapView.class.getResource("resource/icons/info.png")),
315                                    R("MapPaneButtons.Info.TT")), false);
316    
317                  // Zoom in                  // Zoom in
318                  addTool(new MapPaneToolBarAction(TOOL_ZOOMIN, this, "", new ImageIcon(                  addTool(new MapPaneToolBarAction(TOOL_ZOOMIN, this, "", new ImageIcon(
319                                  MapView.class.getResource("resource/icons/zoom_in.png")), R("MapPaneButtons.ZoomIn.TT")),                                  MapView.class.getResource("resource/icons/zoom_in.png")),
320                                  false);                                  R("MapPaneButtons.ZoomIn.TT")), false);
321                  // Zoom out                  // Zoom out
322                  addTool(new MapPaneToolBarAction(TOOL_ZOOMOUT, this, "", new ImageIcon(                  addTool(new MapPaneToolBarAction(TOOL_ZOOMOUT, this, "", new ImageIcon(
323                                  MapView.class.getResource("resource/icons/zoom_out.png")), R("MapPaneButtons.ZoomOut.TT")),                                  MapView.class.getResource("resource/icons/zoom_out.png")),
324                                  false);                                  R("MapPaneButtons.ZoomOut.TT")), false);
325    
326                  // Action button to revert the last zoom                  // Action button to revert the last zoom
327                  addAction(new MapPaneToolBarAction(ACTION_ZOOM_BACK, this, "",                  addAction(new MapPaneToolBarAction(ACTION_ZOOM_BACK, this, "",
328                                  new ImageIcon(MapView.class                                  new ImageIcon(MapView.class
329                                                  .getResource("resource/icons/zoom_back.png")), R("MapPaneButtons.LastZoom.TT")), false);                                                  .getResource("resource/icons/zoom_back.png")),
330                                    R("MapPaneButtons.LastZoom.TT")), false);
331                  setButtonEnabled(ACTION_ZOOM_BACK, false);                  setButtonEnabled(ACTION_ZOOM_BACK, false);
332    
333                  // Action button to redo the last zoom                  // Action button to redo the last zoom
334                  addAction(new MapPaneToolBarAction(ACTION_ZOOM_FORWARD, this, "",                  addAction(new MapPaneToolBarAction(ACTION_ZOOM_FORWARD, this, "",
335                                  new ImageIcon(MapView.class                                  new ImageIcon(MapView.class
336                                                  .getResource("resource/icons/zoom_forward.png")), R("MapPaneButtons.NextZoom.TT")),                                                  .getResource("resource/icons/zoom_forward.png")),
337                                  false);                                  R("MapPaneButtons.NextZoom.TT")), false);
338                  setButtonEnabled(ACTION_ZOOM_FORWARD, false);                  setButtonEnabled(ACTION_ZOOM_FORWARD, false);
339    
340                  // set the selected tool enabled                  // set the selected tool enabled
# Line 294  public class MapPaneToolBar extends JToo Line 342  public class MapPaneToolBar extends JToo
342    
343          }          }
344    
345            @Override
346            public void paint(Graphics g) {
347                    super.paint(g);
348            }
349    
350          /**          /**
351           * 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
352           * the tool bar.           * the tool bar.
353           */           */
354          public void initToolBar() {          public void initToolBar() {
355                  setAlignmentY(1f);                  // setOpaque(true);
356                    //              
357                    // setAlignmentY(1f);
358                    // setAlignmentX(0.5f);
359                  removeAll();                  removeAll();
360                  // Separator to the left of the tool actions to start  
361                  // the tool buttons with the map (not with the coordinate grid)                  // // Separator to the left of the tool actions to start
362                  Dimension dimension = new Dimension(49, 10);                  // // the tool buttons with the map (not with the coordinate grid)
363                  addSeparator(dimension);                  // Dimension dimension = new Dimension(49, 10);
364                    // addSeparator(dimension);
365    
366                  // Tool buttons                  // Tool buttons
367                  for (JComponent b : toolAndActionButtons.values())                  for (Integer bKey : toolAndActionButtons.keySet()) {
368    
369                            JComponent b = toolAndActionButtons.get(bKey);
370    
371                            if (b instanceof JToggleButton) {
372                                    JToggleButton tb = (JToggleButton) b;
373                                    tb.setName(bKey.toString());
374                                    tb.addActionListener(toolSelectedListener);
375                            }
376    
377                          add(b);                          add(b);
378                    }
379    
380                  if (!toolAndActionButtons.containsKey(selectedTool)) {                  if (!toolAndActionButtons.containsKey(selectedTool)) {
381                          /**                          /**
# Line 325  public class MapPaneToolBar extends JToo Line 393  public class MapPaneToolBar extends JToo
393                          }                          }
394    
395                  }                  }
396                    
397                  revalidate();                  revalidate();
398                  repaint();                  repaint();
399          }          }
# Line 359  public class MapPaneToolBar extends JToo Line 427  public class MapPaneToolBar extends JToo
427                          // Set the mouse tool to "Panning"                          // Set the mouse tool to "Panning"
428                          mapPane.setWindowSelectionState(JMapPane.NONE);                          mapPane.setWindowSelectionState(JMapPane.NONE);
429                          mapPane.setState(JMapPane.PAN);                          mapPane.setState(JMapPane.PAN);
                         mapPane.setHighlight(false);  
430                          mapPane.setNormalCursor(SwingUtil.PAN_CURSOR);                          mapPane.setNormalCursor(SwingUtil.PAN_CURSOR);
431                          break;                          break;
432                  case TOOL_INFO:                  case TOOL_INFO:
# Line 367  public class MapPaneToolBar extends JToo Line 434  public class MapPaneToolBar extends JToo
434                          mapPane.setWindowSelectionState(JMapPane.NONE);                          mapPane.setWindowSelectionState(JMapPane.NONE);
435                          mapPane.setState(JMapPane.SELECT_TOP); // Why not:                          mapPane.setState(JMapPane.SELECT_TOP); // Why not:
436                          // JMapPane.SELECT_TOP_ONEONLY                          // JMapPane.SELECT_TOP_ONEONLY
                         mapPane.setHighlight(false);// SK: Was true, but since it not works  
437                          // properly removed it to save                          // properly removed it to save
438                          // performance                          // performance
439                          mapPane.setNormalCursor(SwingUtil.CROSSHAIR_CURSOR);                          mapPane.setNormalCursor(SwingUtil.CROSSHAIR_CURSOR);
# Line 376  public class MapPaneToolBar extends JToo Line 442  public class MapPaneToolBar extends JToo
442                          // Set the mouse tool to "Zoom in"                          // Set the mouse tool to "Zoom in"
443                          mapPane.setWindowSelectionState(JMapPane.ZOOM_IN);                          mapPane.setWindowSelectionState(JMapPane.ZOOM_IN);
444                          mapPane.setState(JMapPane.ZOOM_IN);                          mapPane.setState(JMapPane.ZOOM_IN);
                         mapPane.setHighlight(false);  
445                          mapPane.setNormalCursor(SwingUtil.ZOOMIN_CURSOR);                          mapPane.setNormalCursor(SwingUtil.ZOOMIN_CURSOR);
446                          break;                          break;
447                  case TOOL_ZOOMOUT:                  case TOOL_ZOOMOUT:
448                          // Set the mouse tool to "Zoom out"                          // Set the mouse tool to "Zoom out"
449                          mapPane.setWindowSelectionState(JMapPane.NONE);                          mapPane.setWindowSelectionState(JMapPane.NONE);
450                          mapPane.setState(JMapPane.ZOOM_OUT);                          mapPane.setState(JMapPane.ZOOM_OUT);
                         mapPane.setHighlight(false);  
451                          mapPane.setNormalCursor(SwingUtil.ZOOMOUT_CURSOR);                          mapPane.setNormalCursor(SwingUtil.ZOOMOUT_CURSOR);
452                          break;                          break;
453                  default:                  default:
454                          // Set map actions to default                          // Set map actions to default
455                          mapPane.setWindowSelectionState(JMapPane.NONE);                          mapPane.setWindowSelectionState(JMapPane.NONE);
456                          mapPane.setState(JMapPane.NONE);                          mapPane.setState(JMapPane.NONE);
                         mapPane.setHighlight(false);  
457                          mapPane.setNormalCursor(null);                          mapPane.setNormalCursor(null);
458                          break;                          break;
459                  }                  }
# Line 474  public class MapPaneToolBar extends JToo Line 537  public class MapPaneToolBar extends JToo
537                          initToolBar();                          initToolBar();
538          }          }
539    
   
   
540          /**          /**
541           * Adds a tool to the tool bar and resets the toolbar GUI.           * Adds a tool to the tool bar and resets the toolbar GUI.
542           *           *
# Line 511  public class MapPaneToolBar extends JToo Line 572  public class MapPaneToolBar extends JToo
572                  if (resetToolBar)                  if (resetToolBar)
573                          initToolBar();                          initToolBar();
574          }          }
575            
576          /**          /**
577           * Adds any JComponent to the tool bar. Does nothing if a tool or action with the           * Adds any JComponent to the tool bar. Does nothing if a tool or action
578           * specified ID already exists!           * with the specified ID already exists!
579           *           *
580           * @param component A {@link JComponent} that shall be added           * @param component
581           * @param id The ID associaded with the {@link JComponent}           *            A {@link JComponent} that shall be added
582             * @param id
583             *            The ID associaded with the {@link JComponent}
584           * @param resetToolBar           * @param resetToolBar
585           *            indicates whether the toolbar GUI is reset after adding the           *            indicates whether the toolbar GUI is reset after adding the
586           *            button (if adding several actions it useful only to reset the           *            button (if adding several actions it useful only to reset the
587           *            GUI for the last added tool)           *            GUI for the last added tool)
588           */           */
589          public void addJComponent(JComponent component, int id,          public void addJComponent(JComponent component, int id, boolean resetToolBar) {
                         boolean resetToolBar) {  
590    
591                  if (isButtonIDUsed(id)) {                  if (isButtonIDUsed(id)) {
592                          LOGGER                          LOGGER
593                          .warn("addAction(.) ignored because ID already used for tool or action: "                                          .warn("addAction(.) ignored because ID already used for tool or action: "
594                                          + id);                                                          + id);
595                          return;                          return;
596                  }                  }
597                    
598                  toolAndActionButtons.put(id, component);                  toolAndActionButtons.put(id, component);
599                  if (resetToolBar)                  if (resetToolBar)
600                          initToolBar();                          initToolBar();
# Line 804  public class MapPaneToolBar extends JToo Line 866  public class MapPaneToolBar extends JToo
866                   */                   */
867                  public MapPaneToolBarAction(int id, MapPaneToolBar toolBar,                  public MapPaneToolBarAction(int id, MapPaneToolBar toolBar,
868                                  String name, Icon icon) {                                  String name, Icon icon) {
869                          this (id, toolBar, name, icon, null);                          this(id, toolBar, name, icon, null);
870                  }                  }
871                    
872                  /**                  /**
873                   * Creates a new action.                   * Creates a new action.
874                   *                   *
# Line 824  public class MapPaneToolBar extends JToo Line 886  public class MapPaneToolBar extends JToo
886                  public MapPaneToolBarAction(int id, MapPaneToolBar toolBar,                  public MapPaneToolBarAction(int id, MapPaneToolBar toolBar,
887                                  String name, Icon icon, String toolTip) {                                  String name, Icon icon, String toolTip) {
888                          super(name, icon);                          super(name, icon);
889                            
890                          if (toolTip != null && !toolTip.trim().isEmpty()){                          if (toolTip != null && !toolTip.trim().isEmpty()) {
891                                  putValue(Action.SHORT_DESCRIPTION, toolTip);                                  putValue(Action.SHORT_DESCRIPTION, toolTip);
892                          }                          }
893                            
894                          this.id = id;                          this.id = id;
895                          this.toolBar = toolBar;                          this.toolBar = toolBar;
896                  }                  }
# Line 856  public class MapPaneToolBar extends JToo Line 918  public class MapPaneToolBar extends JToo
918                          return id;                          return id;
919                  }                  }
920          }          }
921            
922            /**          /**
923             * 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
924             * Hintergrund auf WEISS gesetzt.           * wird) wird. Dann werden wird der Hintergrund auf WEISS gesetzt.
925             *           *
926             * @author <a href="mailto:[email protected]">Stefan Alfons           * @author <a href="mailto:[email protected]">Stefan Alfons
927             *         Kr&uuml;ger</a>           *         Kr&uuml;ger</a>
928             */           */
929            @Override          @Override
930            public void print(Graphics g) {          public void print(Graphics g) {
931                Color orig = getBackground();                  Color orig = getBackground();
932                setBackground(Color.WHITE);                  setBackground(Color.WHITE);
933                // wrap in try/finally so that we always restore the state                  // wrap in try/finally so that we always restore the state
934                try {                  try {
935                    super.print(g);                          super.print(g);
936                } finally {                  } finally {
937                    setBackground(orig);                          setBackground(orig);
938                }                  }
939            }          }
940    
941  }  }

Legend:
Removed from v.315  
changed lines
  Added in v.445

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26