Esempio n. 1
0
 public void refreshMap() {
   IMap activeMap = ApplicationGIS.getActiveMap();
   RenderedImage image = activeMap.getRenderManager().getImage();
   if (image == null) {
     return;
   }
   List<ILayer> mapLayers = activeMap.getMapLayers();
   for (ILayer iLayer : mapLayers) {
     iLayer.refresh(null);
   }
 }
  /**
   * @see
   *     net.refractions.udig.project.ui.tool.AbstractTool#mousePressed(net.refractions.udig.project.render.displayAdapter.MapMouseEvent)
   */
  public void mousePressed(MapMouseEvent e) {
    // validate the mouse click (only left mouse clicks should work for dragging)
    if (!validModifierButtonCombo(e)) {
      return;
    }

    // set the active objects and also force a redraw of
    // the composed map image before panning so that we can
    // remove the placemarker being dragged
    List<ILayer> mapLayers = getContext().getMap().getMapLayers();
    Iterator<ILayer> iterator = mapLayers.iterator();
    while (iterator.hasNext()) {
      ILayer layer = iterator.next();
      GeoReferenceMapGraphic mapGraphic;
      try {
        mapGraphic = layer.getResource(GeoReferenceMapGraphic.class, null);
        if (mapGraphic != null) {
          activeMapGraphic = mapGraphic;
          GlassPane glass = getContext().getViewportPane().getGlass();
          if (glass instanceof GeoReferenceGlassPane) {
            activeGlassPane = (GeoReferenceGlassPane) glass;
          }
          layer.refresh(null); // refresh to ensure the selected graphic disappears
          break;
        }
      } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
      }
    }

    // determine if the user clicked on a placemarker or not. If multiple
    // placemarkers are clicked on, choose the closest one to the click
    draggingPlaceMarker = null;
    if (activeMapGraphic != null) {
      Point point = e.getPoint();
      List<PlaceMarker> imageMarkers = activeMapGraphic.getImageMarkers();
      List<PlaceMarker> basemapMarkers = activeMapGraphic.getBasemapMarkers();
      if (imageMarkers == null || imageMarkers.size() < 1) {
        return;
      }
      double currentdistance = PlaceMarker.DRAWING_SIZE + 1;
      for (PlaceMarker marker : imageMarkers) {
        Point markerPoint = marker.getPoint();
        if (markerPoint != null) {
          double distance = point.distance(markerPoint.x, markerPoint.y);
          if (distance <= PlaceMarker.DRAWING_SIZE && distance < currentdistance) {
            draggingPlaceMarker = marker;
            currentdistance = distance;
          }
        }
      }

      // check basemap markers if we haven't matched one yet
      if (draggingPlaceMarker == null && basemapMarkers != null) {
        for (PlaceMarker marker : basemapMarkers) {
          // compare the map coordinate not the screen point since
          // the screen point could be out of whack if the user
          // panned/zoomed the map
          Coordinate markerCoord = marker.getCoord();
          if (markerCoord != null) {
            Point markerPoint = getContext().worldToPixel(markerCoord);
            if (markerPoint != null) {
              double distance = point.distance(markerPoint.x, markerPoint.y);
              if (distance <= PlaceMarker.DRAWING_SIZE && distance < currentdistance) {
                draggingPlaceMarker = marker;
                currentdistance = distance;
              }
            }
          }
        }
      }
    }

    // did we find a marker?
    if (draggingPlaceMarker == null) {
      return;
    }

    // marker found, set dragging vars
    dragging = true;
    draggingPlaceMarker.setDragging(true);
    mouseStart = e.getPoint();
    if (activeGlassPane != null && draggingPlaceMarker != null) {
      Point point = null;
      if (draggingPlaceMarker.isBasemapMarker() && draggingPlaceMarker.getCoord() != null) {
        point = getContext().worldToPixel(draggingPlaceMarker.getCoord());
      } else {
        point = draggingPlaceMarker.getPoint();
      }
      activeGlassPane.startDraggingMarker(draggingPlaceMarker, point.x, point.y);
    }
  }