2011-08-03 28 views
8

我正在做一個Eclipse插件,我想利用內置在Eclipse「撤消」動作(org.eclipse.core.commands.operations)每當用戶按下與插件相關的撤銷按鈕。如何在Eclipse插件中腳本化撤消操作?

理想的情況下,它只是複製,當您按下CTRL + Z會發生什麼,但我沒有得到模擬按鍵的工作。

我已經試過這些代碼片段:

撤消在工作臺上進行:

IWorkbenchOperationSupport operationSupport = PlatformUI.getWorkbench().getOperationSupport(); 
IUndoContext context = operationSupport.getUndoContext(); 
IOperationHistory operationHistory = operationSupport.getOperationHistory();  
IStatus status = operationHistory.undo(context, null, null); 

撤消在工作區中進行:

IWorkbenchOperationSupport operationSupport = PlatformUI.getWorkbench().getOperationSupport(); 
IUndoContext context= (IUndoContext)ResourcesPlugin.getWorkspace().getAdapter(IUndoContext.class); 
IOperationHistory operationHistory = operationSupport.getOperationHistory(); 
IStatus status = operationHistory.undo(context, null, null); 

什麼我再想找,類似,是這樣的,但它不工作:

撤消編輯/文件執行:

IWorkbenchOperationSupport operationSupport = PlatformUI.getWorkbench().getOperationSupport(); 
IEditorPart currentEditor = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor(); 
IUndoContext context = (IUndoContext) currentEditor.getAdapter(IUndoContext.class); 
IOperationHistory operationHistory = operationSupport.getOperationHistory(); 
IStatus status = operationHistory.undo(context, null, null); 
+1

我工作在這個問題上也是如此。我發現這樣的例子功能使用此更新站點(http://download.eclipse.org/eclipse/updates/4.2)(org.eclipse.sdk.examples.feature),並且它有一個很好的實現工作得很好。我只需要弄清楚如何讓我的視圖部分參與IUndoContext,以便撤消和重做操作在Eclipse Undo和Redo菜單項中顯示。奇怪的是,只有當示例視圖部分處於活動狀態時,撤消重做操作才能正常工作,而當視圖部分處於活動狀態時則無法正常工作。我想我還沒完全理解IUndoContext。 – twindham

+0

使用安裝新軟件對話框嘗試安裝Eclipse SDK示例時,請使用過濾器框並鍵入'示例',但一定要取消選中按類別分組的項目,否則您將看不到任何結果。 – twindham

+0

對於這種工作方式,您應該在編輯器中執行所有更改,方法是向操作堆棧添加可撤消命令。 – Basilevs

回答

0

我不知道如果我理解正確的話,但我認爲這可能是你的東西:http://www.eclipsezone.com/eclipse/forums/t80577.html#92048329

這是一個有點過時,但這個想法還算不錯。

+0

我真的更喜歡使用Eclipse的內置包(org.eclipse.core.commands.operations)。我在網上閱讀了很多很多的例子,聲稱這個包是實現這個功能的理想之選。我已經成功地添加了一個實現IUndoableOperation的類並且能夠執行()這些動作,但是我似乎無法使undo()工作...? – blearn

1

如果你的編輯器上有一個瀏覽器(例如TextViewer,SourceViewer的,ProjectionViewer),那麼你可以添加對觀衆,例如要求撤消操作撤消動作

Action undoAction = new Action() 
{ 
    @Override 
    public void run() 
    { 
    getViewer().doOperation(ITextOperationTarget.UNDO); 
    } 
};