KickJava   Java API By Example, From Geeks To Geeks.

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


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 package org.eclipse.ui.internal;
12
13 import org.eclipse.core.runtime.IAdaptable;
14 import org.eclipse.ui.IEditorInput;
15 import org.eclipse.ui.IEditorPart;
16 import org.eclipse.ui.IElementFactory;
17 import org.eclipse.ui.IMemento;
18 import org.eclipse.ui.IPersistableElement;
19 import org.eclipse.ui.PlatformUI;
20 import org.eclipse.ui.XMLMemento;
21
22 /**
23  * Keeps the info to save, restore or identify and editor.
24  * Instances of this class are shared between history entries and there should be
25  * only one instance making reference to the same editor.
26  */

27 public class NavigationHistoryEditorInfo {
28     String JavaDoc editorID;
29
30     IEditorInput editorInput;
31
32     int refCount = 0;
33
34     IMemento memento;
35
36     NavigationHistoryEditorInfo(IEditorPart part) {
37         editorID = part.getSite().getId();
38         editorInput = part.getEditorInput();
39     }
40
41     NavigationHistoryEditorInfo(IMemento memento) {
42         this.memento = memento;
43     }
44
45     boolean isPersistable() {
46         if (editorInput != null) {
47             IPersistableElement persistable = editorInput.getPersistable();
48             return persistable != null;
49         }
50         return memento != null;
51     }
52
53     void handlePartClosed() {
54         if (!isPersistable()) {
55             return;
56         }
57         if (memento == null) {
58             IPersistableElement persistable = editorInput.getPersistable();
59             memento = XMLMemento
60                     .createWriteRoot(IWorkbenchConstants.TAG_EDITOR);
61             memento.putString(IWorkbenchConstants.TAG_ID, editorID);
62             memento.putString(IWorkbenchConstants.TAG_FACTORY_ID, persistable
63                     .getFactoryId());
64             persistable.saveState(memento);
65         }
66         editorID = null;
67         editorInput = null;
68     }
69
70     void restoreEditor() {
71         if (memento == null) {
72             return;
73         }
74         String JavaDoc factoryID = memento
75                 .getString(IWorkbenchConstants.TAG_FACTORY_ID);
76         IElementFactory factory = PlatformUI.getWorkbench().getElementFactory(
77                 factoryID);
78         if (factory != null) {
79             IAdaptable element = factory.createElement(memento);
80             if (element instanceof IEditorInput) {
81                 editorInput = (IEditorInput) element;
82                 editorID = memento.getString(IWorkbenchConstants.TAG_ID);
83             }
84         }
85         memento = null;
86     }
87
88     void saveState(IMemento mem) {
89         if (editorInput != null) {
90             IPersistableElement persistable = editorInput.getPersistable();
91             mem.putString(IWorkbenchConstants.TAG_ID, editorID);
92             mem.putString(IWorkbenchConstants.TAG_FACTORY_ID, persistable
93                     .getFactoryId());
94             persistable.saveState(mem);
95         } else if (memento != null) {
96             mem.putMemento(memento);
97         }
98     }
99 }
100
Popular Tags