basechildnodecollection.cs

来自「浏览器端看到树型目录结构,用户可以完整地看到像windows资源管理器一样的效果」· CS 代码 · 共 487 行 · 第 1/2 页

CS
487
字号
//------------------------------------------------------------------------------
// Copyright (c) 2000-2003 Microsoft Corporation. All Rights Reserved.
//------------------------------------------------------------------------------

namespace Microsoft.Web.UI.WebControls
{
    using System;
    using System.Collections;
    using System.Reflection;
    using System.Web.UI;
    using System.ComponentModel;
    using System.Drawing.Design;

    /// <summary>
    /// Base class for collections of BaseChildNode objects.
    /// </summary>
    [Editor(typeof(Microsoft.Web.UI.WebControls.Design.ItemCollectionEditor), typeof(UITypeEditor))]
    public abstract class BaseChildNodeCollection : CollectionBase, ICloneable, IStateManager
    {
        private bool _Tracking = false;
        private bool _Reloading = false;
        private ArrayList _Actions = new ArrayList(4);

        /// <summary>
        /// Creates a new deep copy of the current collection.
        /// </summary>
        /// <returns>A new object that is a deep copy of this instance.</returns>
        public virtual object Clone()
        {
            BaseChildNodeCollection copy = (BaseChildNodeCollection)Activator.CreateInstance(this.GetType(), BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.CreateInstance, null, null, null);

            foreach (BaseChildNode node in List)
            {
                copy.List.Add(node.Clone());
            }

            copy._Tracking = this._Tracking;
            copy._Reloading = this._Reloading;
            copy._Actions.Clear();
            foreach (Action action in this._Actions)
            {
                copy._Actions.Add(action.Clone());
            }

            return copy;
        }

        /// <summary>
        /// List of actions made to this collection. Used for state tracking.
        /// </summary>
        private ArrayList Actions
        {
            get { return _Actions; }
        }

        /// <summary>
        /// Type of action made to this collection.
        /// </summary>
        private enum ActionType { Clear, Insert, Remove };

        /// <summary>
        /// Stores information about an action made to this collection
        /// </summary>
        private class Action
        {
            /// <summary>
            /// The type of action that this object represents (Clear, Insert, or Remove)
            /// </summary>
            public ActionType ActionType;

            /// <summary>
            /// The index at which this action occurred.
            /// </summary>
            public int Index;

            private string nodeType = String.Empty;
            private int nodeTypeIndex = -1;
            private static string[] knownTypes =
            {
                typeof(Microsoft.Web.UI.WebControls.Tab).AssemblyQualifiedName,
                typeof(Microsoft.Web.UI.WebControls.TabSeparator).AssemblyQualifiedName,
                typeof(Microsoft.Web.UI.WebControls.TreeNode).AssemblyQualifiedName,
                typeof(Microsoft.Web.UI.WebControls.TreeNodeType).AssemblyQualifiedName,
                typeof(Microsoft.Web.UI.WebControls.ToolbarLabel).AssemblyQualifiedName,
                typeof(Microsoft.Web.UI.WebControls.ToolbarButton).AssemblyQualifiedName,
                typeof(Microsoft.Web.UI.WebControls.ToolbarCheckButton).AssemblyQualifiedName,
                typeof(Microsoft.Web.UI.WebControls.ToolbarCheckGroup).AssemblyQualifiedName,
                typeof(Microsoft.Web.UI.WebControls.ToolbarSeparator).AssemblyQualifiedName,
                typeof(Microsoft.Web.UI.WebControls.ToolbarDropDownList).AssemblyQualifiedName,
                typeof(Microsoft.Web.UI.WebControls.ToolbarTextBox).AssemblyQualifiedName,
            };

            /// <summary>
            /// The type name of the node that is being inserted.
            /// </summary>
            public string NodeType
            {
                get { return nodeType; }
                set
                {
                    nodeType = value;
                    int index = System.Array.IndexOf(knownTypes, nodeType);
                    if (index >= 0)
                    {
                        nodeTypeIndex = index;
                    }
                }
            }

            /// <summary>
            /// Loads an Action from ViewState.
            /// </summary>
            /// <param name="stateObj">The state object.</param>
            public void Load(object stateObj)
            {
                if (stateObj != null)
                {
                    object[] state = (object[])stateObj;
                    ActionType = (ActionType)state[0];

                    switch (ActionType)
                    {
                        case ActionType.Insert:
                            Index = (int)state[1];
                            if (state[2] is string)
                            {
                                nodeType = (string)state[2];
                            }
                            else
                            {
                                // Load from an index
                                nodeTypeIndex = (int)state[2];
                                nodeType = (string)knownTypes[nodeTypeIndex];
                            }
                            break;

                        case ActionType.Remove:
                            Index = (int)state[1];
                            break;
                    }
                }
            }

            /// <summary>
            /// Saves an Action to ViewState.
            /// </summary>
            /// <returns>The state object.</returns>
            public object Save()
            {
                object[] state;
                switch (ActionType)
                {
                    case ActionType.Insert:
                        state = new object[3];
                        state[1] = Index;
                        state[2] = (nodeTypeIndex >= 0) ? (object)nodeTypeIndex : (object)nodeType;
                        break;

                    case ActionType.Remove:
                        state = new object[2];
                        state[1] = Index;
                        break;

                    default:
                        state = new object[1];
                        break;
                }
                state[0] = ActionType;

                return state;
            }

            /// <summary>
            /// Clones an Action object.
            /// </summary>
            /// <returns>The copy.</returns>
            public Action Clone()
            {
                Action action = new Action();
                action.ActionType = this.ActionType;
                action.Index = this.Index;
                action.nodeType = this.nodeType;
                action.nodeTypeIndex = this.nodeTypeIndex;
                return action;
            }
        }

        /// <summary>
        /// Records the Clear action when clearing the contents of the collection.
        /// </summary>
        protected override void OnClear()
        {
            bool needClear = (Count > 0);
            base.OnClear();

            if (needClear && ((IStateManager)this).IsTrackingViewState)
            {
                // Since the entire list is cleared, all prior
                // actions do not need to be saved
                Actions.Clear();

                Action action = new Action();
                action.ActionType = ActionType.Clear;

                Actions.Add(action);
            }
        }

        /// <summary>
        /// Sets the entire view state for the collection and everything under it to dirty
        /// </summary>
        internal virtual void SetViewStateDirty()
        {
            if (!((IStateManager)this).IsTrackingViewState)
            {
                ((IStateManager)this).TrackViewState();
            }

            Actions.Clear();
            Action action = new Action();
            action.ActionType = ActionType.Clear;
            Actions.Add(action);
            for (int index = 0; index < Count; index++)
            {
                BaseChildNode item = (BaseChildNode)List[index];

                action = new Action();
                action.ActionType = ActionType.Insert;
                action.Index = index;
                action.NodeType = item.GetType().FullName;

                Actions.Add(action);

                item.SetViewStateDirty();
            }
        }

        /// <summary>
        /// Records the Remove action when removing a node from the collection.
        /// </summary>
        /// <param name="index">The zero-based index at which value can be found.</param>
        /// <param name="value">The value of the element to remove from index.</param>
        protected override void OnRemove(int index, object value)
        {

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?