arcgissamples\mapbean\PanMapUsingArrowKeys.java—ArcObjects 10.4 Help for Java | ArcGIS for Desktop
Pan map using arrow keys
arcgissamples\mapbean\PanMapUsingArrowKeys.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.mapbean;

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.system.AoInitialize;
import com.esri.arcgis.system.EngineInitializer;
import com.esri.arcgis.system.esriLicenseProductCode;
import com.esri.arcgis.system.esriLicenseStatus;
import com.esri.arcgis.systemUI.esriKeyIntercept;

/**
 * Description: This sample illustrates how the MapBean can be configured to pan around the display using the arrow keys
 * on the keyboard and to zoom in and out of the display using the mouse wheel, when the MapBean has focus. Most
 * container dialogs use the arrow keys to change control focus and do not pass these key values onto the MapBean. The
 * setKeyIntercept method allows these keys to be intercepted before the container dialog processes them. The
 * setAutoKeyboardScrolling method is used to enable automatic panning of the display when the arrow keys are
 * intercepted and the setAutoMouseWheel property is used to enable automatic zooming on the display with the mouse
 * wheel.
 */

public class PanMapUsingArrowKeys extends JFrame
{

  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;
  
  static AoInitialize aoInit;

  /**
   * This method initializes toolbarbean1
   * 
   * @return com.esri.arcgis.beans.toolbar.ToolbarBean
   */
  private ToolbarBean getToolbarbean1()
  {
    if (toolbarbean1 == null)
    {
      toolbarbean1 = new ToolbarBean();
      try
      {
        toolbarbean1.setBuddyControl(getMapbean1());
      }
      catch (IOException e)
      {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
      toolbarbean1.setItemsString("5|controls/ControlsOpenDocCommand|0|-1|0|0|1");
    }
    return toolbarbean1;
  }

  /**
   * This method initializes mapbean1
   * 
   * @return com.esri.arcgis.beans.map.MapBean
   */
  private MapBean getMapbean1()
  {
    if (mapbean1 == null)
    {
      mapbean1 = new MapBean();
      try
      {
        // Allow the MapControl to intercept arrowkeys when it has focus
        mapbean1.setKeyIntercept(esriKeyIntercept.esriKeyInterceptArrowKeys);

        // Set the mouse wheel and keyboard scrolling methods
        mapbean1.setAutoMouseWheel(true);
        mapbean1.setAutoKeyboardScrolling(true);

      }
      catch (IOException e)
      {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }

    }
    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 arrow keys for keyboard panning and the mouse wheel for mouse navigation.");
        jLabel2.setPreferredSize(new java.awt.Dimension(500, 16));
        jLabel1 = new JLabel();
        jLabel1.setText(" 1) Load a map document and then click on the map control to set the focus.");
        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();
    PanMapUsingArrowKeys akp = new PanMapUsingArrowKeys();
    akp.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    akp.setVisible(true);
  }

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

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

  void initializeArcGISLicenses()
  {
    try
    {
      aoInit = new AoInitialize();
      if (aoInit.isProductCodeAvailable(esriLicenseProductCode.esriLicenseProductCodeEngine) 
          == esriLicenseStatus.esriLicenseAvailable)
        aoInit.initialize(esriLicenseProductCode.esriLicenseProductCodeEngine);
      else if (aoInit.isProductCodeAvailable(esriLicenseProductCode.esriLicenseProductCodeBasic) 
          == esriLicenseStatus.esriLicenseAvailable)
        aoInit.initialize(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"