Draw graphic elements on map
arcgissamples\toolbarbean\DrawGraphicElementsOnMap.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.Rectangle;

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

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.ControlsNewCircleTool;
import com.esri.arcgis.controls.ControlsNewCurveTool;
import com.esri.arcgis.controls.ControlsNewEllipseTool;
import com.esri.arcgis.controls.ControlsNewFreeHandTool;
import com.esri.arcgis.controls.ControlsNewLineTool;
import com.esri.arcgis.controls.ControlsNewPolygonTool;
import com.esri.arcgis.controls.ControlsNewRectangleTool;
import com.esri.arcgis.controls.ControlsOpenDocCommand;
import com.esri.arcgis.controls.ControlsSelectTool;
import com.esri.arcgis.controls.IPageLayoutControlEventsAdapter;
import com.esri.arcgis.controls.IPageLayoutControlEventsOnMouseDownEvent;
import com.esri.arcgis.controls.IToolbarPalette;
import com.esri.arcgis.controls.ToolbarPalette;
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 toolbar palette of drawing tools. The palette is accessed from the
 * ToolbarBean or by right-clicking on the PageLayoutBean. The addItem method is used within the setBuddyControlAndTools
 * to add new item's to the ToolbarBean with their Style set. Within the PageLayoutBean's OnMouseDown event, the
 * popupPalette method is used to display the ToolbarPalette when the right mouse button has been used.
 */

public class DrawGraphicElementsOnMap extends JFrame
{
  PageLayoutBean PageLayoutBean1 = new PageLayoutBean();
  ToolbarBean toolbarBean1 = new ToolbarBean();
  JTextPane jTextPane1 = new JTextPane();
  JTextPane jTextPane2 = new JTextPane();
  JTextPane jTextPane3 = new JTextPane();
  IToolbarPalette toolbarPalette1 = null;

  public DrawGraphicElementsOnMap()
  {

    setTitle("Drawing Tools Toolbar Palette");
    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 ToolbarPalette
      toolbarPalette1 = new ToolbarPalette();

      // Add items to the Palette
      toolbarPalette1.addItem(new ControlsSelectTool(), 0, -1);
      toolbarPalette1.addItem(new ControlsNewCircleTool(), 0, -1);
      toolbarPalette1.addItem(new ControlsNewCurveTool(), 0, -1);
      toolbarPalette1.addItem(new ControlsNewEllipseTool(), 0, -1);
      toolbarPalette1.addItem(new ControlsNewLineTool(), 0, -1);
      toolbarPalette1.addItem(new ControlsNewPolygonTool(), 0, -1);
      toolbarPalette1.addItem(new ControlsNewRectangleTool(), 0, -1);
      toolbarPalette1.addItem(new ControlsNewFreeHandTool(), 0, -1);

      // Add items to the toolbar...
      toolbarBean1.addItem(toolbarPalette1, 0, -1, true, -1, esriCommandStyles.esriCommandStyleIconOnly);
      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);

      // Add PageLayoutcontrol listener
      PageLayoutBean1.addIPageLayoutControlEventsListener(new PageLayoutControlListener());

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

  }

  public static void main(String[] args)
  {

    EngineInitializer.initializeVisualBeans();
    initializeArcGISLicenses();

    DrawGraphicElementsOnMap tbPalette = new DrawGraphicElementsOnMap();
    tbPalette.setSize(650, 475);

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

    tbPalette.setDefaultCloseOperation(DrawGraphicElementsOnMap.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 PageLayoutBean.");
    jTextPane1.setBounds(new Rectangle(464, 54, 137, 50));
    jTextPane2.setBounds(new Rectangle(464, 109, 135, 60));
    jTextPane2.setText("Use the palette on the Toolbar to draw some elements on the PageLayout.");
    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("Right-click on the PageLayout to display the popup palette.");
    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);
  }

  /**
   * Description: Class which extends pagelayoutcontrol event class IPageLayoutControlEvents2Adapter
   * 
   * @see com.esri.arcgis.controls.IPageLayoutControlEventsAdapter
   */

  class PageLayoutControlListener extends IPageLayoutControlEventsAdapter
  {

    public void onMouseDown(IPageLayoutControlEventsOnMouseDownEvent theEvent)
    {
      try
      {
        if (theEvent.getButton() == 2)

          // Popup the palette
          toolbarPalette1.popupPalette(theEvent.getX(), theEvent.getY(), PageLayoutBean1.getHWnd());
      }
      catch (Exception ex)
      {
        System.out.println("Exception in PageLayoutControlListener#onMouseMove : " + 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);
    }catch (Exception e){e.printStackTrace();}
  }
}