我正在开发一个多页面编辑器,用于在Eclipse中编辑/创建自定义XML文件.
>实现类是MyXMLFormEditor,它扩展了FormEditor.
> FormEditor的每个页面都扩展了FormPage(即MyXMLFormPage扩展了FormPage).
>在FormEditor和实际XML文件之间我维护JDOM模型.
>我也实现了脏标志处理.因此,用户对表单编辑器的输入将保存到JDOM中,直到用户按下“保存”按钮为止.当用户按下保存按钮时,JDOM被写入/序列化为XML文件.
在具有上述功能的编辑器中,我想实现undo / redo功能,如下所示:
>当编辑器变脏(用户将某些内容更改为表单编辑器并且未保存)时,撤消操作应将表单编辑器中的更改以及JDOM恢复到其原始状态(即编辑器非脏时的状态)并重做操作应该再次将更改恢复到FormEditor以及JDOM和编辑器应该变脏.
以下是我的代码段:
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或工作台的现有撤消/重做框架,那将是很好的.
> 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->“new value”或id->“new value”之类的东西,它可以是单个操作来处理所有修改.或者,您可以使用一系列不同的操作来修改每种类型的数据模型.由你决定.
3)仅通过您创建的操作对数据进行每次修改
MyDataModifyingOperation op = new MyDataModifyingOperation(xpath,newValue,oldValue);
op.addContext(myUndoContext);
IStatus status = OperationHistoryFactory.getOperationHistory().execute(operation,null,null);
一旦你掌握了基本的东西,你就需要看一些其他高级的东西,比如在数据模型上添加某种变化监听机制,这样当undo / redo修改数据时,你可以更新UI.此外,在大多数情况下,当您执行操作时,您需要记录UI选择状态,以便在撤消或后续重做时,您可以将选择恢复到已修改的元素,以便用户立即识别Ctrl z / Ctrl y时更改的内容被压了.