周洪
新手
新手
  • UID203
  • 粉丝1
  • 关注0
  • 发帖数5
阅读:7466回复:0

编辑器保存的实现

楼主#
更多 发布于:2016-08-19 17:20
保存编辑器的实现:
1.重写方法变更标记的实现
@Override
public boolean isDirty() {
return getCommandStack().isDirty();
}

@Override
public void commandStackChanged(EventObject event) {
super.commandStackChanged(event);
firePropertyChange(PROP_DIRTY);
}
2.序列化所以要保存的模型包含连线
3.重写dosave方法
@Override
public void doSave(IProgressMonitor monitor) {
// TODO Auto-generated method stub

try{
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(new File("D:\\objectFile.obj")));
        ProgrammerModel model=(ProgrammerModel)getGraphicalViewer().getContents().getModel();
        out.writeObject(model);
        out.close();
}catch(Exception e){
System.out.println(e);
}
//保存快捷�?
getCommandStack().markSaveLocation();      
}
4打开编辑器读取保存内容
@Override
protected void initializeGraphicalViewer() {
// TODO Auto-generated method stub

GraphicalViewer viewer = getGraphicalViewer();
ObjectInputStream ois = null;
ProgrammerModel root = null;
if(new File("D:\\objectFile.obj").exists()){
try {
ois = new ObjectInputStream(new FileInputStream(new File("D:\\objectFile.obj")));
root = (ProgrammerModel) ois.readObject();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
else{
root=new ProgrammerModel();
root.setName("");
root.setLayout(new Rectangle(30,50,30,50));

}

viewer.setContents(root);
}
游客

返回顶部