前面的开发基本已经完成了。接下来的工作是使得Editor能够持久化到本地。
在下一次启动时,能够自动打开原来已经打开的Editor.
第一步:将EditorInput持久化
让EditInput实现IPersistableElement,并且在getPersistable()返回this.
代码
@Override
public IPersistableElement getPersistable() {
return this ;
}
@Override
public String getFactoryId() {
// TODO Auto-generated method stub
return null ;
}
@Override
public void saveState(IMemento memento) {
// TODO Auto-generated method stub
}
public IPersistableElement getPersistable() {
return this ;
}
@Override
public String getFactoryId() {
// TODO Auto-generated method stub
return null ;
}
@Override
public void saveState(IMemento memento) {
// TODO Auto-generated method stub
}
第二步:定义Factory的扩展以及在getFactory中返回这个ID
代码
<
extension
point = " org.eclipse.ui.elementFactories " >
< factory
class = " com.sg.intellifmea.editor.ProjectEditorInputFactory "
id = " com.sg.intellifmea.editor.ProjectEditorInputFactory " >
</ factory >
</ extension >
point = " org.eclipse.ui.elementFactories " >
< factory
class = " com.sg.intellifmea.editor.ProjectEditorInputFactory "
id = " com.sg.intellifmea.editor.ProjectEditorInputFactory " >
</ factory >
</ extension >
@Override
public String getFactoryId() {
return " com.sg.intellifmea.editor.ProjectEditorInputFactory " ;
}
public String getFactoryId() {
return " com.sg.intellifmea.editor.ProjectEditorInputFactory " ;
}
第三步:实现saveState(IMemento memento),将EditorInput保存到本地的memento中
@Override
public void saveState(IMemento memento) {
if (data != null && data.getOuid() != null ){
memento.putString( " ouid " , data.getOuid());
}
}
public void saveState(IMemento memento) {
if (data != null && data.getOuid() != null ){
memento.putString( " ouid " , data.getOuid());
}
}
第四步:实现 IElementFactory
代码
public
class
ProjectEditorInputFactory
implements
IElementFactory {
@Override
public IAdaptable createElement(IMemento memento) {
String ouid = memento.getString( " ouid " );
return new ProjectInput( new LazyDTO(ouid));
}
}
@Override
public IAdaptable createElement(IMemento memento) {
String ouid = memento.getString( " ouid " );
return new ProjectInput( new LazyDTO(ouid));
}
}
看起来已经完成了 ,但是,似乎还是不能自动打开上次退出系统时没有关闭的编辑器
检查后,发现EditorInput中还有一个方法要改:
@Override
public boolean exists() {
return true ;
}
public boolean exists() {
return true ;
}
exists返回ture,告知系统这个input是否还存在。
可以理解editor是面向客户端文件设计的,而显示来自数据库的数据时,应当考虑数据库的这条记录以及所编辑的记录是否还存在或者完整。上面的内容我不详细写完了。可以根据自己的逻辑需要来写。