Common Custom controls
Common_CustomControls_CSharp\ADFWebPart\MapWebPart.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.
// 


namespace ADFWebPart
{
    /// <summary>
    /// WebPart containing an ArcGIS Server Web ADF Map, Toc, and Toolbar with Zoom In, Zoom Out,
    /// Pan, and Identify
    /// </summary>
    public class MapWebPart : AJAXSharePointWebPart
    {
        #region Instance Variable Declarations

        private const int TOOLBARHEIGHT = 32;
        private ESRI.ArcGIS.ADF.Web.UI.WebControls.Map m_adfMap = null;
        private ESRI.ArcGIS.ADF.Web.UI.WebControls.MapResourceManager m_mapResourceManager = null;
        private ESRI.ArcGIS.ADF.Web.UI.WebControls.Toolbar m_adfToolbar = null;
        private ESRI.ArcGIS.ADF.Web.UI.WebControls.Toc m_adfToc = null;

        private string m_dataSourceType;
        private string m_dataSource;
        private string m_mapResourceDefinition;

        #endregion

        #region Constructor

        /// <summary>
        /// Creates a new, uninitialized instance of the MapWebPart class.
        /// </summary>
        public MapWebPart()
        {
            // Default property values
            m_dataSourceType = "ArcGIS Server Internet";
            m_dataSource = "http://serverapps.esri.com/arcgis/services";
            m_mapResourceDefinition = "Layers@SamplesNET/NorthAmerica";
        }

        #endregion

        #region WebControl Life Cycle Event Handlers

        // Renders the contents of the control
        protected override void RenderContents(System.Web.UI.HtmlTextWriter writer)
        {
            // Render the control's ID if the control is being drawn at design-time, or render normally
            // if the control is being rendered at run-time
            if (DesignMode)
            {
                writer.WriteLine(this.ID);
            }
            else
            {
                base.RenderContents(writer);
            }
        }

        // Create the controls making up the WebPart and add them to the page
        protected override void CreateChildControls()
        {
            try
            {
                base.CreateChildControls();

                if (propertyCheck())
                {
                    // Create and initialize a MapResourceManager
                    m_mapResourceManager = new ESRI.ArcGIS.ADF.Web.UI.WebControls.MapResourceManager();
                    m_mapResourceManager.ID = this.ID + "_webPartMapResourceManager";
                    Controls.Add(m_mapResourceManager);

                    // Create the WebPart's Map Control
                    m_adfMap = new ESRI.ArcGIS.ADF.Web.UI.WebControls.Map();
                    m_adfMap.Visible = true;
                    m_adfMap.Width = 400;
                    m_adfMap.Height = 400;
                    m_adfMap.ID = this.ID + "_webPartMap";
                    m_adfMap.MapResourceManager = m_mapResourceManager.UniqueID;

                    // Create the WebPart's Toc and buddy it to the map
                    m_adfToc = new ESRI.ArcGIS.ADF.Web.UI.WebControls.Toc();
                    m_adfToc.Visible = true;
                    m_adfToc.Width = 200;
                    m_adfToc.Height = 300;
                    m_adfToc.ID = this.ID + "_webPartToc";
                    m_adfToc.BuddyControl = m_adfMap.UniqueID;

                    // Create the WebPart's toolbar and buddy it to the map
                    m_adfToolbar = CreateToolbar();
                    m_adfToolbar.BuddyControls.Add(new ESRI.ArcGIS.ADF.Web.UI.WebControls.BuddyControl(
                        m_adfMap.UniqueID));

                    Controls.Add(m_adfToolbar);

                    // Create a table to hold the Map and Toc so they appear side-by-side
                    System.Web.UI.WebControls.Table table = new System.Web.UI.WebControls.Table();
                    System.Web.UI.WebControls.TableRow tableRow = new System.Web.UI.WebControls.TableRow();
                    table.Controls.Add(tableRow);
                    System.Web.UI.WebControls.TableCell tableCell = new System.Web.UI.WebControls.TableCell();

                    // Add the Map to the table
                    tableCell.Controls.Add(m_adfMap);
                    tableCell.Style.Add(System.Web.UI.HtmlTextWriterStyle.VerticalAlign, "top");
                    tableRow.Controls.Add(tableCell);

                    // Add the Toc to the table
                    tableCell = new System.Web.UI.WebControls.TableCell();
                    tableCell.Controls.Add(m_adfToc);
                    tableCell.Style.Add(System.Web.UI.HtmlTextWriterStyle.VerticalAlign, "top");
                    tableRow.Controls.Add(tableCell);

                    // Add the table to the WebPart's controls
                    Controls.Add(table);
                }
                else
                {
                    System.Web.UI.WebControls.Label noResourceDefLabel =
new System.Web.UI.WebControls.Label();
                    noResourceDefLabel.ID = "lblNoResourceDef";
                    noResourceDefLabel.Text = "Data source type and definition, resource definition, and data layer name must be defined.";
                    Controls.Add(noResourceDefLabel);
                }
            }
            catch (System.Exception ex)
            {
                // Call the method to output the stack trace if an error occurs during initialization
                ShowErrorMessage(ex);
            }
        }

