Use context menu
arcgissamples\toolbarbean\UseContextMenu.java
/* Copyright 2010 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.
* 
*/
package arcgissamples.toolbarbean;

import java.awt.BorderLayout;
import java.io.IOException;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
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.ControlsOpenDocCommand;
import com.esri.arcgis.controls.IMapControlEvents2Adapter;
import com.esri.arcgis.controls.IMapControlEvents2OnMouseDownEvent;
import com.esri.arcgis.controls.IMapControlEvents2OnMouseMoveEvent;
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.IItemDef;
import com.esri.arcgis.systemUI.IMenuDef;
import com.esri.arcgis.systemUI.esriCommandStyles;

/**
 * Description: This sample demonstrates creating a popup menu and uses the ToolbarBean in conjunction with the MapBean
 * and the default ToolbarBean commands. The AddItem method is used within the initControl to add new item's to the
 * ToolbarBean with their Style set. An IMenuDef object is created from a menu defined within the same application. The
 * menu is defined within a class implementing the IMenuDef interface and is made up of default ToolbarBean commands.
 * This MenuDef is added to a ToolbarMenu using the AddMenuDef method and the ToolbarMenu's Hook property is set to the
 * ToolbarBean. Within the MapBean's OnMouseDown event the PopupMenu method is used to display the ToolbarMenu when the
 * right mouse button has been used.
 */

public class UseContextMenu extends JFrame
{
  JPanel leftPanel = null;
  JPanel mainPanel = null;
  JPanel rightPanel = null;
  MapBean mapBean = null;
  ToolbarBean toolbarBean = null;
  IMenuDef menudef = null;
  ToolbarMenu toolbarMenu = null;

  String helpString = "<HTML> Browse to a map document <BR> to load into the MapBean. <BR>"
      + "Navigate around the data <BR> using the commands on the <BR> ToolbarBean. <BR><BR>"
      + "Click on the MapBean with <BR> the right hand mouse button to <BR> display the popup menu" + "</HTML>";
  JLabel helpLabel = null;

  public UseContextMenu()
  {
    super("Context Menu");
    buildFrame();
    setSize(650, 500);
    setVisible(true);
    initControl();

  }

  /**
   * This method builds 'this' frame as per the following diagram:
   * /----------------------------------------------------------------------| | BorderLayout.NORTH | | |
   * ToolBarControl | | |------------------------------------------------| BorderLayout | | | EAST | | | | | | JLabel
   * | | BorderLayout.CENTER | | | MapBean | | | | | | | | | | | | | | | | | | | | | | | | | |
   * \------------------------------------------------|---------------------|
   */

  public void buildFrame()
  {
    leftPanel = new JPanel();
    rightPanel = new JPanel();
    helpLabel = new JLabel();
    helpLabel.setText(helpString);
    rightPanel.setLayout(new BoxLayout(rightPanel, BoxLayout.Y_AXIS));
    rightPanel.add(Box.createVerticalStrut(40));
    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);
    leftPanel.setLayout(new BorderLayout());
    leftPanel.add(toolbarBean, BorderLayout.NORTH);
    leftPanel.add(mapBean, BorderLayout.CENTER);

    mainPanel = new JPanel();
    mainPanel.setLayout(new BorderLayout());

    mainPanel.add(leftPanel, 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, 0, 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, 20,
          esriCommandStyles.esriCommandStyleIconAndText); // FullExtent

      // Add mapcontrol lisetener
      mapBean.addIMapControlEvents2Listener(new MapControlListener());

      // Create a MenuDef object
      menudef = new NavigationMenu();
      // Create a new ToolbarMenu
      toolbarMenu = new ToolbarMenu();
      toolbarMenu.addItem(menudef, 0, -1, false, esriCommandStyles.esriCommandStyleIconAndText);
      // Set the Toolbarmenu's hook
      toolbarMenu.setHook(toolbarBean.getObject());
    }
    catch (IOException ex)
    {
      System.out.println("Exception in initControl : " + ex);
      ex.printStackTrace();
    }
  }

  /**
   * Description: Class which extends map control event class IMapControlEvents2Adapter
   * 
   * @see com.esri.arcgis.beans.map.IMapControlEvents2Adapter
   */
  class MapControlListener extends IMapControlEvents2Adapter
  {

    /**
     * @see com.esri.arcgis.beans.map.IMapControlEvents2Adapter#onMouseMove(IMapControlEvents2OnMouseMoveEvent
     *      theEvent)
     * @param theEvent
     */
    public void onMouseDown(IMapControlEvents2OnMouseDownEvent theEvent)
    {
      try
      {
        if (theEvent.getButton() == 2)
          // Popup the menu
          toolbarMenu.popupMenu(theEvent.getX(), theEvent.getY(), mapBean.getHWnd());
      }
      catch (Exception ex)
      {
        System.out.println("Exception in MapControlListener#onMouseMove : " + ex);
        ex.printStackTrace();
      }
    }
  }

  /**
   * Description: Class which implements IMenuDef interface. Returns default toolbar control commands
   * 
   * @see com.esri.arcgis.systemUI.IMenuDef
   */
  class NavigationMenu implements IMenuDef
  {
    /**
     * The caption of this menu.
     */
    public String getCaption()
    {
      // The caption of this menu.
      return "Navigation";
    }

    /**
     * The number of items in this menu
     */
    public int getItemCount()
    {
      // The number of items in this menu.
      return 5;
    }

    /**
     * toolbar control commands
     */
    public void getItemInfo(int pos, IItemDef itemDef)
    {
      try
      {
        // Commands for the menu
        switch (pos)
        {
        case 0:
          itemDef.setID("esriControlCommands.ControlsMapZoomInFixedCommand");
          break;
        case 1:
          itemDef.setID("esriControlCommands.ControlsMapZoomOutFixedCommand");
          break;
        case 2:
          itemDef.setID("esriControlCommands.ControlsMapFullExtentCommand");
          break;
        case 3:
          itemDef.setID("esriControlCommands.ControlsMapZoomToLastExtentBackCommand");
          itemDef.setGroup(true);
          break;
        case 4:
          itemDef.setID("esriControlCommands.ControlsMapZoomToLastExtentForwardCommand");
          break;
        default:
          // do nothing.
        }
      }
      catch (IOException ex)
      {
      }
    }

    /**
     * The name of this menu.
     **/
    public String getName()
    {
      return "Navigation";
    }
  }

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

      UseContextMenu popupMenu = new UseContextMenu();
      popupMenu.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.esriLicenseProductCodeArcView)
          == esriLicenseStatus.esriLicenseAvailable)
        ao.initialize(esriLicenseProductCode.esriLicenseProductCodeArcView);
      else if (ao.isProductCodeAvailable(esriLicenseProductCode.esriLicenseProductCodeArcEditor) 
          == esriLicenseStatus.esriLicenseAvailable)
        ao.initialize(esriLicenseProductCode.esriLicenseProductCodeArcEditor);
      else if (ao.isProductCodeAvailable(esriLicenseProductCode.esriLicenseProductCodeArcInfo) 
          == esriLicenseStatus.esriLicenseAvailable)
        ao.initialize(esriLicenseProductCode.esriLicenseProductCodeArcInfo);
    } catch (Exception e) {e.printStackTrace();}
  }
}