/**
   * @see
   *     net.refractions.udig.project.ui.tool.AbstractTool#mouseReleased(net.refractions.udig.project.render.displayAdapter.MapMouseEvent)
   */
  public void mouseReleased(MapMouseEvent e) {
    // turn dragging off
    if (dragging) {
      if (activeGlassPane != null) {
        Point point = null;
        if (draggingPlaceMarker.isBasemapMarker() && draggingPlaceMarker.getCoord() != null) {
          point = getContext().worldToPixel(draggingPlaceMarker.getCoord());
        } else {
          point = draggingPlaceMarker.getPoint();
        }
        int x = point.x + (e.x - mouseStart.x);
        int y = point.y + (e.y - mouseStart.y);
        // make sure any image markers stay within the bounds of the image
        Point validatedPoint = new Point(x, y);
        if (!draggingPlaceMarker.isBasemapMarker() && draggingPlaceMarker.getImage() != null) {
          GeoReferenceImage image = draggingPlaceMarker.getImage();
          validatedPoint = validateImagePoint(image, x, y);
        }
        activeGlassPane.endDraggingMarker(validatedPoint.x, validatedPoint.y);
      }

      // clear any events before we try to pan. This dramatically reduces the number
      // of images drawn to the screen in the wrong spot
      ((ViewportPane) getContext().getMapDisplay()).update();

      dragging = false;
      if (draggingPlaceMarker != null) {
        draggingPlaceMarker.setDragging(false);
      }
    }

    // refresh the layer
    if (activeMapGraphic != null) {
      activeMapGraphic.getLayer(getContext().getMap()).refresh(null); // refresh to ensure the
      // dragged placemarker
      // reappears
    }
  }
  /**
   * @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);
    }
  }