        // Executes before the control is rendered
        protected override void OnPreRender(System.EventArgs e)
        {
            base.OnPreRender(e);

            // Add the user-specified resource to the WebPart's MapResourceManager and initialize
            // the Map extent
            if (propertyCheck())
            {
                ESRI.ArcGIS.ADF.Web.UI.WebControls.MapResourceItem mapResourceItem =
                    AddResourceToMapResourceManager();
                if (m_mapResourceManager != null && mapResourceItem != null)
                {
                    ESRI.ArcGIS.ADF.Web.DataSources.IMapResource commonMapResource =
                        mapResourceItem.Resource as ESRI.ArcGIS.ADF.Web.DataSources.IMapResource;
                    m_adfMap.Reset();
                    m_adfMap.Extent = commonMapResource.MapInformation.DefaultExtent;

                }
            }
        }

        #endregion

        #region Child Control Initialization Methods

        // Initializes the WebPart's toolbar
        private ESRI.ArcGIS.ADF.Web.UI.WebControls.Toolbar CreateToolbar()
        {
            // Initialize toolbar properties
            m_adfToolbar = new ESRI.ArcGIS.ADF.Web.UI.WebControls.Toolbar();
            m_adfToolbar.ID = this.ID + "_mapWebPartToolbar";
            m_adfToolbar.BuddyControlType = ESRI.ArcGIS.ADF.Web.UI.WebControls.BuddyControlType.Map;
            m_adfToolbar.ToolbarStyle = ESRI.ArcGIS.ADF.Web.UI.WebControls.ToolbarStyle.ImageOnly;
            m_adfToolbar.Height = new System.Web.UI.WebControls.Unit(TOOLBARHEIGHT,
                System.Web.UI.WebControls.UnitType.Pixel);
            m_adfToolbar.Width = new System.Web.UI.WebControls.Unit(120,
                System.Web.UI.WebControls.UnitType.Pixel);

            // Initialize and add a zoom in tool
            ESRI.ArcGIS.ADF.Web.UI.WebControls.Tool zoomInTool =
                new ESRI.ArcGIS.ADF.Web.UI.WebControls.Tool();
            zoomInTool.Name = "MapZoomIn";
            zoomInTool.ServerActionAssembly = "ESRI.ArcGIS.ADF.Web.UI.WebControls";
            zoomInTool.ServerActionClass = "ESRI.ArcGIS.ADF.Web.UI.WebControls.Tools.MapZoomIn";
            zoomInTool.ToolTip = "Zoom In";
            zoomInTool.ClientAction = ESRI.ArcGIS.ADF.Web.UI.WebControls.Constants.HTML_DRAG_RECTANGLE;
            zoomInTool.DefaultImage = Page.ClientScript.GetWebResourceUrl(GetType(),
                "ADFWebPart.images.zoom-in.png");
            zoomInTool.SelectedImage = Page.ClientScript.GetWebResourceUrl(GetType(),
                "ADFWebPart.images.zoom-in.png");
            zoomInTool.HoverImage = Page.ClientScript.GetWebResourceUrl(GetType(),
                "ADFWebPart.images.zoom-in.png");
            zoomInTool.EnablePostBack = false;
            m_adfToolbar.ToolbarItems.Add(zoomInTool);

            // Initialize and add a zoom out tool
            ESRI.ArcGIS.ADF.Web.UI.WebControls.Tool zoomOutTool =
                new ESRI.ArcGIS.ADF.Web.UI.WebControls.Tool();
            zoomOutTool.Name = "MapZoomOut";
            zoomOutTool.ServerActionAssembly = "ESRI.ArcGIS.ADF.Web.UI.WebControls";
            zoomOutTool.ServerActionClass = "ESRI.ArcGIS.ADF.Web.UI.WebControls.Tools.MapZoomOut";
            zoomOutTool.ToolTip = "Zoom Out";
            zoomOutTool.ClientAction = ESRI.ArcGIS.ADF.Web.UI.WebControls.Constants.HTML_DRAG_RECTANGLE;
            zoomOutTool.DefaultImage = Page.ClientScript.GetWebResourceUrl(GetType(),
                "ADFWebPart.images.zoom-out.png");
            zoomOutTool.SelectedImage = Page.ClientScript.GetWebResourceUrl(GetType(),
                "ADFWebPart.images.zoom-out.png");
            zoomOutTool.HoverImage = Page.ClientScript.GetWebResourceUrl(GetType(),
                "ADFWebPart.images.zoom-out.png");
            m_adfToolbar.ToolbarItems.Add(zoomOutTool);

            // initialize and add a pan tool
            ESRI.ArcGIS.ADF.Web.UI.WebControls.Tool panTool =
                new ESRI.ArcGIS.ADF.Web.UI.WebControls.Tool();
            panTool.Name = "MapPan";
            panTool.ServerActionAssembly = "ESRI.ArcGIS.ADF.Web.UI.WebControls";
            panTool.ServerActionClass = "ESRI.ArcGIS.ADF.Web.UI.WebControls.Tools.MapPan";
            panTool.ToolTip = "Pan";
            panTool.ClientAction = ESRI.ArcGIS.ADF.Web.UI.WebControls.Constants.HTML_DRAG_IMAGE;
            panTool.DefaultImage = Page.ClientScript.GetWebResourceUrl(GetType(),
                "ADFWebPart.images.pan.png");
            panTool.SelectedImage = Page.ClientScript.GetWebResourceUrl(GetType(),
                "ADFWebPart.images.pan.png");
            panTool.HoverImage = Page.ClientScript.GetWebResourceUrl(GetType(),
                "ADFWebPart.images.pan.png");
            m_adfToolbar.ToolbarItems.Add(panTool);

            // initialize and add a custom identify tool that references the ADFWebPart.MapIdentify
            // class
            ESRI.ArcGIS.ADF.Web.UI.WebControls.Tool identifyTool =
                new ESRI.ArcGIS.ADF.Web.UI.WebControls.Tool();
            identifyTool.Name = "MapIdentify";
            identifyTool.ToolTip = "Identify";
            identifyTool.ServerActionAssembly = "ADFWebPart";
            identifyTool.ServerActionClass = "ADFWebPart.MapIdentify";
            identifyTool.ClientAction = ESRI.ArcGIS.ADF.Web.UI.WebControls.Constants.HTML_POINT;
            identifyTool.DefaultImage = Page.ClientScript.GetWebResourceUrl(GetType(),
                "ADFWebPart.images.identify.png");
            identifyTool.SelectedImage = Page.ClientScript.GetWebResourceUrl(GetType(),
                "ADFWebPart.images.identify.png");
            identifyTool.HoverImage = Page.ClientScript.GetWebResourceUrl(GetType(),
                "ADFWebPart.images.identify.png");
            // Use a full page postback for the Identify tool
            identifyTool.EnablePostBack = true;
            m_adfToolbar.ToolbarItems.Add(identifyTool);

            return m_adfToolbar;
        }

