001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.internal;
011:
012: import org.eclipse.core.runtime.Assert;
013: import org.eclipse.core.runtime.IAdaptable;
014: import org.eclipse.core.runtime.IStatus;
015: import org.eclipse.core.runtime.Status;
016: import org.eclipse.ui.IEditorDescriptor;
017: import org.eclipse.ui.IEditorInput;
018: import org.eclipse.ui.IEditorRegistry;
019: import org.eclipse.ui.IElementFactory;
020: import org.eclipse.ui.IMemento;
021: import org.eclipse.ui.IPersistableElement;
022: import org.eclipse.ui.PlatformUI;
023:
024: /**
025: * An item in the editor history.
026: */
027: public class EditorHistoryItem {
028:
029: private IEditorInput input;
030:
031: private IEditorDescriptor descriptor;
032:
033: private IMemento memento;
034:
035: /**
036: * Constructs a new item.
037: */
038: public EditorHistoryItem(IEditorInput input,
039: IEditorDescriptor descriptor) {
040: this .input = input;
041: this .descriptor = descriptor;
042: }
043:
044: /**
045: * Constructs a new item from a memento.
046: */
047: public EditorHistoryItem(IMemento memento) {
048: this .memento = memento;
049: }
050:
051: /**
052: * Returns the editor descriptor.
053: *
054: * @return the editor descriptor.
055: */
056: public IEditorDescriptor getDescriptor() {
057: return descriptor;
058: }
059:
060: /**
061: * Returns the editor input.
062: *
063: * @return the editor input.
064: */
065: public IEditorInput getInput() {
066: return input;
067: }
068:
069: /**
070: * Returns whether this item has been restored from the memento.
071: */
072: public boolean isRestored() {
073: return memento == null;
074: }
075:
076: /**
077: * Returns the name of this item, either from the input if restored,
078: * otherwise from the memento.
079: */
080: public String getName() {
081: if (isRestored() && getInput() != null) {
082: return getInput().getName();
083: } else if (memento != null) {
084: String name = memento
085: .getString(IWorkbenchConstants.TAG_NAME);
086: if (name != null) {
087: return name;
088: }
089: }
090: return ""; //$NON-NLS-1$
091: }
092:
093: /**
094: * Returns the tooltip text of this item, either from the input if restored,
095: * otherwise from the memento.
096: */
097: public String getToolTipText() {
098: if (isRestored() && getInput() != null) {
099: return getInput().getToolTipText();
100: } else if (memento != null) {
101: String name = memento
102: .getString(IWorkbenchConstants.TAG_TOOLTIP);
103: if (name != null) {
104: return name;
105: }
106: }
107: return ""; //$NON-NLS-1$
108: }
109:
110: /**
111: * Returns whether this item matches the given editor input.
112: */
113: public boolean matches(IEditorInput input) {
114: if (isRestored()) {
115: return input.equals(getInput());
116: }
117: // if not restored, compare name, tool tip text and factory id,
118: // avoiding as much work as possible
119: if (!getName().equals(input.getName())) {
120: return false;
121: }
122: if (!getToolTipText().equals(input.getToolTipText())) {
123: return false;
124: }
125: IPersistableElement persistable = input.getPersistable();
126: String inputId = persistable == null ? null : persistable
127: .getFactoryId();
128: String myId = getFactoryId();
129: return myId == null ? inputId == null : myId.equals(inputId);
130: }
131:
132: /**
133: * Returns the factory id of this item, either from the input if restored,
134: * otherwise from the memento.
135: * Returns <code>null</code> if there is no factory id.
136: */
137: public String getFactoryId() {
138: if (isRestored()) {
139: if (input != null) {
140: IPersistableElement persistable = input
141: .getPersistable();
142: if (persistable != null) {
143: return persistable.getFactoryId();
144: }
145: }
146: } else if (memento != null) {
147: return memento
148: .getString(IWorkbenchConstants.TAG_FACTORY_ID);
149: }
150: return null;
151: }
152:
153: /**
154: * Restores the object state from the memento.
155: */
156: public IStatus restoreState() {
157: Assert.isTrue(!isRestored());
158:
159: Status result = new Status(IStatus.OK, PlatformUI.PLUGIN_ID, 0,
160: "", null); //$NON-NLS-1$
161: IMemento memento = this .memento;
162: this .memento = null;
163:
164: String factoryId = memento
165: .getString(IWorkbenchConstants.TAG_FACTORY_ID);
166: if (factoryId == null) {
167: WorkbenchPlugin
168: .log("Unable to restore mru list - no input factory ID.");//$NON-NLS-1$
169: return result;
170: }
171: IElementFactory factory = PlatformUI.getWorkbench()
172: .getElementFactory(factoryId);
173: if (factory == null) {
174: return result;
175: }
176: IMemento persistableMemento = memento
177: .getChild(IWorkbenchConstants.TAG_PERSISTABLE);
178: if (persistableMemento == null) {
179: WorkbenchPlugin
180: .log("Unable to restore mru list - no input element state: " + factoryId);//$NON-NLS-1$
181: return result;
182: }
183: IAdaptable adaptable = factory
184: .createElement(persistableMemento);
185: if (adaptable == null
186: || (adaptable instanceof IEditorInput) == false) {
187: return result;
188: }
189: input = (IEditorInput) adaptable;
190: // Get the editor descriptor.
191: String editorId = memento.getString(IWorkbenchConstants.TAG_ID);
192: if (editorId != null) {
193: IEditorRegistry registry = WorkbenchPlugin.getDefault()
194: .getEditorRegistry();
195: descriptor = registry.findEditor(editorId);
196: }
197: return result;
198: }
199:
200: /**
201: * Returns whether this history item can be saved.
202: */
203: public boolean canSave() {
204: return !isRestored()
205: || (getInput() != null && getInput().getPersistable() != null);
206: }
207:
208: /**
209: * Saves the object state in the given memento.
210: *
211: * @param memento the memento to save the object state in
212: */
213: public IStatus saveState(IMemento memento) {
214: if (!isRestored()) {
215: memento.putMemento(this .memento);
216: } else if (input != null) {
217:
218: IPersistableElement persistable = input.getPersistable();
219: if (persistable != null) {
220: /*
221: * Store IPersistable of the IEditorInput in a separate section
222: * since it could potentially use a tag already used in the parent
223: * memento and thus overwrite data.
224: */
225: IMemento persistableMemento = memento
226: .createChild(IWorkbenchConstants.TAG_PERSISTABLE);
227: persistable.saveState(persistableMemento);
228: memento.putString(IWorkbenchConstants.TAG_FACTORY_ID,
229: persistable.getFactoryId());
230: if (descriptor != null && descriptor.getId() != null) {
231: memento.putString(IWorkbenchConstants.TAG_ID,
232: descriptor.getId());
233: }
234: // save the name and tooltip separately so they can be restored
235: // without having to instantiate the input, which can activate plugins
236: memento.putString(IWorkbenchConstants.TAG_NAME, input
237: .getName());
238: memento.putString(IWorkbenchConstants.TAG_TOOLTIP,
239: input.getToolTipText());
240: }
241: }
242: return new Status(IStatus.OK, PlatformUI.PLUGIN_ID, 0, "", null); //$NON-NLS-1$
243: }
244:
245: }
|