Update Note Geometry
DockWindow.cs
// 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.
// 

using System;
using System.Collections.Generic;
using System.ComponentModel;
//using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using ESRI.ArcGISExplorer;
using ESRI.ArcGISExplorer.Application;
using ESRI.ArcGISExplorer.Mapping;
using ESRI.ArcGISExplorer.Geometry;
using ESRI.ArcGISExplorer.Data;
using ESRI.ArcGISExplorer.Threading;

namespace UpdateNoteGeometryCS
{
  enum UpdateProperty
  {
    X,
    Y,
  }

  public partial class DockWindow : ESRI.ArcGISExplorer.Application.DockWindow
  {
    MapDisplay _disp = null;
    Note _currentNote = null;
    Geometry _currentGeom = null;
    bool _selectingItem = false;
    int _selectedIndex = -1;

    #region Displaying and updating coordinates

    private void UpdateGeometryOfNote(UpdateProperty updateProperty)
    {
      // This function deals with updating the coordinates of existing 
      // Points or the coordinates of the vertices of Polylines and Polygons.

      // First check the entered value is valid.
      double newCoordinate = Double.NaN;
      bool edited = false;
      if (updateProperty == UpdateProperty.X)
        edited = double.TryParse(this.txtX.Text, out newCoordinate);
      else
        edited = double.TryParse(this.txtY.Text, out newCoordinate);

      if (edited)
      {
        // For each type of Geometry, get a Point representing the
        // existing Point, and then change the edited coordinate value;
        // this will ensure all other properties of the Point are
        // preserved (instead of creating a new Point).
        if (_currentGeom.GeometryType == GeometryType.Point)
        {
          Point pt = _currentGeom as Point;
          if (updateProperty == UpdateProperty.X)
            pt.X = newCoordinate;
          if (updateProperty == UpdateProperty.Y)
            pt.Y = newCoordinate;
          _currentNote.Graphic.Geometry = pt;
        }
        else if ((_currentGeom.GeometryType == GeometryType.Polyline) || (_currentGeom.GeometryType == GeometryType.Polygon))
        {
          // Only the first part of multipart Polygons or Polylines is edited in this sample.
          VertexGeometry vgeom = new VertexGeometry(_currentGeom);
          Point vertex = vgeom.GetPoint(_selectedIndex);
          if (updateProperty == UpdateProperty.X)
            vertex.X = newCoordinate;
          if (updateProperty == UpdateProperty.Y)
            vertex.Y = newCoordinate;
          vgeom.SetPoint(_selectedIndex, vertex);
          _currentNote.Graphic.Geometry = _currentGeom;

          RefreshListItem(_selectedIndex, vertex);
        }
      }
      else
      {
        // User entered an invalid value - reset the UI.
        ShowGeometryCoords();
      }
    }

    private void ShowGeometryCoords()
    {
      _selectingItem = true;

      SetupControlsForSelectedGeom();

      if (_currentGeom.GeometryType == ESRI.ArcGISExplorer.Geometry.GeometryType.Point)
      {
        Point pt = _currentGeom as Point;
        ShowSelectedCoords(pt);
      }
      else if ((_currentGeom.GeometryType == GeometryType.Polyline) || (_currentGeom.GeometryType == GeometryType.Polygon))
      {
        VertexGeometry vgeom = new VertexGeometry(_currentGeom);
        for (int i = 0; i < vgeom.PointCount(); i++)
        {
          AddVertexToListView(i, vgeom.GetPoint(i));
        }
      }
      _selectingItem = false;
    }

    #endregion

    #region UI Controls Updating

    private void RefreshListItem(int _selectedIndex, Point lineVertex)
    {
      ListViewItem item = this.lvwVertices.Items[_selectedIndex];
      item.SubItems[1].Text = lineVertex.X.ToString();
      item.SubItems[2].Text = lineVertex.Y.ToString();
    }

    private void ShowSelectedCoords(Point pt)
    {
      this.txtX.Text = pt.X.ToString();
      this.txtY.Text = pt.Y.ToString();
    }

    private void AddVertexToListView(int vertexIndex, Point point)
    {
      this.lvwVertices.Items.Add(new ListViewItem(
         new string[] { vertexIndex.ToString(), point.X.ToString(), point.Y.ToString() }));
    }

    private void SetupControlsForSelectedGeom()
    {
      this.txtX.Text = "";
      this.txtY.Text = "";
      this.lvwVertices.Items.Clear();

      if (_currentGeom == null)
      {
        this.splitCoordinates.Enabled = false;
        this.lvwVertices.Enabled = false;
        this.txtMultiVertex.Enabled = false;
      }
      else if (_currentGeom.GeometryType == GeometryType.Point)
      {
        this.splitCoordinates.Enabled = true;
        this.lvwVertices.Enabled = false;
        this.txtMultiVertex.Enabled = false;
      }
      else if ((_currentGeom.GeometryType == GeometryType.Polyline) || (_currentGeom.GeometryType == GeometryType.Polygon))
      {
        this.splitCoordinates.Enabled = true;
        this.lvwVertices.Enabled = true;
        this.txtMultiVertex.Enabled = true;
      }
    }

    #endregion

    #region UI event handlers functions

    public DockWindow()
    {
      InitializeComponent();

      // Set up an event sink for clicking or selecting a Note.
      _disp = ESRI.ArcGISExplorer.Application.Application.ActiveMapDisplay;
      _disp.MapItemClicked += new EventHandler<MapItemMouseEventArgs>(disp_MapItemClicked);
      ESRI.ArcGISExplorer.Application.Application.MapItemSelectionChanged += new EventHandler(Application_MapItemSelectionChanged);

      SetupControlsForSelectedGeom();
    }

    void Application_MapItemSelectionChanged(object sender, EventArgs e)
    {
      // Event handler for when a MapItem is clicked.
      SelectedItemsCollection selItems = ESRI.ArcGISExplorer.Application.Application.SelectedItems;
      if (selItems.Count == 1)
      {
        if (selItems[0] is Note)
        {
          _currentNote = selItems[0] as Note;
          _currentGeom = _currentNote.Graphic.Geometry;

          ShowGeometryCoords();
        }
      }
    }

    void disp_MapItemClicked(object sender, MapItemMouseEventArgs e)
    {
      // Event handler for when a MapItem is clicked.
      if (e.MapItems[0] is Note)
      {
        _currentNote = e.MapItems[0] as Note;
        _currentGeom = _currentNote.Graphic.Geometry;

        ShowGeometryCoords();
      }
    }

    private void txtX_TextChanged(object sender, EventArgs e)
    {
      if (_selectingItem) return;
      UpdateGeometryOfNote(UpdateProperty.X);
    }

    private void txtY_TextChanged(object sender, EventArgs e)
    {
      if (_selectingItem) return;
      UpdateGeometryOfNote(UpdateProperty.Y);
    }

    private void lvwVertices_SelectedIndexChanged(object sender, EventArgs e)
    {
      if (this.lvwVertices.SelectedItems.Count == 1)
      {
        _selectingItem = true;

        ListViewItem selected = this.lvwVertices.SelectedItems[0];
        _selectedIndex = selected.Index;
        this.txtX.Text = selected.SubItems[1].Text;
        this.txtY.Text = selected.SubItems[2].Text;

        _selectingItem = false;
      }
    }

    #endregion

  }
}