        // Adds the user-specified resource to the WebPart's MapResourceManager
        private ESRI.ArcGIS.ADF.Web.UI.WebControls.MapResourceItem AddResourceToMapResourceManager()
        {
            // Optional: define default data source-resource properties
            //if (string.IsNullOrEmpty(m_dataSource))
            //{
            //    // Define default resource item properties if MapWebPart properties are empty
            //    m_mapResourceType = "ArcGIS Server Internet";
            //    m_dataSource = "http://localhost/arcgis/services";
            //    m_mapResourceDefinition = "(default)@MapService";
            //}
            ESRI.ArcGIS.ADF.Web.UI.WebControls.MapResourceItem mapResourceItem = null;

            // Make sure the map resource manager exists and that it does not contain any resource
            // items before adding the resource item to it
            if (m_mapResourceManager != null)
            {
                // Create a GISResourceItemDefinition with user-specified parameters
                ESRI.ArcGIS.ADF.Web.UI.WebControls.GISResourceItemDefinition gisResourceItemDefinition =
                       Utility.CreateGISResourceItemDefinition(m_dataSource, m_dataSourceType, string.Empty,
                       m_mapResourceDefinition, true);

                if (m_mapResourceManager.ResourceItems.Count > 0)
                {
                    string existingResourceItemDefinition = m_mapResourceManager.ResourceItems[0].Definition.ToString();

                    if (existingResourceItemDefinition == gisResourceItemDefinition.ToString())
                    {
                        return mapResourceItem;
                    }
                }

                m_mapResourceManager.ResourceItems.Clear();

                // Create a mapResourceItem from the resource item definition
                mapResourceItem =
                    Utility.CreateResourceItem("AGSMapResource<!--" + this.UniqueID + "-->",
                    gisResourceItemDefinition);

                // Assign the parent of the map resource item and initialize the underlying resource
                mapResourceItem.Parent = m_mapResourceManager;
                mapResourceItem.InitializeResource();

                Utility.AddMapResourceItemToResourceManager(m_mapResourceManager, false, mapResourceItem);
            }

            //if (m_mapResourceManager != null && m_mapResourceManager.ResourceItems.Count == 0)
            //{
            //    // Create a GISResourceItemDefinition with user-specified parameters
            //    ESRI.ArcGIS.ADF.Web.UI.WebControls.GISResourceItemDefinition gisResourceItemDefinition =
            //        Utility.CreateGISResourceItemDefinition(m_dataSource, m_dataSourceType, string.Empty,
            //        m_mapResourceDefinition, true);

            //    // Create a mapResourceItem from the resource item definition
            //    mapResourceItem =
            //        Utility.CreateResourceItem("AGSMapResource<!--" + this.UniqueID + "-->",
            //        gisResourceItemDefinition);

            //    // Assign the parent of the map resource item and initialize the underlying resource
            //    mapResourceItem.Parent = m_mapResourceManager;
            //    mapResourceItem.InitializeResource();

            //    Utility.AddMapResourceItemToResourceManager(m_mapResourceManager, false, mapResourceItem);
            //}

            return mapResourceItem;
        }

