Skip To Content ArcGIS for Developers Sign In Dashboard

Geodatabase version

Download Sample Viewer

Description

This sample shows how to switch between geodatabase (gdb) versions of an ArcGISFeatureLayer. The application shows two maps side-by-side. You can set the geodatabase version of each using the drop down menus listing versions. This allows you to compare the data from different versions.

Code snippet


    // create ArcGISFeatureLayer from URL of a versioned feature service layer
    final ArcGISFeatureLayer featureLayer = new ArcGISFeatureLayer(VERSIONED_LAYER);
    // Set the geodatabase version using a version String
    featureLayer.setGdbVersion(gdbVersion);
  
  

Sample Code

/* Copyright 2014 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 com.esri.client.samples.featurelayers;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.List;

import javax.swing.BoxLayout;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.border.LineBorder;

import com.esri.toolkit.overlays.InfoPopupOverlay;
import com.esri.core.geometry.Envelope;
import com.esri.core.map.Graphic;
import com.esri.core.tasks.ags.geoprocessing.GPParameter;
import com.esri.core.tasks.ags.geoprocessing.GPRecordSet;
import com.esri.core.tasks.ags.geoprocessing.Geoprocessor;
import com.esri.map.ArcGISFeatureLayer;
import com.esri.map.ArcGISTiledMapServiceLayer;
import com.esri.map.JMap;
import com.esri.map.LayerInitializeCompleteEvent;
import com.esri.map.LayerInitializeCompleteListener;
import com.esri.map.LayerList;
import com.esri.map.QueryMode;

/**
 * This sample shows how to switch between geodatabase (gdb) versions of an 
 * <code>ArcGISFeatureLayer</code>. The application shows two maps side-by-side. 
 * You can set the geodatabase version of each using the drop down menus listing 
 * versions. This allows you to compare the data from different versions.
 */
public class GdbVersionApp {

  // resources
  final static String BASE_LAYER = 
      "http://services.arcgisonline.com/ArcGIS/rest/services/World_Topo_Map/MapServer";
  final static String VERSIONED_LAYER = 
      "http://sampleserver6.arcgisonline.com/arcgis/rest/services/Water_Network/FeatureServer/23";
  final static String GET_GEODATABASE_VERSIONS_SERVICE = 
      "http://sampleserver6.arcgisonline.com/arcgis/rest/services/GDBVersions/GPServer/ListVersions";
  final List<String> GEODATABASE_VERSIONS = new ArrayList<>();

  // application components
  private ArcGISFeatureLayer featureLayer1;
  private ArcGISFeatureLayer featureLayer2;
  private JMap leftMap;
  private JMap rightMap;

  // ------------------------------------------------------------------------
  // Constructor
  // ------------------------------------------------------------------------
  public GdbVersionApp() {
  }

  // ------------------------------------------------------------------------
  // Core functionality
  // ------------------------------------------------------------------------
  /**
   * Creates an ArcGISFeatureLayer in OnDemand mode.
   */
  private ArcGISFeatureLayer createFeatureLayer(String gdbVersion) {
    final ArcGISFeatureLayer featureLayer = new ArcGISFeatureLayer(VERSIONED_LAYER);
    featureLayer.setGdbVersion(gdbVersion);

    // OnDemand mode will activate only those features in the current extent.
    featureLayer.setOperationMode(QueryMode.ON_DEMAND);

    featureLayer.addLayerInitializeCompleteListener(new LayerInitializeCompleteListener() {
      @Override
      public void layerInitializeComplete(LayerInitializeCompleteEvent e) {
        System.out.println("Layer isDataVersioned? " + featureLayer.isDataVersioned());
        System.out.println("Geodatabase Version " + featureLayer.getGdbVersion());
      }
    });

    return featureLayer;
  }

