arcgissamples\cartography\FirstCustomRendererMain.java—ArcObjects 10.4 Help for Java | ArcGIS for Desktop
First custom renderer
arcgissamples\cartography\FirstCustomRendererMain.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.Button;
import java.awt.Dimension;
import java.awt.FileDialog;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.IOException;

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

import com.esri.arcgis.beans.map.MapBean;
import com.esri.arcgis.carto.FeatureLayer;
import com.esri.arcgis.carto.IFeatureRenderer;
import com.esri.arcgis.carto.esriViewDrawPhase;
import com.esri.arcgis.interop.AutomationException;
import com.esri.arcgis.system.AoInitialize;
import com.esri.arcgis.system.EngineContext;
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.
 * The application will work with point line or polygon shapefiles.
 * Load shapefile and click "Set Custom Renderer" button.
 */
public class FirstCustomRendererMain extends JFrame implements ActionListener {
  private static final long serialVersionUID = 1L;
  MapBean mapBean = new MapBean();
  Button buttonOpenFile = new Button();
  Button buttonSetCustomRenderer = new Button();
  FeatureLayer featureLayer = null;
  static AoInitialize aoInit;

  /**
   * Method to start the program execution.
   */
  public static void main(String s[]) {
    System.out.println("Starting \"First Custom Renderer\" - An ArcObjects Java SDK Developer Sample");
    //Get DEVKITHOME Home
    String devKitHome = System.getenv("AGSDEVKITJAVA");
    
    if (!(new File(devKitHome).exists())) {
      System.out.println(devKitHome + " does not exist.\nExiting...");
      System.exit(-1);
    }
    
    //
    // Change the following lines if you want to start with different data
    //
    String workspacePath = devKitHome + File.separator + "java" + File.separator + "samples" 
                      + File.separator + "data" + File.separator + "usa";
    String featureClassName = "states";

    File shapefileFile = new File(workspacePath, featureClassName + ".shp");
    if (!shapefileFile.exists()) {
      System.out.println("Shapefile does not exist: " + shapefileFile.getAbsolutePath());
      System.exit(0);
    }
    
    try {
      EngineInitializer.initializeVisualBeans();
      UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
      initializeArcGISLicenses();
      FirstCustomRendererMain thisApp = new FirstCustomRendererMain();
      thisApp.initUI(workspacePath, featureClassName);
    } catch (Exception ex) {
      System.out.println("Sample failed: " + ex.getMessage());
      ex.printStackTrace();
    }
  }

  static void initializeArcGISLicenses() {
    try {
      aoInit = new AoInitialize();
      
      if (aoInit.isProductCodeAvailable(esriLicenseProductCode.esriLicenseProductCodeEngine) 
          == esriLicenseStatus.esriLicenseAvailable)
        aoInit.initialize(esriLicenseProductCode.esriLicenseProductCodeEngine);
    } catch (Exception e) {e.printStackTrace();}
  }

  /*
   * Lays out the User Interface, and sets up event listeners
   */
  private void initUI(String path, String name) throws AutomationException, IOException {
    this.setTitle("FirstCustomRenderer Java Sample Application");
    this.setSize(new Dimension(600, 500));
    this.getContentPane().setLayout(new BorderLayout());

    Panel loadPanel = new Panel(new FlowLayout(FlowLayout.LEADING));
    Label labelComment = new Label();
    labelComment.setText("Open java/samples/data/usa wind.shp or ushigh.shp or states.shp (default) files.");
    this.buttonOpenFile.setLabel("Open File ...");
    this.buttonSetCustomRenderer.setLabel("Set Custom Renderer");
    loadPanel.add(labelComment);
    loadPanel.add(this.buttonOpenFile);

    Panel customPanel = new Panel(new FlowLayout(FlowLayout.LEADING));
    customPanel.add(this.buttonSetCustomRenderer);

    Panel toolPanel = new Panel(new GridLayout(2,1));
    toolPanel.add(loadPanel);
    toolPanel.add(customPanel);

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

    this.getContentPane().add(this.mapBean, BorderLayout.CENTER);
    this.getContentPane().add(nestPanel, BorderLayout.SOUTH);

    this.mapBean.clearLayers();
    this.mapBean.addShapeFile(path, name);
    this.featureLayer = (FeatureLayer) this.mapBean.getLayer(0);

    // update map
    this.setVisible(true);
    this.mapBean.refresh(esriViewDrawPhase.esriViewGeography, null, null);

    this.buttonOpenFile.addActionListener(this);
    this.buttonSetCustomRenderer.addActionListener(this);
    this.addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        try {
          aoInit.shutdown();
        } catch(IOException ex) {}
        
        System.out.println("Done.");
        System.exit(0);
      }
    });
  }

  /**
   * @see ActionListener#actionPerformed
   */
  public void actionPerformed(ActionEvent event) {
    Object evt = event.getSource();
    if (evt == this.buttonOpenFile) {
      try {
        FileDialog fileDialog = new FileDialog(this, "Open File",FileDialog.LOAD);
        fileDialog.setVisible(true);
        String path = fileDialog.getDirectory();
        String name = fileDialog.getFile();
        this.mapBean.clearLayers();
        this.mapBean.addShapeFile(path, name);
        this.featureLayer = (FeatureLayer) this.mapBean.getLayer(0);
        this.mapBean.refresh(esriViewDrawPhase.esriViewGeography, null, null);
      } catch (java.io.IOException ex) {
        System.out.println("Can't load file, exception:" + ex);
      }
    }
    if (evt == this.buttonSetCustomRenderer) {
      try {
        IFeatureRenderer firstCustomRenderer = (IFeatureRenderer) EngineContext.createObject(FirstCustomRenderer.class);
        this.featureLayer.setRendererByRef(firstCustomRenderer);
        // redraw map
        this.mapBean.refresh(esriViewDrawPhase.esriViewBackground, null, null);
      } catch (java.io.IOException ex) {
        System.out.println("Exception when setting FirstCustomRenderer:" + ex);
      }
    }
  }
}