        #endregion

        #region Other Instance Methods

        // Removes all the web control's child controls and replaces them with a Literal control 
        // containing the stack trace of the passed-in exception.
        protected void ShowErrorMessage(System.Exception ex)
        {
            System.Web.UI.WebControls.Literal errorMessageLiteral =
                new System.Web.UI.WebControls.Literal();
            errorMessageLiteral.Text = ex.StackTrace;
            this.Controls.Clear();
            this.Controls.Add(errorMessageLiteral);
        }

        #endregion

        private bool propertyCheck()
        {
            if (!string.IsNullOrEmpty(m_dataSource) && !string.IsNullOrEmpty(m_dataSourceType) &&
                !string.IsNullOrEmpty(m_mapResourceDefinition))
            {
                return true;
            }

            return false;
        }

        #region Instance Properties

        /// <summary>
        /// ArcGIS Server resource types
        /// <remarks>Possible values: "ArcGIS Server Local" and "ArcGIS Server Internet"</remarks>
        /// </summary>
        [System.Web.UI.WebControls.WebParts.Personalizable(
            System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared),
        System.Web.UI.WebControls.WebParts.WebBrowsable(true),
        System.ComponentModel.Category("Resources"),
        System.Web.UI.WebControls.WebParts.WebDescription("Valid values are 'ArcGIS Server Local' and 'ArcGIS Server Internet'"),
        System.Web.UI.WebControls.WebParts.WebDisplayName("ArcGIS Server Data Source Type")]
        public string DataSourceType
        {
            get
            {
                return m_dataSourceType;
            }
            set
            {
                m_dataSourceType = value;
            }
        }

        /// <summary>
        /// Machine name or URL of the ArcGIS Server services host
        /// </summary>
        [System.Web.UI.WebControls.WebParts.Personalizable(
            System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared),
        System.Web.UI.WebControls.WebParts.WebBrowsable(true),
        System.ComponentModel.Category("Resources"),
        System.Web.UI.WebControls.WebParts.WebDescription("ArcGIS Server machine name or Url"),
        System.Web.UI.WebControls.WebParts.WebDisplayName("ArcGIS Server Data Source")]
        public string DataSource
        {
            get
            {
                return m_dataSource;
            }
            set
            {
                m_dataSource = value;
            }
        }

        /// <summary>
        /// ArcGIS Server Resource.  Must be formatted as &lt;DataFrameName&gt;@&lt;ServiceName&gt;
        /// </summary>
        [System.Web.UI.WebControls.WebParts.Personalizable(
            System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared),
        System.Web.UI.WebControls.WebParts.WebBrowsable(true),
        System.ComponentModel.Category("Resources"),
       System.Web.UI.WebControls.WebParts.WebDescription("Formatted as DataFrameName@ServiceName"),
        System.Web.UI.WebControls.WebParts.WebDisplayName("ArcGIS Server Resource String")]
        public string ResourceDefinition
        {
            get
            {
                return m_mapResourceDefinition;
            }
            set
            {
                m_mapResourceDefinition = value;
            }
        }

        #endregion
    }
}