Editing in a custom application
arcgissamples\editing\CustomEditingApplication.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.
* 
*/
/* 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 at <your ArcGIS install location>/DeveloperKit10.0/userestrictions.txt.
* 
*/
package arcgissamples.editing;

import java.awt.BorderLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

import com.esri.arcgis.beans.map.MapBean;
import com.esri.arcgis.carto.FeatureLayer;
import com.esri.arcgis.carto.IFeatureLayer;
import com.esri.arcgis.carto.IGeoFeatureLayer;
import com.esri.arcgis.carto.ILayer;
import com.esri.arcgis.carto.SimpleRenderer;
import com.esri.arcgis.controls.CommandPool;
import com.esri.arcgis.controls.EngineEditor;
import com.esri.arcgis.datasourcesfile.ShapefileWorkspaceFactory;
import com.esri.arcgis.display.IRgbColor;
import com.esri.arcgis.display.RgbColor;
import com.esri.arcgis.display.SimpleFillSymbol;
import com.esri.arcgis.geodatabase.IFeatureClass;
import com.esri.arcgis.geodatabase.IFeatureWorkspace;
import com.esri.arcgis.geodatabase.IWorkspace;
import com.esri.arcgis.geodatabase.IWorkspaceFactory;
import com.esri.arcgis.system.AoInitialize;
import com.esri.arcgis.system.EngineInitializer;
import com.esri.arcgis.system.esriLicenseProductCode;
import com.esri.arcgis.system.esriLicenseStatus;
import com.esri.arcgis.systemUI.ControlsOperationStack;

public class CustomEditingApplication extends JFrame {
  private static final long serialVersionUID = 1L;
  private MapBean map;

  public static void main(String[] args) throws Exception {
    // initialize the interop
    initializeInterop();
    // initialize to a license level
    initializeArcGISLicenses();
    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    // start the sample
    final CustomEditingApplication thisClass = new CustomEditingApplication();
    SwingUtilities.invokeLater(new Runnable() {
      public void run() {
        thisClass.initializeUI();
        thisClass.setVisible(true);
      }
    });

  }


