Circle callout
arcgissamples\cartography\CustomCircleCalloutMain.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.cartography;

import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.Panel;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;

import javax.swing.JFrame;
import javax.swing.JRadioButton;
import javax.swing.UIManager;

import com.esri.arcgis.beans.map.MapBean;
import com.esri.arcgis.carto.TextElement;
import com.esri.arcgis.carto.esriViewDrawPhase;
import com.esri.arcgis.display.RgbColor;
import com.esri.arcgis.display.TextSymbol;
import com.esri.arcgis.geometry.Point;
import com.esri.arcgis.system.AoInitialize;
import com.esri.arcgis.system.EngineInitializer;
import com.esri.arcgis.system.esriLicenseProductCode;
import com.esri.arcgis.system.esriLicenseStatus;

/**
 * This class provides a framework for demo application.
 */
public class CustomCircleCalloutMain extends JFrame implements ActionListener {

  private static final long serialVersionUID = 1L;
  static AoInitialize aoInit;
  MapBean mapBean = new MapBean();
  Button buttonShowSymbol = new Button();
  JRadioButton radioColor0 = new JRadioButton();
  JRadioButton radioColor1 = new JRadioButton();
  JRadioButton radioColor2 = new JRadioButton();
  TextField textSize = new TextField();
  TextField textString = new TextField();
  int color = 0x0000FFFF; // yellow
  CustomCircleCallout customCircleCallout;

  /**
   * Constructor
   */
  public CustomCircleCalloutMain() {
    initUI();
  }

  /**
   * Method to start the program execution.
   * @param s String[]
   */
  public static void main(String s[]) {
    try {
      EngineInitializer.initializeVisualBeans();
      UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
      initializeArcGISLicenses();
      new CustomCircleCalloutMain();
    } catch (Exception ex) {
      ex.printStackTrace();
    }
  }
  
  static void initializeArcGISLicenses() {
    try {
      aoInit = new AoInitialize();
      
      if (aoInit.isProductCodeAvailable(esriLicenseProductCode.esriLicenseProductCodeEngine) 
          == esriLicenseStatus.esriLicenseAvailable)
        aoInit.initialize(esriLicenseProductCode.esriLicenseProductCodeEngine);
      else if (aoInit.isProductCodeAvailable(esriLicenseProductCode.esriLicenseProductCodeArcView) 
          == esriLicenseStatus.esriLicenseAvailable)
        aoInit.initialize(esriLicenseProductCode.esriLicenseProductCodeArcView);
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  /*
   * Lays out the User Interface, and sets up event listeners
   */
  private void initUI() {
    this.customCircleCallout = new CustomCircleCallout();

    this.setTitle("Custom Circle Callout Sample Application");
    this.setSize(new Dimension(600, 500));
    this.getContentPane().setLayout(new BorderLayout());

    this.buttonShowSymbol.setLabel("Show Symbol");
    Label label1 = new Label();
    Label label2 = new Label();
    Label label3 = new Label();
    label1.setText("Text:");
    label2.setText("Size:");
    label3.setText("Color:");
    this.radioColor0.setText("Red");
    this.radioColor1.setText("Green");
    this.radioColor2.setText("Blue");
    this.textSize.setSelectionStart(2);
    this.textSize.setText("20");
    this.textString.setText("Demo");
    this.textString.setColumns(12);

    Panel textPanel = new Panel(new FlowLayout(FlowLayout.LEADING));
    textPanel.add(label1);
    textPanel.add(this.textString);
    Panel colorPanel = new Panel(new FlowLayout(FlowLayout.LEADING));
    colorPanel.add(label3);
    colorPanel.add(this.radioColor0);
    colorPanel.add(this.radioColor1);
    colorPanel.add(this.radioColor2);
    Panel sizePanel = new Panel(new FlowLayout(FlowLayout.LEADING));
    sizePanel.add(label2);
    sizePanel.add(this.textSize);
    Panel buttonPanel = new Panel(new FlowLayout(FlowLayout.LEADING));
    buttonPanel.add(this.buttonShowSymbol);

    Panel toolPanel = new Panel(new GridLayout(4,1));
    toolPanel.add(textPanel);
    toolPanel.add(colorPanel);
    toolPanel.add(sizePanel);
    toolPanel.add(buttonPanel);

    Panel nestPanel = new Panel();
    nestPanel.add(toolPanel);

    this.getContentPane().add(this.mapBean, BorderLayout.CENTER);
    this.getContentPane().add(nestPanel, BorderLayout.EAST);
    this.setVisible(true);

    // set listener
    this.buttonShowSymbol.addActionListener(this);
    this.radioColor0.addActionListener(this);
    this.radioColor1.addActionListener(this);
    this.radioColor2.addActionListener(this);
    this.addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        try {
          aoInit.shutdown();
        } catch(IOException ex) {
          // exit anyway
        }
        System.exit(0);
      }
    });
  }


  /**
   * implements interface ActionListener
   * @see java.awt.event.ActionListener#actionPerformed
   * @param event
   */
  public void actionPerformed(ActionEvent event) {
    try {
      Object eventSource = event.getSource();
      if (eventSource == this.radioColor0) {
        this.radioColor0.setSelected(true);
        this.radioColor1.setSelected(false);
        this.radioColor2.setSelected(false);
        this.color = 0x000000FF;
      }
      if (eventSource == this.radioColor1) {
        this.radioColor0.setSelected(false);
        this.radioColor1.setSelected(true);
        this.radioColor2.setSelected(false);
        this.color = 0x0000FF00;
      }
      if (eventSource == this.radioColor2) {
        this.radioColor0.setSelected(false);
        this.radioColor1.setSelected(false);
        this.radioColor2.setSelected(true);
        this.color = 0x00FF0000;
      }

      if (eventSource == this.buttonShowSymbol) {
        //
        Point anchorPoint = new Point();
        anchorPoint.setX(250.0);
        anchorPoint.setY(250.0);
        this.customCircleCallout.setAnchorPoint(anchorPoint);
        // set size
        double size = Double.parseDouble(this.textSize.getText());
        this.customCircleCallout.setSize(size);
        // set color
        RgbColor clr = new RgbColor();
        clr.setRGB(this.color);
        this.customCircleCallout.setColor(clr);
        TextSymbol textSymbol = new TextSymbol();
        textSymbol.setBackgroundByRef(this.customCircleCallout);
        TextElement textElement = new TextElement();
        textElement.setScaleText(false);
        textElement.setSymbol(textSymbol);
        textElement.setText(this.textString.getText());
        Point point = new Point();
        point.setX(450.0);
        point.setY(450.0);
        textElement.setGeometry(point);
        this.mapBean.getActiveView().getGraphicsContainer().deleteAllElements();
        this.mapBean.getActiveView().getGraphicsContainer().addElement(textElement, 0);
        this.mapBean.getActiveView().partialRefresh(esriViewDrawPhase.esriViewGraphics, null, null);
      }
    } catch (java.lang.Exception ex) {
      System.out.println("exception :" + ex); // never happened
    }
  }

}