arcgissamples\pagelayoutbean\UseOverviewControl.java—ArcObjects 10.4 Help for Java | ArcGIS for Desktop
Use page layout controls
arcgissamples\pagelayoutbean\UseOverviewControl.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.pagelayoutbean;

import java.awt.BorderLayout;
import java.awt.FileDialog;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

import com.esri.arcgis.beans.pagelayout.PageLayoutBean;
import com.esri.arcgis.carto.IElement;
import com.esri.arcgis.carto.RectangleElement;
import com.esri.arcgis.carto.esriViewDrawPhase;
import com.esri.arcgis.controls.IPageLayoutControlEventsAdapter;
import com.esri.arcgis.controls.IPageLayoutControlEventsOnExtentUpdatedEvent;
import com.esri.arcgis.controls.IPageLayoutControlEventsOnMouseDownEvent;
import com.esri.arcgis.controls.IPageLayoutControlEventsOnPageLayoutReplacedEvent;
import com.esri.arcgis.controls.PageLayoutControl;
import com.esri.arcgis.controls.esriControlsAppearance;
import com.esri.arcgis.controls.esriControlsBorderStyle;
import com.esri.arcgis.display.RgbColor;
import com.esri.arcgis.display.SimpleFillSymbol;
import com.esri.arcgis.display.SimpleLineSymbol;
import com.esri.arcgis.system.AoInitialize;
import com.esri.arcgis.system.EngineInitializer;
import com.esri.arcgis.system.esriLicenseProductCode;
import com.esri.arcgis.system.esriLicenseStatus;

/**
 * Description:This application contains two PageLayoutControls. The filechooser dialog allows users to search and
 * select map documents, which are validated and loaded into the main PageLayoutBean, thus triggering the
 * OnPageLayoutReplaced event. The OnPageLayoutReplaced event determines the MxPath property of the main PageLayoutBean
 * and uses it to load the same document into the second PageLayoutBean with the LoadMxFile method. The OnExtentUpdated
 * event is triggered when a user zooms in on the main PageLayoutBean. The event uses the AddElement method to add an
 * element displaying the visible extent of the main PageLayoutBean on the second PageLayoutBean. Any previous elements
 * on the second PageLayoutBean are found using the FindElementByName method and are deleted from the GraphicsContainer.
 * The Refresh method is used to refresh the graphics to reflect the changes.
 */
public class UseOverviewControl extends JFrame implements ActionListener
{
  JPanel topPanel = null;
  JPanel mainPanel = null;
  JPanel rightPanel = null;
  JPanel buttonLabelPanel = null;

  JButton loadDocumentButton = null;
  JTextField pathField = null;
  String helpString = "Use the left mouse button \nto drag a rectangle \nand  the right mouse \nbutton to pan. ";
  JTextArea helpArea = null;
  JButton zoomToPageButton = null;

  PageLayoutBean pageLayoutBean;
  PageLayoutBean pageLayoutBeanlOverView;

  public UseOverviewControl()
  {
    super("OverView");
    buildFrame();
    setSize(800, 600);
    setVisible(true);
    initControl();
  }

  /**
   * This method builds 'this' frame as per the following diagram:
   * /-------------------------------------------------------------\ | JButton BorderLayout.NORTH | | JTextField |
   * |-------------------------------------------------------------| | | BorderLayout.EAST | | | | | | PageLayoutBean
   * | | | | | PageLayout Control | JLabel | | BorderLayout.CENTER | JButton | | | | | | | | | | | | |
   * |-------------------------------------------------------------|
   */

