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

Diff of /branches/1.0-gt2-2.6/src/skrueger/geotools/MapPaneToolBar.java

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

revision 140 by alfonx, Sun Jun 14 17:24:44 2009 UTC revision 180 by alfonx, Thu Jul 2 14:44:57 2009 UTC
# Line 1  Line 1 
1  package skrueger.geotools;  package skrueger.geotools;
2    
3    import java.awt.Color;
4  import java.awt.Dimension;  import java.awt.Dimension;
5    import java.awt.Graphics;
6  import java.awt.event.ActionEvent;  import java.awt.event.ActionEvent;
7  import java.util.ArrayList;  import java.util.ArrayList;
8    import java.util.Locale;
9  import java.util.SortedMap;  import java.util.SortedMap;
10  import java.util.TreeMap;  import java.util.TreeMap;
11    
12  import javax.swing.AbstractAction;  import javax.swing.AbstractAction;
13  import javax.swing.AbstractButton;  import javax.swing.AbstractButton;
14    import javax.swing.Action;
15  import javax.swing.BorderFactory;  import javax.swing.BorderFactory;
16  import javax.swing.Icon;  import javax.swing.Icon;
17  import javax.swing.ImageIcon;  import javax.swing.ImageIcon;
# Line 22  import schmitzm.geotools.gui.JMapPane; Line 26  import schmitzm.geotools.gui.JMapPane;
26  import schmitzm.geotools.map.event.JMapPaneEvent;  import schmitzm.geotools.map.event.JMapPaneEvent;
27  import schmitzm.geotools.map.event.JMapPaneListener;  import schmitzm.geotools.map.event.JMapPaneListener;
28  import schmitzm.geotools.map.event.MapAreaChangedEvent;  import schmitzm.geotools.map.event.MapAreaChangedEvent;
29    import schmitzm.lang.LangUtil;
30    import schmitzm.lang.ResourceProvider;
31  import schmitzm.swing.ButtonGroup;  import schmitzm.swing.ButtonGroup;
32  import schmitzm.swing.SwingUtil;  import schmitzm.swing.SwingUtil;
33    
# Line 41  import com.vividsolutions.jts.geom.Envel Line 47  import com.vividsolutions.jts.geom.Envel
47  public class MapPaneToolBar extends JToolBar {  public class MapPaneToolBar extends JToolBar {
48          private static final Logger LOGGER = Logger.getLogger(MapPaneToolBar.class          private static final Logger LOGGER = Logger.getLogger(MapPaneToolBar.class
49                          .getName());                          .getName());
50            
51            public static ResourceProvider RESOURCE = new ResourceProvider(LangUtil
52                            .extendPackagePath(MapPaneToolBar.class,
53                                            "resource.locales.mapPaneToolbar"), Locale.ENGLISH);
54            
55            public static String R(String key, Object... values) {
56                    return RESOURCE.getString(key, values);
57            }
58    
59          /** Constant for the tool "Panning" (10). */          /** Constant for the tool "Panning" (10). */
60          public static final int TOOL_PAN = 10;          public static final int TOOL_PAN = 10;
61          /** Constant for the tool "Info" (20). */          /** Constant for the tool "Info" (20). */
# Line 125  public class MapPaneToolBar extends JToo Line 140  public class MapPaneToolBar extends JToo
140                  super("Control the map", JToolBar.HORIZONTAL);                  super("Control the map", JToolBar.HORIZONTAL);
141                  this.toolAndActionButtons = new TreeMap<Integer, JComponent>();                  this.toolAndActionButtons = new TreeMap<Integer, JComponent>();
142                  this.toolButtonGroup = new ButtonGroup();                  this.toolButtonGroup = new ButtonGroup();
143                  // Create a Listener to sniff the zooms on the JMapPane                  
144                    // Create a Listener to listen to the zooms on the JMapPane
145                  this.mapPaneListener = new JMapPaneListener() {                  this.mapPaneListener = new JMapPaneListener() {
146                          public void performMapPaneEvent(JMapPaneEvent e) {                          public void performMapPaneEvent(JMapPaneEvent e) {
147                                  if (!(e instanceof MapAreaChangedEvent))                                  if (!(e instanceof MapAreaChangedEvent))
# Line 136  public class MapPaneToolBar extends JToo Line 152  public class MapPaneToolBar extends JToo
152                                          return;                                          return;
153                                  }                                  }
154    
155                                  Envelope oldMapArea = ((MapAreaChangedEvent) e).getOldMapArea();                                  final MapAreaChangedEvent mapAreaChangedEvent = (MapAreaChangedEvent) e;
156                                    Envelope oldMapArea = mapAreaChangedEvent.getOldMapArea();
157                                    
158                                    final Envelope mapArea = mapAreaChangedEvent.getNewMapArea();
159                                    if (mapArea == null || mapArea.equals(oldMapArea) ) {
160                                            // If the MapArea didn't change... we don't want to register it as a zoom action.
161                                            return;
162                                    }
163                                    
164                                  if (lastZooms.size() == 0 && oldMapArea != null) {                                  if (lastZooms.size() == 0 && oldMapArea != null) {
165                                          lastZooms.add(oldMapArea);                                          lastZooms.add(oldMapArea);
166                                          zoomBackIndex = 1;                                          zoomBackIndex = 1;
167                                  }                                  }
   
                                 final Envelope mapArea = ((MapAreaChangedEvent) e)  
                                                 .getNewMapArea();  
168                                  if (mapArea == null)                                  if (mapArea == null)
169                                          return;                                          return;
170    
# Line 210  public class MapPaneToolBar extends JToo Line 231  public class MapPaneToolBar extends JToo
231          protected void initToolsAndActions() {          protected void initToolsAndActions() {
232                  // Panning                  // Panning
233                  addTool(new MapPaneToolBarAction(TOOL_PAN, this, "", new ImageIcon(                  addTool(new MapPaneToolBarAction(TOOL_PAN, this, "", new ImageIcon(
234                                  MapView.class.getResource("resource/icons/pan.png"))), false);                                  MapView.class.getResource("resource/icons/pan.png")), R("MapPaneButtons.Pan.TT")), false);
235                  // Info                  // Info
236                  addTool(new MapPaneToolBarAction(TOOL_INFO, this, "", new ImageIcon(                  addTool(new MapPaneToolBarAction(TOOL_INFO, this, "", new ImageIcon(
237                                  MapView.class.getResource("resource/icons/info.png"))), false);                                  MapView.class.getResource("resource/icons/info.png")),R("MapPaneButtons.Info.TT")), false);
238    
239                  // Zoom in                  // Zoom in
240                  addTool(new MapPaneToolBarAction(TOOL_ZOOMIN, this, "", new ImageIcon(                  addTool(new MapPaneToolBarAction(TOOL_ZOOMIN, this, "", new ImageIcon(
241                                  MapView.class.getResource("resource/icons/zoom_in.png"))),                                  MapView.class.getResource("resource/icons/zoom_in.png")), R("MapPaneButtons.ZoomIn.TT")),
242                                  false);                                  false);
243                  // Zoom out                  // Zoom out
244                  addTool(new MapPaneToolBarAction(TOOL_ZOOMOUT, this, "", new ImageIcon(                  addTool(new MapPaneToolBarAction(TOOL_ZOOMOUT, this, "", new ImageIcon(
245                                  MapView.class.getResource("resource/icons/zoom_out.png"))),                                  MapView.class.getResource("resource/icons/zoom_out.png")), R("MapPaneButtons.ZoomOut.TT")),
246                                  false);                                  false);
247    
248                  // Action button to revert the last zoom                  // Action button to revert the last zoom
249                  addAction(new MapPaneToolBarAction(ACTION_ZOOM_BACK, this, "",                  addAction(new MapPaneToolBarAction(ACTION_ZOOM_BACK, this, "",
250                                  new ImageIcon(MapView.class                                  new ImageIcon(MapView.class
251                                                  .getResource("resource/icons/zoom_back.png"))), false);                                                  .getResource("resource/icons/zoom_back.png")), R("MapPaneButtons.LastZoom.TT")), false);
252                  setButtonEnabled(ACTION_ZOOM_BACK, false);                  setButtonEnabled(ACTION_ZOOM_BACK, false);
253    
254                  // Action button to redo the last zoom                  // Action button to redo the last zoom
255                  addAction(new MapPaneToolBarAction(ACTION_ZOOM_FORWARD, this, "",                  addAction(new MapPaneToolBarAction(ACTION_ZOOM_FORWARD, this, "",
256                                  new ImageIcon(MapView.class                                  new ImageIcon(MapView.class
257                                                  .getResource("resource/icons/zoom_forward.png"))),                                                  .getResource("resource/icons/zoom_forward.png")), R("MapPaneButtons.NextZoom.TT")),
258                                  false);                                  false);
259                  setButtonEnabled(ACTION_ZOOM_FORWARD, false);                  setButtonEnabled(ACTION_ZOOM_FORWARD, false);
260    
# Line 246  public class MapPaneToolBar extends JToo Line 267  public class MapPaneToolBar extends JToo
267           * 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
268           * the tool bar.           * the tool bar.
269           */           */
270          protected void initToolBar() {          public void initToolBar() {
271                  setAlignmentY(1f);                  setAlignmentY(1f);
272                  removeAll();                  removeAll();
273                  // Separator to the left of the tool actions to start                  // Separator to the left of the tool actions to start
# Line 256  public class MapPaneToolBar extends JToo Line 277  public class MapPaneToolBar extends JToo
277                  // Tool buttons                  // Tool buttons
278                  for (JComponent b : toolAndActionButtons.values())                  for (JComponent b : toolAndActionButtons.values())
279                          add(b);                          add(b);
280    
281                    if (!toolAndActionButtons.containsKey(selectedTool)) {
282                            /**
283                             * This might be a bit specific, but IF selection buttons are
284                             * available, select one of them.. if not, select the INFO tool.
285                             */
286    
287                            if (toolAndActionButtons.containsKey(TOOL_SELECTION_SET)) {
288                                    setSelectedTool(TOOL_SELECTION_SET);
289                            } else if (toolAndActionButtons.containsKey(TOOL_INFO)) {
290                                    setSelectedTool(TOOL_INFO);
291                            } else {
292                                    // TODO What to do now?!
293                                    setSelectedTool(null);
294                            }
295    
296                    }
297                    
298                    revalidate();
299                    repaint();
300          }          }
301    
302          // Space between tool buttons and action buttons          // Space between tool buttons and action buttons
# Line 294  public class MapPaneToolBar extends JToo Line 335  public class MapPaneToolBar extends JToo
335                          // Set the mouse tool to "Info"                          // Set the mouse tool to "Info"
336                          mapPane.setWindowSelectionState(JMapPane.NONE);                          mapPane.setWindowSelectionState(JMapPane.NONE);
337                          mapPane.setState(JMapPane.SELECT_TOP); // Why not:                          mapPane.setState(JMapPane.SELECT_TOP); // Why not:
338                                                                                                          // JMapPane.SELECT_TOP_ONEONLY                          // JMapPane.SELECT_TOP_ONEONLY
339                          mapPane.setHighlight(false);// SK: Was true, but since it not works                          mapPane.setHighlight(false);// SK: Was true, but since it not works
340                                                                                  // properly removed it to save                          // properly removed it to save
341                                                                                  // performance                          // performance
342                          mapPane.setNormalCursor(SwingUtil.CROSSHAIR_CURSOR);                          mapPane.setNormalCursor(SwingUtil.CROSSHAIR_CURSOR);
343                          break;                          break;
344                  case TOOL_ZOOMIN:                  case TOOL_ZOOMIN:
# Line 314  public class MapPaneToolBar extends JToo Line 355  public class MapPaneToolBar extends JToo
355                          mapPane.setHighlight(false);                          mapPane.setHighlight(false);
356                          mapPane.setNormalCursor(SwingUtil.ZOOMOUT_CURSOR);                          mapPane.setNormalCursor(SwingUtil.ZOOMOUT_CURSOR);
357                          break;                          break;
358  //              default:                  default:
359  //                      // Set map actions to default                          // Set map actions to default
360  //                      mapPane.setWindowSelectionState(JMapPane.NONE);                          mapPane.setWindowSelectionState(JMapPane.NONE);
361  //                      mapPane.setState(JMapPane.NONE);                          mapPane.setState(JMapPane.NONE);
362  //                      mapPane.setHighlight(false);                          mapPane.setHighlight(false);
363  //                      mapPane.setNormalCursor(null);                          mapPane.setNormalCursor(null);
364  //                      break;                          break;
365                  }                  }
366                  mapPane.updateCursor();                  mapPane.updateCursor();
367          }          }
368            
369          /**          /**
370           * @param id The ID of the Component to remove           * @param id
371             *            The ID of the Component to remove. The change will not be
372             *            visible until {@link #initToolBar()} is called.
373           * @return <code>null</code> or the component that has been removed.           * @return <code>null</code> or the component that has been removed.
374           */           */
375          public JComponent removeId(int id){          public JComponent removeId(int id) {
376                  return toolAndActionButtons.remove(id);                  return toolAndActionButtons.remove(id);
377          }          }
378    
# Line 372  public class MapPaneToolBar extends JToo Line 415  public class MapPaneToolBar extends JToo
415                                  mapPane.refresh();                                  mapPane.refresh();
416                          }                          }
417                  }                  }
   