  public void initializeUI() {
    try {
      this.setTitle("Java Editing Task - ReshapePolyline");
      this.add(getMap(), BorderLayout.CENTER);

      //Create the custom editing panel
      EditingPanel editingPanel = new EditingPanel();
      //Instantiate the action listener and register it with the custom editing panel
      EditingPanelActionListener listener = new EditingPanelActionListener(editingPanel,getMap());
      editingPanel.registerActionListener(listener);
      //Buddy up the toolbar controls with the map
      editingPanel.getToolbarEditing().setBuddyControl(getMap());
      editingPanel.getToolbarUndoRedo().setBuddyControl(getMap());

      //Create the panel which contains the TOC and the Navigation toolbar
      TOCToolbarPanel toctoolbarPanel = new TOCToolbarPanel();
      //Buddy up the TOC and toolbar controls with the map
      toctoolbarPanel.getToc().setBuddyControl(getMap());
      toctoolbarPanel.getToolbarNavigation().setBuddyControl(getMap());

      //Create and share the command pool
      CommandPool pool = new CommandPool();
      toctoolbarPanel.getToolbarNavigation().setCommandPoolByRef(pool);
      editingPanel.getToolbarEditing().setCommandPoolByRef(pool);
      editingPanel.getToolbarUndoRedo().setCommandPoolByRef(pool);

      //Create and share the operation stack
      ControlsOperationStack stack = new ControlsOperationStack();
      toctoolbarPanel.getToolbarNavigation().setOperationStackByRef(stack);
      editingPanel.getToolbarEditing().setOperationStackByRef(stack);
      editingPanel.getToolbarUndoRedo().setOperationStackByRef(stack);

      //Add the custom panels to another container in the correct positions
      JPanel westPanel = new JPanel();
      westPanel.setLayout(new BorderLayout());
      westPanel.add(toctoolbarPanel, BorderLayout.CENTER);
      westPanel.add(editingPanel,BorderLayout.PAGE_END);
      westPanel.setSize(400,100);

      //Add the container to the application
      this.add(westPanel, BorderLayout.WEST);


      this.setSize(800, 500);
      this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      this.addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
          try {
            // Forcibly end the editing session. Discard the edits
            new EngineEditor().stopEditing(false);
            new AoInitialize().shutdown();
          } catch (Exception swallow) {
            // dont care
          }finally{
            System.exit(0);
          }
        }
      });

    } catch (Exception e) {
      e.printStackTrace();
    }

  }

  public MapBean getMap() throws Exception {
    if (map == null) {
      map = new MapBean();
      // Add layers to the map to perform some edits on
      ILayer lakesLayer = getLayer("us_lakes");

      //create a SimplerRenderer
      IRgbColor color = new RgbColor();
      color.setRed(190);
      color.setGreen(232);
      color.setBlue(255);
      SimpleFillSymbol sym = new SimpleFillSymbol();
      sym.setColor(color);
      SimpleRenderer renderer = new SimpleRenderer();
      renderer.setSymbolByRef(sym);

      ((IGeoFeatureLayer)lakesLayer).setRendererByRef(renderer);
      map.addLayer(lakesLayer,0);
      map.addLayer(getLayer("ushigh"), 0);
    }
    return map;
  }

  private ILayer getLayer(String fcName)throws Exception {
    //Get DEVKITHOME Home
    final String devKitHome = System.getenv("AGSDEVKITJAVA");
    
    if (devKitHome == null) {
      System.out.println("The developer kit environment variable is not set, exiting application");
      System.exit(-1);
    }
    //Get the ushigh.shp shapefile from sample data and return it as ILayer
    IWorkspaceFactory factory = new ShapefileWorkspaceFactory();
    IWorkspace workspace = factory.openFromFile(devKitHome + "java" + File.separator + 
                                 "samples" + File.separator + 
                                 "data" + File.separator + 
                                 "usa", 0);
    IFeatureWorkspace featureWorkspace = (IFeatureWorkspace) workspace;
    IFeatureClass featureClass = featureWorkspace.openFeatureClass(fcName);
    ILayer layer = new FeatureLayer();
    layer.setName(fcName);
    ((IFeatureLayer) layer).setFeatureClassByRef(featureClass);
    return layer;

  }


  static void initializeInterop() {
    // Visual beans mode required for multi-threaded applciations like Swing.
    EngineInitializer.initializeVisualBeans();
  }

  static void initializeArcGISLicenses() {
    try {
      AoInitialize ao= new AoInitialize();
      if (ao.isProductCodeAvailable(com.esri.arcgis.system.esriLicenseProductCode.esriLicenseProductCodeEngine) == com.esri.arcgis.system.esriLicenseStatus.esriLicenseAvailable)
        ao.initialize(com.esri.arcgis.system.esriLicenseProductCode.esriLicenseProductCodeEngine);
      else if (ao.isProductCodeAvailable(com.esri.arcgis.system.esriLicenseProductCode.esriLicenseProductCodeArcView) == com.esri.arcgis.system.esriLicenseStatus.esriLicenseAvailable)
        ao.initialize(com.esri.arcgis.system.esriLicenseProductCode.esriLicenseProductCodeArcView);
      else if (ao.isProductCodeAvailable(com.esri.arcgis.system.esriLicenseProductCode.esriLicenseProductCodeArcEditor) == com.esri.arcgis.system.esriLicenseStatus.esriLicenseAvailable)
        ao.initialize(com.esri.arcgis.system.esriLicenseProductCode.esriLicenseProductCodeArcEditor);
      else if (ao.isProductCodeAvailable(com.esri.arcgis.system.esriLicenseProductCode.esriLicenseProductCodeArcInfo) == com.esri.arcgis.system.esriLicenseStatus.esriLicenseAvailable)
        ao.initialize(com.esri.arcgis.system.esriLicenseProductCode.esriLicenseProductCodeArcInfo);
      else
      {
        System.err.println("Could not initialize an Engine, ArcView, ArcEditor, or ArcInfo license. Exiting application.");
        System.exit(-1);
      }  
      ao.checkOutExtension(com.esri.arcgis.system.esriLicenseExtensionCode.esriLicenseExtensionCodeSpatialAnalyst);
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}