arcgissamples\symbologybean\tools\NorthArrowTool.java—ArcObjects 10.4 Help for Java | ArcGIS for Desktop
Add map surrounds
arcgissamples\symbologybean\tools\NorthArrowTool.java
/* Copyright 2015 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 at <your ArcGIS install location>/DeveloperKit10.4/userestrictions.txt.
* 
*/
package arcgissamples.symbologybean.tools;

import javax.swing.SwingUtilities;

import arcgissamples.symbologybean.ui.SymbologyFrame;

import com.esri.arcgis.controls.BaseTool;
import com.esri.arcgis.controls.HookHelper;
import com.esri.arcgis.display.INewEnvelopeFeedback;
import com.esri.arcgis.display.NewEnvelopeFeedback;
import com.esri.arcgis.geometry.Envelope;
import com.esri.arcgis.geometry.IPoint;

public class NorthArrowTool extends BaseTool {
  private HookHelper hookHelper;
  private IPoint point;
  private INewEnvelopeFeedback envFeedback;
  private boolean inUse = false;
  private SymbologyFrame symbolForm = null;
  private Envelope envelope = null;

  public NorthArrowTool() {
    name = "AddNorthArrow";
    message = "Adds a NorthArrow element to the page layout";
    caption = "NorthArrow";
    toolTip = "Add a North Arrow";
    category = "CustomTool";
    enabled = true;
  }

  /*
     * On Create of NorthArrow Tool
     */
  public void onCreate(Object obj) {
    try {
      // Create a hookhelper to hook pagelayput
      hookHelper = new HookHelper();
      // Setting the hook to pagelayout
      hookHelper.setHookByRef(obj);
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  /*
     * On Mousedown after clicking on NorthArrow Tool
     */
  public void onMouseDown(int button, int shift, int x, int y) {
    // Create a point in map coordinates
    try {
      point = hookHelper.getActiveView().getScreenDisplay().getDisplayTransformation().toMapPoint(x, y);
    } catch (Exception e) {
      e.printStackTrace();
    }
    inUse = true;
  }

  /*
     * On MouseMove after clicking on NorthArrow Tool
     */
  public void onMouseMove(int button, int shift, int x, int y) {
    if (inUse == false)
      return;
    try {
      // Start an envelope feedback
      if (envFeedback == null) {
        envFeedback = new NewEnvelopeFeedback();
        envFeedback.setDisplayByRef(hookHelper.getActiveView().getScreenDisplay());
        envFeedback.start(point);
      }
      // Move the envelope feedback
      envFeedback.moveTo(hookHelper.getActiveView().getScreenDisplay().getDisplayTransformation().toMapPoint(x, y));
    } catch (Exception e) {
      e.printStackTrace();
    }

  }

  /*
     * On Mouseup after clicking on NorthArrow Tool
     */
  public void onMouseUp(int button, int shift, int x, int y) {
    if (inUse == false)
      return;
    // If an envelope has not been tracked or its height/width is 0
    if (envFeedback == null) {
      inUse = false;
      return;
    }
    try {
      envelope = (Envelope) envFeedback.stop();
      envFeedback.refresh(hookHelper.getActiveView().getScreenDisplay().getHWnd());
      if ((envelope.isEmpty()) || (envelope.getWidth() == 0)
          || (envelope.getHeight() == 0)) {
        envFeedback = null;
        inUse = false;
        return;
      }
      SwingUtilities.invokeLater(new Runnable() {
        public void run(){
        try{
          // Create the form with the SymbologyControl
          symbolForm = new SymbologyFrame(hookHelper.getActiveView(), envelope,"northarrow");
          symbolForm.setVisible(true);
          symbolForm.setSize(487, 299);
        }
        catch(Exception e){
          e.printStackTrace();
        }
        }
      });
      envFeedback = null;
      inUse = false;
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}