Common Custom controls
Common_CustomControls_CSharp\ADFCompositeControl\MapToolTocControl.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 ADFCompositeControl
{
    public class MapToolTocControl : System.Web.UI.WebControls.CompositeControl
    {
        #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_mapResourceType = string.Empty;
        private string m_dataSource = string.Empty;
        private string m_mapResourceDefinition = string.Empty;

        #endregion

        #region Constructor

        /// <summary>
        /// Creates a new, uninitialized instance of the MapToolTocControl class.
        /// </summary>
        public MapToolTocControl()
        {
        }

        #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();

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

                // Create the CompositeControl's Map Control
                m_adfMap = new ESRI.ArcGIS.ADF.Web.UI.WebControls.Map();
                m_adfMap.Visible = true;
                m_adfMap.Width = (int)(Width.Value * 0.7);
                m_adfMap.Height = (int)(Height.Value * 0.8);
                m_adfMap.ID = this.ID + "_Map";
                m_adfMap.MapResourceManager = m_mapResourceManager.UniqueID;

                // Create the CompositeControl'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 = (int)(Width.Value * 0.3);
                m_adfToc.Height = (int)(Height.Value * 0.8);
                m_adfToc.ID = this.ID + "_Toc";
                m_adfToc.BuddyControl = m_adfMap.UniqueID;

                // Create the CompositeControl'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 CompositeControl's controls
                Controls.Add(table);
            }
            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 CompositeControl's MapResourceManager and initialize
            // the Map extent
            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.Extent = commonMapResource.MapInformation.DefaultExtent;
            }
        }

        #endregion

        #region Child Control Initialization Methods

        // Initializes the CompositeControl'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 + "_Toolbar";
            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(),
                "ADFCompositeControl.images.zoom-in.png");
            zoomInTool.SelectedImage = Page.ClientScript.GetWebResourceUrl(GetType(),
                "ADFCompositeControl.images.zoom-in.png");
            zoomInTool.HoverImage = Page.ClientScript.GetWebResourceUrl(GetType(),
                "ADFCompositeControl.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(),
                "ADFCompositeControl.images.zoom-out.png");
            zoomOutTool.SelectedImage = Page.ClientScript.GetWebResourceUrl(GetType(),
                "ADFCompositeControl.images.zoom-out.png");
            zoomOutTool.HoverImage = Page.ClientScript.GetWebResourceUrl(GetType(),
                "ADFCompositeControl.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(),
                "ADFCompositeControl.images.pan.png");
            panTool.SelectedImage = Page.ClientScript.GetWebResourceUrl(GetType(),
                "ADFCompositeControl.images.pan.png");
            panTool.HoverImage = Page.ClientScript.GetWebResourceUrl(GetType(),
                "ADFCompositeControl.images.pan.png");
            m_adfToolbar.ToolbarItems.Add(panTool);

            // initialize and add a custom identify tool that references the ADFCompositeControl.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 = "ADFCompositeControl";
            identifyTool.ServerActionClass = "ADFCompositeControl.MapIdentify";
            identifyTool.ClientAction = ESRI.ArcGIS.ADF.Web.UI.WebControls.Constants.HTML_POINT;
            identifyTool.DefaultImage = Page.ClientScript.GetWebResourceUrl(GetType(),
                "ADFCompositeControl.images.identify.png");
            identifyTool.SelectedImage = Page.ClientScript.GetWebResourceUrl(GetType(),
                "ADFCompositeControl.images.identify.png");
            identifyTool.HoverImage = Page.ClientScript.GetWebResourceUrl(GetType(),
                "ADFCompositeControl.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 CompositeControl's MapResourceManager
        private ESRI.ArcGIS.ADF.Web.UI.WebControls.MapResourceItem AddResourceToMapResourceManager()
        {
            if (string.IsNullOrEmpty(m_dataSource))
            {
                // Define default resource item properties if ToolTocControl properties are empty
                m_mapResourceType = "ArcGIS Server Internet";
                m_dataSource = "http://localhost/arcgis/services";
                m_mapResourceDefinition = "(default)@MapService";
            }

            // Create a GISResourceItemDefinition with user-specified parameters
            ESRI.ArcGIS.ADF.Web.UI.WebControls.GISResourceItemDefinition gisResourceItemDefinition =
                CreateGISResourceItemDefinition(m_dataSource, m_mapResourceType, string.Empty,
                m_mapResourceDefinition, true);

            // Create a mapResourceItem from the resource item definition
            ESRI.ArcGIS.ADF.Web.UI.WebControls.MapResourceItem mapResourceItem =
                CreateResourceItem("AGSMapResource<!--" + this.UniqueID + "-->",
                gisResourceItemDefinition);

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

            // 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 && m_mapResourceManager.ResourceItems.Count == 0)
            {
                AddMapResourceItemToResourceManager(m_mapResourceManager, false, mapResourceItem);
            }

            return mapResourceItem;
        }

        // Adds the passed-in resource to the passed-in map resource manager
        private void AddMapResourceItemToResourceManager(
            ESRI.ArcGIS.ADF.Web.UI.WebControls.MapResourceManager mapResourceManager,
            bool insertIntoBeginning, ESRI.ArcGIS.ADF.Web.UI.WebControls.MapResourceItem mapResourceItem)
        {
            if (insertIntoBeginning)
            {
                mapResourceManager.ResourceItems.Insert(0, mapResourceItem);
            }
            else
            {
                mapResourceManager.ResourceItems.Add(mapResourceItem);
            }
        }


        #endregion

        #region Other Instance Methods

        // Removes all the CompositeControl'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);
        }

        // Creates a resource item with the passed-in name, the passed-in resource definition string, 
        // and default settings
        private ESRI.ArcGIS.ADF.Web.UI.WebControls.MapResourceItem
            CreateResourceItem(string resourceName,
            ESRI.ArcGIS.ADF.Web.UI.WebControls.GISResourceItemDefinition gisResourceItemDefinition)
        {
            ESRI.ArcGIS.ADF.Web.UI.WebControls.MapResourceItem mapResourceItem =
                new ESRI.ArcGIS.ADF.Web.UI.WebControls.MapResourceItem();
            mapResourceItem.Definition = gisResourceItemDefinition;
            mapResourceItem.Name = resourceName;
            mapResourceItem.DisplaySettings = new ESRI.ArcGIS.ADF.Web.DisplaySettings();
            mapResourceItem.DisplaySettings.Visible = true;
            ESRI.ArcGIS.ADF.Web.ImageDescriptor imageDescriptor =
                new ESRI.ArcGIS.ADF.Web.ImageDescriptor();
            imageDescriptor.ImageFormat = ESRI.ArcGIS.ADF.Web.ImageFormat.PNG8;
            imageDescriptor.TransparentBackground = true;
            imageDescriptor.TransparentColor = System.Drawing.Color.White;
            imageDescriptor.ReturnMimeData = true;
            mapResourceItem.DisplaySettings.ImageDescriptor = imageDescriptor;
            mapResourceItem.DisplaySettings.Transparency = 0;
            return mapResourceItem;
        }

        // Creates a GISResourceItemDefinition with the passed-in parameters
        private ESRI.ArcGIS.ADF.Web.UI.WebControls.GISResourceItemDefinition
            CreateGISResourceItemDefinition(string dataSourceDefinition, string dataSourceType,
            string identity, string resourceDefinition, bool dataSourceShared)
        {
            ESRI.ArcGIS.ADF.Web.UI.WebControls.GISResourceItemDefinition gisResourceItemDefinition =
                new ESRI.ArcGIS.ADF.Web.UI.WebControls.GISResourceItemDefinition();
            gisResourceItemDefinition.DataSourceDefinition = dataSourceDefinition;
            gisResourceItemDefinition.Identity = (identity == null) ? string.Empty : identity;
            gisResourceItemDefinition.ResourceDefinition = resourceDefinition;
            gisResourceItemDefinition.DataSourceShared = dataSourceShared;
            gisResourceItemDefinition.DataSourceType = dataSourceType;
            return gisResourceItemDefinition;
        }

        #endregion

        #region Instance Properties

        /// <summary>
        /// ArcGIS Server resource types
        /// <remarks>Possible values: "ArcGIS Server Local" and "ArcGIS Server Internet"</remarks>
        /// </summary>
        [System.ComponentModel.Category("Resources")]
        public string MapResourceType
        {
            get
            {

                return m_mapResourceType;
            }
            set
            {
                m_mapResourceType = value;
            }
        }

        /// <summary>
        /// Machine name or URL of the ArcGIS Server services host
        /// </summary>
        [System.ComponentModel.Category("Resources")]
        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.ComponentModel.Category("Resources")]
        public string ResourceDefinition
        {
            get
            {
                return m_mapResourceDefinition;
            }
            set
            {
                m_mapResourceDefinition = value;
            }
        }

        #endregion
    }
}