arcgissamples\symbologybean\AddMapSurrounds.java—ArcObjects 10.4 Help for Java | ArcGIS for Desktop
Add map surrounds
arcgissamples\symbologybean\AddMapSurrounds.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;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.IOException;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

import arcgissamples.symbologybean.tools.CreateScaleBarTool;
import arcgissamples.symbologybean.tools.CreateScaleTextTool;
import arcgissamples.symbologybean.tools.NorthArrowTool;

import com.esri.arcgis.beans.TOC.TOCBean;
import com.esri.arcgis.beans.pagelayout.PageLayoutBean;
import com.esri.arcgis.beans.toolbar.ToolbarBean;
import com.esri.arcgis.controls.ControlsMapFullExtentCommand;
import com.esri.arcgis.controls.ControlsMapPanTool;
import com.esri.arcgis.controls.ControlsMapZoomInTool;
import com.esri.arcgis.controls.ControlsMapZoomOutTool;
import com.esri.arcgis.controls.ControlsOpenDocCommand;
import com.esri.arcgis.controls.ControlsPageZoomInTool;
import com.esri.arcgis.controls.ControlsPageZoomOutTool;
import com.esri.arcgis.controls.ControlsPageZoomWholePageCommand;
import com.esri.arcgis.controls.ControlsSelectTool;
import com.esri.arcgis.controls.ToolbarMenu;
import com.esri.arcgis.interop.AutomationException;
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.esriCommandStyles;

/**
 * This sample demonstrates using the SymbologyControl to display north arrow, scale bar, and scale text symbols
 */
public class AddMapSurrounds extends JFrame
{
  private ToolbarBean toolbarBean = null;
  private PageLayoutBean pageLayoutBean = null;
  private TOCBean tocBean = null;  
  private AoInitialize ao = null;

  public static void main(String s[])
  {
    System.out.println("Starting AssignSymbolToGraphicElements - An ArcObjects Java SDK Developer Sample");

    EngineInitializer.initializeVisualBeans();

    final AddMapSurrounds app = new AddMapSurrounds();
    app.initializeArcGISLicenses();
    
    SwingUtilities.invokeLater(new Runnable()
    {
      public void run()
      {
        try
        {
          app.buildAppFrame();
          app.setVisible(true);
          Thread documentLoader = new Thread(new Runnable()
          {
            public void run()
            {
              try
              {
                //Get DEVKITHOME Home
                String devKitHome = System.getenv("AGSDEVKITJAVA");
                
                if (devKitHome == null || devKitHome.equalsIgnoreCase(""))
                {
                  JOptionPane.showMessageDialog(app, "ERROR - ArcGIS developer kit home invalid. Exiting application...");
                  System.exit(-1);
                }

                String fileName = devKitHome + "java" + File.separator + "samples" + File.separator
                    + "data" + File.separator + "mxds" + File.separator + "brazil.mxd";

                File file = new File(fileName);
                if (!file.exists())
                {
                  JOptionPane.showMessageDialog(app, "ERROR - Unable to find map document "
                      + fileName
                      + ".\nPlease use the Open file command to locate your map document.");
                }
                else
                {
                  app.pageLayoutBean.loadMxFile(fileName, null);
                }
              }
              catch (java.lang.Throwable e)
              {
                e.printStackTrace();
              }
            }

          }, "Map document loader");
          documentLoader.start();
        }
        catch (Exception e)
        {
          e.printStackTrace();
        }
      }
    });    
    
  }

  /**
   * 
   */
  public AddMapSurrounds()
  {
    addWindowListener(new WindowAdapter()
    {
      public void windowClosed(WindowEvent e)
      {
        try
        {
          // Release license
          ao.shutdown();

          // Do not make any call to ArcObjects after ShutDownApplication()
          EngineInitializer.releaseAll();

          // Exit app
          System.exit(0);
        }
        catch (AutomationException e1)
        {
          e1.printStackTrace();
        }
        catch (IOException e1)
        {
          e1.printStackTrace();
        }
      }
    });    
  }
  
  /**
   * Builds the application frame with Engine Controls and Swing Components
   */
  private void buildAppFrame() throws Exception
  {
    this.setSize(900, 600);
    this.setTitle("Add MapSurrounds");
    setContentPane(getAppContentPanel());
    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    setVisible(true);
    setDefaultCloseOperation(AddMapSurrounds.DISPOSE_ON_CLOSE);
  }

