KickJava   Java API By Example, From Geeks To Geeks.

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


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 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 /**
27  * An item in the editor history.
28  */

29 public class EditorHistoryItem {
30
31     private IEditorInput input;
32
33     private IEditorDescriptor descriptor;
34
35     private IMemento memento;
36
37     /**
38      * Constructs a new item.
39      */

40     public EditorHistoryItem(IEditorInput input, IEditorDescriptor descriptor) {
41         this.input = input;
42         this.descriptor = descriptor;
43     }
44
45     /**
46      * Constructs a new item from a memento.
47      */

48     public EditorHistoryItem(IMemento memento) {
49         this.memento = memento;
50     }
51
52     /**
53      * Returns the editor descriptor.
54      *
55      * @return the editor descriptor.
56      */

57     public IEditorDescriptor getDescriptor() {
58         return descriptor;
59     }
60
61     /**
62      * Returns the editor input.
63      *
64      * @return the editor input.
65      */

66     public IEditorInput getInput() {
67         return input;
68     }
69
70     /**
71      * Returns whether this item has been restored from the memento.
72      */

73     public boolean isRestored() {
74         return memento == null;
75     }
76
77     /**
78      * Returns the name of this item, either from the input if restored,
79      * otherwise from the memento.
80      */

81     public String JavaDoc getName() {
82         if (isRestored() && getInput() != null) {
83             return getInput().getName();
84         } else if (memento != null) {
85             String JavaDoc name = memento.getString(IWorkbenchConstants.TAG_NAME);
86             if (name != null) {
87                 return name;
88             }
89         }
90         return ""; //$NON-NLS-1$
91
}
92
93     /**
94      * Returns the tooltip text of this item, either from the input if restored,
95      * otherwise from the memento.
96      */

97     public String JavaDoc getToolTipText() {
98         if (isRestored() && getInput() != null) {
99             return getInput().getToolTipText();
100         } else if (memento != null) {
101             String JavaDoc name = memento.getString(IWorkbenchConstants.TAG_TOOLTIP);
102             if (name != null) {
103                 return name;
104             }
105         }
106         return ""; //$NON-NLS-1$
107
}
108
109     /**
110      * Returns whether this item matches the given editor input.
111      */

112     public boolean matches(IEditorInput input) {
113         if (isRestored()) {
114             return input.equals(getInput());
115         }
116         // if not restored, compare name, tool tip text and factory id,
117
// avoiding as much work as possible
118
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 JavaDoc inputId = persistable == null ? null : persistable
126                 .getFactoryId();
127         String JavaDoc myId = getFactoryId();
128         return myId == null ? inputId == null : myId.equals(inputId);
129     }
130
131     /**
132      * Returns the factory id of this item, either from the input if restored,
133      * otherwise from the memento.
134      * Returns <code>null</code> if there is no factory id.
135      */

136     public String JavaDoc 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     /**
151      * Restores the object state from the memento.
152      */

153     public IStatus restoreState() {
154         Assert.isTrue(!isRestored());
155
156         Status result = new Status(IStatus.OK, PlatformUI.PLUGIN_ID, 0,
157                 "", null); //$NON-NLS-1$
158
IMemento memento = this.memento;
159         this.memento = null;
160
161         String JavaDoc 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.");//$NON-NLS-1$
166
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);//$NON-NLS-1$
178
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         // Get the editor descriptor.
186
String JavaDoc 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     /**
196      * Returns whether this history item can be saved.
197      */

198     public boolean canSave() {
199         return !isRestored()
200                 || (getInput() != null && getInput().getPersistable() != null);
201     }
202
203     /**
204      * Saves the object state in the given memento.
205      *
206      * @param memento the memento to save the object state in
207      */

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                 /*
216                  * Store IPersistable of the IEditorInput in a separate section
217                  * since it could potentially use a tag already used in the parent
218                  * memento and thus overwrite data.
219                  */

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                 // save the name and tooltip separately so they can be restored
230
// without having to instantiate the input, which can activate plugins
231
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); //$NON-NLS-1$
239
}
240
241 }
242
Popular Tags