如何在 Eclipse FormEditor 中实现撤消/重做功能?

Posted

技术标签:

【中文标题】如何在 Eclipse FormEditor 中实现撤消/重做功能?【英文标题】:How to implement undo/redo functionality into Eclipse FormEditor? 【发布时间】:2012-12-16 17:04:03 【问题描述】:

我正在开发一个多页表单编辑器来在 Eclipse 中编辑/创建自定义 XML 文件。

    实现类是 MyXMLFormEditor,它扩展了 FormEditor。

    FormEditor 的每一页都扩展了 FormPage(即 MyXMLFormPage 扩展了 FormPage)。

    在 FormEditor 和实际 XML 文件之间,我正在维护 JDOM 模型。

    我还实现了脏标志处理。因此,用户在表单编辑器中的输入会保存到 JDOM 中,直到用户按下保存按钮。当用户按下保存按钮时,JDOM 被写入/序列化为 XML 文件。

在具有上述功能的编辑器中,我想实现如下撤消/重做功能:

当编辑器变脏时(用户将某些内容更改为表单编辑器但未保存)撤消操作应将表单编辑器和 JDOM 中的更改恢复到其原始状态(即编辑器未变脏时的状态)和重做操作应该再次将更改带回 FormEditor 以及 JDOM 和编辑器应该变脏。

以下是我的代码 sn-p:

MyXMLFormEditor.java

public class MyXMLFormEditor extends FormEditor 

    MyXMLFormEditor()
                                super();                                
                                

                @Override
                protected FormToolkit createToolkit(Display display) 
                                // Create a toolkit that shares colors between editors.
                                return new FormToolkit(Activator.getDefault().getFormColors(display));
                

                @Override
                public void init(IEditorSite site, IEditorInput editorInput) 
                                setSite(site);
                                mSite = site;
                                setInput(editorInput);
                                try 
                                                super.init(site, editorInput);
                                 catch (PartInitException e1) 
                                                e1.printStackTrace();
                                
                                if (!(editorInput instanceof IFileEditorInput))
                                                try 
                                                                throw new PartInitException("Invalid Input: Must be IFileEditorInput");
                                                                 catch (PartInitException e) 
                                                                                e.printStackTrace();
                                                                
                                setPartName(fileName);
                
                public void setUpProgFile(IEditorSite site, IEditorInput editorInput)                       
                                IFileEditorInput fileInput = ((IFileEditorInput) editorInput);

                                //create document builder and prepare JDom model for xml file.
                


                @Override
                protected void addPages() 
                                try 
                                                //add 'Main' page
                                                objMyXMLFormPage = new MyXMLFormPage (this, "FirstPage","Main");
                                                //set rootNode of MyXMLFormPage 
                                                objMyXMLFormPage.rootNode = getRootNode();
                                                objMyXMLFormPage.filePath = filePath;
                                                objMyXMLFormPage.document = document;
                                                addPage(objMyXMLFormPage);

                                 catch (PartInitException e) 
                                                e.printStackTrace();
                                
                

                @Override
                public void doSave(IProgressMonitor monitor) 
                                System.out.println("MyXMLFormEditor: doSave");

                                //logic to write jdom contents into xml file.
                                objMyXMLFormPage.setDirty(false);
                

                @Override
                public void doSaveAs() 
                                System.out.println("MyXMLFormEditor: doSaveAs");
                
                @Override
                public boolean isSaveAsAllowed() 
                                System.out.println("MyXMLFormEditor: isSaveAsAllowed");
                                return true;
                


MyXMLFormPage .java

public class MyXMLFormPage  extends FormPage

                //private members declaration.

                public MyXMLFormPage (MyXMLFormEditor editor,String title, String id) 
                                // initialize the editor and set its title and name.
                                super(editor,title,id );
                                

                @Override
                public void createFormContent(IManagedForm managedForm) 
                    // Set page title
                                super.createFormContent(managedForm);

                                FormToolkit mMyXMLFormPage Toolkit = managedForm.getToolkit();

                                //Logic to creat UI and populating its contents from JDom

                


                private void makeEditorDirty() 
                                updateJdom =  true;       
                                setDirty(true);                                                   
                

                private void updateJDom() 
                                if(updateJdom)
                                                System.out.println("*** Jdom updated ***");
                                                updateJdom = false;
                                
                

                @Override
                public boolean isDirty() 
                                return isDirtyFlag;
                

                protected void setDirty(boolean value) 
                                isDirtyFlag = value;
                                dirtyStateChanged();
                

                public void dirtyStateChanged() 
                                getEditor().editorDirtyStateChanged();

                

                @Override
                public boolean isSaveAsAllowed() 
                                System.out.println("MyXMLFormPage .isSaveAsAllowed");
                      return false;
                   

                @Override
                public void doSave(IProgressMonitor monitor) 
                                System.out.println("MyXMLFormPage .doSave");
                


谁能向我提供有关如何在 FormEditor 中实现撤消/重做功能的指针/示例? 如果该方法利用 Eclipse PDE 或工作台的现有撤消/重做框架会很好。

【问题讨论】:

你提出问题的方式,看起来像家庭作业,提供一点你做过的代码sn-p 【参考方案1】:

您需要阅读以下资源。这似乎是额外的工作,但相信我,您的工作会轻松很多,而且这些文章并不长。

Undoable operations Undo and IDE Workbench Undo Example

您需要执行的基本步骤是:

1) 在编辑器中为撤消/重做操作添加操作处理程序

@Override
public void init(IEditorSite site, IEditorInput editorInput) 
    ...

    UndoRedoActionGroup historyActionGroup = new UndoRedoActionGroup(editorSite, myUndoContext, true);
    historyActionGroup.fillActionBars(editorSite.getActionBars());

如果您在想什么是myUndoContext,您将通过阅读第一篇文章了解这一点。

2) 为用户可以对您的数据进行的不同类型的修改创建您自己的IUndoableOperation 实现。如果它只依赖于诸如 XPath->“新值”或 id->“新值”之类的东西,它可能是一个处理所有修改的单个操作。或者,您可以有一系列不同的操作来修改每种类型的数据模型。由你决定。

3)通过您创建的操作对您的数据进行每一次修改

MyDataModifyingOperation op = new MyDataModifyingOperation(xpath, newValue, oldValue);
op.addContext(myUndoContext);
IStatus status = OperationHistoryFactory.getOperationHistory().execute(operation, null, null);

一旦基本的东西正常工作,您将需要查看其他一些高级的东西,例如在数据模型上添加某种更改监听机制,以便在撤消/重做修改数据时,您可以更新 UI。同样在大多数情况下,最好在执行操作时记录 UI 选择状态,以便在撤消或后续重做时,您可以将选择恢复到已修改的元素,以便用户在 Ctrl+z/Ctrl 时立即识别更改的内容+y 被按下。

【讨论】:

以上是关于如何在 Eclipse FormEditor 中实现撤消/重做功能?的主要内容,如果未能解决你的问题,请参考以下文章

如何在 Eclipse 中创建区域,如 Visual Studio

如何在 JGit 中实现稀疏结帐?

Eclipse中实用的快捷键大全

如何最小化 Eclipse 中的代码区域?

在 Eclipse 中使用 J2EE Preview 运行时在 Java EE 中实现 RESTful 服务

如何在 Eclipse Android 开发中使用自定义视图?