KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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 java.net.URI JavaDoc;
14
15 import org.eclipse.core.filesystem.EFS;
16 import org.eclipse.core.filesystem.IFileStore;
17 import org.eclipse.core.resources.IFile;
18 import org.eclipse.core.resources.IStorage;
19 import org.eclipse.core.runtime.CoreException;
20 import org.eclipse.core.runtime.IPath;
21 import org.eclipse.core.runtime.Path;
22 import org.eclipse.core.runtime.content.IContentType;
23 import org.eclipse.jface.resource.ImageDescriptor;
24 import org.eclipse.ui.IFileEditorInput;
25 import org.eclipse.ui.IMemento;
26 import org.eclipse.ui.IPathEditorInput;
27 import org.eclipse.ui.IPersistableElement;
28 import org.eclipse.ui.IURIEditorInput;
29 import org.eclipse.ui.PlatformUI;
30 import org.eclipse.ui.ide.IDE;
31 import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin;
32
33 /**
34  * Adapter for making a file resource a suitable input for an editor.
35  * <p>
36  * This class may be instantiated; it is not intended to be subclassed.
37  * </p>
38  */

39 public class FileEditorInput implements IFileEditorInput, IPathEditorInput, IURIEditorInput,
40         IPersistableElement {
41     private IFile file;
42
43     /**
44      * Creates an editor input based of the given file resource.
45      *
46      * @param file the file resource
47      */

48     public FileEditorInput(IFile file) {
49         if (file == null) {
50             throw new IllegalArgumentException JavaDoc();
51         }
52         this.file = file;
53     }
54
55     /* (non-Javadoc)
56      * Method declared on Object.
57      */

58     public int hashCode() {
59         return file.hashCode();
60     }
61
62     /* (non-Javadoc)
63      * Method declared on Object.
64      *
65      * The <code>FileEditorInput</code> implementation of this <code>Object</code>
66      * method bases the equality of two <code>FileEditorInput</code> objects on the
67      * equality of their underlying <code>IFile</code> resources.
68      */

69     public boolean equals(Object JavaDoc obj) {
70         if (this == obj) {
71             return true;
72         }
73         if (!(obj instanceof IFileEditorInput)) {
74             return false;
75         }
76         IFileEditorInput other = (IFileEditorInput) obj;
77         return file.equals(other.getFile());
78     }
79
80     /* (non-Javadoc)
81      * Method declared on IEditorInput.
82      */

83     public boolean exists() {
84         return file.exists();
85     }
86
87     /* (non-Javadoc)
88      * Method declared on IAdaptable.
89      */

90     public Object JavaDoc getAdapter(Class JavaDoc adapter) {
91         if (adapter == IFile.class) {
92             return file;
93         }
94         return file.getAdapter(adapter);
95     }
96
97     /* (non-Javadoc)
98      * Method declared on IPersistableElement.
99      */

100     public String JavaDoc getFactoryId() {
101         return FileEditorInputFactory.getFactoryId();
102     }
103
104     /* (non-Javadoc)
105      * Method declared on IFileEditorInput.
106      */

107     public IFile getFile() {
108         return file;
109     }
110
111     /* (non-Javadoc)
112      * Method declared on IEditorInput.
113      */

114     public ImageDescriptor getImageDescriptor() {
115         IContentType contentType = IDE.getContentType(file);
116         return PlatformUI.getWorkbench().getEditorRegistry()
117                 .getImageDescriptor(file.getName(), contentType);
118     }
119
120     /* (non-Javadoc)
121      * Method declared on IEditorInput.
122      */

123     public String JavaDoc getName() {
124         return file.getName();
125     }
126
127     /* (non-Javadoc)
128      * Method declared on IEditorInput.
129      */

130     public IPersistableElement getPersistable() {
131         return this;
132     }
133
134     /* (non-Javadoc)
135      * Method declared on IStorageEditorInput.
136      */

137     public IStorage getStorage() {
138         return file;
139     }
140
141     /* (non-Javadoc)
142      * Method declared on IEditorInput.
143      */

144     public String JavaDoc getToolTipText() {
145         return file.getFullPath().makeRelative().toString();
146     }
147
148     /* (non-Javadoc)
149      * Method declared on IPersistableElement.
150      */

151     public void saveState(IMemento memento) {
152         FileEditorInputFactory.saveState(memento, this);
153     }
154
155     
156
157     /* (non-Javadoc)
158      * @see org.eclipse.ui.IURIEditorInput#getURI()
159      */

160     public URI JavaDoc getURI() {
161         return file.getLocationURI();
162     }
163     
164     /* (non-Javadoc)
165      * Method declared on IPathEditorInput
166      * @since 3.0
167      */

168     public IPath getPath() {
169         IPath location = file.getLocation();
170         if (location != null)
171             return location;
172         //this is not a local file, so try to obtain a local file
173
try {
174             final URI JavaDoc locationURI = file.getLocationURI();
175             IFileStore store = EFS.getStore(locationURI);
176             //first try to obtain a local file directly fo1r this store
177
java.io.File JavaDoc localFile = store.toLocalFile(EFS.NONE, null);
178             //if no local file is available, obtain a cached file
179
if (localFile == null)
180                 localFile = store.toLocalFile(EFS.CACHE, null);
181             return Path.fromOSString(localFile.getAbsolutePath());
182         } catch (CoreException e) {
183             //this can only happen if the file system is not available for this scheme
184
IDEWorkbenchPlugin.log(
185                     "Failed to obtain file store for resource", e); //$NON-NLS-1$
186
throw new RuntimeException JavaDoc(e);
187         }
188     }
189
190     /* (non-Javadoc)
191      * @see java.lang.Object#toString()
192      */

193     public String JavaDoc toString() {
194         return getClass().getName() + "(" + getFile().getFullPath() + ")"; //$NON-NLS-1$ //$NON-NLS-2$
195
}
196 }
197
Popular Tags