做编辑器Editor插件,肯定离不开对“脏”的处理。以前虽然也写过部分编辑器,总感觉对脏的处理比较零乱。今天偶尔翻看了一下原来的代码,整理如下文:
(本文对脏的处理不求正规,仅求使用)大家如有好的方法or捷径,欢迎拍砖
背景:多页编辑器+gef
对editor的处理:
//第一步,对editor的构造函数添加EditDomain
public MyGraphicalEditor() {
setEditDomain(new DefaultEditDomain(this));
}
//第二步,对Command堆栈进行监听
public void init(IEditorSite site, IEditorInput input) throws PartInitException {
super.init(site, input);
getCommandStack().addCommandStackListener(this);
}
//第三步,重载CommandStackListener的CommandStackChanged()方法
public void commandStackChanged(EventObject event) {
super.commandStackChanged(event);
firePropertyChange(IEditorPart.PROP_DIRTY);// 别忘了
}
//第四步,定义一个boolean类型的变量dirty,并写其set方法。供涉及Command堆栈之外的操作对“脏”状态改的需求
public void setDirty(boolean dirty){
if (dirty != this.dirty) {
this.dirty = dirty;
firePropertyChange(IEditorPart.PROP_DIRTY);
}
}
//第五步,重载editor的isDirty()方法
public boolean isDirty() {
return (getCommandStack().isDirty()) | dirty;
}
// 最后一步,doSave()方法的处理
public void doSave(IProgressMonitor arg0) {
getCommandStack().markSaveLocation();
dirty = false;
firePropertyChange(IEditorPart.PROP_DIRTY);
// Todo others.....
}
在edtor中可以直接调用setDirty() 来设置编辑器的“脏”状态,在其他页面(非editor类中),我们可以通过以下方法获取编辑器editor:
private MyGraphicalEditor getEditor(){
IEditorPart editor = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor();
if(editor instanceof MyGraphicalEditor)
return (MyGraphicalEditor)editor;
return null;
}
然后就可以设置编辑器的脏状态了。
over。。。。
分享到:
相关推荐
- **继承自`org.eclipse.ui.part.EditorPart`**:利用EditorPart提供的基本功能,如文件加载、保存、脏标记处理等。 - **实现图形渲染**:在Editor中集成GEF的Viewer组件,用于渲染图形模型。 - **处理用户交互**...
3. 清洁玻璃瓶:将剪碎的纸片放入脏的玻璃瓶中,加上一些水,然后摇晃,这样可以有效清洁瓶子内部难以清洗的污垢。 4. 煮饺子防粘:煮水饺时,加入适量的盐,能防止饺子粘皮粘锅,使饺子更容易煮熟。 5. 床铺摆放...
这是因为Editor继承自`EditorPart`,提供了文件保存、脏标记(dirty flag)处理等更完善的功能,适合于需要持久化数据的场景。 ### GEF的基本结构 GEF的核心结构可以分为三个主要部分: 1. **模型(Model)**:这...
GEF通常被嵌入到Eclipse的RCP(Rich Client Platform)项目中,以利用其提供的文件保存和脏标记功能。步骤如下: 1. **新建插件项目**:在Eclipse中选择“File”>“New”>“Other”,找到并选择“Plug-in Project”...
Rails 2.1增加了对时区的支持,这意味着应用程序可以更好地处理不同地理位置用户的时间显示问题。通过这一特性,开发人员能够确保应用程序中的日期和时间能够准确地反映用户的本地时间设置。 **脏数据追踪(Dirty ...
- **标签装饰**:在标签上显示额外的信息,如脏状态、锁状态等。 - **链接资源**:设置外部文件夹与项目之间的链接。 - **历史纪录**:管理文件的历史版本。 - **视景**:保存和恢复视景布局。 - **搜寻**:定制搜索...