arcgissamples\symbologybean\ui\SymbologyFrame.java—ArcObjects 10.4 Help for Java | ArcGIS for Desktop
Add text elements to map
arcgissamples\symbologybean\ui\SymbologyFrame.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.symbologybean.ui;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

import com.esri.arcgis.controls.ISymbologyControlEventsAdapter;
import com.esri.arcgis.controls.ISymbologyControlEventsOnItemSelectedEvent;
import com.esri.arcgis.controls.SymbologyControl;
import com.esri.arcgis.controls.esriSymbologyStyleClass;
import com.esri.arcgis.display.IStyleGalleryItem;
import com.esri.arcgis.display.IStyleGalleryItemProxy;

@SuppressWarnings("serial")
public class SymbologyFrame extends JFrame implements ActionListener {

  private JPanel jMainPanel;
  private JPanel jButtonPanel;
  private SymbologyControl symbologyControl;
  private JButton jBtnOK;
  private JButton jBtnCancel;
  private IStyleGalleryItem styleGalleryItem;

  public SymbologyFrame() {
    initialize();
    loadStyle();
    addListener();
  }

  /*
   *  Initialize the symbology Frame and set size and Title
   */
  private void initialize() {
    this.setSize(487, 300);
    this.setContentPane(getJContentPanel());
    this.setTitle("Text Symbol");
  }

  /*
   * Load the style file and set the style class
   */
  public void loadStyle() {
    try {
      //Get the ArcGIS Engine runtime, if it is available
      String runtimeHome = System.getenv("AGSENGINEJAVA");
      
      //If the ArcGIS Engine runtime is not available, then we can try ArcGIS Desktop runtime
      if(runtimeHome == null){
        runtimeHome = System.getenv("AGSDESKTOPJAVA");
      }
      
      //If no runtime is available, exit application gracefully
      if(runtimeHome == null){
        if(System.getProperty("os.name").toLowerCase().indexOf("win") > -1){
          System.err.println("You must have ArcGIS Engine Runtime or ArcGIS Desktop " + 
              "installed in order to execute this sample.");
          System.err.println("Install one of the products above, then re-run this sample.");
            System.err.println("Exiting execution of this sample...");
            System.exit(0);
        }else{
          System.err.println("You must have ArcGIS Engine Runtime installed " + 
              "in order to execute this sample.");
          System.err.println("Install the product above, then re-run this sample.");
          System.err.println("Exiting execution of this sample...");
          System.exit(0);  
        }
      }
      
      //Obtain relative path to the ESRI.ServerStyle file
      String esriStylePath = runtimeHome + "styles" + File.separator + "ESRI.ServerStyle";
      
      File styleFile = new File(esriStylePath);

          //Test to make sure style file is present
      if(!styleFile.exists()){
            System.err.println("The ESRI.ServerStyle was not found in the following location: " +
                                                  styleFile.getParent());
              System.err.println("Verify that ESRI.ServerStyle can be located in the specified folder.");
              System.err.println("If not present, try uninstalling your ArcGIS software and reinstalling it.");
              System.err.println("Exiting execution of this sample...");
              System.exit(0);
          }
    
      //Load the ESRI.ServerStyle file into the SymbologyControl
      symbologyControl.loadStyleFile(esriStylePath);
      
      // Set the style class
      symbologyControl.setStyleClass(esriSymbologyStyleClass.esriStyleClassTextSymbols);
    } catch (Exception e) {e.printStackTrace();}
  }

  /*
   * Initialize jContentPanel
   */
  private JPanel getJContentPanel() {
    if (jMainPanel == null) {
      jMainPanel = new JPanel();
      jButtonPanel = new JPanel();
      jButtonPanel.setLayout(new FlowLayout());
      jMainPanel.setLayout(new BoxLayout(jMainPanel,BoxLayout.Y_AXIS));
      jButtonPanel.add(getJButtonOK(), null);
      jButtonPanel.add(getJButtonCancel(), null);
      jMainPanel.add(getSymbologyControl());
      jMainPanel.add(jButtonPanel);
    }
    return jMainPanel;
  }

  /*
   * Initialize symbologyControl
   */
  private SymbologyControl getSymbologyControl() {
    if (symbologyControl == null) {
      symbologyControl = new SymbologyControl();
      symbologyControl.setBounds(new java.awt.Rectangle(4, 3, 302,265));
    }
    return symbologyControl;
  }

  /*
   * Initialize button btnOK
   */
  private JButton getJButtonOK() {
    if (jBtnOK == null) {
      jBtnOK = new JButton();
      jBtnOK.setText("OK");
      jBtnOK.setEnabled(false);
      jBtnOK.addActionListener(this);
    }
    return jBtnOK;
  }

  /*
   * Initialize button btnCancel
   */
  private JButton getJButtonCancel() {
    if (jBtnCancel == null) {
      jBtnCancel = new JButton();
      jBtnCancel.setText("Cancel");
      jBtnCancel.addActionListener(this);
    }
    return jBtnCancel;
  }

  /*
   * This method is called when button OK/Cancel is clicked
   */
  public void actionPerformed(ActionEvent e) {
    if (e.getSource() == jBtnOK) {
      if(styleGalleryItem == null) return;
      this.dispose();
    }
    if (e.getSource() == jBtnCancel) {
      styleGalleryItem = null;
      this.dispose();
    }
  }

  /*
   * Add necessary listener to symbologycontrol to handle the ItemSelecetd event
   */
  @SuppressWarnings("serial")
  public void addListener() {
    try {
      symbologyControl.addISymbologyControlEventsListener(new ISymbologyControlEventsAdapter() {
        @SuppressWarnings("deprecation")
        public void onItemSelected(ISymbologyControlEventsOnItemSelectedEvent theEvent) {
          jBtnOK.setEnabled(true);
          try {
            // get the selected stylegalleryitem
            styleGalleryItem = new IStyleGalleryItemProxy(theEvent.getStyleGalleryItem());
          } catch (Exception e) {
            e.printStackTrace();
          }
        }
      });
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  /*
   * Get the StyleGalleryItem
   */
    public IStyleGalleryItem getSelectedStyleItem()
    {
      return styleGalleryItem;
    }
}