1 11 12 package org.eclipse.ui.internal.ide.undo; 13 14 import java.io.ByteArrayInputStream ; 15 import java.io.InputStream ; 16 import java.net.URI ; 17 18 import org.eclipse.core.resources.IFile; 19 import org.eclipse.core.resources.IFileState; 20 import org.eclipse.core.resources.IResource; 21 import org.eclipse.core.resources.IResourceStatus; 22 import org.eclipse.core.resources.IWorkspaceRoot; 23 import org.eclipse.core.runtime.Assert; 24 import org.eclipse.core.runtime.CoreException; 25 import org.eclipse.core.runtime.IPath; 26 import org.eclipse.core.runtime.IProgressMonitor; 27 import org.eclipse.core.runtime.OperationCanceledException; 28 import org.eclipse.core.runtime.SubProgressMonitor; 29 30 39 public class FileDescription extends AbstractResourceDescription { 40 41 String name; 42 43 URI location; 44 45 String charset; 46 47 private IFileContentDescription fileContentDescription; 48 49 57 public FileDescription(IFile file) { 58 super(file); 59 this.name = file.getName(); 60 try { 61 this.charset = file.getCharset(false); 62 } catch (CoreException e) { 63 } 65 if (file.isLinked()) { 66 location = file.getLocationURI(); 67 } 68 69 } 70 71 87 public FileDescription(IFile file, URI linkLocation, 88 IFileContentDescription fileContentDescription) { 89 super(file); 90 this.name = file.getName(); 91 this.location = linkLocation; 92 this.charset = null; 93 this.fileContentDescription = fileContentDescription; 94 } 95 96 102 public void recordStateFromHistory(IResource resource, 103 IProgressMonitor monitor) throws CoreException { 104 Assert.isLegal(resource.getType() == IResource.FILE); 105 106 if (location != null) { 107 return; 109 } 110 IFileState[] states = ((IFile) resource).getHistory(monitor); 111 if (states.length > 0) { 112 final IFileState state = getMatchingFileState(states); 113 this.fileContentDescription = new IFileContentDescription() { 114 119 public boolean exists() { 120 return state.exists(); 121 } 122 123 128 public InputStream getContents() throws CoreException { 129 return state.getContents(); 130 } 131 132 137 public String getCharset() throws CoreException { 138 return state.getCharset(); 139 } 140 }; 141 } 142 } 143 144 149 public IResource createResourceHandle() { 150 IWorkspaceRoot workspaceRoot = parent.getWorkspace().getRoot(); 151 IPath fullPath = parent.getFullPath().append(name); 152 return workspaceRoot.getFile(fullPath); 153 } 154 155 161 public void createExistentResourceFromHandle(IResource resource, 162 IProgressMonitor monitor) throws CoreException { 163 164 Assert.isLegal(resource instanceof IFile); 165 if (resource.exists()) { 166 return; 167 } 168 IFile fileHandle = (IFile) resource; 169 monitor.beginTask("", 200); monitor.setTaskName(UndoMessages.FileDescription_NewFileProgress); 171 try { 172 if (monitor.isCanceled()) { 173 throw new OperationCanceledException(); 174 } 175 if (location != null) { 176 fileHandle.createLink(location, IResource.ALLOW_MISSING_LOCAL, 177 new SubProgressMonitor(monitor, 200)); 178 } else { 179 InputStream contents = new ByteArrayInputStream ( 180 UndoMessages.FileDescription_ContentsCouldNotBeRestored 181 .getBytes()); 182 if (fileContentDescription != null 187 && fileContentDescription.exists()) { 188 contents = fileContentDescription.getContents(); 189 } 190 fileHandle.create(contents, false, new SubProgressMonitor( 191 monitor, 100)); 192 fileHandle.setCharset(charset, new SubProgressMonitor(monitor, 193 100)); 194 } 195 if (monitor.isCanceled()) { 196 throw new OperationCanceledException(); 197 } 198 } catch (CoreException e) { 199 if (e.getStatus().getCode() == IResourceStatus.PATH_OCCUPIED) { 200 fileHandle.refreshLocal(IResource.DEPTH_ZERO, null); 201 } else { 202 throw e; 203 } 204 } finally { 205 monitor.done(); 206 } 207 } 208 209 214 public boolean isValid() { 215 if (location != null) { 216 return super.isValid(); 217 } 218 return super.isValid() && fileContentDescription != null 219 && fileContentDescription.exists(); 220 } 221 222 227 public String getName() { 228 return name; 229 } 230 231 236 private IFileState getMatchingFileState(IFileState[] states) { 237 for (int i = 0; i < states.length; i++) { 238 if (localTimeStamp == states[i].getModificationTime()) { 239 return states[i]; 240 } 241 } 242 return states[0]; 243 244 } 245 246 251 protected void restoreResourceAttributes(IResource resource) 252 throws CoreException { 253 super.restoreResourceAttributes(resource); 254 Assert.isLegal(resource instanceof IFile); 255 IFile file = (IFile) resource; 256 if (charset != null) { 257 file.setCharset(charset, null); 258 } 259 } 260 } 261 | Popular Tags |