Events lifecycle
arcgissamples\editing\EditingApplication.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.
* 
*/
package arcgissamples.editing;

import java.awt.BorderLayout;
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.JOptionPane;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

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.IFeatureLayer;
import com.esri.arcgis.carto.ILayer;
import com.esri.arcgis.controls.ControlsEditingSketchContextMenu;
import com.esri.arcgis.controls.ControlsEditingTaskToolControl;
import com.esri.arcgis.controls.ControlsEditingToolbar;
import com.esri.arcgis.controls.ControlsMapNavigationToolbar;
import com.esri.arcgis.controls.ControlsRedoCommand;
import com.esri.arcgis.controls.ControlsUndoCommand;
import com.esri.arcgis.controls.EngineEditor;
import com.esri.arcgis.controls.IMapControlEvents2Adapter;
import com.esri.arcgis.controls.IMapControlEvents2OnMouseDownEvent;
import com.esri.arcgis.controls.ToolbarMenu;
import com.esri.arcgis.controls.esriToolbarOrientation;
import com.esri.arcgis.datasourcesfile.ShapefileWorkspaceFactory;
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.interop.AutomationException;
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;
import com.esri.arcgis.systemUI.esriCommandStyles;

public class EditingApplication extends JFrame{
  private static final long serialVersionUID = 1L;

  private MapBean map;

  private ToolbarBean editToolbar, navToolbar;

  private ToolbarMenu menu;

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

  }

  public void initializeUI() {
    try {
      this.setTitle("Manage Snap Agents");
      this.add(getMap(), BorderLayout.CENTER);
      this.add(getEditToolbar(), BorderLayout.NORTH);
      this.add(getNavToolbar(), BorderLayout.WEST);
      this.add(new EventsPane(), BorderLayout.EAST);
      this.setSize(800, 700);
      this.addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
          try {
            EngineEditor editor = new EngineEditor();
            //Save or discard outstanding edits
            if(editor.hasEdits()){
              int result = JOptionPane.showConfirmDialog(null,
                  "Do you want to save your edits?","Edits",
                  JOptionPane.YES_NO_OPTION);
              editor.stopEditing((result==JOptionPane.YES_OPTION?true:false));
            }
            //Always cleanly shutdown the interop
            new AoInitialize().shutdown();
          } catch (Exception swallow) {
            // dont care
          } 
        }
      });

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

  }

  public MapBean getMap() throws Exception {
    if (map == null) {
      map = new MapBean();
      // Add a layer to the map to perform some edits on
      map.addLayer(getHighwayLayer(), 0);
      // Add a listener to listen to the mouse down event
      map.addIMapControlEvents2Listener(new IMapControlEvents2Adapter() {
        private static final long serialVersionUID = 1L;

        public void onMouseDown(IMapControlEvents2OnMouseDownEvent event)
            throws IOException, AutomationException {
          // display the Editing menu on right-click
          if (event.getButton() == 2) {
            try {
              getMenu().popupMenu(event.getX(), event.getY(),
                  getMap().getHWnd());
            } catch (Exception e) {
              e.printStackTrace();
            }
          }
        }

      });
    }
    return map;
  }

  private ILayer getHighwayLayer() 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("ushigh");
    ILayer layer = new FeatureLayer();
    layer.setName("Highways");
    ((IFeatureLayer) layer).setFeatureClassByRef(featureClass);
    return layer;

  }

  public ToolbarMenu getMenu() throws Exception {
    if (menu == null) {
      menu = new ToolbarMenu();
      //Set the hook for the context menu, otherwise it will appear diabled
      menu.setHook(getEditToolbar());
      menu.addItem(new ControlsEditingSketchContextMenu(), 0, 0, false,
          esriCommandStyles.esriCommandStyleIconOnly);
      menu.setCommandPoolByRef(getEditToolbar().getCommandPool());
    }
    return menu;
  }

  public ToolbarBean getNavToolbar() throws Exception {
    if (navToolbar == null) {
      navToolbar = new ToolbarBean();
      navToolbar.setBuddyControl(getMap());
      navToolbar.addItem(new ControlsMapNavigationToolbar(), 0, 0, false,
          0, esriCommandStyles.esriCommandStyleIconOnly);
      navToolbar
          .setOrientation(esriToolbarOrientation.esriToolbarOrientationVertical);
      navToolbar.setCommandPoolByRef(getEditToolbar().getCommandPool());
      navToolbar.setSize(100, 25);
    }
    return navToolbar;
  }

  public ToolbarBean getEditToolbar() throws Exception {
    if (editToolbar == null) {
      editToolbar = new ToolbarBean();
      editToolbar.setBuddyControl(getMap());
      editToolbar.setOperationStackByRef(new ControlsOperationStack());
      editToolbar.addItem(new ControlsUndoCommand(), 0, 0, false, 0,
          esriCommandStyles.esriCommandStyleIconOnly);
      editToolbar.addItem(new ControlsRedoCommand(), 0, 0, false, 0,
          esriCommandStyles.esriCommandStyleIconOnly);
      editToolbar.addItem(new ControlsEditingTaskToolControl(), 0, 0,
          false, 0, esriCommandStyles.esriCommandStyleIconOnly);
      editToolbar.addItem(new ControlsEditingToolbar(), 0, 0, false, 0,
          esriCommandStyles.esriCommandStyleIconOnly);
    }
    return editToolbar;
  }

  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(esriLicenseProductCode.esriLicenseProductCodeEngine) 
          == esriLicenseStatus.esriLicenseAvailable)
        ao.initialize(esriLicenseProductCode.esriLicenseProductCodeEngine);
      else if (ao.isProductCodeAvailable(esriLicenseProductCode.esriLicenseProductCodeArcEditor) 
          == esriLicenseStatus.esriLicenseAvailable)
        ao.initialize(esriLicenseProductCode.esriLicenseProductCodeArcEditor);
      else {
        System.err
            .println("Could not initialize an Engine or ArcEditor license. Exiting application.");
        System.exit(-1);
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}