/[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 444 by alfonx, Fri Sep 25 16:34:10 2009 UTC revision 445 by alfonx, Wed Oct 7 13:17:02 2009 UTC
# Line 78  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 158  public class MapPaneToolBar extends JToo Line 158  public class MapPaneToolBar extends JToo
158          /** Listeners what want to be informed about a change of the selected tool **/          /** Listeners what want to be informed about a change of the selected tool **/
159          protected Set<MapPaneToolSelectedListener> toolSelectionListeners = new HashSet<MapPaneToolSelectedListener>();          protected Set<MapPaneToolSelectedListener> toolSelectionListeners = new HashSet<MapPaneToolSelectedListener>();
160    
161          /** This listener is added to all {@link JToggleButton}  **/          /** This listener is added to all {@link JToggleButton} **/
162          private final ActionListener toolSelectedListener = new ActionListener(){          private final ActionListener toolSelectedListener = new ActionListener() {
163    
164                  @Override                  @Override
165                  public void actionPerformed(ActionEvent e) {                  public void actionPerformed(ActionEvent e) {
166                          JToggleButton tb = (JToggleButton) e.getSource();                          JToggleButton tb = (JToggleButton) e.getSource();
167                            
168                          // Inform the listeners about a newly selected tool                          // Inform the listeners about a newly selected tool
169                          for (MapPaneToolSelectedListener l : toolSelectionListeners) {                          for (MapPaneToolSelectedListener l : toolSelectionListeners) {
170                                  l.toolSelected(Integer.valueOf( tb.getName() ) ) ;                                  l.toolSelected(Integer.valueOf(tb.getName()));
171                          }                          }
172                  }                  }
173                    
174          };          };
175    
176          /**          /**
# Line 180  public class MapPaneToolBar extends JToo Line 180  public class MapPaneToolBar extends JToo
180          public MapPaneToolBar() {          public MapPaneToolBar() {
181                  this(null);                  this(null);
182          }          }
183            
184          public void addButtonSelectedListener(MapPaneToolSelectedListener listener ) {          public void addButtonSelectedListener(MapPaneToolSelectedListener listener) {
185                  toolSelectionListeners.add(listener);                  toolSelectionListeners.add(listener);
186          }          }
187    
188          public void removeButtonSelectedListener(MapPaneToolSelectedListener listener ) {          public void removeButtonSelectedListener(
189                  toolSelectionListeners .remove(listener);                          MapPaneToolSelectedListener listener) {
190                    toolSelectionListeners.remove(listener);
191          }          }
192    
193          /**          /**
# Line 197  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                    
202                  // I want to see nothing on the background                  // I want to see nothing on the background
203                  setOpaque(false);                  setOpaque(false);
204                  setBorder(null);                  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 217  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 276  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 295  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
341                  setSelectedTool(selectedTool);                  setSelectedTool(selectedTool);
342    
343          }          }
344            
345          @Override          @Override
346          public void paint(Graphics g) {          public void paint(Graphics g) {
347                  super.paint(g);                  super.paint(g);
# Line 337  public class MapPaneToolBar extends JToo Line 352  public class MapPaneToolBar extends JToo
352           * the tool bar.           * the tool bar.
353           */           */
354          public void initToolBar() {          public void initToolBar() {
355  //              setOpaque(true);                  // setOpaque(true);
356  //                                //              
357  //              setAlignmentY(1f);                  // setAlignmentY(1f);
358  //              setAlignmentX(0.5f);                  // setAlignmentX(0.5f);
359                  removeAll();                  removeAll();
360                    
361  //              // Separator to the left of the tool actions to start                  // // Separator to the left of the tool actions to start
362  //              // the tool buttons with the map (not with the coordinate grid)                  // // the tool buttons with the map (not with the coordinate grid)
363  //              Dimension dimension = new Dimension(49, 10);                  // Dimension dimension = new Dimension(49, 10);
364  //              addSeparator(dimension);                  // addSeparator(dimension);
365                    
                   
366                  // Tool buttons                  // Tool buttons
367                  for (Integer bKey : toolAndActionButtons.keySet()) {                  for (Integer bKey : toolAndActionButtons.keySet()) {
368                            
369                          JComponent b = toolAndActionButtons.get(bKey);                          JComponent b = toolAndActionButtons.get(bKey);
370                            
371                          if (b instanceof JToggleButton) {                          if (b instanceof JToggleButton) {
372                                  JToggleButton tb = (JToggleButton) b;                                  JToggleButton tb = (JToggleButton) b;
373                                  tb.setName(bKey.toString());                                  tb.setName(bKey.toString());
374                                  tb.addActionListener( toolSelectedListener );                                  tb.addActionListener(toolSelectedListener);
375                          }                          }
376                            
377                          add(b);                          add(b);
378                  }                  }
379    
# Line 379  public class MapPaneToolBar extends JToo Line 393  public class MapPaneToolBar extends JToo
393                          }                          }
394    
395                  }                  }
396                    
397                  revalidate();                  revalidate();
398                  repaint();                  repaint();
399          }          }
# Line 523  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 560  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 853  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 873  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 905  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.444  
changed lines
  Added in v.445

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26