1 11 package org.eclipse.ui.part; 12 13 import java.net.URI ; 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 39 public class FileEditorInput implements IFileEditorInput, IPathEditorInput, IURIEditorInput, 40 IPersistableElement { 41 private IFile file; 42 43 48 public FileEditorInput(IFile file) { 49 if (file == null) { 50 throw new IllegalArgumentException (); 51 } 52 this.file = file; 53 } 54 55 58 public int hashCode() { 59 return file.hashCode(); 60 } 61 62 69 public boolean equals(Object 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 83 public boolean exists() { 84 return file.exists(); 85 } 86 87 90 public Object getAdapter(Class adapter) { 91 if (adapter == IFile.class) { 92 return file; 93 } 94 return file.getAdapter(adapter); 95 } 96 97 100 public String getFactoryId() { 101 return FileEditorInputFactory.getFactoryId(); 102 } 103 104 107 public IFile getFile() { 108 return file; 109 } 110 111 114 public ImageDescriptor getImageDescriptor() { 115 IContentType contentType = IDE.getContentType(file); 116 return PlatformUI.getWorkbench().getEditorRegistry() 117 .getImageDescriptor(file.getName(), contentType); 118 } 119 120 123 public String getName() { 124 return file.getName(); 125 } 126 127 130 public IPersistableElement getPersistable() { 131 return this; 132 } 133 134 137 public IStorage getStorage() { 138 return file; 139 } 140 141 144 public String getToolTipText() { 145 return file.getFullPath().makeRelative().toString(); 146 } 147 148 151 public void saveState(IMemento memento) { 152 FileEditorInputFactory.saveState(memento, this); 153 } 154 155 156 157 160 public URI getURI() { 161 return file.getLocationURI(); 162 } 163 164 168 public IPath getPath() { 169 IPath location = file.getLocation(); 170 if (location != null) 171 return location; 172 try { 174 final URI locationURI = file.getLocationURI(); 175 IFileStore store = EFS.getStore(locationURI); 176 java.io.File localFile = store.toLocalFile(EFS.NONE, null); 178 if (localFile == null) 180 localFile = store.toLocalFile(EFS.CACHE, null); 181 return Path.fromOSString(localFile.getAbsolutePath()); 182 } catch (CoreException e) { 183 IDEWorkbenchPlugin.log( 185 "Failed to obtain file store for resource", e); throw new RuntimeException (e); 187 } 188 } 189 190 193 public String toString() { 194 return getClass().getName() + "(" + getFile().getFullPath() + ")"; } 196 } 197 | Popular Tags |