arcgissamples\display\RubberBandZoom.java—ArcObjects 10.4 Help for Java | ArcGIS for Desktop
RubberBand zoom
arcgissamples\display\RubberBandZoom.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.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.controls.BaseTool;
import com.esri.arcgis.controls.HookHelper;
import com.esri.arcgis.controls.IHookHelper;
import com.esri.arcgis.display.IRubberBand;
import com.esri.arcgis.display.RubberRectangularPolygon;
import com.esri.arcgis.geometry.Envelope;
import com.esri.arcgis.geometry.IGeometry;
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 the usage of the RubberPolygon object. This object is tied with the notion of
  RubberBand objects that are broadly used to allow users to digitize geometries on the display using the
  mouse - either to create whole new geometry objects or to update existing ones. As such, they can be
  viewed as simple versions of the Feedback objects in the Display API. In this sample, we show how to
  allow users to draw an extent on the Map to zoom into.
 */


public class RubberBandZoom 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;
  private JLabel jLabel3 = null;

  class RubberBandZoomTool extends BaseTool {
    private static final long serialVersionUID = 1L;
    private IHookHelper hookHelper = null;
    private IRubberBand rubberBand = null;
    private IGeometry geometry = null;

    RubberBandZoomTool()
    {
      super();

      super.category = "Developer Samples";
      super.caption = "Rubber Band Tool";
      super.message = "This tool allows zooming into a newly drawn RubberPolygon";
      super.toolTip = "RubberBandZoom";
      super.name = "DevSamples_RubberBandZoom";
    }

    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.
    }

    public boolean onContextMenu(int button, int shift)
    {
      // returning true for this function overrides the default
      // context menu - we don't want them to come up.
      return true;
    }


    public void onMouseDown(int button, int shift, int x, int y)
    {
      try
      {
        rubberBand = new RubberRectangularPolygon();

        if(button == 1)
        {
          // create a new rubberband polygon
          geometry = rubberBand.trackNew(hookHelper.getActiveView().getScreenDisplay(), null);

          // zoom to the selected envelope as long as it is not zero
          if(!geometry.isEmpty())
          {
            if((((Envelope)geometry.getEnvelope()).getHeight() != 0) && (((Envelope)geometry.getEnvelope()).getWidth() != 0))
            {
              hookHelper.getActiveView().setExtent(geometry.getEnvelope());

              // refresh to show changes
              hookHelper.getActiveView().refresh();
            }
          }
        }
        else if(button == 2) // right click
        {
          // zoom out either to previous extent, or to full extent of the active view
          if(hookHelper.getActiveView().getExtentStack().canUndo())
            // if we can, go to the previous zoom extent
            hookHelper.getActiveView().getExtentStack().undo();
          else
            // if no previous extent exists, go to the full extent of the active view
            hookHelper.getActiveView().setExtent(hookHelper.getActiveView().getFullExtent().getEnvelope());

          hookHelper.getActiveView().refresh();
        }
      }
      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.setItemsString("5|controls/ControlsOpenDocCommand|0|-1|0|0|1");
        toolbarbean1.addItem(new RubberBandZoomTool(), 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 {

        jLabel3 = new JLabel();
        jLabel3.setText(" 3) Right click to restore the previous extent.");
        jLabel3.setPreferredSize(new java.awt.Dimension(600,25));
        jLabel2 = new JLabel();
        jLabel2.setText(" 2) Use the mouse to draw a polygon to set the extent for the map to zoom in.");
        jLabel2.setPreferredSize(new java.awt.Dimension(600,25));
        jLabel1 = new JLabel();
        jLabel1.setText(" 1) Load a map document.");
        jLabel1.setPreferredSize(new java.awt.Dimension(600,25));
        jPanel = new JPanel();
        jPanel.setPreferredSize(new java.awt.Dimension(1027,100));
        jPanel.add(jLabel1, null);
        jPanel.add(jLabel2, null);
        jPanel.add(jLabel3, null);
      } catch (java.lang.Throwable e) {
        // TODO: Something
      }
    }
    return jPanel;
  }

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

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

  /**
   * This method initializes this
   *
   * @return void
   */
  private void initialize() {
    this.setSize(650, 562);
    this.setContentPane(getJContentPane());
    this.setTitle("RubberBandZoom");
    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.esriLicenseProductCodeBasic) == com.esri.arcgis.system.esriLicenseStatus.esriLicenseAvailable)
        ao.initialize(com.esri.arcgis.system.esriLicenseProductCode.esriLicenseProductCodeBasic);
    } 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"