Feature attribute editing

Download Sample Viewer

Description

This sample demonstrates editing the attributes of a feature using an infopopup in editing mode. To use the sample, click on a feature in the map to view the attributes in an infopopup. Edit values using the infopopup, pressing enter in a text box when you are done editing a field, and click 'Save' to save the edits to the database, or 'Cancel' otherwise.

Code snippet


    final HitTestOverlay hitTestOverlay = new HitTestOverlay(featureLayer);
    hitTestOverlay.addHitTestListener(new HitTestListener() {
      @Override
      public void featureHit(HitTestEvent event) {
        HitTestOverlay overlay = event.getOverlay();
        Graphic hitGraphic = (Graphic) overlay.getHitFeatures().get(0);
        try {
          // create a popup in edit view 
          PopupView contentPanel = PopupView.createEditView("Edit Attributes", incidentsLayer); 
          // you can optionally set a preferred size for the PopupView 
          //contentPanel.setPreferredSize(new Dimension(300, 300)); 
          contentPanel.setGraphic(incidentsLayer, hitGraphic);
          // create map popup to display the popup view
          final PopupDialog popup = incidentsLayer.getMap().createPopup(
              new JComponent[]{contentPanel}, hitGraphic);
          popup.setTitle("Edit Attributes");
          popup.setVisible(true);
          // ...
        }
      }
    });
    jMap.addMapOverlay(hitTestOverlay);
  

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.editing;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
import javax.swing.border.LineBorder;

import com.esri.toolkit.overlays.HitTestEvent;
import com.esri.toolkit.overlays.HitTestListener;
import com.esri.toolkit.overlays.HitTestOverlay;
import com.esri.core.map.Feature;
import com.esri.core.map.Graphic;
import com.esri.map.ArcGISFeatureLayer;
import com.esri.map.JMap;
import com.esri.map.MapOptions;
import com.esri.map.MapOptions.MapType;
import com.esri.map.popup.PopupDialog;
import com.esri.map.popup.PopupView;
import com.esri.map.popup.PopupViewEvent;
import com.esri.map.popup.PopupViewListener;

/***
 * This sample demonstrates editing the attributes of a feature using an
 * infopopup in editing mode. To use the sample, click on a feature in the map
 * to view the attributes in an infopopup. Edit values using the infopopup,
 * pressing enter in a text box when you are done editing a field, and
 * click 'Save' to save the edits to the database, or 'Cancel' otherwise.
 */
public class FeatureEditingApp {

  private JMap map;
  private JComponent contentPane;

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

  // ------------------------------------------------------------------------
  // Core Functionality
  // ------------------------------------------------------------------------

  /**
   * Creates the map with a basemap and an editable feature layer.
   * Adds a hit test overlay to react to mouse clicks: when the mouse
   * clicks on a feature, an editable infopopup is displayed.
   * @return a map.
   */
  private JMap createMap() throws Exception {

    // focus on Redlands, California
    MapOptions mapOptions = new MapOptions(MapType.TOPO, 34.0515888762, -117.190346717, 14);
    final JMap jMap = new JMap(mapOptions);

    // feature layer with editable features (non-secure layer)
    final ArcGISFeatureLayer incidentsLayer = new ArcGISFeatureLayer(
        "http://sampleserver6.arcgisonline.com/arcgis/rest/services/PhoneIncidents/FeatureServer/0");
    jMap.getLayers().add(incidentsLayer);

    // use a hit test overlay to show an editable popup on mouse clicks
    HitTestOverlay hitTestOverlay = new HitTestOverlay(incidentsLayer);
    hitTestOverlay.addHitTestListener(new HitTestListener() {

      @Override
      public void featureHit(HitTestEvent event) {
        HitTestOverlay overlay = event.getOverlay();
        // get first (top-most) graphic hit by the mouse
        Graphic hitGraphic = (Graphic) overlay.getHitFeatures().get(0);
        try {
          // create a popup in edit view
          PopupView contentPanel = PopupView.createEditView("Edit Attributes", incidentsLayer);
          // you can optionally set a preferred size for the PopupView
          //contentPanel.setPreferredSize(new Dimension(300, 300));
          contentPanel.setGraphic(incidentsLayer, hitGraphic);
          final PopupDialog popup = jMap.createPopup(new JComponent[]{contentPanel}, hitGraphic);
          popup.setTitle("Edit Attributes");
          popup.setVisible(true);
          contentPanel.addPopupViewListener(new PopupViewListener() {

            @Override
            public void onCommitEdit(PopupViewEvent popupViewEvent, Feature feature) {
              popup.close();
            }

            @Override
            public void onCancelEdit(PopupViewEvent popupViewEvent, Feature feature) {
              popup.close();
            }
          });
        } catch (Exception e) {
          // handle any error here as desired
          e.printStackTrace();
        }
      }
    });
    jMap.addMapOverlay(hitTestOverlay);

    return jMap;
  }

  // ------------------------------------------------------------------------
  // 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
          FeatureEditingApp featureDataFormApp = new FeatureEditingApp();

          // create the UI, including the map, for the application.
          JFrame appWindow = featureDataFormApp.createWindow();
          appWindow.add(featureDataFormApp.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 {
    contentPane = createContentPane();

    // description
    JPanel description = createDescription();
    contentPane.add(description);

    // map
    map = createMap();
    contentPane.add(map);

    return contentPane;
  }

  // ------------------------------------------------------------------------
  // Private methods
  // ------------------------------------------------------------------------
  /**
   * Creates the application window, disposes the map when the window is closing.
   * @return a window.
   */
  private JFrame createWindow() {
    JFrame window = new JFrame();
    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);
        if (map != null) {
          map.dispose();
        }
      }
    });
    return window;
  }

  /**
   * Creates a layered pane in order to display the description on top of the map.
   * @return a content pane.
   */
  private static JLayeredPane createContentPane() {
    JLayeredPane contentPane = new JLayeredPane();
    contentPane.setLayout(new BorderLayout(0, 0));
    contentPane.setVisible(true);
    return contentPane;
  }

  /**
   * Creates a description for this application.
   * @return description
   */
  private JPanel createDescription() {
    JPanel descriptionContainer = new JPanel();
    descriptionContainer.setLocation(10,10);
    descriptionContainer.setLayout(new BoxLayout(descriptionContainer, 0));
    descriptionContainer.setSize(290, 85);
    descriptionContainer.setDoubleBuffered(true);
    JTextArea description = new JTextArea(
        "Click on a feature in the map to view the attributes in " +
            "an editable infopopup. Edit values in the data form and click " +
            "'Save' to save the edits to the database, or 'Cancel' otherwise"
        );
    description.setFont(new Font("Verdana", Font.PLAIN, 11));
    description.setForeground(Color.WHITE);
    description.setBackground(new Color(0, 0, 0, 180));
    description.setEditable(false);
    description.setLineWrap(true);
    description.setWrapStyleWord(true);
    description.setBorder(BorderFactory.createEmptyBorder(5,10,5,5));
    descriptionContainer.add(description);
    descriptionContainer.setBackground(new Color(0, 0, 0, 0)); // transparent
    descriptionContainer.setBorder(new LineBorder(Color.BLACK, 3, false));
    return descriptionContainer;
  }
}
Feedback on this topic?