418          }          }
419    
420          /**          /**
# Line 519  public class MapPaneToolBar extends JToo Line 561  public class MapPaneToolBar extends JToo
561                          return;                          return;
562                  button.setSelected(true);                  button.setSelected(true);
563                  button.getAction().actionPerformed(null);                  button.getAction().actionPerformed(null);
564    
565                    selectedTool = tool;
566          }          }
567    
568          /**          /**
# Line 701  public class MapPaneToolBar extends JToo Line 745  public class MapPaneToolBar extends JToo
745                   */                   */
746                  public MapPaneToolBarAction(int id, MapPaneToolBar toolBar,                  public MapPaneToolBarAction(int id, MapPaneToolBar toolBar,
747                                  String name, Icon icon) {                                  String name, Icon icon) {
748                            this (id, toolBar, name, icon, null);
749                    }
750                    
751                    /**
752                     * Creates a new action.
753                     *
754                     * @param id
755                     *            unique ID for the action
756                     * @param toolBar
757                     *            The {@link MapPaneToolBar} this action is made for
758                     * @param name
759                     *            description used for buttons or menus
760                     * @param icon
761                     *            icon used for buttons or menus
762                     * @param toolTip
763                     *            Tooltip to use for the button or menu
764                     */
765                    public MapPaneToolBarAction(int id, MapPaneToolBar toolBar,
766                                    String name, Icon icon, String toolTip) {
767                          super(name, icon);                          super(name, icon);
768                            
769                            if (toolTip != null && !toolTip.trim().isEmpty()){
770                                    putValue(Action.SHORT_DESCRIPTION, toolTip);
771                            }
772                            
773                          this.id = id;                          this.id = id;
774                          this.toolBar = toolBar;                          this.toolBar = toolBar;
775                  }                  }
# Line 729  public class MapPaneToolBar extends JToo Line 797  public class MapPaneToolBar extends JToo
797                          return id;                          return id;
798                  }                  }
799          }          }
800            
801              /**
802               * Nuetzlich wenn die Componente gedruckt (z.B. wenn ein Screenshot gemacht wird) wird. Dann werden wird der
803               * Hintergrund auf WEISS gesetzt.
804               *
805               * @author <a href="mailto:[email protected]">Stefan Alfons
806               *         Kr&uuml;ger</a>
807               */
808              @Override
809              public void print(Graphics g) {
810                  Color orig = getBackground();
811                  setBackground(Color.WHITE);
812                  // wrap in try/finally so that we always restore the state
813                  try {
814                      super.print(g);
815                  } finally {
816                      setBackground(orig);
817                  }
818              }
819  }  }

Legend:
Removed from v.140  
changed lines
  Added in v.180

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26