arcgissamples\cartography\ValenceRendererMain.java—ArcObjects 10.4 Help for Java | ArcGIS for Desktop
Valence renderer
arcgissamples\cartography\ValenceRendererMain.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.FlowLayout;
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.TOC.TOCBean;
import com.esri.arcgis.beans.map.MapBean;
import com.esri.arcgis.beans.toolbar.ToolbarBean;
import com.esri.arcgis.carto.FeatureLayer;
import com.esri.arcgis.carto.IFeatureRenderer;
import com.esri.arcgis.carto.IGeoFeatureLayer;
import com.esri.arcgis.carto.esriViewDrawPhase;
import com.esri.arcgis.datasourcesGDB.FileGDBWorkspaceFactory;
import com.esri.arcgis.geodatabase.FeatureDataset;
import com.esri.arcgis.geodatabase.IDatasetName;
import com.esri.arcgis.geodatabase.IEnumDatasetName;
import com.esri.arcgis.geodatabase.IFeatureClass;
import com.esri.arcgis.geodatabase.IGeometricNetwork;
import com.esri.arcgis.geodatabase.Workspace;
import com.esri.arcgis.geodatabase.esriDatasetType;
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. You should load the
 * true mdb database with predefined "Networkset" dataset. The valence renderer
 * allows the user to display simple junctions based on the number of connected
 * edges. The code also demonstrates how to limit renderers to a specific set of
 * feature classes (in this case, simple junctions). This sample also creates a
 * working entry for the rendered layer that appears in the Table of Contents
 * and will display in a legend.
 */
public class ValenceRendererMain extends JFrame implements ActionListener {

  /**
   * 
   */
  private static final long serialVersionUID = 1L;
  static AoInitialize aoInit;
  MapBean mapBean = new MapBean();
  TOCBean tocBean = new TOCBean();
  Button buttonSetValenceRenderer = new Button();
  FeatureLayer featureLayer;
  ToolbarBean toolbarBean = null;

  /**
   * Constructor.
   */
  public ValenceRendererMain() {
    try {
      initUI();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  /**
   * Method to start the program execution.
   * @param s String[]
   */
  public static void main(String s[]) {
    try {
      EngineInitializer.initializeVisualBeans();
      UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
      initializeArcGISLicenses();
      new ValenceRendererMain();
    } catch (Exception ex) {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() throws Exception {
    this.setTitle("ValenceRenderer - Engine Application");
    this.setSize(new Dimension(700, 600));
    this.getContentPane().setLayout(new BorderLayout());
    this.buttonSetValenceRenderer.setLabel("Apply Renderer");
    Panel buttonPanel = new Panel(new FlowLayout(FlowLayout.CENTER));
    buttonPanel.add(this.buttonSetValenceRenderer);
    this.getContentPane().add(this.mapBean, BorderLayout.CENTER);
    this.getContentPane().add(getToolbarBean(), BorderLayout.NORTH);
    this.getContentPane().add(this.tocBean, BorderLayout.WEST);
    this.getContentPane().add(buttonPanel, BorderLayout.SOUTH);

    //Get DEVKITHOME Home
    String devKitHome = System.getenv("AGSDEVKITJAVA");
    
    if (!(new File(devKitHome).exists())) {
      System.out.println(devKitHome + " does not exist.\nExiting...");
      System.exit(-1);
    }
    String workspacePath = devKitHome + File.separator + "java" + File.separator + "samples" +
                      File.separator + "data" + File.separator + "usa" +
                      File.separator + "usa.gdb";
    FileGDBWorkspaceFactory factory = new FileGDBWorkspaceFactory();
    Workspace workspace = (Workspace) factory.openFromFile(workspacePath,0);
    IEnumDatasetName datasets = workspace.getDatasetNames(esriDatasetType.esriDTFeatureDataset);
    IDatasetName name = datasets.next();
    FeatureDataset featureDataset = null;
    while (name != null) {
      if (name.getName().equals("highway")) {
        featureDataset = new FeatureDataset(workspace.openFeatureDataset(name.getName()));
        break;
      }
      name = datasets.next();
    }
    if (featureDataset == null) {
      System.out.println("Couldn't find highway dataset in " + workspacePath);
      return;
    }
    IGeometricNetwork geometricNetwork = featureDataset.getGeometricNetwork(0);
    IFeatureClass featureClass = geometricNetwork.getOrphanJunctionFeatureClass();
    this.featureLayer = new FeatureLayer();
    this.featureLayer.setName(featureClass.getAliasName());
    this.featureLayer.setFeatureClassByRef(featureClass);
    this.mapBean.clearLayers();
    this.mapBean.addLayer(this.featureLayer, 0);
    this.tocBean.setBuddyControl(this.mapBean);
    this.mapBean.refresh(esriViewDrawPhase.esriViewGeography, null, null);    
    this.buttonSetValenceRenderer.addActionListener(this);
    this.setVisible(true);
    this.addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        try {
          aoInit.shutdown();
        } catch (IOException ex) {
          // exit anyway
        }
        System.exit(0);
      }
    });
  }

  private ToolbarBean getToolbarBean() throws Exception {
    if (toolbarBean == null) {
      toolbarBean = new ToolbarBean();
      toolbarBean.setItemsString("4|controls/ControlsAddDataCommand|0|-1|0|0|1;4|controls/ControlsOpenDocCommand|0|-1|0|0|1;4|controls/ControlsSaveAsDocCommand|0|-1|0|0|1;10|controls/ControlsMapFullExtentCommand|0|-1|0|0|1;10|controls/ControlsMapZoomInFixedCommand|0|-1|0|0|1;10|controls/ControlsMapZoomOutFixedCommand|0|-1|0|0|1;10|controls/ControlsMapZoomInTool|0|-1|0|0|1;10|controls/ControlsMapZoomOutTool|0|-1|0|0|1;10|controls/ControlsMapPanTool|0|-1|0|0|1;10|controls/ControlsMapZoomToLastExtentBackCommand|0|-1|0|0|1;10|controls/ControlsMapZoomToLastExtentForwardCommand|0|-1|0|0|1;3|controls/ControlsClearSelectionCommand|0|-1|0|0|1;3|controls/ControlsSelectAllCommand|0|-1|0|0|1;3|controls/ControlsSelectByGraphicsCommand|0|-1|0|0|1;3|controls/ControlsZoomToSelectedCommand|0|-1|0|0|1;3|controls/ControlsSelectFeaturesTool|0|-1|0|0|1;4|controls/ControlsMapIdentifyTool|0|-1|0|0|1");
      toolbarBean.setBuddyControl(mapBean);
    }
    return toolbarBean;
  }

  /**
   * implements interface ActionListener
   * @see ActionListener#actionPerformed
   * @param event
   */
  public void actionPerformed(ActionEvent event) {
    Object eventSource = event.getSource();
    if (eventSource == this.buttonSetValenceRenderer) {
      try {
        IFeatureRenderer rend = (IFeatureRenderer) EngineContext.createObject(ValenceRenderer.class);
        this.featureLayer = (FeatureLayer) (this.mapBean.getLayer(0));
        IGeoFeatureLayer geoFeatureLayer = this.featureLayer;
        geoFeatureLayer.setRendererByRef(rend);
        this.mapBean.refresh(esriViewDrawPhase.esriViewBackground, null,null);
        this.tocBean.update();
      } catch (java.io.IOException ex) {
        ex.printStackTrace();
      }
    }
  }
}