About the Dynamic cache layer manager controller Sample
[C#]
CacheManagerDlg.cs
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using ESRI.ArcGIS.Controls; using ESRI.ArcGIS.Carto; using ESRI.ArcGIS.Display; using ESRI.ArcGIS.esriSystem; using ESRI.ArcGIS.SystemUI; namespace DynamicCacheLayerManagerController { public partial class CacheManagerDlg : Form, IDisposable { #region dialog class members public struct LayerCacheInfo { public string name; public string folderName; public string folderPath; public string format; public bool alwaysDrawCoarsestLevel; public bool strictOnDemand; public int progressiveDrawingLevels; public int progressiveFetchingLevels; public double detailsThreshold; public double maxCacheScale; // override ToString method in order to show only the layer name in the combo itself public override string ToString() { return name; } } private IHookHelper m_hookHelper = null; private Dictionary<string, LayerCacheInfo> m_layerCacheInfos = new Dictionary<string, LayerCacheInfo>(); #endregion #region dialog constructor public CacheManagerDlg(IHookHelper hookHelper) { m_hookHelper = hookHelper; InitializeComponent(); } #endregion #region private methods private void CacheManagerDlg_Load(object sender, EventArgs e) { InitializeForm(); } private void cboLayerNames_SelectedIndexChanged(object sender, EventArgs e) { // get the layers from the map if (m_hookHelper.FocusMap.LayerCount == 0) return; IMap map = m_hookHelper.FocusMap; IDynamicCacheLayerManager dynamicCacheLayerManager = new DynamicCacheLayerManagerClass(); // get all of the non-dynamic layers ILayer layer; for (int i = 0; i < map.LayerCount; i++) { layer = map.get_Layer(i); if (layer is IDynamicLayer) continue; dynamicCacheLayerManager.Init(map, layer); LayerCacheInfo layerInfo = (LayerCacheInfo)cboLayerNames.SelectedItem; if (dynamicCacheLayerManager.FolderName == layerInfo.folderName) { // populate the form lblCacheFolderName.Text = dynamicCacheLayerManager.FolderName; lblCacheFolderPath.Text = dynamicCacheLayerManager.FolderPath; if (dynamicCacheLayerManager.Format.ToUpper().IndexOf("PNG") > -1) rdoPNG.Checked = true; else rdoJPEG.Checked = true; chkAlwaysDrawCoarsestLevel.Checked = dynamicCacheLayerManager.AlwaysDrawCoarsestLevel; numDetaildThreshold.Value = Convert.ToDecimal(dynamicCacheLayerManager.DetailsThreshold); chkStrictOnDemandMode.Checked = dynamicCacheLayerManager.StrictOnDemandMode; numProgressiveDrawingLevels.Value = dynamicCacheLayerManager.ProgressiveDrawingLevels; numProgressiveFetchingLevels.Value = dynamicCacheLayerManager.ProgressiveFetchingLevels; numMaxCacheScale.Value = Convert.ToDecimal(dynamicCacheLayerManager.MaxCacheScale); return; } } } private void btnFolderPath_Click(object sender, EventArgs e) { FolderBrowserDialog dlg = new FolderBrowserDialog(); dlg.ShowNewFolderButton = true; dlg.Description = "Cache Folder"; if (cboLayerNames.SelectedIndex != -1) { LayerCacheInfo layerInfo = (LayerCacheInfo)cboLayerNames.SelectedItem; dlg.SelectedPath = layerInfo.folderPath; } if (DialogResult.OK == dlg.ShowDialog()) { if (System.IO.Directory.Exists(dlg.SelectedPath)) lblCacheFolderPath.Text = dlg.SelectedPath; } } private void btnRestoreDefaults_Click(object sender, EventArgs e) { // get the selected item if (cboLayerNames.SelectedIndex == -1) return; LayerCacheInfo layerInfo = (LayerCacheInfo)cboLayerNames.SelectedItem; if (m_layerCacheInfos.ContainsKey(layerInfo.folderName)) { layerInfo = m_layerCacheInfos[layerInfo.folderName]; // populate the form lblCacheFolderName.Text = layerInfo.folderName; lblCacheFolderPath.Text = layerInfo.folderPath; if (layerInfo.format.ToUpper().IndexOf("PNG") > -1) rdoPNG.Checked = true; else rdoJPEG.Checked = true; chkAlwaysDrawCoarsestLevel.Checked = layerInfo.alwaysDrawCoarsestLevel; chkStrictOnDemandMode.Checked = layerInfo.strictOnDemand; numDetaildThreshold.Value = Convert.ToDecimal(layerInfo.detailsThreshold); numProgressiveDrawingLevels.Value = layerInfo.progressiveDrawingLevels; numProgressiveFetchingLevels.Value = layerInfo.progressiveFetchingLevels; numMaxCacheScale.Value = Convert.ToDecimal(layerInfo.maxCacheScale); } } private void btnDismiss_Click(object sender, EventArgs e) { Close(); } private void btnApply_Click(object sender, EventArgs e) { UpdateLayer(); } private void btnOK_Click(object sender, EventArgs e) { UpdateLayer(); Close(); } private void GetCacheDefaultsProps() { m_layerCacheInfos.Clear(); if (m_hookHelper.FocusMap.LayerCount == 0) return; IMap map = m_hookHelper.FocusMap; IDynamicCacheLayerManager dynamicCacheLayerManager = new DynamicCacheLayerManagerClass(); // get all of the non-dynamic layers ILayer layer; for (int i = 0; i < map.LayerCount; i++) { layer = map.get_Layer(i); if (layer is IDynamicLayer) continue; dynamicCacheLayerManager.Init(map, layer); LayerCacheInfo layerCacheInfo; layerCacheInfo.name = layer.Name; layerCacheInfo.folderName = dynamicCacheLayerManager.FolderName; layerCacheInfo.folderPath = dynamicCacheLayerManager.FolderPath; layerCacheInfo.format = dynamicCacheLayerManager.Format; layerCacheInfo.detailsThreshold = dynamicCacheLayerManager.DetailsThreshold; layerCacheInfo.alwaysDrawCoarsestLevel = dynamicCacheLayerManager.AlwaysDrawCoarsestLevel; layerCacheInfo.progressiveDrawingLevels = dynamicCacheLayerManager.ProgressiveDrawingLevels; layerCacheInfo.progressiveFetchingLevels = dynamicCacheLayerManager.ProgressiveFetchingLevels; layerCacheInfo.strictOnDemand = dynamicCacheLayerManager.StrictOnDemandMode; layerCacheInfo.maxCacheScale = dynamicCacheLayerManager.MaxCacheScale; m_layerCacheInfos.Add(layerCacheInfo.folderName, layerCacheInfo); } } private void InitializeForm() { GetCacheDefaultsProps(); if (m_layerCacheInfos.Count == 0) { // clear the form cboLayerNames.Items.Clear(); cboLayerNames.SelectedIndex = -1; lblCacheFolderPath.Text = string.Empty; btnFolderPath.Enabled = false; numProgressiveDrawingLevels.Value = 0; numProgressiveFetchingLevels.Value = 0; numMaxCacheScale.Value = 0; groupDrawingProps.Enabled = false; btnOK.Enabled = false; btnApply.Enabled = false; return; } groupDrawingProps.Enabled = true; btnFolderPath.Enabled = true; btnOK.Enabled = true; btnApply.Enabled = true; string selectedLayerName = string.Empty; LayerCacheInfo layerInfo; layerInfo.name = string.Empty; layerInfo.folderPath = string.Empty; layerInfo.folderName = string.Empty; layerInfo.format = string.Empty; layerInfo.alwaysDrawCoarsestLevel = false; layerInfo.detailsThreshold = 0; layerInfo.strictOnDemand = false; layerInfo.progressiveDrawingLevels = 0; layerInfo.progressiveFetchingLevels = 0; layerInfo.maxCacheScale = 0; int selectedIndex = cboLayerNames.SelectedIndex; if (selectedIndex != -1) { selectedLayerName = ((LayerCacheInfo)cboLayerNames.SelectedItem).folderName; if (m_layerCacheInfos.ContainsKey(selectedLayerName)) layerInfo = m_layerCacheInfos[selectedLayerName]; } // populate the combo cboLayerNames.Items.Clear(); int index = 0; bool bFirst = true; foreach (KeyValuePair<string, LayerCacheInfo> layerCacheInfoPair in m_layerCacheInfos) { cboLayerNames.Items.Add(layerCacheInfoPair.Value); if (bFirst && layerInfo.name == string.Empty) { layerInfo = layerCacheInfoPair.Value; cboLayerNames.SelectedIndex = 0; bFirst = false; } if (selectedLayerName == layerCacheInfoPair.Key) { // make it the selected item cboLayerNames.SelectedIndex = index; } index++; } // populate the form lblCacheFolderName.Text = layerInfo.folderName; lblCacheFolderPath.Text = layerInfo.folderPath; if (layerInfo.format.ToUpper().IndexOf("PNG") > -1) rdoPNG.Checked = true; else rdoJPEG.Checked = true; chkAlwaysDrawCoarsestLevel.Checked = layerInfo.alwaysDrawCoarsestLevel; chkStrictOnDemandMode.Checked = layerInfo.strictOnDemand; numDetaildThreshold.Value = Convert.ToDecimal(layerInfo.detailsThreshold); numProgressiveDrawingLevels.Value = layerInfo.progressiveDrawingLevels; numProgressiveFetchingLevels.Value = layerInfo.progressiveFetchingLevels; numMaxCacheScale.Value = Convert.ToDecimal(layerInfo.maxCacheScale); } private void UpdateLayer() { // get the selected layer from the map if (m_hookHelper.FocusMap.LayerCount == 0) return; if (cboLayerNames.SelectedIndex == -1) return; IMap map = m_hookHelper.FocusMap; IDynamicCacheLayerManager dynamicCacheLayerManager = new DynamicCacheLayerManagerClass(); // get all of the non-dynamic layers ILayer layer; for (int i = 0; i < map.LayerCount; i++) { layer = map.get_Layer(i); if (layer is IDynamicLayer) continue; dynamicCacheLayerManager.Init(map, layer); LayerCacheInfo layerInfo = (LayerCacheInfo)cboLayerNames.SelectedItem; if (dynamicCacheLayerManager.FolderName == layerInfo.folderName) { dynamicCacheLayerManager.FolderPath = lblCacheFolderPath.Text; dynamicCacheLayerManager.Format = (rdoPNG.Checked ? "PNG32" : "JPEG32"); dynamicCacheLayerManager.StrictOnDemandMode = chkStrictOnDemandMode.Checked; dynamicCacheLayerManager.AlwaysDrawCoarsestLevel = chkAlwaysDrawCoarsestLevel.Checked; dynamicCacheLayerManager.DetailsThreshold = Convert.ToDouble(numDetaildThreshold.Value); dynamicCacheLayerManager.ProgressiveDrawingLevels = Convert.ToInt32(numProgressiveDrawingLevels.Value); dynamicCacheLayerManager.ProgressiveFetchingLevels = Convert.ToInt32(numProgressiveFetchingLevels.Value); dynamicCacheLayerManager.MaxCacheScale = Convert.ToDouble(numMaxCacheScale.Value); } } } #endregion } }
[Visual Basic .NET]
CacheManagerDlg.vb
Imports Microsoft.VisualBasic Imports System Imports System.Collections.Generic Imports System.ComponentModel Imports System.Data Imports System.Drawing Imports System.Text Imports System.Windows.Forms Imports ESRI.ArcGIS.Controls Imports ESRI.ArcGIS.Carto Imports ESRI.ArcGIS.Display Imports ESRI.ArcGIS.esriSystem Imports ESRI.ArcGIS.SystemUI Public Partial Class CacheManagerDlg : Inherits Form : Implements IDisposable #Region "dialog class members" Public Structure LayerCacheInfo Public name As String Public folderName As String Public folderPath As String Public format As String Public alwaysDrawCoarsestLevel As Boolean Public strictOnDemand As Boolean Public progressiveDrawingLevels As Integer Public progressiveFetchingLevels As Integer Public detailsThreshold As Double Public maxCacheScale As Double ' override ToString method in order to show only the layer name in the combo itself Public Overrides Function ToString() As String Return name End Function End Structure Private m_hookHelper As IHookHelper = Nothing Private m_layerCacheInfos As Dictionary(Of String, LayerCacheInfo) = New Dictionary(Of String, LayerCacheInfo)() #End Region #Region "dialog constructor" Public Sub New(ByVal hookHelper As IHookHelper) m_hookHelper = hookHelper InitializeComponent() End Sub #End Region #Region "private methods" Private Sub CacheManagerDlg_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load InitializeForm() End Sub Private Sub cboLayerNames_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles cboLayerNames.SelectedIndexChanged ' get the layers from the map If m_hookHelper.FocusMap.LayerCount = 0 Then Return End If Dim map As IMap = m_hookHelper.FocusMap Dim dynamicCacheLayerManager As IDynamicCacheLayerManager = New DynamicCacheLayerManagerClass() ' get all of the non-dynamic layers Dim layer As ILayer Dim i As Integer = 0 Do While i < map.LayerCount layer = map.Layer(i) If TypeOf layer Is IDynamicLayer Then Continue Do End If dynamicCacheLayerManager.Init(map, layer) Dim layerInfo As LayerCacheInfo = CType(cboLayerNames.SelectedItem, LayerCacheInfo) If dynamicCacheLayerManager.FolderName = layerInfo.folderName Then ' populate the form lblCacheFolderName.Text = dynamicCacheLayerManager.FolderName lblCacheFolderPath.Text = dynamicCacheLayerManager.FolderPath If dynamicCacheLayerManager.Format.ToUpper().IndexOf("PNG") > -1 Then rdoPNG.Checked = True Else rdoJPEG.Checked = True End If chkAlwaysDrawCoarsestLevel.Checked = dynamicCacheLayerManager.AlwaysDrawCoarsestLevel numDetaildThreshold.Value = Convert.ToDecimal(dynamicCacheLayerManager.DetailsThreshold) chkStrictOnDemandMode.Checked = dynamicCacheLayerManager.StrictOnDemandMode numProgressiveDrawingLevels.Value = dynamicCacheLayerManager.ProgressiveDrawingLevels numProgressiveFetchingLevels.Value = dynamicCacheLayerManager.ProgressiveFetchingLevels numMaxCacheScale.Value = Convert.ToDecimal(dynamicCacheLayerManager.MaxCacheScale) Return End If i += 1 Loop End Sub Private Sub btnFolderPath_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnFolderPath.Click Dim dlg As FolderBrowserDialog = New FolderBrowserDialog() dlg.ShowNewFolderButton = True dlg.Description = "Cache Folder" If cboLayerNames.SelectedIndex <> -1 Then Dim layerInfo As LayerCacheInfo = CType(cboLayerNames.SelectedItem, LayerCacheInfo) dlg.SelectedPath = layerInfo.folderPath End If If System.Windows.Forms.DialogResult.OK = dlg.ShowDialog() Then If System.IO.Directory.Exists(dlg.SelectedPath) Then lblCacheFolderPath.Text = dlg.SelectedPath End If End If End Sub Private Sub btnRestoreDefaults_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnRestoreDefaults.Click ' get the selected item If cboLayerNames.SelectedIndex = -1 Then Return End If Dim layerInfo As LayerCacheInfo = CType(cboLayerNames.SelectedItem, LayerCacheInfo) If m_layerCacheInfos.ContainsKey(layerInfo.folderName) Then layerInfo = m_layerCacheInfos(layerInfo.folderName) ' populate the form lblCacheFolderName.Text = layerInfo.folderName lblCacheFolderPath.Text = layerInfo.folderPath If layerInfo.format.ToUpper().IndexOf("PNG") > -1 Then rdoPNG.Checked = True Else rdoJPEG.Checked = True End If chkAlwaysDrawCoarsestLevel.Checked = layerInfo.alwaysDrawCoarsestLevel chkStrictOnDemandMode.Checked = layerInfo.strictOnDemand numDetaildThreshold.Value = Convert.ToDecimal(layerInfo.detailsThreshold) numProgressiveDrawingLevels.Value = layerInfo.progressiveDrawingLevels numProgressiveFetchingLevels.Value = layerInfo.progressiveFetchingLevels numMaxCacheScale.Value = Convert.ToDecimal(layerInfo.maxCacheScale) End If End Sub Private Sub btnDismiss_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnDismiss.Click Close() End Sub Private Sub btnApply_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnApply.Click UpdateLayer() End Sub Private Sub btnOK_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnOK.Click UpdateLayer() Close() End Sub Private Sub GetCacheDefaultsProps() m_layerCacheInfos.Clear() If m_hookHelper.FocusMap.LayerCount = 0 Then Return End If Dim map As IMap = m_hookHelper.FocusMap Dim dynamicCacheLayerManager As IDynamicCacheLayerManager = New DynamicCacheLayerManagerClass() ' get all of the non-dynamic layers Dim layer As ILayer Dim i As Integer = 0 Do While i < map.LayerCount layer = map.Layer(i) If TypeOf layer Is IDynamicLayer Then Continue Do End If dynamicCacheLayerManager.Init(map, layer) Dim layerCacheInfo As LayerCacheInfo layerCacheInfo.name = layer.Name layerCacheInfo.folderName = dynamicCacheLayerManager.FolderName layerCacheInfo.folderPath = dynamicCacheLayerManager.FolderPath layerCacheInfo.format = dynamicCacheLayerManager.Format layerCacheInfo.detailsThreshold = dynamicCacheLayerManager.DetailsThreshold layerCacheInfo.alwaysDrawCoarsestLevel = dynamicCacheLayerManager.AlwaysDrawCoarsestLevel layerCacheInfo.progressiveDrawingLevels = dynamicCacheLayerManager.ProgressiveDrawingLevels layerCacheInfo.progressiveFetchingLevels = dynamicCacheLayerManager.ProgressiveFetchingLevels layerCacheInfo.strictOnDemand = dynamicCacheLayerManager.StrictOnDemandMode layerCacheInfo.maxCacheScale = dynamicCacheLayerManager.MaxCacheScale m_layerCacheInfos.Add(layerCacheInfo.folderName, layerCacheInfo) i += 1 Loop End Sub Private Sub InitializeForm() GetCacheDefaultsProps() If m_layerCacheInfos.Count = 0 Then ' clear the form cboLayerNames.Items.Clear() cboLayerNames.SelectedIndex = -1 lblCacheFolderPath.Text = String.Empty btnFolderPath.Enabled = False numProgressiveDrawingLevels.Value = 0 numProgressiveFetchingLevels.Value = 0 numMaxCacheScale.Value = 0 groupDrawingProps.Enabled = False btnOK.Enabled = False btnApply.Enabled = False Return End If groupDrawingProps.Enabled = True btnFolderPath.Enabled = True btnOK.Enabled = True btnApply.Enabled = True Dim selectedLayerName As String = String.Empty Dim layerInfo As LayerCacheInfo layerInfo.name = String.Empty layerInfo.folderPath = String.Empty layerInfo.folderName = String.Empty layerInfo.format = String.Empty layerInfo.alwaysDrawCoarsestLevel = False layerInfo.detailsThreshold = 0 layerInfo.strictOnDemand = False layerInfo.progressiveDrawingLevels = 0 layerInfo.progressiveFetchingLevels = 0 layerInfo.maxCacheScale = 0 Dim selectedIndex As Integer = cboLayerNames.SelectedIndex If selectedIndex <> -1 Then selectedLayerName = (CType(cboLayerNames.SelectedItem, LayerCacheInfo)).folderName If m_layerCacheInfos.ContainsKey(selectedLayerName) Then layerInfo = m_layerCacheInfos(selectedLayerName) End If End If ' populate the combo cboLayerNames.Items.Clear() Dim index As Integer = 0 Dim bFirst As Boolean = True For Each layerCacheInfoPair As KeyValuePair(Of String, LayerCacheInfo) In m_layerCacheInfos cboLayerNames.Items.Add(layerCacheInfoPair.Value) If bFirst AndAlso layerInfo.name = String.Empty Then layerInfo = layerCacheInfoPair.Value cboLayerNames.SelectedIndex = 0 bFirst = False End If If selectedLayerName = layerCacheInfoPair.Key Then ' make it the selected item cboLayerNames.SelectedIndex = index End If index += 1 Next layerCacheInfoPair ' populate the form lblCacheFolderName.Text = layerInfo.folderName lblCacheFolderPath.Text = layerInfo.folderPath If layerInfo.format.ToUpper().IndexOf("PNG") > -1 Then rdoPNG.Checked = True Else rdoJPEG.Checked = True End If chkAlwaysDrawCoarsestLevel.Checked = layerInfo.alwaysDrawCoarsestLevel chkStrictOnDemandMode.Checked = layerInfo.strictOnDemand numDetaildThreshold.Value = Convert.ToDecimal(layerInfo.detailsThreshold) numProgressiveDrawingLevels.Value = layerInfo.progressiveDrawingLevels numProgressiveFetchingLevels.Value = layerInfo.progressiveFetchingLevels numMaxCacheScale.Value = Convert.ToDecimal(layerInfo.maxCacheScale) End Sub Private Sub UpdateLayer() ' get the selected layer from the map If m_hookHelper.FocusMap.LayerCount = 0 Then Return End If If cboLayerNames.SelectedIndex = -1 Then Return End If Dim map As IMap = m_hookHelper.FocusMap Dim dynamicCacheLayerManager As IDynamicCacheLayerManager = New DynamicCacheLayerManagerClass() ' get all of the non-dynamic layers Dim layer As ILayer Dim i As Integer = 0 Do While i < map.LayerCount layer = map.Layer(i) If TypeOf layer Is IDynamicLayer Then Continue Do End If dynamicCacheLayerManager.Init(map, layer) Dim layerInfo As LayerCacheInfo = CType(cboLayerNames.SelectedItem, LayerCacheInfo) If dynamicCacheLayerManager.FolderName = layerInfo.folderName Then dynamicCacheLayerManager.FolderPath = lblCacheFolderPath.Text If rdoPNG.Checked Then dynamicCacheLayerManager.Format = ("PNG32") Else dynamicCacheLayerManager.Format = ("JPEG32") End If dynamicCacheLayerManager.StrictOnDemandMode = chkStrictOnDemandMode.Checked dynamicCacheLayerManager.AlwaysDrawCoarsestLevel = chkAlwaysDrawCoarsestLevel.Checked dynamicCacheLayerManager.DetailsThreshold = Convert.ToDouble(numDetaildThreshold.Value) dynamicCacheLayerManager.ProgressiveDrawingLevels = Convert.ToInt32(numProgressiveDrawingLevels.Value) dynamicCacheLayerManager.ProgressiveFetchingLevels = Convert.ToInt32(numProgressiveFetchingLevels.Value) dynamicCacheLayerManager.MaxCacheScale = Convert.ToDouble(numMaxCacheScale.Value) End If i += 1 Loop End Sub #End Region End Class