  public void buildFrame()
  {
    topPanel = new JPanel();
    mainPanel = new JPanel();
    rightPanel = new JPanel();
    buttonLabelPanel = new JPanel();

    topPanel.setLayout(new BoxLayout(topPanel, BoxLayout.X_AXIS));
    mainPanel.setLayout(new BorderLayout());
    rightPanel.setLayout(new BorderLayout());
    buttonLabelPanel.setLayout(new BorderLayout());

    loadDocumentButton = new JButton("Load Document");
    loadDocumentButton.addActionListener(this);

    pathField = new JTextField();
    pathField.setEditable(false);
    topPanel.add(loadDocumentButton);
    topPanel.add(Box.createHorizontalStrut(5));
    topPanel.add(pathField);
    topPanel.setBorder(BorderFactory.createEmptyBorder(0, 0, 10, 0));

    pageLayoutBeanlOverView = new PageLayoutBean();
    helpArea = new JTextArea(helpString);

    zoomToPageButton = new JButton("Zoom To Page");
    zoomToPageButton.addActionListener(this);

    buttonLabelPanel.add(helpArea, BorderLayout.NORTH);
    buttonLabelPanel.add(zoomToPageButton, BorderLayout.SOUTH);

    rightPanel.add(pageLayoutBeanlOverView, BorderLayout.CENTER);
    rightPanel.add(buttonLabelPanel, BorderLayout.SOUTH);
    rightPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 0));

    // Create page layout control add it to the center of the main panel.
    pageLayoutBean = new PageLayoutBean();
    mainPanel.add(topPanel, BorderLayout.NORTH);
    mainPanel.add(pageLayoutBean, BorderLayout.CENTER);
    mainPanel.add(rightPanel, BorderLayout.EAST);
    mainPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));

    getContentPane().add(mainPanel, BorderLayout.CENTER);
  }

  /*
   * Intializes control
   */
  public void initControl()
  {
    try
    {
      // Set PageLayoutBean properties
      pageLayoutBean.setEnabled(true);
      pageLayoutBeanlOverView.setEnabled(false);
      pageLayoutBean.setAppearance(esriControlsAppearance.esri3D);
      pageLayoutBeanlOverView.setAppearance(esriControlsAppearance.esriFlat);
      pageLayoutBean.setBorderStyle(esriControlsBorderStyle.esriBorder);
      pageLayoutBeanlOverView.setBorderStyle(esriControlsBorderStyle.esriNoBorder);

      // Add control listener
      pageLayoutBean.addIPageLayoutControlEventsListener(new PageLayoutControlListener());
    }
    catch (IOException ex)
    {
      System.out.println("Exception in initControl : " + ex);
      ex.printStackTrace();
    }
  }

  /**
   * @see java.awt.event.ActionListener#actionPerformed(ActionEvent event)
   * @param event
   */

  public void actionPerformed(ActionEvent event)
  {
    if (event.getSource() == loadDocumentButton)
    {
      try
      {
        loadFile();
      }
      catch (IOException ex)
      {
        System.out.println("Exception in loadDocumentButton#actionPerformed: " + ex);
        ex.printStackTrace();
      }
    }

    if (event.getSource() == zoomToPageButton)
    {
      try
      {
        // Zoom to the whole page
        pageLayoutBean.zoomToWholePage();
        IElement element = pageLayoutBeanlOverView.findElementByName("ZoomExtent", 1);
        // Get the IElement interface by finding an element by its name
        if (element != null)
        {

          // Delete the graphic
          pageLayoutBeanlOverView.getGraphicsContainer().deleteElement(element);
          // Refresh the graphics
        }
        pageLayoutBeanlOverView.refresh(esriViewDrawPhase.esriViewGraphics, null, null);
      }
      catch (IOException ex)
      {
        System.out.println("Exception in zoomToPageButton#actionPerformed: " + ex);
        ex.printStackTrace();
      }
    }
  }

  private String fileChosen;

  /**
   * Method loadFile loads the specified mxd file.
   */
  public boolean loadFile() throws IOException
  {
    boolean loaded = false;
    FileDialog fileDialog = new FileDialog(new JFrame(), "Open MXD|MXT|PMF file", FileDialog.LOAD);
    fileDialog.setVisible(true);
    String path = fileDialog.getDirectory();
    String name = fileDialog.getFile();
    fileChosen = path + "/" + name;
    if (pageLayoutBean.checkMxFile(fileChosen))
    {
      // load the document into the pagelayout bean.
      System.out.print("Loading " + name + " ... ");
      SwingUtilities.invokeLater(new Runnable()
      {
        public void run()
        {
          pathField.setText(fileChosen);
        }

      });
      pageLayoutBean.loadMxFile(fileChosen, null);
      System.out.println("done");
      loaded = true;
    }
    else
    {
      JOptionPane.showMessageDialog(null, "The current document cannot be loaded.");
      loaded = false;
    }
    return loaded;
  }

  /**
   * Description: Class which extends map control event class IPageLayoutControlEvents2Adapter
   * 
   * @see com.esri.arcgis.beans.pagelayout.IPageLayoutControlEventsAdapter
   */

  class PageLayoutControlListener extends IPageLayoutControlEventsAdapter
  {
    /**
     * @see com.esri.arcgis.beans.pagelayout.onMouseDown(IPageLayoutControlEventsOnMouseDownEvent theEvent)
     * @param theEvent
     */
    public void onMouseDown(IPageLayoutControlEventsOnMouseDownEvent theEvent)
    {
      try
      {
        // Zoom in
        if (theEvent.getButton() == 1)
        {
          pageLayoutBean.setExtent(pageLayoutBean.trackRectangle());
        }
        // Pan
        else if (theEvent.getButton() == 2)
          pageLayoutBean.pan();

      }
      catch (Exception ex)
      {
        System.out.println("Exception in PageLayoutControlListener#onMouseDown : " + ex);
        ex.printStackTrace();
      }
    }

    /**
     * @see com.esri.arcgis.beans.pagelayout.onPageLayoutReplaced(IPageLayoutControlEventsOnPageLayoutReplacedEvent
     *      theEvent) {
     * @param theEvent
     */

    public void onPageLayoutReplaced(IPageLayoutControlEventsOnPageLayoutReplacedEvent theEvent)
    {
      try
      {
        // Validate and load the Mx document
        if (pageLayoutBeanlOverView.checkMxFile(fileChosen))
        {
          pageLayoutBeanlOverView.loadMxFile(fileChosen, null);
        }
        // Create RGBColor object
      }
      catch (Exception ex)
      {
        System.out.println("Exception in PageLayoutControlListener#onPageLayoutReplaced : " + ex);
        ex.printStackTrace();
      }
    }

    /**
     * @see com.esri.arcgis.beans.pagelayout.onExtentUpdated(IPageLayoutControlEventsOnExtentUpdatedEvent theEvent)
     *      {
     * @param theEvent
     */

    public void onExtentUpdated(IPageLayoutControlEventsOnExtentUpdatedEvent theEvent)
    {
      try
      {
        // Get the IElement interface by finding an element by its name
        IElement element = pageLayoutBeanlOverView.findElementByName("ZoomExtent", 1);
        if (element != null)
          // Delete the graphic
          pageLayoutBeanlOverView.getGraphicsContainer().deleteElement(element);

        RectangleElement rectangleElement = new RectangleElement();

        // Create RGBColor object
        RgbColor rgbColor = new RgbColor();
        // Set the color properties
        rgbColor.setRed(255);
        rgbColor.setGreen(0);
        rgbColor.setBlue(0);
        rgbColor.setTransparency((byte) 255);

        // Create line symbol..
        SimpleLineSymbol outLine = new SimpleLineSymbol();
        // Set symbol properties
        outLine.setWidth(10);
        outLine.setColor(rgbColor);

        // Create RGBColor object
        RgbColor fillColor = new RgbColor();
        // Set the color properties
        fillColor.setRed(255);
        fillColor.setGreen(0);
        fillColor.setBlue(0);
        fillColor.setTransparency((byte) 0);

        // Create SimpleFillSymbol object
        SimpleFillSymbol simpleFillSymbol = new SimpleFillSymbol();
        // Set the fill symbol properties
        simpleFillSymbol.setColor(fillColor);
        simpleFillSymbol.setOutline(outLine);
        // add symbol to rectangleElement
        rectangleElement.setSymbol(simpleFillSymbol);
        // Add the element
        pageLayoutBeanlOverView.addElement(rectangleElement, theEvent.getNewEnvelope(), null, "ZoomExtent", 0);
        // Refresh the graphics
        pageLayoutBeanlOverView.refresh(esriViewDrawPhase.esriViewGraphics, null, null);

      }
      catch (Exception ex)
      {
        System.out.println("Exception in PageLayoutControlListener#onExtentUpdated : " + ex);
        ex.printStackTrace();
      }
    }
  } // End of PageLayoutListener class

  /**
   * Main program to start the program execution.
   * 
   * @param s
   */
  public static void main(String s[])
  {
    try
    {
      EngineInitializer.initializeVisualBeans();
      // Set the system look and feel
      UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

      initializeArcGISLicenses();

      UseOverviewControl overView = new UseOverviewControl();
      overView.setDefaultCloseOperation(UseOverviewControl.EXIT_ON_CLOSE);
    }
    catch (Exception ex)
    {
      ex.printStackTrace();
    }
  }

  static void initializeArcGISLicenses() {
    try {
      AoInitialize ao = new AoInitialize();
      
      if (ao.isProductCodeAvailable(esriLicenseProductCode.esriLicenseProductCodeEngine) 
          == esriLicenseStatus.esriLicenseAvailable)
        ao.initialize(esriLicenseProductCode.esriLicenseProductCodeEngine);
      else if (ao.isProductCodeAvailable(esriLicenseProductCode.esriLicenseProductCodeBasic) 
          == esriLicenseStatus.esriLicenseAvailable)
        ao.initialize(esriLicenseProductCode.esriLicenseProductCodeBasic);
      else if (ao.isProductCodeAvailable(esriLicenseProductCode.esriLicenseProductCodeStandard) 
          == esriLicenseStatus.esriLicenseAvailable)
        ao.initialize(esriLicenseProductCode.esriLicenseProductCodeStandard);
      else if (ao.isProductCodeAvailable(esriLicenseProductCode.esriLicenseProductCodeAdvanced) 
          == esriLicenseStatus.esriLicenseAvailable)
        ao.initialize(esriLicenseProductCode.esriLicenseProductCodeAdvanced);
    } catch (Exception e) {e.printStackTrace();}
  }
}