  // ------------------------------------------------------------------------
  // Static methods
  // ------------------------------------------------------------------------
  /**
   * Starting point of this application.
   * @param args arguments to this application.
   */
  public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
      @Override
      public void run() {
        try {
          // instance of this application
          GdbVersionApp app = new GdbVersionApp();

          // create the UI, including the map, for the application.
          JFrame appWindow = app.createWindow();
          appWindow.add(app.createUI());
          appWindow.setVisible(true);
        } catch (Exception e) {
          // on any error, display the stack trace.
          e.printStackTrace();
        }
      }
    });
  }

  // ------------------------------------------------------------------------
  // Public methods
  // ------------------------------------------------------------------------
  /**
   * Creates and displays the UI, including the map, for this application.
   */
  public JComponent createUI() throws Exception {
    // fetch the gdb versions first
    getGDBVersions();

    // application content
    JPanel contentPane = new JPanel();
    contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.X_AXIS));
    contentPane.setVisible(true);

    // left map
    featureLayer1 = createFeatureLayer("sde.DEFAULT");
    JPanel leftHeader = createHeader(featureLayer1);
    leftMap = createMap(featureLayer1);
    JPanel leftContent = createContent(leftMap, leftHeader);

    // right map
    featureLayer2 = createFeatureLayer("gdb.Test");
    JPanel rightHeader = createHeader(featureLayer2);
    rightMap = createMap(featureLayer2);
    JPanel rightContent = createContent(rightMap, rightHeader);

    contentPane.add(leftContent);
    contentPane.add(rightContent);

    return contentPane;
  }

  // ------------------------------------------------------------------------
  // Private methods
  // ------------------------------------------------------------------------
  private void getGDBVersions() throws Exception {
    // create a Geoprocessor that points to the remote geoprocessing service.
    Geoprocessor geoprocessor = new Geoprocessor(GET_GEODATABASE_VERSIONS_SERVICE);
    GPParameter[] result = geoprocessor.execute(new ArrayList<GPParameter>());
    for (GPParameter outputParameter : result) {
      if (outputParameter instanceof GPRecordSet) {
        GPRecordSet resultSet = (GPRecordSet) outputParameter;
        for (Graphic graphic : resultSet.getGraphics()) {
          GEODATABASE_VERSIONS.add((String) graphic.getAttributeValue("name"));
        }
      }
    }
    GEODATABASE_VERSIONS.add("invalid");
  }

  private JPanel createHeader(final ArcGISFeatureLayer layer) {
    JPanel header = new JPanel();
    header.setVisible(true);
    // version selection
    final JLabel lblVersion = new JLabel("GDB Version: ");

    final JComboBox<String> cbxVersion = new JComboBox<>(GEODATABASE_VERSIONS.toArray(new String[0]));
    cbxVersion.setMaximumSize(new Dimension(100, 25));
    cbxVersion.setAlignmentX(Component.LEFT_ALIGNMENT);
    cbxVersion.addItemListener(new ItemListener() {

      @Override
      public void itemStateChanged(ItemEvent arg0) {
        if (arg0.getStateChange() == ItemEvent.SELECTED) {
          layer.setGdbVersion((String) cbxVersion.getSelectedItem());
        }
      }
    });
    cbxVersion.setSelectedItem(layer.getGdbVersion() == null ? GEODATABASE_VERSIONS.get(0) : layer.getGdbVersion());
    header.add(lblVersion);
    header.add(cbxVersion);

    return header;
  }

  /**
   * Creates a window.
   * @return a window.
   */
  private JFrame createWindow() {
    JFrame window = new JFrame("ArcGISFeatureLayer Geodatabase Versions");
    window.setBounds(100, 100, 1000, 700);
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    window.getContentPane().setLayout(new BorderLayout(0, 0));
    window.addWindowListener(new WindowAdapter() {
      @Override
      public void windowClosing(WindowEvent windowEvent) {
        super.windowClosing(windowEvent);
        leftMap.dispose();
        rightMap.dispose();
      }
    });
    return window;
  }

  /**
   * Creates a content pane.
   * @return a content pane.
   */
  private static JPanel createContent(JMap map, JPanel header) {
    JPanel contentPane = new JPanel();
    contentPane.setVisible(true);
    contentPane.setLayout(new BorderLayout());
    contentPane.add(header, BorderLayout.NORTH);
    contentPane.add(map, BorderLayout.CENTER);
    return contentPane;
  }

  /**
   * Creates a map.
   * @return a map.
   */
  private JMap createMap(final ArcGISFeatureLayer featureLayer) throws Exception {
    final JMap jMap = new JMap();
    jMap.setBorder(new LineBorder(Color.RED));
    
    // Base Layer
    final ArcGISTiledMapServiceLayer baseLayer = new ArcGISTiledMapServiceLayer(BASE_LAYER);
    jMap.setExtent(new Envelope(-9812724.728632046, 5125391.214189326, -9811959.528498642, 5126237.289133579));
    LayerList layers = jMap.getLayers();
    layers.add(baseLayer);

    // Feature Layer
    layers.add(featureLayer);

    // InfoPopupOverlay - a map overlay to display attributes of an active feature
    final InfoPopupOverlay infoPopupOverlay = new InfoPopupOverlay();
    infoPopupOverlay.setPopupTitle("Feature");
    jMap.addMapOverlay(infoPopupOverlay);

    featureLayer.addLayerInitializeCompleteListener(new LayerInitializeCompleteListener()
    {
      @Override
      public void layerInitializeComplete(LayerInitializeCompleteEvent arg0)
      {
        infoPopupOverlay.addLayer(arg0.getLayer());
      }
    });

    return jMap;
  }
}
Feedback on this topic?