arcgissamples\toolbarbean\CustomizeToolbar.java—ArcObjects 10.4 Help for Java | ArcGIS for Desktop
Customize toolbar
arcgissamples\toolbarbean\CustomizeToolbar.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.BorderLayout;
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.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;

import com.esri.arcgis.beans.map.MapBean;
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.ControlsMapZoomToLastExtentBackCommand;
import com.esri.arcgis.controls.ControlsMapZoomToLastExtentForwardCommand;
import com.esri.arcgis.controls.ControlsOpenDocCommand;
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 customizing the ToolbarBean at run time. The ToolbarBean is used in conjunction
 * with the MapBean and the default ToolbarBean commands. The AddItem method is used within the Form_Load event to add
 * new Items to the ToolbarBean with their Style set. The Customize method determines whether the ToolbarBean can be
 * customized at run time.
 */

public class CustomizeToolbar extends JFrame implements ActionListener
{
  JPanel mainPanel = null;
  JPanel rightPanel = null;
  JCheckBox customizeCheck = null;
  MapBean mapBean = null;
  ToolbarBean toolbarBean = null;

  String helpString = "<HTML> To move an toobar item, select it with the left <BR> mouse button and drag and drop it to <BR> the location indicated by the black <BR> vertical bar. <BR><BR>"
      + "To delete an item, either select it with <BR> the left mouse button and drag it off the <BR> ToolbarBean or select it with the right <BR> mouse button and choose delete from<BR> the customize menu.. <BR><BR>"
      + "To change the group, group spacing or <BR> style of an item, select it with the right <BR> mouse button to display the customize<BR> menu."
      + "</HTML>";
  JLabel helpLabel = null;

  public CustomizeToolbar()
  {
    super("Toolbar Customization");
    buildFrame();
    setSize(650, 500);
    setVisible(true);
    initControl();

  }

  public void buildFrame()
  {
    rightPanel = new JPanel();
    customizeCheck = new JCheckBox("Customize Toolbar");
    customizeCheck.addActionListener(this);
    helpLabel = new JLabel();
    helpLabel.setText(helpString);
    rightPanel.setLayout(new BoxLayout(rightPanel, BoxLayout.Y_AXIS));
    rightPanel.add(customizeCheck);
    rightPanel.add(Box.createVerticalStrut(10));
    rightPanel.add(helpLabel);
    rightPanel.add(Box.createVerticalGlue());
    rightPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));

    mapBean = new MapBean();
    toolbarBean = new ToolbarBean();
    toolbarBean.setSize(490, 20);
    mainPanel = new JPanel();
    mainPanel.setLayout(new BorderLayout());
    mainPanel.add(toolbarBean, BorderLayout.NORTH);
    mainPanel.add(mapBean, BorderLayout.CENTER);
    mainPanel.add(rightPanel, BorderLayout.EAST);
    mainPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
    getContentPane().add(mainPanel, BorderLayout.CENTER);
  }

  /**
   * Initilizes control
   **/

  public void initControl()
  {
    try
    {
      // Set the Buddy
      toolbarBean.setBuddyControl(mapBean);
      // Add tool bar items..
      toolbarBean.addItem(new ControlsOpenDocCommand(), 0, -1, false, 0,
          esriCommandStyles.esriCommandStyleIconOnly); // open
      toolbarBean.addItem(new ControlsMapZoomInTool(), 0, -1, true, 0,
          esriCommandStyles.esriCommandStyleIconAndText); // ZoomIn
      toolbarBean.addItem(new ControlsMapZoomOutTool(), 0, -1, false, 0,
          esriCommandStyles.esriCommandStyleIconAndText); // ZoomOut
      toolbarBean.addItem(new ControlsMapPanTool(), 0, -1, false, 0,
          esriCommandStyles.esriCommandStyleIconAndText); // Pan
      toolbarBean.addItem(new ControlsMapFullExtentCommand(), 0, -1, true, 0,
          esriCommandStyles.esriCommandStyleTextOnly); // FullExtent
      toolbarBean.addItem(new ControlsMapZoomToLastExtentBackCommand(), 0, -1, false, 0,
          esriCommandStyles.esriCommandStyleTextOnly); // ZoomToLastExtentBackCommand
      toolbarBean.addItem(new ControlsMapZoomToLastExtentForwardCommand(), 0, -1, false, 0,
          esriCommandStyles.esriCommandStyleTextOnly); // ZoomToLastExtentForwardCommand

    }
    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() == customizeCheck)
    {
      try
      {
        // Set whether control can be customized at run time
        if (customizeCheck.isSelected())
        {
          toolbarBean.setCustomize(true);
        }
        else
        {
          toolbarBean.setCustomize(false);
        }
      }
      catch (IOException ex)
      {
        System.out.println("Exception in ActionListener#actionPerformed:" + ex);
        ex.printStackTrace();
      }
    }
  }

  /**
   * 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();

      CustomizeToolbar customization = new CustomizeToolbar();
      customization.setDefaultCloseOperation(JFrame.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();}
  }
}