eclipse 的redo undo功能中operation history的dispose函数分析

原来是做一个类似清空回收站的功能。
即在回收站中清除某个节点后必须将该节点对应的redo undo操作删除,即从操作历史堆栈中将对应的记录删除。

开始觉得operation history的dispose函数可能能实现这样的功能,看代码的注解说当operation的context与指定的context相符合时从操作堆栈删除记录,后半句关于引用的那句话一开始没看懂。

于是自定义了CustomUndoContext,然后利用dispose函数过滤,但是好像并没有删除。

于是看这部分的源代码,如下:

public void dispose(IUndoContext context, boolean flushUndo,
boolean flushRedo, boolean flushContext) {
// dispose of any limit that was set for the context if it is not to be
// used again.
if (flushContext) {
if (DEBUG_OPERATION_HISTORY_DISPOSE) {
Tracing.printTrace("OPERATIONHISTORY", "Flushing context " //$NON-NLS-1$//$NON-NLS-2$
+ context);
}
flushUndo(context);
flushRedo(context);
limits.remove(context);
return;
}
if (flushUndo) {
flushUndo(context);
}
if (flushRedo) {
flushRedo(context);
}

}


里面调用了flushUndo与flushRedo,拿flushUndo作为例子来说,继续看flushUndo的代码:

private void flushUndo(IUndoContext context) {
if (DEBUG_OPERATION_HISTORY_DISPOSE) {
Tracing.printTrace("OPERATIONHISTORY", "Flushing undo history for " //$NON-NLS-1$ //$NON-NLS-2$
+ context);
}

// Get all operations that have the context (or one that matches)
Object[] filtered = filter(undoList, context);
for (int i = 0; i < filtered.length; i++) {
IUndoableOperation operation = (IUndoableOperation) filtered[i];
if (context == GLOBAL_UNDO_CONTEXT
|| operation.getContexts().length == 1) {
// remove the operation if it only has the context or we are
// flushing all
undoList.remove(operation);
internalRemove(operation);
} else {
// remove the reference to the context.
// See https://bugs.eclipse.org/bugs/show_bug.cgi?id=161786
// It is not enough to simply remove the context. There could
// be one or more contexts that match the one we are trying to
// dispose.
IUndoContext[] contexts = operation.getContexts();
for (int j = 0; j < contexts.length; j++) {
if (contexts[j].matches(context)) {
operation.removeContext(contexts[j]);
}
}
if (operation.getContexts().length == 0) {
undoList.remove(operation);
internalRemove(operation);
}
}
}
ICompositeOperation endedComposite = null;
synchronized (openCompositeLock) {
if (openComposite != null) {
if (openComposite.hasContext(context)) {
if (context == GLOBAL_UNDO_CONTEXT
|| openComposite.getContexts().length == 1) {
endedComposite = openComposite;
openComposite = null;
} else {
openComposite.removeContext(context);
}
}
}
}
// notify outside of the synchronized block.
if (endedComposite != null) {
notifyNotOK(endedComposite);
}
}


这里应该好好看看,if-else中说出了原来注解后半句话的意思。原来在operation中有多个context的情况下,如果该operation中含有指定的context,如果operation的context中只有唯一的该context,则才将operation从操作历史记录删除。而context有多个时,只是在operation的context中将指定的该context移除。

继续跟进filter函数:

private IUndoableOperation[] filter(List list, IUndoContext context) {
List filtered = new ArrayList();
Iterator iterator = list.iterator();
synchronized (undoRedoHistoryLock) {
while (iterator.hasNext()) {
IUndoableOperation operation = (IUndoableOperation) iterator
.next();
if (operation.hasContext(context)) {
filtered.add(operation);
}
}
}
return (IUndoableOperation[]) filtered
.toArray(new IUndoableOperation[filtered.size()]);
}


filter函数无非是遍历操作历史堆栈,看各个operation是否包含指定的context。operation是否包含这里用到了hasContext方法,实际上调用的是operation中的contexts(是个list)的逐项比较。

注意,以上源代码分析基于DefaultOperationHistory和AbstractOperation类。

这样,实现回收站删除相应节点对应的历史记录的代码为:

//在操作栈中删除对应的历史记录
IWorkbench workbench = getSite().getWorkbenchWindow().getWorkbench();
IUndoContext context=StudioView.getCustomUndoContext(studio.getName());
IOperationHistory operationHistory=workbench.getOperationSupport().getOperationHistory();
IUndoableOperation[] operations=operationHistory.getUndoHistory(context);
for(IUndoableOperation operation:operations){
IUndoContext[] undoContexts=operation.getContexts();
for(IUndoContext undoContext:undoContexts){
if(!undoContext.matches(context)){
operation.removeContext(undoContext);
}
}
}
workbench.getOperationSupport().getOperationHistory().
dispose(context, true, true, true);


这样做有点暴力,但至少功能实现了。做法是:先获取满足指定的context的operation,然后在将operation中除该指定的context除外的context移除,然后调用dispose函数就能实现相应的功能。

最后,总结一下IBM的代码,个人觉得IBM程序员写出来的代码没什么特别的,有些地方效率未必很高,我觉得我很他们在写代码的差距上不大,或者我觉得没什么差距。我现在发现优秀的软件之所以优秀主要是在于软件的架构。突然想起了微软的.NET之父,当初ms将其从Borland挖过来的时候就花了不少钱,那个丹麦人软件架构能力确实很厉害。据说微软从不缺底层写代码的优秀程序员,而高层的架构师微软是将其作为人才极力挽留的。Got it!
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值