Display feedback
arcgissamples\display\EnvelopeFeedback.java
/* Copyright 2010 ESRI
* 
* All rights reserved under the copyright laws of the United States
* and applicable international laws, treaties, and conventions.
* 
* You may freely redistribute and use this sample code, with or
* without modification, provided you include the original copyright
* notice and use restrictions.
* 
* See the use restrictions.
* 
*/
package arcgissamples.display;

import java.awt.BorderLayout;
import java.io.IOException;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import com.esri.arcgis.beans.map.MapBean;
import com.esri.arcgis.beans.toolbar.ToolbarBean;
import com.esri.arcgis.carto.IElement;
import com.esri.arcgis.carto.IEnumElement;
import com.esri.arcgis.carto.IGraphicsContainer;
import com.esri.arcgis.carto.RectangleElement;
import com.esri.arcgis.carto.esriViewDrawPhase;
import com.esri.arcgis.controls.BaseTool;
import com.esri.arcgis.controls.HookHelper;
import com.esri.arcgis.controls.IHookHelper;
import com.esri.arcgis.display.IMoveEnvelopeFeedback;
import com.esri.arcgis.display.INewEnvelopeFeedback;
import com.esri.arcgis.display.IScreenDisplay;
import com.esri.arcgis.display.ISimpleLineSymbol;
import com.esri.arcgis.display.ISymbol;
import com.esri.arcgis.display.MoveEnvelopeFeedback;
import com.esri.arcgis.display.NewEnvelopeFeedback;
import com.esri.arcgis.display.SimpleLineSymbol;
import com.esri.arcgis.display.esriSimpleLineStyle;
import com.esri.arcgis.geometry.Envelope;
import com.esri.arcgis.geometry.IPoint;
import com.esri.arcgis.interop.AutomationException;
import com.esri.arcgis.system.AoInitialize;
import com.esri.arcgis.system.EngineInitializer;
import com.esri.arcgis.systemUI.esriCommandStyles;


/**
  This sample demonstrates via two tool the usage of two objects, namely, NewEnvelopeFeedback and MoveEnvelopeFeedback, which
  fall within a bigger collection of feedback objects in the Display package. In general, feedback objects are
  meant to provide you with fine grained control over customizing the visual feedback when using the mouse
  to form shapes on the screen display. You can direct the precise visual feedback for tasks such as adding, moving
  or removing features or graphics elements. The objects can also be used without creating any features or elements
  for a task, such as measuring the distance between two points. Typically, you would use the feedback objects
  within code that handles the mouse events of a tool based on the ITool interface.

  This first tool shows how to draw a Rectangle with instantaneous visual feedback showing the envelope
  at every mouse position and the second tool shows how to move an existing Rectangle element from one position
  to another.
 */


public class EnvelopeFeedback extends JFrame {
  private static final long serialVersionUID = 1L;
  private JPanel jContentPane = null;
  private ToolbarBean toolbarbean1 = null;
  private MapBean mapbean1 = null;
  private JLabel jLabel = null;
  private JPanel jPanel = null;
  private JLabel jLabel1 = null;
  private JLabel jLabel2 = null;


  class NewEnvelopeFeedbackTool extends BaseTool
  {
    private static final long serialVersionUID = 1L;
    private IHookHelper hookHelper = null;
    private INewEnvelopeFeedback newEnvelopeFeedback = null;
    private Envelope feedbackEnvelope = null;
    private IElement feedbackElement = null;
    private IScreenDisplay feedbackScreenDisplay = null;
    private ISimpleLineSymbol feedbackLineSymbol = null;
    private com.esri.arcgis.geometry.Point feedbackStartPoint = null;
    private com.esri.arcgis.geometry.Point feedbackMovePoint = null;

    NewEnvelopeFeedbackTool()
    {
      super();

      super.category = "Developer Samples";
      super.caption = "NewEnvelopeFeedback";
      super.message = "This tool draws a rectangle element using a display feedback object";
      super.toolTip = "This tool draws a rectangle element using a display feedback object";
      super.name = "DevSamples_NewEnvelopeFeedback tool";
    }

