arcgissamples\cartography\FilledLeaderCalloutMain.java—ArcObjects 10.4 Help for Java | ArcGIS for Desktop
Filled leader callout
arcgissamples\cartography\FilledLeaderCalloutMain.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.cartography;

import java.awt.BorderLayout;
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.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
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.IColor;
import com.esri.arcgis.display.IFillSymbol;
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 FilledLeaderCalloutMain extends JFrame implements ActionListener {

  private static final long serialVersionUID = 1L;
  static AoInitialize aoInit;
  MapBean mapBean = new MapBean();
  TextField textlLeftMargin = new TextField();
  TextField textRightMargin = new TextField();
  TextField textTopMargin = new TextField();
  TextField textBottomMargin = new TextField();
  JRadioButton radioBackColorR = new JRadioButton();
  JRadioButton radioBackColorG = new JRadioButton();
  JRadioButton radioBackColorB = new JRadioButton();
  JRadioButton radioLeaderColorR = new JRadioButton();
  JRadioButton radioLeaderColorG = new JRadioButton();
  JRadioButton radioLeaderColorB = new JRadioButton();
  JButton buttonShowSymbol = new JButton();
  int valueBackColor = 0x00FF0000;
  int valueLeaderColor = 0x000000FF;

  /**
   * Constructor
   */
  public FilledLeaderCalloutMain() {
    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 FilledLeaderCalloutMain();
    } 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.esriLicenseProductCodeBasic) == com.esri.arcgis.system.esriLicenseStatus.esriLicenseAvailable)
        aoInit.initialize(esriLicenseProductCode.esriLicenseProductCodeBasic);
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  /*
   * Lays out the User Interface, and sets up event listeners
   */
  private void initUI() {
    this.setTitle("Filled Leader Callout Sample Application");
    this.setSize(new Dimension(600, 500));
    this.getContentPane().setLayout(new BorderLayout());

    Label labelLeftMargin = new Label();
    Label labelRightMargin = new Label();
    Label labelTopMargin = new Label();
    Label labelBottomMargin = new Label();
    Label labelBackColor = new Label();
    Label labelLeaderColor = new Label();

    labelLeftMargin.setText("Left Margin:");
    this.textlLeftMargin.setText("5");
    labelRightMargin.setText("Right Margin:");
    this.textRightMargin.setText("5");
    labelTopMargin.setText("Top Margin:");
    this.textTopMargin.setText("5");
    this.textBottomMargin.setText("5");
    labelBottomMargin.setText("Bottom Margin:");
    labelBackColor.setText("BackColor:");
    labelLeaderColor.setText("LeaderColor:");
    this.radioBackColorR.setText("R");
    this.radioBackColorG.setText("G");
    this.radioBackColorB.setText("B");
    this.radioLeaderColorR.setText("R");
    this.radioLeaderColorG.setText("G");
    this.radioLeaderColorB.setText("B");
    this.buttonShowSymbol.setText("Show Symbol");

    JPanel backColorPanel = new JPanel(new FlowLayout(FlowLayout.LEADING));
    backColorPanel.add(labelBackColor);
    backColorPanel.add(labelBackColor);
    backColorPanel.add(this.radioBackColorR);
    backColorPanel.add(this.radioBackColorG);
    backColorPanel.add(this.radioBackColorB);

    JPanel leaderColorPanel = new JPanel(new FlowLayout(FlowLayout.LEADING));
    leaderColorPanel.add(labelLeaderColor);
    leaderColorPanel.add(this.radioLeaderColorB);
    leaderColorPanel.add(this.radioLeaderColorR);
    leaderColorPanel.add(this.radioLeaderColorG);

    JPanel leftMarginPanel = new JPanel(new FlowLayout(FlowLayout.LEADING));
    leftMarginPanel.add(labelLeftMargin);
    leftMarginPanel.add(this.textlLeftMargin);

    JPanel rightMarginPanel = new JPanel(new FlowLayout(FlowLayout.LEADING));
    rightMarginPanel.add(labelRightMargin);
    rightMarginPanel.add(this.textRightMargin);

    JPanel topMarginPanel = new JPanel(new FlowLayout(FlowLayout.LEADING));
    topMarginPanel.add(labelTopMargin);
    topMarginPanel.add(this.textTopMargin);

    JPanel bottomMarginPanel = new JPanel(new FlowLayout(FlowLayout.LEADING));
    bottomMarginPanel.add(labelBottomMargin, null);
    bottomMarginPanel.add(this.textBottomMargin, null);

    JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.LEADING));
    buttonPanel.add(this.buttonShowSymbol);

    Panel toolPanel = new Panel(new GridLayout(7,1));
    toolPanel.add(backColorPanel);
    toolPanel.add(leaderColorPanel);
    toolPanel.add(leftMarginPanel);
    toolPanel.add(rightMarginPanel);
    toolPanel.add(topMarginPanel);
    toolPanel.add(bottomMarginPanel);
    toolPanel.add(buttonPanel);

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

    this.getContentPane().add(this.mapBean, BorderLayout.CENTER);
    this.getContentPane().add(nestPanel, BorderLayout.EAST);
    this.setVisible(true);
    // set listeners
    this.radioBackColorR.addActionListener(this);
    this.radioBackColorG.addActionListener(this);
    this.radioBackColorB.addActionListener(this);
    this.radioLeaderColorR.addActionListener(this);
    this.radioLeaderColorG.addActionListener(this);
    this.radioLeaderColorB.addActionListener(this);
    this.buttonShowSymbol.addActionListener(this);
    this.addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        //System.out.print(e);
        try {
          aoInit.shutdown();
        } catch(IOException ex) {
          // exit anyway
        }
        System.exit(0);
      }
    });
  }

  /**
   * implements interface ActionListener
   * @see ActionListener#actionPerformed
   * @param event
   */
  public void actionPerformed(ActionEvent event) {
    try {
      Object eventSource = event.getSource();
      if (eventSource == this.radioBackColorR) {
        this.radioBackColorR.setSelected(true);
        this.radioBackColorG.setSelected(false);
        this.radioBackColorB.setSelected(false);
        this.valueBackColor = 0x000000FF;
      }
      if (eventSource == this.radioBackColorG) {
        this.radioBackColorR.setSelected(false);
        this.radioBackColorG.setSelected(true);
        this.radioBackColorB.setSelected(false);
        this.valueBackColor = 0x0000FF00;
      }
      if (eventSource == this.radioBackColorB) {
        this.radioBackColorR.setSelected(false);
        this.radioBackColorG.setSelected(false);
        this.radioBackColorB.setSelected(true);
        this.valueBackColor = 0x00FF0000;
      }
      if (eventSource == this.radioLeaderColorR) {
        this.radioLeaderColorR.setSelected(true);
        this.radioLeaderColorG.setSelected(false);
        this.radioLeaderColorB.setSelected(false);
        this.valueLeaderColor = 0x000000FF;
      }
      if (eventSource == this.radioLeaderColorG) {
        this.radioLeaderColorR.setSelected(false);
        this.radioLeaderColorG.setSelected(true);
        this.radioLeaderColorB.setSelected(false);
        this.valueLeaderColor = 0x0000FF00;
      }
      if (eventSource == this.radioLeaderColorB) {
        this.radioLeaderColorR.setSelected(false);
        this.radioLeaderColorG.setSelected(false);
        this.radioLeaderColorB.setSelected(true);
        this.valueLeaderColor = 0x00FF0000;
      }

      if (eventSource == this.buttonShowSymbol) {
        FilledLeaderCallout filledLeaderCallout = new FilledLeaderCallout();
        // set anchor
        Point anchorPoint = new Point();
        anchorPoint.setX(250.0);
        anchorPoint.setY(250.0);
        filledLeaderCallout.setAnchorPoint(anchorPoint);
        //
        // set Back Symbol
        IFillSymbol backSymbol = filledLeaderCallout.getBackSym();
        IColor backColor = backSymbol.getColor();
        backColor.setRGB(this.valueBackColor);
        backSymbol.setColor(backColor);
        // set Leader Symbol
        IFillSymbol leaderSymbol = filledLeaderCallout.getLeaderSym();
        IColor leaderColor = leaderSymbol.getColor();
        leaderColor.setRGB(this.valueLeaderColor);
        leaderSymbol.setColor(leaderColor);
        // set margins
        filledLeaderCallout.setLeft(Double.parseDouble(this.textlLeftMargin.getText()));
        filledLeaderCallout.setRight(Double.parseDouble(this.textRightMargin.getText()));
        filledLeaderCallout.setTop(Double.parseDouble(this.textTopMargin.getText()));
        filledLeaderCallout.setBottom(Double.parseDouble(this.textBottomMargin.getText()));
        //
        TextElement textElement = new TextElement();
        textElement.setScaleText(false);
        TextSymbol textSymbol = new TextSymbol();
        textSymbol.setBackgroundByRef(filledLeaderCallout);
        textElement.setSymbol(textSymbol);
        textElement.setText("Demo");
        Point p1 = new Point();
        p1.setX(550.0);
        p1.setY(450.0);
        textElement.setGeometry(p1);
        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 happens
    }
  }

}