1 11 12 package org.eclipse.ui.internal; 13 14 import org.eclipse.core.runtime.Assert; 15 import org.eclipse.core.runtime.IAdaptable; 16 import org.eclipse.core.runtime.IStatus; 17 import org.eclipse.core.runtime.Status; 18 import org.eclipse.ui.IEditorDescriptor; 19 import org.eclipse.ui.IEditorInput; 20 import org.eclipse.ui.IEditorRegistry; 21 import org.eclipse.ui.IElementFactory; 22 import org.eclipse.ui.IMemento; 23 import org.eclipse.ui.IPersistableElement; 24 import org.eclipse.ui.PlatformUI; 25 26 29 public class EditorHistoryItem { 30 31 private IEditorInput input; 32 33 private IEditorDescriptor descriptor; 34 35 private IMemento memento; 36 37 40 public EditorHistoryItem(IEditorInput input, IEditorDescriptor descriptor) { 41 this.input = input; 42 this.descriptor = descriptor; 43 } 44 45 48 public EditorHistoryItem(IMemento memento) { 49 this.memento = memento; 50 } 51 52 57 public IEditorDescriptor getDescriptor() { 58 return descriptor; 59 } 60 61 66 public IEditorInput getInput() { 67 return input; 68 } 69 70 73 public boolean isRestored() { 74 return memento == null; 75 } 76 77 81 public String getName() { 82 if (isRestored() && getInput() != null) { 83 return getInput().getName(); 84 } else if (memento != null) { 85 String name = memento.getString(IWorkbenchConstants.TAG_NAME); 86 if (name != null) { 87 return name; 88 } 89 } 90 return ""; } 92 93 97 public String getToolTipText() { 98 if (isRestored() && getInput() != null) { 99 return getInput().getToolTipText(); 100 } else if (memento != null) { 101 String name = memento.getString(IWorkbenchConstants.TAG_TOOLTIP); 102 if (name != null) { 103 return name; 104 } 105 } 106 return ""; } 108 109 112 public boolean matches(IEditorInput input) { 113 if (isRestored()) { 114 return input.equals(getInput()); 115 } 116 if (!getName().equals(input.getName())) { 119 return false; 120 } 121 if (!getToolTipText().equals(input.getToolTipText())) { 122 return false; 123 } 124 IPersistableElement persistable = input.getPersistable(); 125 String inputId = persistable == null ? null : persistable 126 .getFactoryId(); 127 String myId = getFactoryId(); 128 return myId == null ? inputId == null : myId.equals(inputId); 129 } 130 131 136 public String getFactoryId() { 137 if (isRestored()) { 138 if (input != null) { 139 IPersistableElement persistable = input.getPersistable(); 140 if (persistable != null) { 141 return persistable.getFactoryId(); 142 } 143 } 144 } else if (memento != null) { 145 return memento.getString(IWorkbenchConstants.TAG_FACTORY_ID); 146 } 147 return null; 148 } 149 150 153 public IStatus restoreState() { 154 Assert.isTrue(!isRestored()); 155 156 Status result = new Status(IStatus.OK, PlatformUI.PLUGIN_ID, 0, 157 "", null); IMemento memento = this.memento; 159 this.memento = null; 160 161 String factoryId = memento 162 .getString(IWorkbenchConstants.TAG_FACTORY_ID); 163 if (factoryId == null) { 164 WorkbenchPlugin 165 .log("Unable to restore mru list - no input factory ID."); return result; 167 } 168 IElementFactory factory = PlatformUI.getWorkbench().getElementFactory( 169 factoryId); 170 if (factory == null) { 171 return result; 172 } 173 IMemento persistableMemento = memento 174 .getChild(IWorkbenchConstants.TAG_PERSISTABLE); 175 if (persistableMemento == null) { 176 WorkbenchPlugin 177 .log("Unable to restore mru list - no input element state: " + factoryId); return result; 179 } 180 IAdaptable adaptable = factory.createElement(persistableMemento); 181 if (adaptable == null || (adaptable instanceof IEditorInput) == false) { 182 return result; 183 } 184 input = (IEditorInput) adaptable; 185 String editorId = memento.getString(IWorkbenchConstants.TAG_ID); 187 if (editorId != null) { 188 IEditorRegistry registry = WorkbenchPlugin.getDefault() 189 .getEditorRegistry(); 190 descriptor = registry.findEditor(editorId); 191 } 192 return result; 193 } 194 195 198 public boolean canSave() { 199 return !isRestored() 200 || (getInput() != null && getInput().getPersistable() != null); 201 } 202 203 208 public IStatus saveState(IMemento memento) { 209 if (!isRestored()) { 210 memento.putMemento(this.memento); 211 } else if (input != null) { 212 213 IPersistableElement persistable = input.getPersistable(); 214 if (persistable != null) { 215 220 IMemento persistableMemento = memento 221 .createChild(IWorkbenchConstants.TAG_PERSISTABLE); 222 persistable.saveState(persistableMemento); 223 memento.putString(IWorkbenchConstants.TAG_FACTORY_ID, 224 persistable.getFactoryId()); 225 if (descriptor != null && descriptor.getId() != null) { 226 memento.putString(IWorkbenchConstants.TAG_ID, descriptor 227 .getId()); 228 } 229 memento 232 .putString(IWorkbenchConstants.TAG_NAME, input 233 .getName()); 234 memento.putString(IWorkbenchConstants.TAG_TOOLTIP, input 235 .getToolTipText()); 236 } 237 } 238 return new Status(IStatus.OK, PlatformUI.PLUGIN_ID, 0, "", null); } 240 241 } 242 | Popular Tags |