Map Content Updates
frmFileGDB.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.Linq;
using System.Windows.Forms;

using ESRI.ArcGISExplorer.Data;
using ESRI.ArcGISExplorer.Mapping;

namespace MapContentUpdatesCS
{
  public partial class frmFileGDB : Form
  {
    public frmFileGDB()
    {
      InitializeComponent();
    }

    private void btnBrowse_Click(object sender, EventArgs e)
    {
      //Routine for adding shapefile data to the map.  Begin by browsing for the data.
      FolderBrowserDialog fdlg = new FolderBrowserDialog();
      fdlg.ShowNewFolderButton = false;
      fdlg.Description = "Find File Geodatabase";

      //If a shapefile is found, then create a FeatureLayer based on it, add symbology,
      //add the data to the map, and then zoom in.
      if (fdlg.ShowDialog() == DialogResult.OK)
        txtDatabase.Text = fdlg.SelectedPath;
    }

    private void btnFind_Click(object sender, EventArgs e)
    {
      if (txtDatabase.Text == "")
      {
        MessageBox.Show("You need to specify a database!!", "No Database", MessageBoxButtons.OK);
        return;
      }

      //If we have a database, then try to connect to it
      Geodatabase fileGDB = null;
      try
      {
        fileGDB = new Geodatabase(txtDatabase.Text);
      }
      catch (Exception)
      {
        MessageBox.Show("Could not connect to database!!", "Could not connect", MessageBoxButtons.OK);
        return;
      }

      if (fileGDB == null)
      {
        MessageBox.Show("Could not connect to database!!", "Could not connect", MessageBoxButtons.OK);
        return;
      }

      //if we are this far, then we've connected and we need to create a tree view of the feature classes
      tvwFeatureClasses.Nodes.Clear();
      //Add the Geodatabase Folders (Feature Datasets) and their contents first
      foreach (GeodatabaseFolder folder in fileGDB.GetGeodatabaseFolders())
      {
        TreeNode folderNode = tvwFeatureClasses.Nodes.Add(folder.Name, folder.Name);
        foreach (Table table in folder.GetTables())
        {
          folderNode.Nodes.Add(table.Name, table.Name);
        }
      }
      //Add standalone tables second
      foreach (Table table2 in fileGDB.GetTables(TableDiscoveryOptions.Spatial, false))
      {
        tvwFeatureClasses.Nodes.Add(table2.Name, table2.Name);
      }
    }

    private void btnCancel_Click(object sender, EventArgs e)
    {
      this.Close();
    }

    private void btnOK_Click(object sender, EventArgs e)
    {
      if (tvwFeatureClasses.Nodes.Count == 0)
      {
        MessageBox.Show("Nothing to add!!", "no data", MessageBoxButtons.OK);
        return;
      }

      if (tvwFeatureClasses.SelectedNode == null)
      {
        MessageBox.Show("Please select a table in the list!", "no data", MessageBoxButtons.OK);
        return;
      }

      //If we have a selection from the tree view, then we are ready to add it to the map

      DataSourceProperties dataSrcProps = new DataSourceProperties(txtDatabase.Text, tvwFeatureClasses.SelectedNode.Text);
      FeatureLayer fLayer = new FeatureLayer(dataSrcProps);
      fLayer.Connect();

      if (fLayer != null)
      {
        if (fLayer.Name != "")
        {
          //if we've created a layer, then add it to the map contents collection and zoom
          MapDisplay mapDisp = ESRI.ArcGISExplorer.Application.Application.ActiveMapDisplay;
          mapDisp.Map.ChildItems.Add(fLayer);
          mapDisp.ZoomTo(fLayer.Extent);
        }
      }

      //close the form
      this.Close();
    }

    private void tvwFeatureClasses_AfterSelect(object sender, TreeViewEventArgs e)
    {
      if (tvwFeatureClasses.SelectedNode != null) btnOK.Enabled = true;
      else btnOK.Enabled = false;
    }
  }
}