GEF开发eclipse插件,多页编辑器实现delete功能

基于GEF开发,多页签编辑器实现

有两种方案:

  • 方案一
    继承FormEditor或MultiPageEditorPart,在其中嵌入自定义的继承自GraphicalEditor的子类的编辑器(作为一个page);并加入其它page。
  • 方案二
    -继承GraphicalEditor的子类,参照MultiPageEditorPart的实现(或者自己做)加入多页签机制。

由于感觉第二种方案工作量稍大,采用第一种方案。具体步骤如下:

  • 自定义继承自GraphicalEditorWithPalette的类WebEditor,具体做法略(网上有文)。
  • 定义继承FormEditor的类WebMultiPageEditor
    重载addPages()方法,在其中调用public int addPage(IEditorPart editor, IEditorInput input)方法添加WebEditor的实例:addPage(new WebEditor(), getEditorInput())
    添加其他编辑器。。。
  • 在改成多页编辑器之前,已经在WebEditor中重载了doSave方法。在WebMultiPageEditor中重载doSave方法,在其中传递调用((WebEditor)getEditor(0)).doSave(monitor)。
  • 在做了以上工作以后运行,已经看到一个多页编辑器了。但是,发现Delete功能却不能用。花费一番功夫才找到原因。还有一重要步骤要做:在WebEditor类中重载selectionChanged方法。我们来看看WebEditor的父类(或者更上层)GraphicalEditor中此方法的实现:
public void selectionChanged(IWorkbenchPart part, ISelection selection) {
 // If not the active editor, ignore selection changed.
 if (this.equals(getSite().getPage().getActiveEditor()))
  updateActions(selectionActions);
}

selectionChanged事件发生时,此方法if语句条件为false。
因此要重载此方法,如下:

public void selectionChanged(IWorkbenchPart part, ISelection selection) {
  // 此处依赖WebUiEditor编辑器.
  if (this.equals(((WebMultiPageEditor)getSite().getPage().getActiveEditor()).getActiveEditor()))
   updateActions(getSelectionActions());
 }

注意 需要在多页编辑器中重写父类的getActiveEditor()方法,或者自己写方法将需要delete的Editor返回即可。

原文地址:https://www.cnblogs.com/zhjj/p/6622415.html