arcgissamples\geodatabase\PluginDatasourceMain.java—ArcObjects 10.4 Help for Java | ArcGIS for Desktop
Point plug-in data source
arcgissamples\geodatabase\PluginDatasourceMain.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.geodatabase;

import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Dimension;
import java.awt.FlowLayout;
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.JFileChooser;
import javax.swing.JFrame;
import javax.swing.UIManager;

import arcgissamples.geodatabase.simplepointdatasource.SimplePointWorkspaceFactoryHelper;

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.IFeatureLayer;
import com.esri.arcgis.carto.ILayer;
import com.esri.arcgis.carto.esriViewDrawPhase;
import com.esri.arcgis.geodatabase.IFeatureClass;
import com.esri.arcgis.geodatabase.IFeatureWorkspace;
import com.esri.arcgis.geodatabase.IWorkspaceFactory;
import com.esri.arcgis.system.AoInitialize;
import com.esri.arcgis.system.EngineContext;
import com.esri.arcgis.system.EngineInitializer;
import com.esri.arcgis.system.esriLicenseProductCode;

public class PluginDatasourceMain extends JFrame implements ActionListener {
  
  private static final long serialVersionUID = 1L;
  MapBean mapBean = new MapBean();
  Button buttonAddpluginData = new Button();
  FeatureLayer featureLayer = null;
  private ToolbarBean toolbarBean = null;
  TOCBean tocBean = new TOCBean();

  /**
   * Method to start the program execution.
   * @param s String[]
   */
  public static void main(String s[]) {
    //Get DEVKITHOME Home
    String devKitHome = System.getenv("AGSDEVKITJAVA");
    
    if (!(new File(devKitHome).exists())) {
      System.out.println(devKitHome + " does not exist.\nExiting...");
      System.exit(-1);
    }
    try {
      EngineInitializer.initializeVisualBeans();
      UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
      
      new AoInitialize().initialize(esriLicenseProductCode.esriLicenseProductCodeEngine);
      
      PluginDatasourceMain thisApp = new PluginDatasourceMain();
      thisApp.initUI();
    } catch (Exception ex) {
      ex.printStackTrace();
    }
  }

  
  
  /*
   * Lays out the User Interface, and sets up event listeners
   */
  private void initUI() throws Exception {
    this.setTitle("Plug-in Datasource - Engine Application");
    this.setSize(new Dimension(700, 600));
    this.getContentPane().setLayout(new BorderLayout());
    Panel toolPanel = new Panel(new FlowLayout(FlowLayout.LEADING));
    Label labelComment = new Label();
    labelComment.setText("Adds the custom data to the map");
    this.buttonAddpluginData.setLabel("Add Custom Data");
    toolPanel.add(this.buttonAddpluginData);
    toolPanel.add(labelComment);

    try {
      tocBean.setBuddyControl(mapBean);
    } catch (IOException e1) {
      e1.printStackTrace();
    }
    this.getContentPane().add(getToolbarBean(), BorderLayout.NORTH);
    this.getContentPane().add(this.tocBean, BorderLayout.WEST);
    this.getContentPane().add(this.mapBean, BorderLayout.CENTER);
    this.getContentPane().add(toolPanel, BorderLayout.SOUTH);

    this.buttonAddpluginData.addActionListener(this);
    this.setVisible(true);
    this.addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        try {
          new AoInitialize().shutdown();
        } catch(IOException ex) {
          // exit anyway
        }
        System.exit(0);
      }
    });
  }

  private ToolbarBean getToolbarBean() throws Exception {
    if (toolbarBean == null) {
      toolbarBean = new ToolbarBean();
      toolbarBean.setItemsString("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;
  }


  /**
   * @see ActionListener#actionPerformed
   * @param event
   */
  public void actionPerformed(ActionEvent event) {
    Object evt = event.getSource();
    if (evt == this.buttonAddpluginData) {
      try {
        JFileChooser fileChooser = new JFileChooser();
        int returnVal = fileChooser.showOpenDialog(this);;
        if (returnVal == JFileChooser.APPROVE_OPTION) {
                File file = fileChooser.getSelectedFile();
                IWorkspaceFactory workspaceFactory = (IWorkspaceFactory) EngineContext.createObject(SimplePointWorkspaceFactoryHelper.class);
          IFeatureWorkspace featureWorkspace = (IFeatureWorkspace)workspaceFactory.openFromFile(file.getParent(), 0);
              IFeatureClass featureClass = featureWorkspace.openFeatureClass(file.getName().substring(0,(file.getName().length()-4)));
              //create a new feature layer and add it to the map
              IFeatureLayer featureLayer = new FeatureLayer();
              featureLayer.setName(featureClass.getAliasName());
              featureLayer.setFeatureClassByRef(featureClass);
              this.mapBean.addLayer((ILayer)featureLayer, 0);
              this.mapBean.refresh(esriViewDrawPhase.esriViewGeography, null, null);
        }
      } catch (Exception ex) {
        System.out.println("Couldn't load data.  Try another shapefile.");
      }
    }
  }
}