arcgissamples\toolbarbean\CreateBookmark.java—ArcObjects 10.4 Help for Java | ArcGIS for Desktop
Create bookmark
arcgissamples\toolbarbean\CreateBookmark.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;

import java.awt.Rectangle;

import javax.swing.JFrame;
import javax.swing.JTextPane;
import javax.swing.UIManager;

import arcgissamples.toolbarbean.commands.CreateBookmarkCommand;
import arcgissamples.toolbarbean.ui.SpatialBookmarks;

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.ToolbarMenu;
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;

/**
 * Description: This sample demonstrates creating a multi-item toolbar menu with spatial bookmarks. It shows usage of
 * the HookHelper to get the focus map and create a new bookmark. The new spatial bookmark is then added to multi-item
 * toolbar menu. A JOptionPane input dialog is used to create a name for the bookmark. The count is maintained by using
 * an IEnumSpatialBookmark.
 */

public class CreateBookmark extends JFrame
{
  PageLayoutBean PageLayoutBean1 = new PageLayoutBean();
  ToolbarBean toolbarBean1 = new ToolbarBean();
  JTextPane jTextPane1 = new JTextPane();
  JTextPane jTextPane2 = new JTextPane();
  JTextPane jTextPane3 = new JTextPane();

  public CreateBookmark()
  {

    setTitle("Multi Item Bookmarks Menu");
    try
    {
      init();
    }
    catch (Exception e)
    {
      e.printStackTrace();
    }

    this.addWindowListener(new java.awt.event.WindowAdapter()
    {
      public void windowClosing(java.awt.event.WindowEvent e)
      {
        try
        {
          new AoInitialize().shutdown();
        }
        catch (Exception ex)
        {
          ex.printStackTrace();
        }
      }
    });

  }

  public void setBuddyControlAndTools()
  {
    try
    {

      // Buddy-up the toolbarcontrol with the mapcontrol
      toolbarBean1.setBuddyControl(PageLayoutBean1);

      // Create a new ToolbarMenu
      ToolbarMenu toolbarMenu = new ToolbarMenu();

      // Set a caption
      toolbarMenu.setCaption("Spatial Bookmarks");
      // Add the custom command to the ToolbarMenu
      toolbarMenu.addItem(new CreateBookmarkCommand(), -1, -1, false, esriCommandStyles.esriCommandStyleTextOnly);
      // Add the custom multiitem to the ToolbarMenu
      toolbarMenu.addMultiItem(new SpatialBookmarks(), -1, true, esriCommandStyles.esriCommandStyleTextOnly);

      // Add the menu item to the ToolbarControl
      toolbarBean1.addItem(toolbarMenu, -1, -1, true, 0, esriCommandStyles.esriCommandStyleMenuBar);

      // Add items to the toolbar...
      toolbarBean1.addItem(new ControlsMapFullExtentCommand(), 0, 0, false, 0,
          esriCommandStyles.esriCommandStyleIconAndText);
      toolbarBean1.addItem(new ControlsMapPanTool(), 0, 0, false, 0,
          esriCommandStyles.esriCommandStyleIconAndText);
      toolbarBean1.addItem(new ControlsMapZoomOutTool(), 0, 0, false, 0,
          esriCommandStyles.esriCommandStyleIconAndText);
      toolbarBean1.addItem(new ControlsMapZoomInTool(), 0, 0, true, 0,
          esriCommandStyles.esriCommandStyleIconAndText);
      toolbarBean1.addItem(new ControlsOpenDocCommand(), 0, 0, false, 0,
          esriCommandStyles.esriCommandStyleIconAndText);

    }
    catch (Exception e)
    {
      e.printStackTrace();
    }

  }

  public static void main(String[] args)
  {

    EngineInitializer.initializeVisualBeans();
    initializeArcGISLicenses();

    CreateBookmark mib = new CreateBookmark();
    mib.setSize(650, 475);

    try
    {
      mib.setBuddyControlAndTools();
    }
    catch (Exception ex)
    {
      ex.printStackTrace();
    }
    mib.setVisible(true);
    mib.PageLayoutBean1.setSize(400, 345);
    mib.toolbarBean1.setSize(540, 25);

    mib.setDefaultCloseOperation(CreateBookmark.EXIT_ON_CLOSE);

  }

  private void init() throws Exception
  {

    // Set up all of the GUI
    this.getContentPane().setLayout(null);
    this.setBackground(new java.awt.Color(236, 233, 216));
    this.getContentPane().setBackground(new java.awt.Color(236, 233, 216));
    this.setResizable(false);
    PageLayoutBean1.setBounds(new Rectangle(15, 49, 400, 345));
    toolbarBean1.setBounds(new Rectangle(16, 13, 512, 25));
    jTextPane1.setBackground(new java.awt.Color(236, 233, 216));
    jTextPane1.setFont(new java.awt.Font("Dialog", 0, 10));
    jTextPane1.setDisabledTextColor(UIManager.getColor("Label.background"));
    jTextPane1.setEditable(false);
    jTextPane1.setText("Browse to a map document to load into the MapBean.");
    jTextPane1.setBounds(new Rectangle(464, 54, 137, 50));
    jTextPane2.setBounds(new Rectangle(464, 109, 135, 60));
    jTextPane2.setText("Create a new bookmark based on the current extent of the focus map.");
    jTextPane2.setBackground(new java.awt.Color(236, 233, 216));
    jTextPane2.setFont(new java.awt.Font("Dialog", 0, 10));
    jTextPane2.setEditable(false);
    jTextPane3.setBackground(new java.awt.Color(236, 233, 216));
    jTextPane3.setFont(new java.awt.Font("Dialog", 0, 10));
    jTextPane3.setEditable(false);
    jTextPane3.setText("Zoom to any bookmarks listed in the spatial bookmark menu.");
    jTextPane3.setBounds(new Rectangle(464, 180, 135, 50));
    this.getContentPane().add(PageLayoutBean1, null);
    this.getContentPane().add(toolbarBean1, null);
    this.getContentPane().add(jTextPane1, null);
    this.getContentPane().add(jTextPane2, null);
    this.getContentPane().add(jTextPane3, null);
  }

  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);
    }catch (Exception e){e.printStackTrace();}
  }
}