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

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

参考博客:https://blog.csdn.net/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);
	}

至此,大功告成

猜你喜欢

转载自blog.csdn.net/qq_26991187/article/details/84852799