eclipse rcp eclipse rcp程序工作状态的保存、重新打开时编辑器等状态的保存

本文介绍了如何在Eclipse RCP应用程序中保存和恢复编辑器等的工作状态,包括在plugin.xml中添加扩展点,实现ElementFactory类,使自定义EditorInput类实现IPersistableElement接口,并调整相关方法,确保状态能在重新打开时正确加载。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

参考文章链接:http://www.it610.com/article/251676.htm,原文讲的很详细,建议直接查看,本文记录本人的实现细节

参考博客:https://blog.youkuaiyun.com/iteye_1533/article/details/81801603

第一步在plugin.xml中的Extensions页面中添加 org.eclipse.ui.elementFactories 扩展点,设置ID和class

   <extension
         point="org.eclipse.ui.elementFactories">
      <factory
            class="com.ober.npu.tte.netEditor.ElementFactory"
            id="EditorFactory">
      </factory>
   </extension>

实现ElementFactory类,这里注意下面的代码  return new TopologyEditorInput(name, absolutePath); 这里的TopologyEditorInput类对应你自定义的EditorInput类,里面的参数是你自定义的EditorInput的初始化函数,这个初始化函数需要的参数可通过IMemento 对象获得

public class ElementFactory implements IElementFactory {

	public static final String ID = "EditorFactory";
	
	@Override
	public IAdaptable createElement(IMemento memento) {
		// TODO Auto-generated method stub
		IMemento childMem = memento.getChild(TopologyEditorInput.TAG_KEY);
		String name = childMem.getString(TopologyEditorInput.TAG_NAME);
	    String absolutePath = childMem.getString(TopologyEditorInput.TAG_PATH);
	    
		return new TopologyEditorInput(name, absolutePath);
	}

}

让自定义的EditorInput类实现IPersistableElement接口,让EditorInput类的getPersistable()方法返回一个IPersistentElement对象(EditorInput本身),IpersistentElement包含两个方法:第一个是getFactoryId(),该方法返回创建input对象的工厂的id由工作台自动去创建;第二个是saveState(Imemento),注意这个方法,上面TopologyEditorInput类所需要的参数就是由这里保存的

	public static String TAG_KEY = "topoEditorInputTagKey";
	public static String TAG_NAME = "topoEditorInputTagName";
	public static String TAG_PATH = "topoEditorInputTagPath";

        @Override
	public void saveState(IMemento memento) {
		// TODO Auto-generated method stub
	    IMemento editorMem =  memento.createChild(TAG_KEY);

	    editorMem.putString(TAG_NAME, this.editorName);
            editorMem.putString(TAG_PATH, this.topologyFilePath);
	}

	@Override
	public String getFactoryId() {
		// TODO Auto-generated method stub
		return ElementFactory.ID;
	}

然后修改自定义的EditorInput类中原有的方法

	@Override
	public boolean exists() {
		// TODO Auto-generated method stub
		//return false;
		return true;
	}

	@Override
	public IPersistableElement getPersistable() {
		// TODO Auto-generated method stub
		//return null;
		return this;
	}

最后修改ApplicationWorkbenchAdavisor类

	@Override
	public void initialize(IWorkbenchConfigurer configurer) {
		super.initialize(configurer);
		//保存上一次的工作状态
		configurer.setSaveAndRestore(true);
	}

至此,大功告成

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值