KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > NavigationHistoryEntry


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11
12 package org.eclipse.ui.internal;
13
14 import java.util.ArrayList JavaDoc;
15
16 import org.eclipse.ui.IEditorPart;
17 import org.eclipse.ui.IMemento;
18 import org.eclipse.ui.INavigationLocation;
19 import org.eclipse.ui.INavigationLocationProvider;
20 import org.eclipse.ui.IWorkbenchPage;
21 import org.eclipse.ui.PartInitException;
22 import org.eclipse.ui.XMLMemento;
23
24 /*
25  * Wraps the INavigationLocation and keeps editor info.
26  */

27 public class NavigationHistoryEntry {
28
29     private IWorkbenchPage page;
30
31     NavigationHistoryEditorInfo editorInfo;
32
33     String JavaDoc historyText;
34
35     /* Both may be set at the same time. */
36     INavigationLocation location;
37
38     private IMemento locationMemento;
39
40     /**
41      * Constructs a new HistoryEntry and intializes its editor input and editor id.
42      */

43     public NavigationHistoryEntry(NavigationHistoryEditorInfo editorInfo,
44             IWorkbenchPage page, IEditorPart part, INavigationLocation location) {
45         this.editorInfo = editorInfo;
46         this.page = page;
47         this.location = location;
48         if (location != null) {
49             historyText = location.getText();
50         }
51         // ensure that the historyText is initialized to something
52
if (historyText == null || historyText.length() == 0) {
53             if (part != null) {
54                 historyText = part.getTitle();
55             }
56         }
57     }
58
59     /**
60      * Restores the state of the entry and the location if needed and then
61      * restores the location.
62      */

63     void restoreLocation() {
64         if (editorInfo.editorInput != null && editorInfo.editorID != null) {
65             try {
66                 IEditorPart editor = page.openEditor(editorInfo.editorInput,
67                         editorInfo.editorID, true);
68                 if (location == null) {
69                     if (editor instanceof INavigationLocationProvider) {
70                         location = ((INavigationLocationProvider) editor)
71                                 .createEmptyNavigationLocation();
72                     }
73                 }
74
75                 if (location != null) {
76                     if (locationMemento != null) {
77                         location.setInput(editorInfo.editorInput);
78                         location.restoreState(locationMemento);
79                         locationMemento = null;
80                     }
81                     location.restoreLocation();
82                 }
83             } catch (PartInitException e) {
84                 // ignore for now
85
}
86         }
87     }
88
89     /**
90      * Return the label to display in the history drop down list. Use the
91      * history entry text if the location has not been restored yet.
92      */

93     String JavaDoc getHistoryText() {
94         if (location != null) {
95             // location exists or has been restored, use its text.
96
// Also update the historyText so that this value will
97
// be saved. Doing so handles cases where getText() value
98
// may be dynamic.
99
String JavaDoc text = location.getText();
100             if ((text == null) || text.equals("")) { //$NON-NLS-1$
101
text = historyText;
102             } else {
103                 historyText = text;
104             }
105             return text;
106         } else {
107             return historyText;
108         }
109     }
110
111     /**
112      * Saves the state of this entry and its location.
113      * Returns true if possible otherwise returns false.
114      */

115     boolean handlePartClosed() {
116         if (!editorInfo.isPersistable()) {
117             return false;
118         }
119         if (location != null) {
120             locationMemento = XMLMemento
121                     .createWriteRoot(IWorkbenchConstants.TAG_POSITION);
122             location.saveState(locationMemento);
123             location.releaseState();
124         }
125         return true;
126     }
127
128     /**
129      * Saves the state of this entry and its location.
130      */

131     void saveState(IMemento mem, ArrayList JavaDoc entries) {
132         mem.putString(IWorkbenchConstants.TAG_HISTORY_LABEL, getHistoryText());
133         if (locationMemento != null) {
134             IMemento childMem = mem
135                     .createChild(IWorkbenchConstants.TAG_POSITION);
136             childMem.putMemento(locationMemento);
137         } else if (location != null) {
138             IMemento childMem = mem
139                     .createChild(IWorkbenchConstants.TAG_POSITION);
140             location.saveState(childMem);
141         }
142     }
143
144     /**
145      * Restore the state of this entry.
146      */

147     void restoreState(IMemento mem) {
148         historyText = mem.getString(IWorkbenchConstants.TAG_HISTORY_LABEL);
149         locationMemento = mem.getChild(IWorkbenchConstants.TAG_POSITION);
150     }
151
152     /*
153      * (non-Javadoc)
154      * Method declared on Object.
155      */

156     public String JavaDoc toString() {
157         return "Input<" + editorInfo.editorInput + "> Details<" + location + ">"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
158
}
159
160     /**
161      * Disposes this entry and its location.
162      */

163     void dispose() {
164         if (location != null) {
165             location.dispose();
166         }
167         editorInfo = null;
168     }
169
170     /**
171      * Merges this entry into the current entry. Returns true
172      * if the merge was possible otherwise returns false.
173      */

174     boolean mergeInto(NavigationHistoryEntry currentEntry) {
175         if (editorInfo.editorInput != null
176                 && editorInfo.editorInput
177                         .equals(currentEntry.editorInfo.editorInput)) {
178             if (location != null) {
179                 if (currentEntry.location == null) {
180                     currentEntry.location = location;
181                     return true;
182                 } else {
183                     return location.mergeInto(currentEntry.location);
184                 }
185             } else if (currentEntry.location == null) {
186                 return true;
187             }
188         }
189         return false;
190     }
191 }
192
Popular Tags