arcgissamples\toolbarbean\commands\ZoomOutCommand.java—ArcObjects 10.4 Help for Java | ArcGIS for Desktop
Map as toolbar buddy
arcgissamples\toolbarbean\commands\ZoomOutCommand.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.toolbarbean.commands;

import java.io.IOException;

import arcgissamples.toolbarbean.MapAsToolbarBuddy;

import com.esri.arcgis.beans.map.MapBean;
import com.esri.arcgis.beans.pagelayout.PageLayoutBean;
import com.esri.arcgis.carto.IActiveView;
import com.esri.arcgis.controls.ToolbarControl;
import com.esri.arcgis.geometry.Envelope;
import com.esri.arcgis.geometry.IEnvelope;
import com.esri.arcgis.systemUI.ICommand;
import com.esri.arcgis.systemUI.ITool;

/**
 * Description: This class is a custom implementaion of tool button. Instance of this class can be added to toolbar control
 * using toolbarBean#addItem.
 * @see com.esri.arcgis.systemUI.ICommand
 * @see com.esri.arcgis.systemUI.ITool
 */


public class ZoomOutCommand implements ITool, ICommand {
  MapAsToolbarBuddy application;

  public int getBitmap() {
    return 0;
  }

  public String getCaption() {
    return "Zoom Out";

  }

  public String getCategory() {
    return "CustomCommands";
  }

  public int getHelpContextID() {
    return -1;
  }


  public String getHelpFile() {
    return null;
  }

  public String getMessage() {
    return "Zooms the display out by rectangle or single click";
  }


  public String getName() {
    return "Sample_Pan/Zoom_Zoom Out";
  }

  public String getTooltip() {
    return "Zoom Out";
  }


  public boolean isChecked() {
    return false;
  }


  public boolean isEnabled() {
    return true;
  }

  public void onClick() {
  }

  public void onCreate(Object hook) {
    try {
      ToolbarControl toolbarControl = new ToolbarControl(hook);
      application = (MapAsToolbarBuddy)toolbarControl.getBuddy();
    }
    catch (IOException ex) {
    }
  }

  public boolean deactivate() {
    return true;
  }

  public int getCursor() {
    return 0;
  }

  public boolean onContextMenu(int x, int y) {
    return false;
  }

  public void onDblClick() {
  }

  public void onKeyDown(int keyCode, int shift) {
  }

  public void onKeyUp(int keyCode, int shift) {
  }

  public void onMouseDown(int button, int shift, int x, int y) {
    try {
      //Get the active control
      IEnvelope envelope = null;
      if (MapAsToolbarBuddy.ACTIVECONTROL ==
        MapAsToolbarBuddy.MAPCONTROL) {
        MapBean mapBean = application.getMapBean();
        envelope = mapBean.trackRectangle();
        mapBean.setExtent(getPanExtent(envelope, mapBean.getActiveView(), x, y));
        mapBean.getActiveView().refresh();

      }
      else if (MapAsToolbarBuddy.ACTIVECONTROL ==
        MapAsToolbarBuddy.PAGELAYOUTCONTROL) {
        PageLayoutBean pageLayoutBean = application.
        getPageLayoutBean();
        envelope = pageLayoutBean.trackRectangle();
        pageLayoutBean.setExtent(getPanExtent(envelope, pageLayoutBean.getActiveView(), x, y));
        pageLayoutBean.getActiveView().refresh();

      }
    }
    catch (IOException ex) {
      ex.printStackTrace();
    }

  }

  public IEnvelope getPanExtent(IEnvelope trackEnvelope, IActiveView activeView, int x, int y) throws IOException {
    //Create an envelope by tracking a rectangle
    //If the tracked envelope is empty
    IEnvelope newExtent;
    IEnvelope currentExtent = activeView.getExtent();
    if(trackEnvelope.isEmpty()) {
      newExtent = currentExtent;
      //Expand the current extent and center
      newExtent.expand(2.0, 2.0, true);
      newExtent.centerAt(activeView.getScreenDisplay().getDisplayTransformation().toMapPoint(x, y));
    }
    else {
      //Create coordinates for the new extent

      //Create new extent height and widths
      newExtent = new Envelope();
      double newWidth = currentExtent.getWidth() * (currentExtent.getWidth() / trackEnvelope.getWidth());
      double newHeight = currentExtent.getHeight() * (currentExtent.getHeight() / trackEnvelope.getHeight());
      //Set the new extent coordinates
      newExtent.putCoords(currentExtent.getXMin() - ((trackEnvelope.getXMin() - currentExtent.getXMin()) * (currentExtent.getWidth() / trackEnvelope.getWidth())),
          currentExtent.getYMin() - ((trackEnvelope.getYMin() - currentExtent.getYMin()) * (currentExtent.getHeight() / trackEnvelope.getHeight())),
          (currentExtent.getXMin() - ((trackEnvelope.getXMin() - currentExtent.getXMin()) * (currentExtent.getWidth() / trackEnvelope.getWidth()))) + newWidth,
          (currentExtent.getYMin() - ((trackEnvelope.getYMin() - currentExtent.getYMin()) * (currentExtent.getHeight() / trackEnvelope.getHeight()))) + newHeight);

    }
    return newExtent;

  }

  public void onMouseMove(int button, int shift, int x, int y) {
  }

  public void onMouseUp(int button, int shift, int x, int y) {
  }

  public void refresh(int hdc) {
  }

}