001: /*******************************************************************************
002: * Copyright (c) 2000, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.part;
011:
012: import java.net.URI;
013:
014: import org.eclipse.core.filesystem.EFS;
015: import org.eclipse.core.filesystem.IFileStore;
016: import org.eclipse.core.resources.IFile;
017: import org.eclipse.core.resources.IStorage;
018: import org.eclipse.core.runtime.CoreException;
019: import org.eclipse.core.runtime.IPath;
020: import org.eclipse.core.runtime.Path;
021: import org.eclipse.core.runtime.content.IContentType;
022: import org.eclipse.jface.resource.ImageDescriptor;
023: import org.eclipse.ui.IFileEditorInput;
024: import org.eclipse.ui.IMemento;
025: import org.eclipse.ui.IPathEditorInput;
026: import org.eclipse.ui.IPersistableElement;
027: import org.eclipse.ui.IURIEditorInput;
028: import org.eclipse.ui.PlatformUI;
029: import org.eclipse.ui.ide.IDE;
030: import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin;
031:
032: /**
033: * Adapter for making a file resource a suitable input for an editor.
034: * <p>
035: * This class may be instantiated; it is not intended to be subclassed.
036: * </p>
037: */
038: public class FileEditorInput implements IFileEditorInput,
039: IPathEditorInput, IURIEditorInput, IPersistableElement {
040: private IFile file;
041:
042: /**
043: * Return whether or not file is local. Only {@link IFile}s with a local
044: * value should call {@link IPathEditorInput#getPath()}
045: * @param file
046: * @return boolean <code>true</code> if the file has a local implementation.
047: */
048: public static boolean isLocalFile(IFile file) {
049:
050: IPath location = file.getLocation();
051: if (location != null)
052: return true;
053: //this is not a local file, so try to obtain a local file
054: try {
055: final URI locationURI = file.getLocationURI();
056: if (locationURI == null)
057: return false;
058: IFileStore store = EFS.getStore(locationURI);
059: //first try to obtain a local file directly fo1r this store
060: java.io.File localFile = store.toLocalFile(EFS.NONE, null);
061: //if no local file is available, obtain a cached file
062: if (localFile == null)
063: localFile = store.toLocalFile(EFS.CACHE, null);
064: if (localFile == null)
065: return false;
066: return true;
067: } catch (CoreException e) {
068: //this can only happen if the file system is not available for this scheme
069: IDEWorkbenchPlugin.log(
070: "Failed to obtain file store for resource", e); //$NON-NLS-1$
071: return false;
072: }
073:
074: }
075:
076: /**
077: * Creates an editor input based of the given file resource.
078: *
079: * @param file the file resource
080: */
081: public FileEditorInput(IFile file) {
082: if (file == null)
083: throw new IllegalArgumentException();
084: this .file = file;
085:
086: }
087:
088: /* (non-Javadoc)
089: * Method declared on Object.
090: */
091: public int hashCode() {
092: return file.hashCode();
093: }
094:
095: /* (non-Javadoc)
096: * Method declared on Object.
097: *
098: * The <code>FileEditorInput</code> implementation of this <code>Object</code>
099: * method bases the equality of two <code>FileEditorInput</code> objects on the
100: * equality of their underlying <code>IFile</code> resources.
101: */
102: public boolean equals(Object obj) {
103: if (this == obj) {
104: return true;
105: }
106: if (!(obj instanceof IFileEditorInput)) {
107: return false;
108: }
109: IFileEditorInput other = (IFileEditorInput) obj;
110: return file.equals(other.getFile());
111: }
112:
113: /* (non-Javadoc)
114: * Method declared on IEditorInput.
115: */
116: public boolean exists() {
117: return file.exists();
118: }
119:
120: /* (non-Javadoc)
121: * Method declared on IAdaptable.
122: */
123: public Object getAdapter(Class adapter) {
124: if (adapter == IFile.class) {
125: return file;
126: }
127: return file.getAdapter(adapter);
128: }
129:
130: /* (non-Javadoc)
131: * Method declared on IPersistableElement.
132: */
133: public String getFactoryId() {
134: return FileEditorInputFactory.getFactoryId();
135: }
136:
137: /* (non-Javadoc)
138: * Method declared on IFileEditorInput.
139: */
140: public IFile getFile() {
141: return file;
142: }
143:
144: /* (non-Javadoc)
145: * Method declared on IEditorInput.
146: */
147: public ImageDescriptor getImageDescriptor() {
148: IContentType contentType = IDE.getContentType(file);
149: return PlatformUI.getWorkbench().getEditorRegistry()
150: .getImageDescriptor(file.getName(), contentType);
151: }
152:
153: /* (non-Javadoc)
154: * Method declared on IEditorInput.
155: */
156: public String getName() {
157: return file.getName();
158: }
159:
160: /* (non-Javadoc)
161: * Method declared on IEditorInput.
162: */
163: public IPersistableElement getPersistable() {
164: return this ;
165: }
166:
167: /* (non-Javadoc)
168: * Method declared on IStorageEditorInput.
169: */
170: public IStorage getStorage() {
171: return file;
172: }
173:
174: /* (non-Javadoc)
175: * Method declared on IEditorInput.
176: */
177: public String getToolTipText() {
178: return file.getFullPath().makeRelative().toString();
179: }
180:
181: /* (non-Javadoc)
182: * Method declared on IPersistableElement.
183: */
184: public void saveState(IMemento memento) {
185: FileEditorInputFactory.saveState(memento, this );
186: }
187:
188: /* (non-Javadoc)
189: * @see org.eclipse.ui.IURIEditorInput#getURI()
190: */
191: public URI getURI() {
192: return file.getLocationURI();
193: }
194:
195: /* (non-Javadoc)
196: * @see org.eclipse.ui.IPathEditorInput#getPath()
197: */
198: public IPath getPath() {
199: IPath location = file.getLocation();
200: if (location != null)
201: return location;
202: //this is not a local file, so try to obtain a local file
203: try {
204: final URI locationURI = file.getLocationURI();
205: if (locationURI == null)
206: throw new IllegalArgumentException();
207: IFileStore store = EFS.getStore(locationURI);
208: //first try to obtain a local file directly fo1r this store
209: java.io.File localFile = store.toLocalFile(EFS.NONE, null);
210: //if no local file is available, obtain a cached file
211: if (localFile == null)
212: localFile = store.toLocalFile(EFS.CACHE, null);
213: if (localFile == null)
214: throw new IllegalArgumentException();
215: return Path.fromOSString(localFile.getAbsolutePath());
216: } catch (CoreException e) {
217: //this can only happen if the file system is not available for this scheme
218: IDEWorkbenchPlugin.log(
219: "Failed to obtain file store for resource", e); //$NON-NLS-1$
220: throw new RuntimeException(e);
221: }
222: }
223:
224: /* (non-Javadoc)
225: * @see java.lang.Object#toString()
226: */
227: public String toString() {
228: return getClass().getName()
229: + "(" + getFile().getFullPath() + ")"; //$NON-NLS-1$ //$NON-NLS-2$
230: }
231: }
|