    public void onCreate(Object hook)
    {
      try
      {
        hookHelper = new HookHelper();
        hookHelper.setHookByRef(hook);

        if(hookHelper.getActiveView() == null)
          hookHelper = null;

        if(hookHelper == null)
          super.enabled = false;
        else
          super.enabled = true;
      }
      catch(AutomationException e)
      {
        e.printStackTrace();
        hookHelper = null;
      }
      catch(IOException e)
      {
        e.printStackTrace();
        hookHelper = null;
      }
    }

    public void onClick()
    {
      // Do nothing. Initialization is handled by onMouseDown
    }

    public void onMouseDown(int button, int shift, int x, int y)
    {
      try
      {
        // Initialize all the variables
        feedbackEnvelope = new Envelope();
        feedbackStartPoint = new com.esri.arcgis.geometry.Point();
        feedbackMovePoint = new com.esri.arcgis.geometry.Point();
        feedbackLineSymbol = new SimpleLineSymbol();
        feedbackScreenDisplay = hookHelper.getActiveView().getScreenDisplay();
        feedbackLineSymbol.setStyle(esriSimpleLineStyle.esriSLSDashDotDot);

        // Initialize the new Envelope feedback class and pass it the symbol and display
        newEnvelopeFeedback = new NewEnvelopeFeedback();
        newEnvelopeFeedback.setDisplayByRef(feedbackScreenDisplay);
        newEnvelopeFeedback.setSymbolByRef((ISymbol)feedbackLineSymbol);

        // pass the start point from the mouse position, transforming it to an appropriate map point.
        feedbackStartPoint = (com.esri.arcgis.geometry.Point)feedbackScreenDisplay.getDisplayTransformation().toMapPoint(x, y);
        newEnvelopeFeedback.start(feedbackStartPoint);
      }
      catch(Exception e)
      {
        e.printStackTrace();
      }
    }

    public void onMouseMove(int button, int shift, int x, int y)
    {
      try
      {
        // Pass the current mouse coords only if the mouse is down.
        if(button == 1)
        {
          feedbackMovePoint = (com.esri.arcgis.geometry.Point)feedbackScreenDisplay.getDisplayTransformation().toMapPoint(x, y);
          newEnvelopeFeedback.moveTo(feedbackMovePoint);
        }
      }
      catch(Exception e)
      {
        e.printStackTrace();
      }
    }

    public void onMouseUp(int button, int shift, int x, int y)
    {
      try
      {
        // when mouse comes up, end the new envelope and pass it to the feedbackEnv
        feedbackEnvelope = (Envelope) newEnvelopeFeedback.stop();

        // Initialize a Rectangle element
        feedbackElement = new RectangleElement();

        // Pass to the rectangle element, the geometry defined by our feedback object
        feedbackElement.setGeometry(feedbackEnvelope);

        // make sure the element is activated in the current view
        feedbackElement.activate(feedbackScreenDisplay);

        // Add the newly created element to the active view's graphics container
        hookHelper.getActiveView().getGraphicsContainer().addElement(feedbackElement, 0);

        // refresh the view to see the change
        hookHelper.getActiveView().partialRefresh(esriViewDrawPhase.esriViewGraphics, feedbackElement, null);
      }
      catch(Exception e)
      {
        e.printStackTrace();
      }
    }
  }


  class MoveEnvelopeFeedbackTool extends BaseTool{
    private static final long serialVersionUID = 1L;
    private IHookHelper hookHelper = null;
    private IMoveEnvelopeFeedback moveEnvelopeFeedback = null;
    private Envelope feedbackEnvelope = null;
    private IElement feedbackElement = null;
    private IElement prevElement = null;
    private IScreenDisplay feedbackScreenDisplay = null;
    private ISimpleLineSymbol feedbackLineSymbol = null;
    private com.esri.arcgis.geometry.Point feedbackStartPoint = null;
    private com.esri.arcgis.geometry.Point feedbackMovePoint = null;

    MoveEnvelopeFeedbackTool()
    {
      super();

      super.category = "Developer Samples";
      super.caption = "MoveEnvelopeFeedback";
      super.message = "This tool moves an existing rectangle to a new position";
      super.toolTip = "This tool moves an existing rectangle to a new position";
      super.name = "DevSamples_MoveEnvelopeFeedback tool";
    }

    public void onCreate(Object hook)
    {
      try
      {
        hookHelper = new HookHelper();
        hookHelper.setHookByRef(hook);

        if(hookHelper.getActiveView() == null)
          hookHelper = null;

        if(hookHelper == null)
          super.enabled = false;
        else
          super.enabled = true;
      }
      catch(AutomationException e)
      {
        e.printStackTrace();
        hookHelper = null;
      }
      catch(IOException e)
      {
        e.printStackTrace();
        hookHelper = null;
      }
    }

