Common Custom tasks
Common_CustomTasks_CSharp\FindNearTask_CSharp\GraphicsResource.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 FindNearTask_CSharp
{
    // Manages the map resource used by FindNearTask to store its user input and buffer GraphicsLayers
    internal class GraphicsResource
    {
        // Construtor that performs initialization logic
        public GraphicsResource(string resourceName, ESRI.ArcGIS.ADF.Web.UI.WebControls.Map mapInstance)
        {
            // Set the resource name and call the initialization method
            this.ResourceName = resourceName;
            this.Initialize(mapInstance);
        }

        public string ResourceName;

        #region Public Methods

        // Retrieves the user input GraphicsLayer
        public ESRI.ArcGIS.ADF.Web.Display.Graphics.ElementGraphicsLayer GetUserInputLayer(
            ESRI.ArcGIS.ADF.Web.UI.WebControls.MapResourceManager mapResourceManager)
        {
            return this.GetLayer(mapResourceManager, "User Input Graphics Layer");
        }

        // Retrieves the buffer GraphicsLayer
        public ESRI.ArcGIS.ADF.Web.Display.Graphics.ElementGraphicsLayer GetBufferLayer(
            ESRI.ArcGIS.ADF.Web.UI.WebControls.MapResourceManager mapResourceManager)
        {
            return this.GetLayer(mapResourceManager, "Buffer Graphics Layer");
        }

        #endregion

        #region Private Methods

        // Creates the graphics resource and graphics layers used by FindNearTask
        private void Initialize(ESRI.ArcGIS.ADF.Web.UI.WebControls.Map mapInstance)
        {
            // Attempt to retrieve the resource item referred to the by the ResourceName property.
            // If it is not found, execute logic to create and populate it.
            ESRI.ArcGIS.ADF.Web.UI.WebControls.MapResourceItem resourceItem =
                mapInstance.MapResourceManagerInstance.ResourceItems.Find(ResourceName);
            if (resourceItem == null)
            {
                // Create a garphics resource item and initialize its properties
                resourceItem = new ESRI.ArcGIS.ADF.Web.UI.WebControls.MapResourceItem();
                string resourceDefinition = @"<Definition DataSourceDefinition=""In Memory"" DataSourceType=""GraphicsLayer"" 
                    Identity="""" ResourceDefinition="""" DataSourceShared=""True""></Definition>";
                resourceItem.Definition = new ESRI.ArcGIS.ADF.Web.UI.WebControls.GISResourceItemDefinition(resourceDefinition);
                resourceItem.Name = ResourceName;
                resourceItem.DisplaySettings = new ESRI.ArcGIS.ADF.Web.DisplaySettings();
                resourceItem.DisplaySettings.Visible = true;
                resourceItem.DisplaySettings.Transparency = 25;
                resourceItem.DisplaySettings.DisplayInTableOfContents = false;

                resourceItem.DisplaySettings.ImageDescriptor = new ESRI.ArcGIS.ADF.Web.ImageDescriptor(
                    ESRI.ArcGIS.ADF.Web.UI.WebControls.Converter.FromWebImageFormat(mapInstance.ImageFormat),
                    (int)mapInstance.Width.Value, (int)mapInstance.Height.Value);
                mapInstance.MapResourceManagerInstance.ResourceItems.Insert(0, resourceItem);
                resourceItem.DisplaySettings.ImageDescriptor.TransparentBackground = true;
                resourceItem.DisplaySettings.ImageDescriptor.TransparentColor =
                    System.Drawing.Color.FromArgb(1, 1, 1);

                // Create the Web ADF MapResource for the resource item and get a reference to it
                ESRI.ArcGIS.ADF.Web.DataSources.Graphics.MapResource graphicsMapResource =
                    resourceItem.CreateResource() as ESRI.ArcGIS.ADF.Web.DataSources.Graphics.MapResource;

                // Create a graphics dataset to hold FindNearTask's GraphicsLayers
                graphicsMapResource.Graphics = new ESRI.ArcGIS.ADF.Web.Display.Graphics.GraphicsDataSet();
                graphicsMapResource.Graphics.DataSetName = "FindNearTask Input Graphics";

                // Create the user input GraphicsLayer and add it to the dataset
                ESRI.ArcGIS.ADF.Web.Display.Graphics.ElementGraphicsLayer elementGraphicsLayer =
                    new ESRI.ArcGIS.ADF.Web.Display.Graphics.ElementGraphicsLayer();
                elementGraphicsLayer.TableName = "User Input Graphics Layer";
                graphicsMapResource.Graphics.Tables.Add(elementGraphicsLayer);

                // Create the buffer GraphicsLayer and add it to the dataset
                elementGraphicsLayer = new ESRI.ArcGIS.ADF.Web.Display.Graphics.ElementGraphicsLayer();
                elementGraphicsLayer.TableName = "Buffer Graphics Layer";
                graphicsMapResource.Graphics.Tables.Add(elementGraphicsLayer);

                // Initialize the resource and functionalities referenced by the resource item
                if (mapInstance.MapResourceManagerInstance.Initialized)
                    resourceItem.InitializeResource();
                if (mapInstance.InitializedFunctionalities)
                    mapInstance.InitializeFunctionality(resourceItem);
            }
        }

        // Retrieves the GraphicsLayer having the passed in name
        private ESRI.ArcGIS.ADF.Web.Display.Graphics.ElementGraphicsLayer GetLayer(
            ESRI.ArcGIS.ADF.Web.UI.WebControls.MapResourceManager mapResourceManager, string layerName)
        {
            ESRI.ArcGIS.ADF.Web.Display.Graphics.ElementGraphicsLayer elementGraphicsLayer = null;

            // Get the resource item corresponding to the ResourceName property and make sure it's 
            // initialized
            ESRI.ArcGIS.ADF.Web.UI.WebControls.MapResourceItem resourceItem =
                mapResourceManager.ResourceItems.Find(this.ResourceName);
            if (!resourceItem.Resource.Initialized)
                resourceItem.InitializeResource();

            // Get the graphics resource underlying the resource item
            ESRI.ArcGIS.ADF.Web.DataSources.Graphics.MapResource graphicsMapResource =
                resourceItem.Resource as ESRI.ArcGIS.ADF.Web.DataSources.Graphics.MapResource;

            // Make sure the graphics resource contains the requested layer
            if (graphicsMapResource != null && graphicsMapResource.Graphics != null &&
                graphicsMapResource.Graphics.Tables[layerName] != null)
            {
                // Get the GraphicsLayer
                elementGraphicsLayer = graphicsMapResource.Graphics.Tables[layerName] as
                    ESRI.ArcGIS.ADF.Web.Display.Graphics.ElementGraphicsLayer;
            }

            return elementGraphicsLayer;
        }

        #endregion

    }
}