1 11 12 package org.eclipse.ui.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.runtime.CoreException; 20 import org.eclipse.ui.internal.ide.undo.ContainerDescription; 21 import org.eclipse.ui.internal.ide.undo.FileDescription; 22 import org.eclipse.ui.internal.ide.undo.IFileContentDescription; 23 24 38 public class CreateFileOperation extends AbstractCreateResourcesOperation { 39 40 53 public CreateFileOperation(IFile fileHandle, URI linkLocation, 54 InputStream contents, String label) { 55 super(null, label); 56 ResourceDescription resourceDescription; 57 if (fileHandle.getParent().exists()) { 58 resourceDescription = new FileDescription(fileHandle, linkLocation, 59 createFileContentDescription(fileHandle, contents)); 60 } else { 61 ContainerDescription containerDescription = ContainerDescription 64 .fromContainer(fileHandle.getParent()); 65 containerDescription.getFirstLeafFolder() 66 .addMember( 67 new FileDescription(fileHandle, linkLocation, 68 createFileContentDescription(fileHandle, 69 contents))); 70 resourceDescription = containerDescription; 71 } 72 setResourceDescriptions(new ResourceDescription[] { resourceDescription }); 73 74 } 75 76 81 private IFileContentDescription createFileContentDescription( 82 final IFile file, final InputStream contents) { 83 return new IFileContentDescription() { 84 89 public InputStream getContents() { 90 if (contents != null) { 91 return contents; 92 } 93 return new ByteArrayInputStream (new byte[0]); 94 } 95 96 101 public String getCharset() { 102 try { 103 return file.getCharset(false); 104 } catch (CoreException e) { 105 return null; 106 } 107 } 108 109 114 public boolean exists() { 115 return true; 116 } 117 }; 118 } 119 } 120 | Popular Tags |