  /**
   * Creates and returns the content panel
   */
  private JPanel getAppContentPanel() throws Exception
  {
    JPanel contentPanel = new JPanel();
    contentPanel.setLayout(new BorderLayout());

    // add a pagelayout bean to content panel
    this.pageLayoutBean = new PageLayoutBean();
    contentPanel.add(this.pageLayoutBean, BorderLayout.CENTER);

    // add a toc bean to content panel
    this.tocBean = new TOCBean();
    tocBean.setBuddyControl(this.pageLayoutBean);
    contentPanel.add(this.tocBean, BorderLayout.WEST);

    // add a toolbar bean to content panel
    this.toolbarBean = createToolbarBean();
    contentPanel.add(this.toolbarBean, BorderLayout.NORTH);

    JPanel labelPanel = new JPanel();
    labelPanel.setLayout(new GridLayout(3, 1));
    JLabel label = new JLabel();
    label.setText("1) Load a map document into the PageLayoutControl. .\n" + 
        "2) Use the Toolbar menu,select the respective elements,drag a rectangle to add map surrounds onto the page.");
    labelPanel.add(label);

    contentPanel.add(labelPanel, BorderLayout.SOUTH);

    return contentPanel;
  }  
  
  /**
   * Create, initialize and return a toolbar bean
   */
  public ToolbarBean createToolbarBean() throws Exception
  {
    ToolbarBean toolbarBean = null;
    try
    {
      toolbarBean = new ToolbarBean();

      // Set the Buddy
      toolbarBean.setBuddyControl(this.pageLayoutBean);

      // Add tool bar items..      
      toolbarBean.addItem(new ControlsOpenDocCommand(), 0, 0, false, 0, esriCommandStyles.esriCommandStyleIconOnly); // open
      toolbarBean.addItem(new ControlsPageZoomInTool(), 0, -1, true, 0, esriCommandStyles.esriCommandStyleIconOnly); // Page ZoomIn
      toolbarBean.addItem(new ControlsPageZoomOutTool(), 0, -1, false, 0, esriCommandStyles.esriCommandStyleIconOnly); // Page ZoomOut
      toolbarBean.addItem(new ControlsPageZoomWholePageCommand(), 0, -1, false, 0, esriCommandStyles.esriCommandStyleIconOnly); // Page Pan
      toolbarBean.addItem(new ControlsMapZoomInTool(), 0, -1, false, 0, esriCommandStyles.esriCommandStyleIconOnly); // Map ZoomIn
      toolbarBean.addItem(new ControlsMapZoomOutTool(), 0, -1, false, 0, esriCommandStyles.esriCommandStyleIconOnly); // Map Zoomout
      toolbarBean.addItem(new ControlsMapPanTool(), 0, -1, false, 0, esriCommandStyles.esriCommandStyleIconOnly); // Map
                                                            // Pan
      toolbarBean.addItem(new ControlsMapFullExtentCommand(), 0, -1, false, 0, esriCommandStyles.esriCommandStyleIconOnly); // MapFullExtent
      toolbarBean.addItem(new ControlsSelectTool(), 0, -1, true, 0, esriCommandStyles.esriCommandStyleIconOnly); // Select
      
      // Create a new ToolbarMenu
      ToolbarMenu toolbarMenu = new ToolbarMenu();
      toolbarMenu.setCaption("Map Surrounds");
      toolbarMenu.addItem(new NorthArrowTool(), -1, 0, false, esriCommandStyles.esriCommandStyleTextOnly);
      toolbarMenu.addItem(new CreateScaleBarTool(), -1, 1, false, esriCommandStyles.esriCommandStyleTextOnly);
      toolbarMenu.addItem(new CreateScaleTextTool(), -1, 2, false, esriCommandStyles.esriCommandStyleTextOnly);
      
      // Add the ToolbarPalette to the ToolbarControl
      toolbarBean.addItem(toolbarMenu, 0, -1, false, 0, esriCommandStyles.esriCommandStyleTextOnly);      
    }
    catch (Exception e)
    {
      System.out.println("Couldn't add commands or tools to toolbar bean.");
      e.printStackTrace();
    }

    return toolbarBean;
  }  


  /**
   * Initializes the lowest available ArcGIS License
   */
  private void initializeArcGISLicenses()
  {
    try{
      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{
        System.err.println("Could not initialize an Engine or Basic License. Exiting application.");
        System.exit(-1);
      }
    }catch (Exception e){e.printStackTrace();}
  }  
}