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. * */ /* 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 java.io.IOException; import javax.swing.JFrame; import javax.swing.SwingUtilities; 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.ControlsEditingEditTool; import com.esri.arcgis.controls.ControlsEditingEditorMenu; import com.esri.arcgis.controls.ControlsEditingSketchContextMenu; import com.esri.arcgis.controls.ControlsEditingSketchTool; import com.esri.arcgis.controls.ControlsEditingTargetToolControl; import com.esri.arcgis.controls.ControlsEditingTaskToolControl; import com.esri.arcgis.controls.ControlsMapIdentifyTool; 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.IToolbarMenu; 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.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(); // start the sample final EditingApplication thisClass = new EditingApplication(); SwingUtilities.invokeLater(new Runnable() { public void run() { thisClass.initializeUI(); thisClass.setVisible(true); } }); } public void initializeUI() { try { this.setTitle("Java Vertex Editing Commands"); this.add(getMap(), BorderLayout.CENTER); this.add(getEditToolbar(), BorderLayout.NORTH); this.add(getNavToolbar(), BorderLayout.EAST); this.setSize(800, 700); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.addWindowListener(new WindowAdapter() { public void windowClosed(WindowEvent e) { try { // Forcibly end the editing session. Discard the edits new EngineEditor().stopEditing(false); } 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 a layer to the map to perform some edits on map.addLayer(getSomeLayer(), 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 getSomeLayer() 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(); menu.setHook(getEditToolbar()); menu.addItem(new ControlsEditingSketchContextMenu(), 0, 0, false, esriCommandStyles.esriCommandStyleIconOnly); } 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.addItem(new ControlsMapIdentifyTool(), 0, 1, false, 0, esriCommandStyles.esriCommandStyleIconOnly); navToolbar.setSize(100, 25); } return navToolbar; } public ToolbarBean getEditToolbar() throws Exception { if (editToolbar == null) { editToolbar = new ToolbarBean(); editToolbar.setBuddyControl(getMap()); editToolbar.addItem(new ControlsEditingEditorMenu(), 0, 0, false, 0, esriCommandStyles.esriCommandStyleIconOnly); editToolbar.addItem(new ControlsEditingEditTool(), 0, 1, false, 0, esriCommandStyles.esriCommandStyleIconOnly); editToolbar.addItem(getVertexCommadsMenu(), 0, 2, false, 0, esriCommandStyles.esriCommandStyleTextOnly); editToolbar.addItem(new ControlsUndoCommand(), 0, 3, false, 0, esriCommandStyles.esriCommandStyleIconOnly); editToolbar.addItem(new ControlsRedoCommand(), 0, 4, false, 0, esriCommandStyles.esriCommandStyleIconOnly); editToolbar.addItem(new ControlsEditingTaskToolControl(), 0, 5, false, 0, esriCommandStyles.esriCommandStyleIconOnly); editToolbar.addItem(new ControlsEditingTargetToolControl(), 0, 6, false, 0, esriCommandStyles.esriCommandStyleIconOnly); editToolbar.addItem(new ControlsEditingSketchTool(), 0, 7, false, 0, esriCommandStyles.esriCommandStyleIconOnly); } return editToolbar; } private IToolbarMenu getVertexCommadsMenu() throws Exception { IToolbarMenu menu = new ToolbarMenu(); menu.setCaption("Vertex Tools"); menu.addItem(new CustomVertexTools(), 1, 0, false, esriCommandStyles.esriCommandStyleIconAndText); menu.addItem(new CustomVertexTools(), 2, 0, false, esriCommandStyles.esriCommandStyleIconAndText); menu.addItem(new OutOfBoxVertexTools(), 1, 0, false, esriCommandStyles.esriCommandStyleIconAndText); menu.addItem(new OutOfBoxVertexTools(), 2, 0, false, esriCommandStyles.esriCommandStyleIconAndText); return menu; } 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(); } } }