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.compare.internal.ContentChangeNotifier; 19 import org.eclipse.compare.patch.PatchConfiguration; 20 import org.eclipse.core.resources.IFile; 21 import org.eclipse.core.runtime.CoreException; 22 23 public class UnmatchedHunkTypedElement extends HunkTypedElement implements IContentChangeNotifier, IEditableContent { 24 25 private ContentChangeNotifier changeNotifier; 26 27 public UnmatchedHunkTypedElement(HunkResult result) { 28 super(result, false, true); 30 } 31 32 35 public synchronized void addContentChangeListener(IContentChangeListener listener) { 36 if (changeNotifier == null) 37 changeNotifier = new ContentChangeNotifier(this); 38 changeNotifier.addContentChangeListener(listener); 39 } 40 41 44 public synchronized void removeContentChangeListener(IContentChangeListener listener) { 45 if (changeNotifier != null) 46 changeNotifier.removeContentChangeListener(listener); 47 } 48 49 52 public boolean isEditable() { 53 IFile file = ((WorkspaceFileDiffResult)getHunkResult().getDiffResult()).getTargetFile(); 54 return file != null && file.isAccessible(); 55 } 56 57 60 public ITypedElement replace(ITypedElement dest, ITypedElement src) { 61 return null; 63 } 64 65 68 public void setContent(byte[] newContent) { 69 getPatcher().setManuallyMerged(getHunkResult().getHunk(), true); 70 getPatcher().cacheContents(getDiff(), newContent); 71 if (changeNotifier != null) 72 changeNotifier.fireContentChanged(); 73 } 74 75 private FileDiff getDiff() { 76 return getHunkResult().getDiffResult().getDiff(); 77 } 78 79 private Patcher getPatcher() { 80 return Patcher.getPatcher(getConfiguration()); 81 } 82 83 86 public InputStream getContents() throws CoreException { 87 if (getPatcher().hasCachedContents(getDiff())) 89 return new ByteArrayInputStream(getPatcher().getCachedContents(getDiff())); 90 List lines = getHunkResult().getDiffResult().getAfterLines(); 92 String content = Patcher.createString(getHunkResult().getDiffResult().isPreserveLineDelimeters(), lines); 93 byte[] bytes = null; 94 if (getCharset() != null) 95 try { 96 bytes = content.getBytes(getCharset()); 97 } catch (UnsupportedEncodingException e) { 98 CompareUIPlugin.log(e); 99 } 100 if (bytes == null) 101 bytes = content.getBytes(); 102 return new ByteArrayInputStream(bytes); 103 } 104 105 private PatchConfiguration getConfiguration() { 106 return getHunkResult().getDiffResult().getConfiguration(); 107 } 108 } 109 | Popular Tags |