Common Custom data source
Common_CustomDataSource_CSharp\REXMLDataSource_CSharp\MapTocFunctionality.cs
// Copyright 2011 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 REXMLDataSource_CSharp
{
    // Along with IGISDataSource and IGISResource, IGISFunctionality is one of the three required
    // interfaces for any Web ADF Data Source implementation.  This interface is responsible for
    // providing members to functionally interact with the underlying data.  Essentially, an 
    // IGISFunctionality implementation can be thought of as describing what can be done with the 
    // data.
    //
    // This particular implementation inherits from IMapTocFunctionality, which implements 
    // IGISFunctionality.  IMapTocFunctionality provides methods and properties that allow
    // interaction with the data source via a Web ADF Toc Control .
    public class MapTocFunctionality : ESRI.ArcGIS.ADF.Web.DataSources.IMapTocFunctionality
    {
        #region Instance Variable Declarations

        private string m_name = string.Empty;
        private ESRI.ArcGIS.ADF.Web.DataSources.IGISResource m_gisResource = null;
        private bool m_initialized = false;
        private REXMLDataSource_CSharp.MapResource m_mapResource;
        [System.NonSerialized]
        System.Web.UI.WebControls.WebControl m_webControl;

        #endregion

        #region Constructor

        // Constructor requiring specification of the functionality name and the MapResource with
        // which to associate the functionality
        public MapTocFunctionality(string name, REXMLDataSource_CSharp.MapResource resource)
        {
            // Initialize instance variables with the passed-in parameters
            m_name = name;
            m_gisResource = resource;
            m_mapResource = resource;
        }

        #endregion

        #region REXML MapTocFunctionality Members

        // Allows for convenient retrieval of a REXML MapFunctionality object by name
        private REXMLDataSource_CSharp.MapFunctionality GetMapFunctionality(string name)
        {
            REXMLDataSource_CSharp.MapFunctionality mapFunctionality =
                m_gisResource.Functionalities.Find(name) as 
                REXMLDataSource_CSharp.MapFunctionality;
            return mapFunctionality;
        }

        // Sets visibility of the layer with the passed-in ID contained in the map functionality with
        // the passed-in name
        public void SetLayerVisibility(string mapFunctionalityName, object layerID, bool visible)
        {
            // Get a reference to the REXML map functionality with the passed-in functionality name
            REXMLDataSource_CSharp.MapFunctionality rexmlMapFunctionality =
                GetMapFunctionality(mapFunctionalityName);

            // Make sure the functionality was found
            if (rexmlMapFunctionality == null)
                throw new System.ArgumentException("A map functionality with the specified name was not found.");

            // Set the visibility of the layer with the passed-in ID according to the passed in boolean
            rexmlMapFunctionality.SetLayerVisibility(layerID.ToString(), visible);
        }

        #endregion

        #region IMapTocFunctionality Members

        public ESRI.ArcGIS.ADF.Web.TocDataFrame[] GetMapContents(string mapFunctionalityName, 
            ESRI.ArcGIS.ADF.Web.WebImageFormat format, bool useMimeData, bool showAllDataFrames)
        {
            ESRI.ArcGIS.ADF.Web.TocDataFrame[] tocDataFrames = new ESRI.ArcGIS.ADF.Web.TocDataFrame[1];
            tocDataFrames[0] = new ESRI.ArcGIS.ADF.Web.TocDataFrame(m_gisResource.Name);
            System.Web.SessionState.HttpSessionState session = null;
            try
            {
                System.Web.HttpContext httpContext = System.Web.HttpContext.Current;
                if (httpContext != null)
                    session = httpContext.Session;
            }
            catch { }
            ESRI.ArcGIS.ADF.Web.Display.Swatch.SwatchInfo swatchInfo = 
                new ESRI.ArcGIS.ADF.Web.Display.Swatch.SwatchInfo(10, 10, session);
            foreach (System.Data.DataTable table in m_mapResource.Graphics.Tables)
            {
                tocDataFrames[0].Add(ESRI.ArcGIS.ADF.Web.Display.Graphics.GraphicsLayer.GetTocLayer(
                    table as ESRI.ArcGIS.ADF.Web.Display.Graphics.GraphicsLayer, swatchInfo));
            }
            return tocDataFrames;
        }
        
        #endregion

        #region IGISFunctionality Members

        public System.Web.UI.WebControls.WebControl WebControl
        {
            get { return m_webControl; }
            set { m_webControl = value; }
        }

        public string Name
        {
            get { return m_name; }
            set { m_name = value; }
        }

        public ESRI.ArcGIS.ADF.Web.DataSources.IGISResource Resource
        {
            get { return m_gisResource; }
            set { m_gisResource = value; }
        }

        public bool Initialized
        {
            get { return m_initialized; }
        }
        
        public void LoadState() { }

        public void Initialize()
        {
            m_initialized = true;
        }

        public void SaveState() { }

        // Set the flag indicating whether the functionality is intitialized to false.  Any necessary
        // disposal logic (e.g. releasing object references) should go here.  Note that, if there is
        // additional logic here, users of this class will have to EXPLCITLY call dispose.  It is not
        // invoked by other Web ADF components or the Page life-cycle.        
        public void Dispose()
        {
            m_initialized = false;
        }

        public bool Supports(string operation)
        {
            return true;
        }
        #endregion
    }
}