// Handle mouse actions
 public void mousePressed(MouseEvent mouseEvent) {
   if (this.isArmed() && this.isUseRubberBand() && mouseEvent.getButton() == MouseEvent.BUTTON1) {
     if ((mouseEvent.getModifiersEx() & MouseEvent.BUTTON1_DOWN_MASK) != 0) {
       if (!mouseEvent.isControlDown()) {
         this.setActive(true);
         measureTool.addControlPoint();
         if (measureTool.getControlPoints().size() == 1) {
           measureTool.addControlPoint(); // Simulate a second click
         }
         // Set the rubber band target to the last control point or the relevant control for
         // regular shapes.
         if (measureTool.isRegularShape()) {
           String initControl =
               measureTool.getShapeInitialControl(measureTool.getWwd().getCurrentPosition());
           rubberBandTarget = measureTool.getControlPoint(initControl);
         } else {
           rubberBandTarget =
               (MeasureTool.ControlPoint)
                   measureTool.getControlPoints().get(measureTool.getControlPoints().size() - 1);
         }
         measureTool.firePropertyChange(MeasureTool.EVENT_RUBBERBAND_START, null, null);
       }
     }
     mouseEvent.consume();
   } else if (!this.isArmed()
       && mouseEvent.getButton() == MouseEvent.BUTTON1
       && mouseEvent.isAltDown()) {
     if (!this.measureTool.isRegularShape()) {
       this.setMoving(true);
       this.movingTarget = this.lastPickedObject;
     }
     mouseEvent.consume();
   }
 }
  // Handle single click for removing control points
  public void mouseClicked(MouseEvent mouseEvent) {
    if (measureTool == null) return;

    if (this.isArmed() && mouseEvent.getButton() == MouseEvent.BUTTON1) {
      if (mouseEvent.isControlDown()) measureTool.removeControlPoint();
      else if (!this.isUseRubberBand()) {
        measureTool.addControlPoint();
        // Disarm after second control point of a line or regular shape
        autoDisarm();
      }
      mouseEvent.consume();
    }
  }
  @SuppressWarnings({"UnusedDeclaration"})
  protected void doMoved(PositionEvent event) {
    if (this.active
        && rubberBandTarget != null
        && this.measureTool.getWwd().getObjectsAtCurrentPosition() != null
        && this.measureTool.getWwd().getObjectsAtCurrentPosition().getTerrainObject() != null) {
      if (!isFreeHand()
          || (!measureTool.getMeasureShapeType().equals(MeasureTool.SHAPE_PATH)
              && !measureTool.getMeasureShapeType().equals(MeasureTool.SHAPE_POLYGON))) {
        // Rubber band - Move control point and update shape
        Position lastPosition = rubberBandTarget.getPosition();
        PickedObjectList pol = measureTool.getWwd().getObjectsAtCurrentPosition();
        PickedObject to = pol.getTerrainObject();
        rubberBandTarget.setPosition(new Position(to.getPosition(), 0));
        measureTool.moveControlPoint(rubberBandTarget);
        measureTool.firePropertyChange(
            MeasureTool.EVENT_POSITION_REPLACE, lastPosition, rubberBandTarget.getPosition());
        measureTool.getWwd().redraw();
      } else {
        // Free hand - Compute distance from current control point (rubber band target)
        Position lastPosition = rubberBandTarget.getPosition();
        Position newPosition = measureTool.getWwd().getCurrentPosition();
        double distance =
            LatLon.greatCircleDistance(lastPosition, newPosition).radians
                * measureTool.getWwd().getModel().getGlobe().getRadius();
        if (distance >= freeHandMinSpacing) {
          // Add new control point
          measureTool.addControlPoint();
          rubberBandTarget =
              (MeasureTool.ControlPoint)
                  getMeasureTool()
                      .getControlPoints()
                      .get(getMeasureTool().getControlPoints().size() - 1);
          measureTool.getWwd().redraw();
        }
      }
    } else if (this.moving
        && movingTarget != null
        && measureTool.getWwd().getCurrentPosition() != null) {
      // Moving the whole shape
      Position lastPosition = movingTarget.getPosition();
      Position newPosition = measureTool.getWwd().getCurrentPosition();
      this.moveToPosition(lastPosition, newPosition);

      // Update the tool tip to follow the shape as it moves.
      if (measureTool.isShowAnnotation()) measureTool.updateAnnotation(movingTarget.getPosition());

      measureTool.getWwd().redraw();
    }
  }