1. 程式人生 > >eclipse rcp eclipse rcp程式工作狀態的儲存、重新開啟時編輯器等狀態的儲存

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);
	}

至此,大功告成