    public void onClick()
    {
      // Do nothing. Initialization is handled by onMouseDown
    }

    public void onMouseDown(int button, int shift, int x, int y)
    {
      try
      {

        // First obtain a RectangleElement underneath the mouse
        IPoint mapPoint = hookHelper.getActiveView().getScreenDisplay().getDisplayTransformation().toMapPoint(x, y);
        IGraphicsContainer graphicsContainer = hookHelper.getActiveView().getGraphicsContainer();
        IEnumElement elements = graphicsContainer.locateElements(mapPoint, 0);

        // Make sure there is an element underneath the mouse. If there is, it is bound to be a RectangleElement
        // since we created it using the NewRectangleFeedback tool.
        if(elements != null && (prevElement = elements.next()) != null)
        {
          // Initialize all the variables
          feedbackEnvelope = new Envelope();
          feedbackStartPoint = new com.esri.arcgis.geometry.Point();
          feedbackMovePoint = new com.esri.arcgis.geometry.Point();
          feedbackLineSymbol = new SimpleLineSymbol();
          feedbackScreenDisplay = hookHelper.getActiveView().getScreenDisplay();
          feedbackLineSymbol.setStyle(esriSimpleLineStyle.esriSLSDashDotDot);

          // Initialize the Move Envelope feedback class and pass it the symbol and display
          moveEnvelopeFeedback = new MoveEnvelopeFeedback();
          moveEnvelopeFeedback.setDisplayByRef(feedbackScreenDisplay);
          moveEnvelopeFeedback.setSymbolByRef((ISymbol)feedbackLineSymbol);

          // pass the start point from the mouse position, transforming it to an
          // appropriate map point.
          feedbackStartPoint = (com.esri.arcgis.geometry.Point)feedbackScreenDisplay.getDisplayTransformation().toMapPoint(x, y);
          moveEnvelopeFeedback.start(prevElement.getGeometry().getEnvelope(), feedbackStartPoint);
        }
      }
      catch(Exception e)
      {
        e.printStackTrace();
      }
    }

    public void onMouseMove(int button, int shift, int x, int y)
    {
      try
      {
        // Pass the current mouse coords only if the mouse is down and a RectangleElement was obtained
        // on mouse down
        if(button == 1 && prevElement != null)
        {
          feedbackMovePoint = (com.esri.arcgis.geometry.Point)feedbackScreenDisplay.getDisplayTransformation().toMapPoint(x, y);
          moveEnvelopeFeedback.moveTo(feedbackMovePoint);
        }
      }
      catch(Exception e)
      {
        e.printStackTrace();
      }
    }

    public void onMouseUp(int button, int shift, int x, int y)
    {
      try
      {
        // Check if a RectangleElement was obtained on mouse down.
        if(prevElement != null)
        {
          // when mouse comes up, end the new envelope and pass it to the feedbackEnv
          feedbackEnvelope = (Envelope) moveEnvelopeFeedback.stop();

          // Initialize a Rectangle element
          feedbackElement = new RectangleElement();

          // Pass to the rectangle element, the geometry defined by our feedback object
          feedbackElement.setGeometry(feedbackEnvelope);

          // make sure the element is activated in the current view
          feedbackElement.activate(feedbackScreenDisplay);

          // remove the previous element from it's orignal position
          hookHelper.getActiveView().getGraphicsContainer().deleteElement(prevElement);

          // Add the newly created element to the active view's graphics container
          hookHelper.getActiveView().getGraphicsContainer().addElement(feedbackElement, 0);

          // finally set the previous element to null
          prevElement = null;

          // refresh the view to see the change
          hookHelper.getActiveView().partialRefresh(esriViewDrawPhase.esriViewGraphics, null, null);
        }
      }
      catch(Exception e)
      {
        e.printStackTrace();
      }
    }
  }



