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.IAdaptable;
013: import org.eclipse.ui.IEditorInput;
014: import org.eclipse.ui.IEditorPart;
015: import org.eclipse.ui.IElementFactory;
016: import org.eclipse.ui.IMemento;
017: import org.eclipse.ui.IPersistableElement;
018: import org.eclipse.ui.PlatformUI;
019: import org.eclipse.ui.XMLMemento;
020:
021: /**
022: * Keeps the info to save, restore or identify and editor.
023: * Instances of this class are shared between history entries and there should be
024: * only one instance making reference to the same editor.
025: */
026: public class NavigationHistoryEditorInfo {
027: String editorID;
028:
029: IEditorInput editorInput;
030:
031: int refCount = 0;
032:
033: IMemento memento;
034:
035: NavigationHistoryEditorInfo(IEditorPart part) {
036: editorID = part.getSite().getId();
037: editorInput = part.getEditorInput();
038: }
039:
040: NavigationHistoryEditorInfo(IMemento memento) {
041: this .memento = memento;
042: }
043:
044: boolean isPersistable() {
045: if (editorInput != null) {
046: IPersistableElement persistable = editorInput
047: .getPersistable();
048: return persistable != null;
049: }
050: return memento != null;
051: }
052:
053: void handlePartClosed() {
054: if (!isPersistable()) {
055: return;
056: }
057: if (memento == null) {
058: IPersistableElement persistable = editorInput
059: .getPersistable();
060: memento = XMLMemento
061: .createWriteRoot(IWorkbenchConstants.TAG_EDITOR);
062: memento.putString(IWorkbenchConstants.TAG_ID, editorID);
063: memento.putString(IWorkbenchConstants.TAG_FACTORY_ID,
064: persistable.getFactoryId());
065: persistable.saveState(memento);
066: }
067: editorID = null;
068: editorInput = null;
069: }
070:
071: void restoreEditor() {
072: if (memento == null) {
073: return;
074: }
075: String factoryID = memento
076: .getString(IWorkbenchConstants.TAG_FACTORY_ID);
077: IElementFactory factory = PlatformUI.getWorkbench()
078: .getElementFactory(factoryID);
079: if (factory != null) {
080: IAdaptable element = factory.createElement(memento);
081: if (element instanceof IEditorInput) {
082: editorInput = (IEditorInput) element;
083: editorID = memento
084: .getString(IWorkbenchConstants.TAG_ID);
085: }
086: }
087: memento = null;
088: }
089:
090: void saveState(IMemento mem) {
091: if (editorInput != null) {
092: IPersistableElement persistable = editorInput
093: .getPersistable();
094: mem.putString(IWorkbenchConstants.TAG_ID, editorID);
095: mem.putString(IWorkbenchConstants.TAG_FACTORY_ID,
096: persistable.getFactoryId());
097: persistable.saveState(mem);
098: } else if (memento != null) {
099: mem.putMemento(memento);
100: }
101: }
102: }
|