Common Custom data source
Common_CustomDataSource_CSharp\TiledMapDataSource_CSharp\MapFunctionality.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.Collections.Generic;
using System.Text;
using System.Web.UI;
using ESRI.ArcGIS.ADF.Web.Geometry;
using ESRI.ArcGIS.ADF.Web.SpatialReference;
using ESRI.ArcGIS.ADF.Web.DataSources;
using System.Collections;

using ESRI.ArcGIS.ADF.Web.Display.Graphics;
using ESRI.ArcGIS.ADF.Web.Display.Drawing;
using ESRI.ArcGIS.ADF.Web;

namespace TiledMapDataSource_CSharp
{
    public class MapFunctionality : IMapFunctionality
    {
        #region Constructor

        public MapFunctionality(string name, MapResource resource)
        {
            this.name = name;
            this.resource = resource;
        }

        public MapResource MapResource
        {
            get
            {
                return resource as MapResource;
            }
        }

        #endregion

        #region IMapFunctionality implementation

        #region Private Member Vars
        private ESRI.ArcGIS.ADF.Web.DisplaySettings displaySettings = null;
        private System.Web.UI.WebControls.WebControl webControl = null;
        private bool maintainsState = false;
        private ESRI.ArcGIS.ADF.Web.SpatialReference.SpatialReference spatialReference = null;
        private Dictionary<string, bool> layerVisibility = null;
        #endregion

        #region Public Properties

        public bool MaintainsState
        {
            get
            {
                return maintainsState;
            }
            set
            {
                maintainsState = value;
            }
        }

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

        public ESRI.ArcGIS.ADF.Web.DataSources.Units Units
        {
            get { throw new NotImplementedException(); }
        }

        public ESRI.ArcGIS.ADF.Web.DisplaySettings DisplaySettings
        {
            get
            {
                if (maintainsState)
                {
                    if (displaySettings == null)
                    {
                        displaySettings = (DisplaySettings)MapResource.DisplaySettings.Clone();
                    }
                    return displaySettings;
                }
                else return MapResource.DisplaySettings;
            }
            set
            {
                if (maintainsState) displaySettings = value;
                else MapResource.DisplaySettings = value;
            }
        }

        public object[] LayerIDs
        {
            get
            {
                object[] ids = new object[LayerVisibility.Count];
                int i = 0;
                foreach (KeyValuePair<string, bool> kvp in LayerVisibility)
                {
                    ids[i] = kvp.Key.ToString();
                    ++i;
                }
                return ids;
            }
        }

        Envelope extent;
        public Envelope Extent
        {
            get
            {
                return extent;
            }
            set
            {
                extent = value;
            }
        }

        public ESRI.ArcGIS.ADF.Web.SpatialReference.SpatialReference SpatialReference
        {
            get
            {
                return spatialReference;
            }
            set
            {
                spatialReference = value;
            }
        }

        public double GetScale()
        {
            return double.NaN;
        }

        public double Rotation
        {
            get
            {
                return double.NaN;
            }
        }
        #endregion

        #region Public Methods

        public void ApplyStateToDataSourceObjects()
        {
            throw new NotImplementedException();
        }

        public void GetStateFromDataSourceObjects()
        {
            throw new NotImplementedException();
        }

        public void GetLayers(out string[] layerids, out string[] layernames)
        {
            throw new NotImplementedException();
        }

        public void GetVisibleScale(string layerid, out double minscale, out double maxscale)
        {
            throw new NotImplementedException();
        }

        public double GetScale(ESRI.ArcGIS.ADF.Web.Geometry.Envelope extent, int mapWidth, int mapHeight)
        {
            throw new NotImplementedException();
        }

        public System.Collections.Generic.Dictionary<string, string> GetCopyrightText()
        {
            throw new NotImplementedException();
        }

        public ESRI.ArcGIS.ADF.Web.MapImage DrawExtent(ESRI.ArcGIS.ADF.Web.Geometry.Envelope extentToDraw)
        {
            throw new NotImplementedException();
        }



        public Dictionary<string, bool> LayerVisibility
        {
            get
            {
                if (maintainsState)
                    return MapResource.layerVisibility;
                else
                    return layerVisibility;
            }
        }

        public bool GetLayerVisibility(string layerID)
        {
            return LayerVisibility[layerID];
        }

        public void SetLayerVisibility(string layerID, bool visible)
        {
            LayerVisibility[layerID] = visible;
        }

        #endregion

        #endregion

        #region IGISFunctionality implementation

        private string name = string.Empty;
        private IGISResource resource = null;
        bool initialized = false;

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

        public IGISResource Resource
        {
            get { return resource; }
            set { resource = value; }
        }

        public bool Initialized
        {
            get { return initialized; }
        }

        public void LoadState()
        {
            if (resource.DataSource == null) return;
            if (resource.DataSource.State == null) return;
            object o = resource.DataSource.State[key];
            if (o != null)
            {
                MapFunctionality mf = o as MapFunctionality;
                layerVisibility = mf.layerVisibility;
            }
        }

        public void Initialize()
        {
            initialized = true;
            if (layerVisibility == null)
            {
                layerVisibility = new Dictionary<string, bool>();
                MapResource mapRes = resource as MapResource;
                foreach (KeyValuePair<string, bool> kvp in mapRes.layerVisibility)
                    layerVisibility.Add(kvp.Key, kvp.Value);
                
            }
        }

        public void SaveState()
        {
            if (resource.DataSource == null) return;
            if (resource.DataSource.State == null) return;
            resource.DataSource.State[key] = this;
        }

        public void Dispose()
        {
            initialized = false;
        }

        public bool Supports(string operation)
        {
            return false;
        }

        #endregion

        #region Private Properties

        private string key
        {
            get
            {
                string szResource = resource.GetType().ToString() + ":" + resource.Name;
                string szThis = this.GetType().ToString() + ":" + name;
                return (szResource + "," + szThis);
            }
        }

        #endregion

    }
}