basechildnodecollection.cs

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

CS
487
字号
            base.OnRemove(index, value);

            if (((IStateManager)this).IsTrackingViewState)
            {
                Action action = new Action();
                action.ActionType = ActionType.Remove;
                action.Index = index;

                Actions.Add(action);
            }
        }

        /// <summary>
        /// Records the Insert action when adding a node to the collection
        /// </summary>
        /// <param name="index">The zero-based index at which to insert value.</param>
        /// <param name="value">The new value of the element at index.</param>
        protected override void OnInsert(int index, object value)
        {
            base.OnInsert(index, value);

            if (((IStateManager)this).IsTrackingViewState)
            {
                Action action = new Action();
                action.ActionType = ActionType.Insert;
                action.Index = index;
                action.NodeType = value.GetType().AssemblyQualifiedName;

                Actions.Add(action);

                // The new node needs to start tracking its view state
                ((IStateManager)value).TrackViewState();
                ((BaseChildNode)value).SetViewStateDirty();
            }
        }

        /// <summary>
        /// Records the Set action when a node is replaced in the collection.
        /// </summary>
        /// <param name="index">The zero-based index at which oldValue can be found.</param>
        /// <param name="oldValue">The value to replace with newValue.</param>
        /// <param name="newValue">The new value of the element at index.</param>
        protected override void OnSet(int index, object oldValue, object newValue)
        {
            base.OnSet(index, oldValue, newValue);

            if (((IStateManager)this).IsTrackingViewState && 
                !oldValue.GetType().Equals(newValue.GetType()))
            {
                Action action = new Action();
                action.ActionType = ActionType.Remove;
                action.Index = index;

                Actions.Add(action);

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

                Actions.Add(action);

                // The new node needs to start tracking its view state
                ((IStateManager)newValue).TrackViewState();
                ((BaseChildNode)newValue).SetViewStateDirty();
            }
        }

        /// <summary>
        /// Recreates a node of type nodeTypeName and inserts it into the index location.
        /// </summary>
        /// <param name="index">The zero-based index at which to insert the node.</param>
        /// <param name="nodeTypeName">The name of the type of the node.</param>
        private void RedoInsert(int index, String nodeTypeName)
        {
            try
            {
                // Create a Type object from the type name
                Type nodeType = Type.GetType(nodeTypeName);

                if (nodeType != null)
                {
                    // Invoke the constructor, creating the object
                    object node = Activator.CreateInstance(nodeType, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.CreateInstance, null, null, null);

                    // Insert the node if creation was successful
                    if ((node != null) && (node is BaseChildNode))
                    {
                        List.Insert(index, node);
                    }
                }
            }
            catch
            {
                // Ignore errors
            }
        }

        /// <summary>
        /// Gets a value indicating whether a server control is tracking its view state changes.
        /// </summary>
        bool IStateManager.IsTrackingViewState
        {
            get { return _Tracking; }
        }

        /// <summary>
        /// Indicates whether the collection is reloading its items
        /// </summary>
        internal bool Reloading
        {
            get { return _Reloading; }
        }

        /// <summary>
        /// Loads the collection's previously saved view state.
        /// </summary>
        /// <param name="state">An Object that contains the saved view state values for the collection.</param>
        void IStateManager.LoadViewState(object state)
        {
            if (state == null)
            {
                return;
            }

            object[] viewState = (object[])state;

            if (viewState[0] != null)
            {
                _Reloading = true;

                // Restore and re-do actions
                object[] actions = (object[])viewState[0];

                ArrayList newActionList = new ArrayList();
                foreach (object actionState in actions)
                {
                    Action action = new Action();
                    action.Load(actionState);

                    newActionList.Add(action);

                    switch (action.ActionType)
                    {
                        case ActionType.Clear:
                            List.Clear();
                            break;

                        case ActionType.Remove:
                            List.RemoveAt(action.Index);
                            break;

                        case ActionType.Insert:
                            RedoInsert(action.Index, action.NodeType);
                            break;
                    }
                }

                _Actions = newActionList;
                _Reloading = false;
            }

            if (viewState[1] != null)
            {
                // Load view state changes

                object[] lists = (object[])viewState[1];
                ArrayList indices = (ArrayList)lists[0];
                ArrayList items = (ArrayList)lists[1];

                for (int i = 0; i < indices.Count; i++)
                {
                    ((IStateManager)List[(int)indices[i]]).LoadViewState(items[i]);
                }
            }
        }

        /// <summary>
        /// Saves the changes to the collection's view state to an Object.
        /// </summary>
        /// <returns>The Object that contains the view state changes.</returns>
        object IStateManager.SaveViewState()
        {
            object[] state = new object[2];

            if (Actions.Count > 0)
            {
                // Save the actions made to the collection
                object[] obj = new object[Actions.Count];
                for (int i = 0; i < Actions.Count; i++)
                {
                    obj[i] = ((Action)Actions[i]).Save();
                }
                state[0] = obj;
            }

            if (List.Count > 0)
            {
                // Save the view state changes of the child nodes

                ArrayList indices = new ArrayList(4);
                ArrayList items = new ArrayList(4);

                for (int i = 0; i < List.Count; i++)
                {
                    object item = ((IStateManager)List[i]).SaveViewState();
                    if (item != null)
                    {
                        indices.Add(i);
                        items.Add(item);
                    }
                }

                if (indices.Count > 0)
                {
                    state[1] = new object[] { indices, items };
                }
            }

            if ((state[0] != null) || (state[1] != null))
            {
                return state;
            }
            
            return null;
        }

        /// <summary>
        /// Instructs the collection to track changes to its view state.
        /// </summary>
        void IStateManager.TrackViewState()
        {
            _Tracking = true;

            // Tracks view state changes in the child nodes
            foreach (BaseChildNode node in List)
            {
                ((IStateManager)node).TrackViewState();
            }
        }
    }
}

⌨️ 快捷键说明

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