  /**
   * This method initializes toolbarbean1
   *
   * @return com.esri.arcgis.beans.toolbar.ToolbarBean
   */
  private ToolbarBean getToolbarbean1() {
    if (toolbarbean1 == null) {
      toolbarbean1 = new ToolbarBean();
      try
      {
        toolbarbean1.setBuddyControl(getMapbean1());
        toolbarbean1.addItem(new NewEnvelopeFeedbackTool(), 0,0,false,0,esriCommandStyles.esriCommandStyleTextOnly);
        toolbarbean1.addItem(new MoveEnvelopeFeedbackTool(), 0,0,false,0,esriCommandStyles.esriCommandStyleTextOnly);
      }
      catch (IOException e)
      {
        e.printStackTrace();
      }
    }
    return toolbarbean1;
  }

  /**
   * This method initializes mapbean1
   *
   * @return com.esri.arcgis.beans.map.MapBean
   */
  private MapBean getMapbean1() {
    if (mapbean1 == null) {
      mapbean1 = new MapBean();
    }
    return mapbean1;
  }


  /**
   * This method initializes jPanel
   *
   * @return javax.swing.JPanel
   */
  private JPanel getJPanel() {
    if (jPanel == null) {
      try {
        jLabel2 = new JLabel();
        jLabel2.setText(" 2) Use the MoveEnvelopeFeedback tool to move an existing envelope.");
        jLabel2.setPreferredSize(new java.awt.Dimension(500,16));
        jLabel1 = new JLabel();
        jLabel1.setText(" 1) Use the NewEnvelopeFeedback tool to draw an envelope.");
        jLabel1.setPreferredSize(new java.awt.Dimension(500,50));
        jPanel = new JPanel();
        jPanel.setPreferredSize(new java.awt.Dimension(1027,100));
        jPanel.add(jLabel1, null);
        jPanel.add(jLabel2, null);
      } catch (java.lang.Throwable e) {
        // TODO: Something
      }
    }
    return jPanel;
  }

  /**
   * @param args
   */
  public static void main(String[] args) {
    EngineInitializer.initializeVisualBeans();
    EnvelopeFeedback akp = new EnvelopeFeedback();
    akp.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    akp.setVisible(true);
  }

  /**
   * This is the default constructor
   */
  public EnvelopeFeedback() {
    super();
    initializeArcGISLicenses();
    initialize();
  }

  /**
   * This method initializes this
   *
   * @return void
   */
  private void initialize() {
    this.setSize(551, 562);
    this.setContentPane(getJContentPane());
    this.setTitle("Envelope Feedback Tools");
    this.addWindowListener(new java.awt.event.WindowAdapter() {
      public void windowClosing(java.awt.event.WindowEvent e) {
        try{
          new AoInitialize().shutdown();
        }catch(Exception ex){
          ex.printStackTrace();
        }
      }
    });
  }

  void initializeArcGISLicenses() {
    try {
      com.esri.arcgis.system.AoInitialize ao = new com.esri.arcgis.system.AoInitialize();
      if (ao.isProductCodeAvailable(com.esri.arcgis.system.esriLicenseProductCode.esriLicenseProductCodeEngine) == com.esri.arcgis.system.esriLicenseStatus.esriLicenseAvailable)
        ao.initialize(com.esri.arcgis.system.esriLicenseProductCode.esriLicenseProductCodeEngine);
      else if (ao.isProductCodeAvailable(com.esri.arcgis.system.esriLicenseProductCode.esriLicenseProductCodeArcView) == com.esri.arcgis.system.esriLicenseStatus.esriLicenseAvailable)
        ao.initialize(com.esri.arcgis.system.esriLicenseProductCode.esriLicenseProductCodeArcView);
    } catch (Exception e) {
      System.out.println("You do not have the proper license to run this application. ");
      System.exit(0);
    }
  }

  /**
   * This method initializes jContentPane
   *
   * @return javax.swing.JPanel
   */
  private JPanel getJContentPane() {
    if (jContentPane == null) {
      jLabel = new JLabel();
      jLabel.setText("");
      jContentPane = new JPanel();
      jContentPane.setLayout(new BorderLayout());
      jContentPane.add(getToolbarbean1(), java.awt.BorderLayout.NORTH);
      jContentPane.add(getMapbean1(), java.awt.BorderLayout.CENTER);
      jContentPane.add(jLabel, java.awt.BorderLayout.WEST);
      jContentPane.add(getJPanel(), java.awt.BorderLayout.SOUTH);
    }
    return jContentPane;
  }

}  //  @jve:decl-index=0:visual-constraint="10,10"