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

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

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

branches/2.0-RC1/src/skrueger/geotools/XMapPane.java revision 620 by alfonx, Mon Jan 25 17:46:06 2010 UTC branches/2.0-RC2/src/skrueger/geotools/XMapPane.java revision 654 by alfonx, Mon Feb 1 18:13:14 2010 UTC
# Line 69  import schmitzm.geotools.map.event.MapLa Line 69  import schmitzm.geotools.map.event.MapLa
69  import schmitzm.lang.LangUtil;  import schmitzm.lang.LangUtil;
70  import schmitzm.swing.JPanel;  import schmitzm.swing.JPanel;
71  import schmitzm.swing.SwingUtil;  import schmitzm.swing.SwingUtil;
72    import schmitzm.swing.event.MouseInputType;
73    
74  import com.vividsolutions.jts.geom.Coordinate;  import com.vividsolutions.jts.geom.Coordinate;
75  import com.vividsolutions.jts.geom.Envelope;  import com.vividsolutions.jts.geom.Envelope;
# Line 107  public class XMapPane extends JPanel { Line 108  public class XMapPane extends JPanel {
108    
109          private final static Logger LOGGER = Logger.getLogger(XMapPane.class);          private final static Logger LOGGER = Logger.getLogger(XMapPane.class);
110    
111            /**
112             * A flag indicating whether the {@link XMapPane} is accepting repaints from
113             * the EDT. @see {@link XMapPane#setPainting(boolean))
114             **/
115          private boolean acceptsRepaintCalls = true;          private boolean acceptsRepaintCalls = true;
116    
117          /**          /**
# Line 296  public class XMapPane extends JPanel { Line 301  public class XMapPane extends JPanel {
301                          final MapLayer layer = event.getLayer();                          final MapLayer layer = event.getLayer();
302                          layer.addMapLayerListener(bgMapLayerListener);                          layer.addMapLayerListener(bgMapLayerListener);
303                          requestStartRendering();                          requestStartRendering();
   
304                  }                  }
305    
306                  @Override                  @Override
# Line 390  public class XMapPane extends JPanel { Line 394  public class XMapPane extends JPanel {
394           * While dragging, the {@link #updateFinalImage()} method is translating the           * While dragging, the {@link #updateFinalImage()} method is translating the
395           * cached images while setting it together.           * cached images while setting it together.
396           **/           **/
397          Point imageOrigin = new Point(0, 0);          private final Point imageOrigin = new Point(0, 0);
398          /**          /**
399           * For every rendering thread started,           * For every rendering thread started,
400           * {@link GTUtil#createGTRenderer(MapContext)} is called to create a new           * {@link GTUtil#createGTRenderer(MapContext)} is called to create a new
# Line 560  public class XMapPane extends JPanel { Line 564  public class XMapPane extends JPanel {
564    
565          /**          /**
566           * The default state is ZOOM_IN, hence by default the           * The default state is ZOOM_IN, hence by default the
567           * {@link #zoomMapPaneMouseListener} is also enabled.           * {@link #xMapPaneMouseListener} is also enabled.
568           **/           **/
569          private int state = ZOOM_IN;          private int state = ZOOM_IN;
570    
# Line 572  public class XMapPane extends JPanel { Line 576  public class XMapPane extends JPanel {
576    
577          private AffineTransform worldToScreen;          private AffineTransform worldToScreen;
578    
579            // /**
580            // * This {@link MouseListener} is managing all zoom related tasks
581            // */
582            // private final ZoomXMapPaneMouseListener zoomMapPaneMouseListener = new
583            // ZoomXMapPaneMouseListener(
584            // this);
585    
586          /**          /**
587           * This {@link MouseListener} is managing all zoom related tasks           * This {@link MouseListener} is managing all zoom related tasks
588           */           */
589          public final ZoomXMapPaneMouseListener zoomMapPaneMouseListener = new ZoomXMapPaneMouseListener(          private final XMapPaneMouseListener xMapPaneMouseListener = new XMapPaneMouseListener(
590                          this);                          this);
591    
592          /** Is set if a renderer has an error **/          /** Is set if a renderer has an error **/
# Line 617  public class XMapPane extends JPanel { Line 628  public class XMapPane extends JPanel {
628                  /**                  /**
629                   * Adding the #zoomMapPaneMouseListener                   * Adding the #zoomMapPaneMouseListener
630                   */                   */
631                  this.addMouseListener(zoomMapPaneMouseListener);                  this.addMouseListener(xMapPaneMouseListener);
632                  this.addMouseMotionListener(zoomMapPaneMouseListener);                  this.addMouseMotionListener(xMapPaneMouseListener);
633                  this.addMouseWheelListener(zoomMapPaneMouseListener);                  this.addMouseWheelListener(xMapPaneMouseListener);
634                    xMapPaneMouseListener.actions.put(MouseInputType.LClick,
635                                    XMapPaneAction.ZOOM_IN);
636                    xMapPaneMouseListener.actions.put(MouseInputType.LDrag,
637                                    XMapPaneAction.ZOOM_IN);
638                    xMapPaneMouseListener.actions.put(MouseInputType.Wheel,
639                                    XMapPaneAction.ZOOM_IN);
640    
641                    xMapPaneMouseListener.actions.put(MouseInputType.RClick,
642                                    XMapPaneAction.SELECT_ALL);
643    
644                    xMapPaneMouseListener.actions.put(MouseInputType.RDrag,
645                                    XMapPaneAction.PAN);
646    
647                  /*                  /*
648                   * We use a Timer object to avoid rendering delays and flickering when                   * We use a Timer object to avoid rendering delays and flickering when
# Line 974  public class XMapPane extends JPanel { Line 997  public class XMapPane extends JPanel {
997                  // Remove all mapPaneListeners that have registered with us                  // Remove all mapPaneListeners that have registered with us
998                  mapPaneListeners.clear();                  mapPaneListeners.clear();
999    
1000                  removeMouseMotionListener(zoomMapPaneMouseListener);                  removeMouseMotionListener(xMapPaneMouseListener);
1001                  removeMouseListener(zoomMapPaneMouseListener);                  removeMouseListener(xMapPaneMouseListener);
1002    
1003                  if (localContext != null)                  if (localContext != null)
1004                          getMapContext().clearLayerList();                          getMapContext().clearLayerList();
# Line 991  public class XMapPane extends JPanel { Line 1014  public class XMapPane extends JPanel {
1014           */           */
1015          protected void drawRectangle(final Graphics graphics, final Point startPos,          protected void drawRectangle(final Graphics graphics, final Point startPos,
1016                          final Point e) {                          final Point e) {
1017                    drawRectangle(graphics, startPos, e, Color.WHITE, false);
1018            }
1019    
1020            /**
1021             * Draws a rectangle in XOR mode from the origin at {@link #startPos} to the
1022             * given point. All in screen coordinates.
1023             */
1024            protected void drawRectangle(final Graphics graphics, final Point startPos,
1025                            final Point e, Color color, boolean fill) {
1026    
1027                  if (!isWellDefined())                  if (!isWellDefined())
1028                          return;                          return;
# Line 1006  public class XMapPane extends JPanel { Line 1038  public class XMapPane extends JPanel {
1038                  if (width == 0 && height == 0)                  if (width == 0 && height == 0)
1039                          return;                          return;
1040    
1041                  graphics.setXORMode(Color.WHITE);                  graphics.setXORMode(color);
1042                    
1043                    if (fill) {
1044                            graphics.fillRect(left, bottom, width, height);
1045                            graphics.setXORMode(Color.WHITE);      
1046                    }
1047                    
1048                    
1049                  graphics.drawRect(left, bottom, width, height);                  graphics.drawRect(left, bottom, width, height);
1050          }          }
1051    
# Line 1345  public class XMapPane extends JPanel { Line 1384  public class XMapPane extends JPanel {
1384          }          }
1385    
1386          /**          /**
1387           * Called from the listeners while the mouse is dragging, this method either           * Usually called from {@link XMapPaneAction_Pan} to pan the image.
          * paints a translated (moved/panned) version of the image, or a rectangle.  
1388           *           *
1389           * @param startPos           * @param startPos
1390           *            in screen coordinates           *            in screen coordinates
1391           * @param lastPos           * @param lastPos
1392           *            in screen coordinates           *            in screen coordinates
          * @param event  
          *            the {@link MouseEvent} to read the mouse buttons from  
1393           */           */
1394          public void mouseDragged(final Point startPos, final Point lastPos,          public void pan(final int dX, final int dY) {
                         final MouseEvent event) {  
   
                 if ((getState() == XMapPane.PAN)  
                                 || ((event.getModifiersEx() & InputEvent.BUTTON3_DOWN_MASK) != 0)) {  
1395    
1396                          // Panning needs a panning coursor                  // Panning needs a panning coursor
1397                          if (getCursor() != SwingUtil.PANNING_CURSOR) {                  if (getCursor() != SwingUtil.PANNING_CURSOR) {
1398                                  setCursor(SwingUtil.PANNING_CURSOR);                          setCursor(SwingUtil.PANNING_CURSOR);
1399    
1400                                  // While panning, we deactivate the rendering. So the tasks are                          // While panning, we deactivate the rendering. So the tasks are
1401                                  // ready to start when the panning is finished.                          // ready to start when the panning is finished.
1402                                  if (bgExecuter != null && bgExecuter.isRunning())                          if (bgExecuter != null && bgExecuter.isRunning())
1403                                          bgExecuter.cancelTask();                                  bgExecuter.cancelTask();
1404                                  if (localExecuter.isRunning())                          if (localExecuter.isRunning())
1405                                          localExecuter.cancelTask();                                  localExecuter.cancelTask();
1406                          }                  }
1407                    //
1408                          if (lastPos.x > 0 && lastPos.y > 0) {                  // if (lastPos.x > 0 && lastPos.y > 0) {
1409                                  final int dx = event.getX() - lastPos.x;                  // final int dx = event.getX() - lastPos.x;
1410                                  final int dy = event.getY() - lastPos.y;                  // final int dy = event.getY() - lastPos.y;
1411    
1412                                  // TODO Stop dragging when the drag would not be valid...                  // TODO Stop dragging when the drag would not be valid...
1413                                  // boolean dragValid = true;                  // boolean dragValid = true;
1414                                  // // check if this panning results in a valid mapArea                  // // check if this panning results in a valid mapArea
1415                                  // {                  // {
1416                                  // Rectangle winBounds = xMapPane.getBounds();                  // Rectangle winBounds = xMapPane.getBounds();
1417                                  // winBounds.translate(xMapPane.imageOrigin.x,                  // winBounds.translate(xMapPane.imageOrigin.x,
1418                                  // -xMapPane.imageOrigin.y);                  // -xMapPane.imageOrigin.y);
1419                                  // Envelope newMapAreaBefore = xMapPane.tranformWindowToGeo(                  // Envelope newMapAreaBefore = xMapPane.tranformWindowToGeo(
1420                                  // winBounds.x, winBounds.y, winBounds.x                  // winBounds.x, winBounds.y, winBounds.x
1421                                  // + winBounds.width, winBounds.y                  // + winBounds.width, winBounds.y
1422                                  // + winBounds.height);                  // + winBounds.height);
1423                                  //                                                        //                                      
1424                                  //                  //
1425                                  // winBounds = xMapPane.getBounds();                  // winBounds = xMapPane.getBounds();
1426                                  // Point testIng = new Point(xMapPane.imageOrigin);                  // Point testIng = new Point(xMapPane.imageOrigin);
1427                                  // testIng.translate(dx, dy);                  // testIng.translate(dx, dy);
1428                                  // winBounds.translate(testIng.x, -testIng.y);                  // winBounds.translate(testIng.x, -testIng.y);
1429                                  // Envelope newMapAreaAfter = xMapPane.tranformWindowToGeo(                  // Envelope newMapAreaAfter = xMapPane.tranformWindowToGeo(
1430                                  // winBounds.x, winBounds.y, winBounds.x                  // winBounds.x, winBounds.y, winBounds.x
1431                                  // + winBounds.width, winBounds.y                  // + winBounds.width, winBounds.y
1432                                  // + winBounds.height);                  // + winBounds.height);
1433                                  //                  //
1434                                  // // If the last drag doesn't change the MapArea anymore cancel                  // // If the last drag doesn't change the MapArea anymore cancel
1435                                  // it.                  // it.
1436                                  // if (xMapPane.bestAllowedMapArea(newMapAreaAfter).equals(                  // if (xMapPane.bestAllowedMapArea(newMapAreaAfter).equals(
1437                                  // xMapPane.bestAllowedMapArea(newMapAreaBefore))){                  // xMapPane.bestAllowedMapArea(newMapAreaBefore))){
1438                                  // dragValid = false;                  // dragValid = false;
1439                                  // return;                  // return;
1440                                  // }                  // }
1441                                  // }                  // }
1442    
1443                                  imageOrigin.translate(dx, dy);                  getImageOrigin().translate(dX, dY);
1444                                  updateFinalImage();                  updateFinalImage();
1445                                  repaint();                  repaint();
1446                          }                  // }
1447    
1448                  } else if ((getState() == XMapPane.ZOOM_IN)                  // } else if ((getState() == XMapPane.ZOOM_IN)
1449                                  || (getState() == XMapPane.ZOOM_OUT)                  // || (getState() == XMapPane.ZOOM_OUT)
1450                                  || (getState() == XMapPane.SELECT_ALL)                  // || (getState() == XMapPane.SELECT_ALL)
1451                                  || (getState() == XMapPane.SELECT_TOP)) {                  // || (getState() == XMapPane.SELECT_TOP)) {
1452                    //
1453                          // Draws a rectangle                  // // Draws a rectangle
1454                          final Graphics2D graphics = (Graphics2D) getGraphics();                  // final Graphics2D graphics = (Graphics2D) getGraphics();
1455                          drawRectangle(graphics, startPos, event.getPoint());                  // drawRectangle(graphics, startPos, event.getPoint());
1456                          if ((lastPos.x > 0) && (lastPos.y > 0))                  // if ((lastPos.x > 0) && (lastPos.y > 0))
1457                                  drawRectangle(graphics, startPos, lastPos);                  // drawRectangle(graphics, startPos, lastPos);
1458                          graphics.dispose();                  // graphics.dispose();
1459                  }                  // }
1460          }          }
1461    
1462          /**          /**
# Line 1586  public class XMapPane extends JPanel { Line 1618  public class XMapPane extends JPanel {
1618                  }                  }
1619    
1620          }          }
1621    //
1622          /**  //      /**
1623           * Performs a {@value #PAN} action. During panning, the displacement is  //       * Performs a {@value #PAN} action. During panning, the displacement is
1624           * stored in {@link #imageOrigin} object. Calling {@link #performPan()} will  //       * stored in {@link #imageOrigin} object. Calling {@link #performPan()} will
1625           * reset the offset and call {@link #setMapArea(Envelope)}.  //       * reset the offset and call {@link #setMapArea(Envelope)}.
1626           */  //       */
1627          public void performPan() {  //      public void performPan() {
1628    //
1629                  final Rectangle winBounds = getVisibleRect();  //              final Rectangle winBounds = getVisibleRect();
1630    //
1631                  winBounds.translate(-imageOrigin.x, -imageOrigin.y);  //              winBounds.translate(-imageOrigin.x, -imageOrigin.y);
1632                  final Envelope newMapArea = tranformWindowToGeo(winBounds.x,  //              final Envelope newMapArea = tranformWindowToGeo(winBounds.x,
1633                                  winBounds.y, winBounds.x + winBounds.width, winBounds.y  //                              winBounds.y, winBounds.x + winBounds.width, winBounds.y
1634                                                  + winBounds.height);  //                                              + winBounds.height);
1635    //
1636                  imageOrigin.x = 0;  //              imageOrigin.x = 0;
1637                  imageOrigin.y = 0;  //              imageOrigin.y = 0;
1638    //
1639                  if (!setMapArea(newMapArea)) {  //              if (!setMapArea(newMapArea)) {
1640                          /**  //                      /**
1641                           * If setMapArea returns true, the finalImage is updated anyways.  //                       * If setMapArea returns true, the finalImage is updated anyways.
1642                           * This if-case exists to ensure that we repaint a correct image  //                       * This if-case exists to ensure that we repaint a correct image
1643                           * even if the new panning area has been denied.  //                       * even if the new panning area has been denied.
1644                           */  //                       */
1645                          updateFinalImage();  //                      updateFinalImage();
1646                          repaint();  //                      repaint();
1647                  }  //              }
1648    //
1649                  if (getCursor() == SwingUtil.PANNING_CURSOR)  //              if (getCursor() == SwingUtil.PANNING_CURSOR)
1650                          setCursor(SwingUtil.PAN_CURSOR);  //                      setCursor(SwingUtil.PAN_CURSOR);
1651          }  //      }
1652    
1653          /**          /**
1654           * Entfernt einen Listener von der Map.           * Entfernt einen Listener von der Map.
# Line 1669  public class XMapPane extends JPanel { Line 1701  public class XMapPane extends JPanel {
1701    
1702                  if (mapArea == null)                  if (mapArea == null)
1703                          return;                          return;
1704                    
1705                  final ReferencedEnvelope refMapEnv = new ReferencedEnvelope(mapArea,                  final ReferencedEnvelope refMapEnv = new ReferencedEnvelope(mapArea,
1706                                  getMapContext().getCoordinateReferenceSystem());                                  getMapContext().getCoordinateReferenceSystem());
1707    
# Line 1925  public class XMapPane extends JPanel { Line 1957  public class XMapPane extends JPanel {
1957    
1958          // /** Stored the time used for the last real rendering in ms. **/          // /** Stored the time used for the last real rendering in ms. **/
1959          private long lastRenderingDuration = 1000;          private long lastRenderingDuration = 1000;
1960            private XMapPaneTool tool = XMapPaneTool.INFO;
1961    
1962          /**          /**
1963           * Set the minimum (nearest) allowed zoom scale. This is the bigger number           * Set the minimum (nearest) allowed zoom scale. This is the bigger number
# Line 1962  public class XMapPane extends JPanel { Line 1995  public class XMapPane extends JPanel {
1995           *           *
1996           * @param state           * @param state
1997           */           */
1998            @Deprecated
1999          public void setState(final int state) {          public void setState(final int state) {
2000                  this.state = state;                  this.state = state;
2001    
2002                  zoomMapPaneMouseListener.setEnabled((state == ZOOM_IN                  // throw new RuntimeException("Old concept.. migrate to new concept!");
2003                                  || state == ZOOM_OUT || state == PAN));  
2004                    // xMapPaneMouseListener.setEnabled((state == ZOOM_IN
2005                    // || state == ZOOM_OUT || state == PAN));
2006    
2007                  // Je nach Aktion den Cursor umsetzen                  // Je nach Aktion den Cursor umsetzen
2008                  updateCursor();                  updateCursor();
2009          }          }
2010    
2011            public void configureMouse(MouseInputType type, XMapPaneAction action) {
2012                    xMapPaneMouseListener.actions.put(type, action);
2013            }
2014            
2015            /**
2016             * Configure the tool that active on the map
2017             */
2018            public void setTool(XMapPaneTool tool) {
2019                    this.tool = tool;
2020                    xMapPaneMouseListener.configure(tool);
2021                    setCursor(tool.cursor);
2022            }
2023    
2024          /**          /**
2025           * Standardmaessig wird der Cursor automatisch je nach MapPane-Aktion (Zoom,           * Standardmaessig wird der Cursor automatisch je nach MapPane-Aktion (Zoom,
2026           * Auswahl, ...) gesetzt. Mit dieser Methode kann ein statischer Cursor           * Auswahl, ...) gesetzt. Mit dieser Methode kann ein statischer Cursor
# Line 2128  public class XMapPane extends JPanel { Line 2177  public class XMapPane extends JPanel {
2177                                  repaint();                                  repaint();
2178                          }                          }
2179                  }                  }
2180                    
2181    //
2182                  // wenn manueller Cursor gesetzt ist, dann diesen verwenden (unabhaengig                  // wenn manueller Cursor gesetzt ist, dann diesen verwenden (unabhaengig
2183                  // von der aktuellen Aktion                  // von der aktuellen Aktion
2184                  if (this.staticCursor != null) {                  if (this.staticCursor != null) {
2185                          setCursor(staticCursor);                          setCursor(staticCursor);
2186                          return;                          return;
2187                  }                  }
2188                    
2189                  if (getCursor() == SwingUtil.PANNING_CURSOR) {                  if (getCursor() == SwingUtil.PANNING_CURSOR) {
2190                          // This cursor will reset itself                          // This cursor will reset itself
2191                          return;                          return;
2192                  }                  }
2193                    
2194                  // Set the cursor depending on what tool is in use...                  setCursor(tool.cursor);
2195                  switch (state) {  //
2196                  case SELECT_TOP:  //              // Set the cursor depending on what tool is in use...
2197                  case SELECT_ONE_FROM_TOP:  //              switch (state) {
2198                  case SELECT_ALL:  //              case SELECT_TOP:
2199                          setCursor(SwingUtil.CROSSHAIR_CURSOR);  //              case SELECT_ONE_FROM_TOP:
2200                          break;  //              case SELECT_ALL:
2201                  case ZOOM_IN:  //                      setCursor(SwingUtil.CROSSHAIR_CURSOR);
2202                          setCursor(SwingUtil.ZOOMIN_CURSOR);  //                      break;
2203                          break;  //              case ZOOM_IN:
2204                  case ZOOM_OUT:  //                      setCursor(SwingUtil.ZOOMIN_CURSOR);
2205                          setCursor(SwingUtil.ZOOMOUT_CURSOR);  //                      break;
2206                          break;  //              case ZOOM_OUT:
2207                  case PAN:  //                      setCursor(SwingUtil.ZOOMOUT_CURSOR);
2208                          setCursor(SwingUtil.PAN_CURSOR);  //                      break;
2209                          break;  //              case PAN:
2210                  default:  //                      setCursor(SwingUtil.PAN_CURSOR);
2211                          setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));  //                      break;
2212                          break;  //              default:
2213                  }  //                      setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
2214    //                      break;
2215    //              }
2216          }          }
2217    
2218          /**          /**
# Line 2189  public class XMapPane extends JPanel { Line 2242  public class XMapPane extends JPanel {
2242    
2243                  final Graphics2D finalG = getFinalImage().createGraphics();                  final Graphics2D finalG = getFinalImage().createGraphics();
2244                  finalG.setBackground(getMapBackgroundColor());                  finalG.setBackground(getMapBackgroundColor());
2245                  finalG.drawImage(getPreFinalImage(), imageOrigin.x, imageOrigin.y,                  finalG.drawImage(getPreFinalImage(), getImageOrigin().x, getImageOrigin().y,
2246                                  getMapBackgroundColor(), null);                                  getMapBackgroundColor(), null);
2247    
2248                  // When panning, we have to clear the area around the image                  // When panning, we have to clear the area around the image
2249                  final Rectangle painedArea = new Rectangle(imageOrigin.x,                  final Rectangle painedArea = new Rectangle(getImageOrigin().x,
2250                                  imageOrigin.y, getFinalImage().getWidth(), getFinalImage()                                  getImageOrigin().y, getFinalImage().getWidth(), getFinalImage()
2251                                                  .getHeight());                                                  .getHeight());
2252                  SwingUtil.clearAround(finalG, painedArea, getVisibleRect(),                  SwingUtil.clearAround(finalG, painedArea, getVisibleRect(),
2253                                  getMapBackgroundColor());                                  getMapBackgroundColor());
2254    
2255                  addGadgets(finalG, false);  //              addGadgets(finalG, false);
2256    
2257                  finalG.dispose();                  finalG.dispose();
2258    
# Line 2592  public class XMapPane extends JPanel { Line 2645  public class XMapPane extends JPanel {
2645    
2646          }          }
2647    
2648            public Point getImageOrigin() {
2649                    return imageOrigin;
2650            }
2651    
2652  }  }

Legend:
Removed from v.620  
changed lines
  Added in v.654

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26