1 11 package org.eclipse.compare.internal; 12 13 import java.io.*; 14 15 import org.eclipse.core.resources.*; 16 import org.eclipse.core.runtime.*; 17 import org.eclipse.compare.*; 18 import org.eclipse.compare.structuremergeviewer.IStructureComparator; 19 20 23 public class BufferedResourceNode extends ResourceNode { 24 25 private boolean fDirty= false; 26 private IFile fDeleteFile; 27 28 33 public BufferedResourceNode(IResource resource) { 34 super(resource); 35 } 36 37 40 public boolean isDirty() { 41 return fDirty; 42 } 43 44 protected IStructureComparator createChild(IResource child) { 45 return new BufferedResourceNode(child); 46 } 47 48 public void setContent(byte[] contents) { 49 fDirty= true; 50 super.setContent(contents); 51 } 52 53 56 public void commit(IProgressMonitor pm) throws CoreException { 57 if (fDirty) { 58 59 if (fDeleteFile != null) { 60 fDeleteFile.delete(true, true, pm); 61 return; 62 } 63 64 IResource resource= getResource(); 65 if (resource instanceof IFile) { 66 67 byte[] bytes= getContent(); 68 ByteArrayInputStream is= new ByteArrayInputStream(bytes); 69 try { 70 IFile file= (IFile) resource; 71 if (file.exists()) 72 file.setContents(is, false, true, pm); 73 else 74 file.create(is, false, pm); 75 fDirty= false; 76 } finally { 77 if (is != null) 78 try { 79 is.close(); 80 } catch(IOException ex) { 81 } 83 } 84 } 85 } 86 } 87 88 public ITypedElement replace(ITypedElement child, ITypedElement other) { 89 90 if (child == null) { IResource resource= getResource(); 93 if (resource instanceof IFolder) { 94 IFolder folder= (IFolder) resource; 95 IFile file= folder.getFile(other.getName()); 96 child= new BufferedResourceNode(file); 97 } 98 } 99 100 if (other == null) { IResource resource= getResource(); 102 if (resource instanceof IFolder) { 103 IFolder folder= (IFolder) resource; 104 IFile file= folder.getFile(child.getName()); 105 if (file != null && file.exists()) { 106 fDeleteFile= file; 107 fDirty= true; 108 } 109 } 110 return null; 111 } 112 113 if (other instanceof IStreamContentAccessor && child instanceof IEditableContent) { 114 IEditableContent dst= (IEditableContent) child; 115 116 try { 117 InputStream is= ((IStreamContentAccessor)other).getContents(); 118 byte[] bytes= Utilities.readBytes(is); 119 if (bytes != null) 120 dst.setContent(bytes); 121 } catch (CoreException ex) { 122 } 124 } 125 return child; 126 } 127 } 128 129 | Popular Tags |