1 11 package org.eclipse.ui.internal; 12 13 import org.eclipse.core.runtime.IAdaptable; 14 import org.eclipse.ui.IEditorInput; 15 import org.eclipse.ui.IEditorPart; 16 import org.eclipse.ui.IElementFactory; 17 import org.eclipse.ui.IMemento; 18 import org.eclipse.ui.IPersistableElement; 19 import org.eclipse.ui.PlatformUI; 20 import org.eclipse.ui.XMLMemento; 21 22 27 public class NavigationHistoryEditorInfo { 28 String editorID; 29 30 IEditorInput editorInput; 31 32 int refCount = 0; 33 34 IMemento memento; 35 36 NavigationHistoryEditorInfo(IEditorPart part) { 37 editorID = part.getSite().getId(); 38 editorInput = part.getEditorInput(); 39 } 40 41 NavigationHistoryEditorInfo(IMemento memento) { 42 this.memento = memento; 43 } 44 45 boolean isPersistable() { 46 if (editorInput != null) { 47 IPersistableElement persistable = editorInput.getPersistable(); 48 return persistable != null; 49 } 50 return memento != null; 51 } 52 53 void handlePartClosed() { 54 if (!isPersistable()) { 55 return; 56 } 57 if (memento == null) { 58 IPersistableElement persistable = editorInput.getPersistable(); 59 memento = XMLMemento 60 .createWriteRoot(IWorkbenchConstants.TAG_EDITOR); 61 memento.putString(IWorkbenchConstants.TAG_ID, editorID); 62 memento.putString(IWorkbenchConstants.TAG_FACTORY_ID, persistable 63 .getFactoryId()); 64 persistable.saveState(memento); 65 } 66 editorID = null; 67 editorInput = null; 68 } 69 70 void restoreEditor() { 71 if (memento == null) { 72 return; 73 } 74 String factoryID = memento 75 .getString(IWorkbenchConstants.TAG_FACTORY_ID); 76 IElementFactory factory = PlatformUI.getWorkbench().getElementFactory( 77 factoryID); 78 if (factory != null) { 79 IAdaptable element = factory.createElement(memento); 80 if (element instanceof IEditorInput) { 81 editorInput = (IEditorInput) element; 82 editorID = memento.getString(IWorkbenchConstants.TAG_ID); 83 } 84 } 85 memento = null; 86 } 87 88 void saveState(IMemento mem) { 89 if (editorInput != null) { 90 IPersistableElement persistable = editorInput.getPersistable(); 91 mem.putString(IWorkbenchConstants.TAG_ID, editorID); 92 mem.putString(IWorkbenchConstants.TAG_FACTORY_ID, persistable 93 .getFactoryId()); 94 persistable.saveState(mem); 95 } else if (memento != null) { 96 mem.putMemento(memento); 97 } 98 } 99 } 100 | Popular Tags |