예제 #1
0
        /// <summary>
        /// 启动修改记录。启动之后会自动异步记录修改。
        /// </summary>
        /// <param name="autoCommit">是否自动提交修改?</param>
        /// <returns>是否处于记录修改状态?</returns>
        protected bool StartLogUndoInfo(bool autoCommit)
        {
            if (this.logUndoStage == LogUndoStage.NotStart)
            {
                // 准备空间,开始修改记录
                this.logUndoStage       = LogUndoStage.Logging;
                this.logUndoCurrChanges = new ArrayList();
                this.mapUndoCurrChanges = new Dictionary <ViDataChangedEventArgs, byte>();

                // 启动异步调用,以结束 Logging
                if (autoCommit)
                {
                    this.Dispatcher.BeginInvoke(new Action(() => this.StopLogUndoInfo()),
                                                DispatcherPriority.ApplicationIdle);
                }
            }

            return(this.logUndoStage == LogUndoStage.Logging);
        }
예제 #2
0
        /// <summary>
        /// 停止修改记录。
        /// </summary>
        protected void StopLogUndoInfo()
        {
            bool logChanged = false;

            if (this.logUndoStage == LogUndoStage.Logging &&
                this.logUndoCurrChanges.Count > 0)
            {
                Debug.Assert(this.logUndoCurrEditIndex >= 0 &&
                             this.logUndoCurrEditIndex <= this.logUndoChangesChain.Count);

                // 有修改,清除后面的修改记录
                if (this.logUndoCurrEditIndex < this.logUndoChangesChain.Count)
                {
                    this.logUndoChangesChain.RemoveRange(this.logUndoCurrEditIndex,
                                                         this.logUndoChangesChain.Count - this.logUndoCurrEditIndex);
                }
                // 如果文件保存的位置要被丢弃了,那只能将保存位置设置为无效值了
                if (this.logUndoCurrSavedIndex > this.logUndoCurrEditIndex)
                {
                    this.logUndoCurrSavedIndex = -1;
                }

                LogUndoItem undoItem = new LogUndoItem(this.logUndoCurrChanges);
                LogUndoItem lastItem = this.logUndoChangesChain.Count <= 0 ? null :
                                       this.logUndoChangesChain[this.logUndoChangesChain.Count - 1] as LogUndoItem;

                if (lastItem != null &&
                    (undoItem.ChangeType & ViChangeType.CauseDirty) == ViChangeType.None)
                { // 当前的是无关紧要的修改,合并入前一个修改项目
                    lastItem.ChangeType |= undoItem.ChangeType;
                    lastItem.Changes.AddRange(undoItem.Changes);
                }
                else if (lastItem != null &&
                         this.logUndoCurrSavedIndex < this.logUndoCurrEditIndex &&
                         (lastItem.ChangeType & ViChangeType.CauseDirty) == ViChangeType.None)
                { // 如果前一个是无关紧要的修改,则合并入前一个修改项目(要考虑保存位置)
                    lastItem.ChangeType |= undoItem.ChangeType;
                    lastItem.Changes.AddRange(undoItem.Changes);
                }
                else
                {
                    // 将修改加入修改记录链表
                    this.logUndoChangesChain.Add(undoItem);
                    ++this.logUndoCurrEditIndex;

                    // 修改记录不能太多,防止占用太多内存
                    if (this.logUndoCurrEditIndex > logUndoChainCapability)
                    {
                        // 如果文件保存的位置要被丢弃了,那只能将保存位置设置为无效值了
                        if (this.logUndoCurrSavedIndex == 0)
                        {
                            this.logUndoCurrSavedIndex = -1;
                        }
                        // 删除最开始的一个修改记录
                        this.logUndoChangesChain.RemoveAt(0);
                        --this.logUndoCurrEditIndex;
                    }
                }

                // LOG 内容发生了变化
                logChanged = true;
            }

            // 恢复修改记录状态
            this.logUndoStage       = LogUndoStage.NotStart;
            this.logUndoCurrChanges = null;
            this.mapUndoCurrChanges = null;

            // LOG 内容发生了变化时给出通知
            if (logChanged)
            {
                this.NotifyContentIsChanged();
            }

            // 刷新各工具的状态
            CommandManager.InvalidateRequerySuggested();
        }