KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > part > FileEditorInputFactory


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.part;
12
13 import org.eclipse.core.resources.IFile;
14 import org.eclipse.core.resources.ResourcesPlugin;
15 import org.eclipse.core.runtime.IAdaptable;
16 import org.eclipse.core.runtime.Path;
17 import org.eclipse.ui.IElementFactory;
18 import org.eclipse.ui.IMemento;
19
20 /**
21  * Factory for saving and restoring a <code>FileEditorInput</code>.
22  * The stored representation of a <code>FileEditorInput</code> remembers
23  * the full path of the file (that is, <code>IFile.getFullPath</code>).
24  * <p>
25  * The workbench will automatically create instances of this class as required.
26  * It is not intended to be instantiated or subclassed by the client.
27  * </p>
28  */

29 public class FileEditorInputFactory implements IElementFactory {
30     /**
31      * Factory id. The workbench plug-in registers a factory by this name
32      * with the "org.eclipse.ui.elementFactories" extension point.
33      */

34     private static final String JavaDoc ID_FACTORY = "org.eclipse.ui.part.FileEditorInputFactory"; //$NON-NLS-1$
35

36     /**
37      * Tag for the IFile.fullPath of the file resource.
38      */

39     private static final String JavaDoc TAG_PATH = "path"; //$NON-NLS-1$
40

41     /**
42      * Creates a new factory.
43      */

44     public FileEditorInputFactory() {
45     }
46
47     /* (non-Javadoc)
48      * Method declared on IElementFactory.
49      */

50     public IAdaptable createElement(IMemento memento) {
51         // Get the file name.
52
String JavaDoc fileName = memento.getString(TAG_PATH);
53         if (fileName == null) {
54             return null;
55         }
56
57         // Get a handle to the IFile...which can be a handle
58
// to a resource that does not exist in workspace
59
IFile file = ResourcesPlugin.getWorkspace().getRoot().getFile(
60                 new Path(fileName));
61         if (file != null) {
62             return new FileEditorInput(file);
63         } else {
64             return null;
65         }
66     }
67
68     /**
69      * Returns the element factory id for this class.
70      *
71      * @return the element factory id
72      */

73     public static String JavaDoc getFactoryId() {
74         return ID_FACTORY;
75     }
76
77     /**
78      * Saves the state of the given file editor input into the given memento.
79      *
80      * @param memento the storage area for element state
81      * @param input the file editor input
82      */

83     public static void saveState(IMemento memento, FileEditorInput input) {
84         IFile file = input.getFile();
85         memento.putString(TAG_PATH, file.getFullPath().toString());
86     }
87 }
88
Popular Tags