如何在Eclipse IDE中手动处理编辑器脏状态
手动设置Eclipse编辑器脏状态的解决方案
你已经能定位到目标编辑器的IEditorReference,接下来可以按以下步骤手动控制脏状态:
- 从编辑器引用中获取实际的编辑器实例
- 转换为你的自定义编辑器类型(或通用的
AbstractEditorPart) - 调用内置的脏状态设置方法
完整代码示例
IEditorReference[] ed = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getEditorReferences(); for (IEditorReference iEditorReference : ed) { if (iEditorReference.getId().equals("your-editor-id")) { // 获取编辑器实例,true表示如果未激活则激活该编辑器 IEditorPart editor = iEditorReference.getEditor(true); // 转换为你的自定义编辑器类(如果有),比如MyTagEditor if (editor instanceof MyTagEditor) { MyTagEditor tagEditor = (MyTagEditor) editor; // 设置脏状态:true标记为已修改,false标记为未修改 tagEditor.setDirty(true); } // 如果不确定自定义类,也可以用AbstractEditorPart(大部分编辑器都继承它) else if (editor instanceof AbstractEditorPart) { ((AbstractEditorPart) editor).setDirty(true); } } }
补充说明
如果你的编辑器是自己实现IEditorPart而没有继承AbstractEditorPart,需要自己维护脏状态逻辑:
- 添加布尔字段维护脏状态:
private boolean isDirty = false; - 实现
isDirty()方法返回该字段值 - 实现
setDirty(boolean dirty)方法,更新字段后触发属性变更通知:
public void setDirty(boolean dirty) { boolean oldValue = this.isDirty; this.isDirty = dirty; firePropertyChange(IEditorPart.PROP_DIRTY, oldValue, dirty); }
这样工作台就能识别到编辑器的脏状态变化,比如显示星号提示、触发保存提醒等。
内容的提问来源于stack exchange,提问作者Johnliveira
相关产品推荐
相关产品推荐

