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 java.util.ArrayList;
013:
014: import org.eclipse.ui.IEditorPart;
015: import org.eclipse.ui.IMemento;
016: import org.eclipse.ui.INavigationLocation;
017: import org.eclipse.ui.INavigationLocationProvider;
018: import org.eclipse.ui.IWorkbenchPage;
019: import org.eclipse.ui.PartInitException;
020: import org.eclipse.ui.XMLMemento;
021:
022: /*
023: * Wraps the INavigationLocation and keeps editor info.
024: */
025: public class NavigationHistoryEntry {
026:
027: private IWorkbenchPage page;
028:
029: NavigationHistoryEditorInfo editorInfo;
030:
031: String historyText;
032:
033: /* Both may be set at the same time. */
034: INavigationLocation location;
035:
036: private IMemento locationMemento;
037:
038: /**
039: * Constructs a new HistoryEntry and intializes its editor input and editor id.
040: */
041: public NavigationHistoryEntry(
042: NavigationHistoryEditorInfo editorInfo,
043: IWorkbenchPage page, IEditorPart part,
044: INavigationLocation location) {
045: this .editorInfo = editorInfo;
046: this .page = page;
047: this .location = location;
048: if (location != null) {
049: historyText = location.getText();
050: }
051: // ensure that the historyText is initialized to something
052: if (historyText == null || historyText.length() == 0) {
053: if (part != null) {
054: historyText = part.getTitle();
055: }
056: }
057: }
058:
059: /**
060: * Restores the state of the entry and the location if needed and then
061: * restores the location.
062: */
063: void restoreLocation() {
064: if (editorInfo.editorInput != null
065: && editorInfo.editorID != null) {
066: try {
067: IEditorPart editor = page.openEditor(
068: editorInfo.editorInput, editorInfo.editorID,
069: true);
070: if (location == null) {
071: if (editor instanceof INavigationLocationProvider) {
072: location = ((INavigationLocationProvider) editor)
073: .createEmptyNavigationLocation();
074: }
075: }
076:
077: if (location != null) {
078: if (locationMemento != null) {
079: location.setInput(editorInfo.editorInput);
080: location.restoreState(locationMemento);
081: locationMemento = null;
082: }
083: location.restoreLocation();
084: }
085: } catch (PartInitException e) {
086: // ignore for now
087: }
088: }
089: }
090:
091: /**
092: * Return the label to display in the history drop down list. Use the
093: * history entry text if the location has not been restored yet.
094: */
095: String getHistoryText() {
096: if (location != null) {
097: // location exists or has been restored, use its text.
098: // Also update the historyText so that this value will
099: // be saved. Doing so handles cases where getText() value
100: // may be dynamic.
101: String text = location.getText();
102: if ((text == null) || text.equals("")) { //$NON-NLS-1$
103: text = historyText;
104: } else {
105: historyText = text;
106: }
107: return text;
108: } else {
109: return historyText;
110: }
111: }
112:
113: /**
114: * Saves the state of this entry and its location.
115: * Returns true if possible otherwise returns false.
116: */
117: boolean handlePartClosed() {
118: if (!editorInfo.isPersistable()) {
119: return false;
120: }
121: if (location != null) {
122: locationMemento = XMLMemento
123: .createWriteRoot(IWorkbenchConstants.TAG_POSITION);
124: location.saveState(locationMemento);
125: location.releaseState();
126: }
127: return true;
128: }
129:
130: /**
131: * Saves the state of this entry and its location.
132: */
133: void saveState(IMemento mem, ArrayList entries) {
134: mem.putString(IWorkbenchConstants.TAG_HISTORY_LABEL,
135: getHistoryText());
136: if (locationMemento != null) {
137: IMemento childMem = mem
138: .createChild(IWorkbenchConstants.TAG_POSITION);
139: childMem.putMemento(locationMemento);
140: } else if (location != null) {
141: IMemento childMem = mem
142: .createChild(IWorkbenchConstants.TAG_POSITION);
143: location.saveState(childMem);
144: }
145: }
146:
147: /**
148: * Restore the state of this entry.
149: */
150: void restoreState(IMemento mem) {
151: historyText = mem
152: .getString(IWorkbenchConstants.TAG_HISTORY_LABEL);
153: locationMemento = mem
154: .getChild(IWorkbenchConstants.TAG_POSITION);
155: }
156:
157: /*
158: * (non-Javadoc)
159: * Method declared on Object.
160: */
161: public String toString() {
162: return "Input<" + editorInfo.editorInput + "> Details<" + location + ">"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
163: }
164:
165: /**
166: * Disposes this entry and its location.
167: */
168: void dispose() {
169: if (location != null) {
170: location.dispose();
171: }
172: editorInfo = null;
173: }
174:
175: /**
176: * Merges this entry into the current entry. Returns true
177: * if the merge was possible otherwise returns false.
178: */
179: boolean mergeInto(NavigationHistoryEntry currentEntry) {
180: if (editorInfo.editorInput != null
181: && editorInfo.editorInput
182: .equals(currentEntry.editorInfo.editorInput)) {
183: if (location != null) {
184: if (currentEntry.location == null) {
185: currentEntry.location = location;
186: return true;
187: } else {
188: return location.mergeInto(currentEntry.location);
189: }
190: } else if (currentEntry.location == null) {
191: return true;
192: }
193: }
194: return false;
195: }
196: }
|