1 11 package org.eclipse.compare.internal.patch; 12 13 import java.io.*; 14 import java.util.List ; 15 16 import org.eclipse.compare.*; 17 import org.eclipse.compare.internal.CompareUIPlugin; 18 import org.eclipse.core.resources.*; 19 import org.eclipse.core.runtime.CoreException; 20 import org.eclipse.jface.resource.LocalResourceManager; 21 import org.eclipse.swt.graphics.Image; 22 23 public class PatchFileTypedElement implements ITypedElement, IEncodedStreamContentAccessor { 24 25 private final FileDiffResult result; 26 private final boolean isAfterState; 27 28 public PatchFileTypedElement(FileDiffResult result, boolean isAfterState) { 29 this.result = result; 30 this.isAfterState = isAfterState; 31 } 32 33 public Image getImage() { 34 IFile file = getPatcher().getTargetFile(result.getDiff()); 35 if (file == null) { 36 DiffProject project = result.getDiff().getProject(); 38 if (project != null) { 39 file = project.getFile(result.getDiff().getPath(result.getConfiguration().isReversed())); 40 } else { 41 IResource target = getPatcher().getTarget(); 42 if (target instanceof IFile) { 43 file = (IFile) target; 44 } else if (target instanceof IContainer) { 45 IContainer container = (IContainer) target; 46 file = container.getFile(result.getTargetPath()); 47 } 48 } 49 } 50 Image image = null; 51 if (file != null) { 52 image = CompareUI.getImage(file); 53 } 54 if (result.containsProblems()) { 55 LocalResourceManager imageCache = PatchCompareEditorInput.getImageCache(result.getConfiguration()); 56 image = HunkTypedElement.getHunkErrorImage(image, imageCache, true); 57 } 58 return image; 59 } 60 61 64 public String getName() { 65 return result.getTargetPath().toString(); 66 } 67 68 71 public String getType() { 72 return result.getTargetPath().getFileExtension(); 73 } 74 75 public String getCharset() throws CoreException { 76 return null; 78 } 79 80 public InputStream getContents() throws CoreException { 81 if (isAfterState && getPatcher().hasCachedContents(result.getDiff())) 83 return new ByteArrayInputStream(getPatcher().getCachedContents(result.getDiff())); 84 List lines; 86 if (isAfterState) { 87 lines = result.getAfterLines(); 88 } else { 89 lines = result.getBeforeLines(); 90 } 91 String contents = Patcher.createString(getPatcher().isPreserveLineDelimeters(), lines); 92 String charSet = getCharset(); 93 byte[] bytes = null; 94 if (charSet != null) { 95 try { 96 bytes = contents.getBytes(charSet); 97 } catch (UnsupportedEncodingException e) { 98 CompareUIPlugin.log(e); 99 } 100 } 101 if (bytes == null) { 102 bytes = contents.getBytes(); 103 } 104 return new ByteArrayInputStream(bytes); 105 } 106 107 private Patcher getPatcher() { 108 return Patcher.getPatcher(result.getConfiguration()); 109 } 110 111 } 112 | Popular Tags |