| 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2000, 2008 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.runtime.IAdaptable; |
| 14 | import org.eclipse.core.runtime.Path; |
| 15 | |
| 16 | import org.eclipse.core.resources.IFile; |
| 17 | import org.eclipse.core.resources.ResourcesPlugin; |
| 18 | |
| 19 | import org.eclipse.ui.IElementFactory; |
| 20 | import org.eclipse.ui.IMemento; |
| 21 | |
| 22 | /** |
| 23 | * Factory for saving and restoring a <code>FileEditorInput</code>. |
| 24 | * The stored representation of a <code>FileEditorInput</code> remembers |
| 25 | * the full path of the file (that is, <code>IFile.getFullPath</code>). |
| 26 | * <p> |
| 27 | * The workbench will automatically create instances of this class as required. |
| 28 | * It is not intended to be instantiated or subclassed by the client. |
| 29 | * </p> |
| 30 | * @noinstantiate This class is not intended to be instantiated by clients. |
| 31 | * @noextend This class is not intended to be subclassed by clients. |
| 32 | */ |
| 33 | public class FileEditorInputFactory implements IElementFactory { |
| 34 | /** |
| 35 | * Factory id. The workbench plug-in registers a factory by this name |
| 36 | * with the "org.eclipse.ui.elementFactories" extension point. |
| 37 | */ |
| 38 | private static final String ID_FACTORY = "org.eclipse.ui.part.FileEditorInputFactory"; //$NON-NLS-1$ |
| 39 | |
| 40 | /** |
| 41 | * Tag for the IFile.fullPath of the file resource. |
| 42 | */ |
| 43 | private static final String TAG_PATH = "path"; //$NON-NLS-1$ |
| 44 | |
| 45 | /** |
| 46 | * Creates a new factory. |
| 47 | */ |
| 48 | public FileEditorInputFactory() { |
| 49 | } |
| 50 | |
| 51 | /* (non-Javadoc) |
| 52 | * Method declared on IElementFactory. |
| 53 | */ |
| 54 | public IAdaptable createElement(IMemento memento) { |
| 55 | // Get the file name. |
| 56 | String fileName = memento.getString(TAG_PATH); |
| 57 | if (fileName == null) { |
| 58 | return null; |
| 59 | } |
| 60 | |
| 61 | // Get a handle to the IFile...which can be a handle |
| 62 | // to a resource that does not exist in workspace |
| 63 | IFile file = ResourcesPlugin.getWorkspace().getRoot().getFile( |
| 64 | new Path(fileName)); |
| 65 | if (file != null) { |
| 66 | return new FileEditorInput(file); |
| 67 | } |
| 68 | return null; |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Returns the element factory id for this class. |
| 73 | * |
| 74 | * @return the element factory id |
| 75 | */ |
| 76 | public static String getFactoryId() { |
| 77 | return ID_FACTORY; |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Saves the state of the given file editor input into the given memento. |
| 82 | * |
| 83 | * @param memento the storage area for element state |
| 84 | * @param input the file editor input |
| 85 | */ |
| 86 | public static void saveState(IMemento memento, FileEditorInput input) { |
| 87 | IFile file = input.getFile(); |
| 88 | memento.putString(TAG_PATH, file.getFullPath().toString()); |
| 89 | } |
| 90 | } |