1 11 package org.eclipse.compare.internal.patch; 12 13 import java.util.*; 14 15 import org.eclipse.compare.patch.*; 16 import org.eclipse.compare.structuremergeviewer.Differencer; 17 import org.eclipse.core.resources.IStorage; 18 import org.eclipse.core.runtime.*; 19 20 24 public class FileDiff implements IFilePatch { 25 26 private IPath fOldPath, fNewPath; 27 private List fHunks= new ArrayList(); 28 private DiffProject fProject; private String header; 30 31 38 protected FileDiff(IPath oldPath, long oldDate, IPath newPath, long newDate) { 39 fOldPath= oldPath; 40 fNewPath= newPath; 41 } 42 43 47 public DiffProject getProject() { 48 return fProject; 49 } 50 51 57 void setProject(DiffProject diffProject) { 58 if (fProject == diffProject) 59 return; 60 if (fProject != null) 61 fProject.remove(this); 62 this.fProject= diffProject; 63 } 64 65 71 protected IPath getPath(boolean reverse) { 72 if (getDiffType(reverse) == Differencer.ADDITION) { 73 if (reverse) 74 return fOldPath; 75 return fNewPath; 76 } 77 if (reverse && fNewPath != null) 78 return fNewPath; 79 if (fOldPath != null) 80 return fOldPath; 81 return fNewPath; 82 } 83 84 88 protected void add(Hunk hunk) { 89 fHunks.add(hunk); 90 hunk.setParent(this); 91 } 92 93 97 protected void remove(Hunk hunk) { 98 fHunks.remove(hunk); 99 } 100 101 105 public Hunk[] getHunks() { 106 return (Hunk[]) fHunks.toArray(new Hunk[fHunks.size()]); 107 } 108 109 113 public int getHunkCount() { 114 return fHunks.size(); 115 } 116 117 122 public int getDiffType(boolean reverse) { 123 if (fHunks.size() == 1) { 124 boolean add = false; 125 boolean delete = false; 126 Iterator iter = fHunks.iterator(); 127 while (iter.hasNext()){ 128 Hunk hunk = (Hunk) iter.next(); 129 int type =hunk.getHunkType(reverse); 130 if (type == Hunk.ADDED){ 131 add = true; 132 } else if (type == Hunk.DELETED ){ 133 delete = true; 134 } 135 } 136 if (add && !delete){ 137 return Differencer.ADDITION; 138 } else if (!add && delete){ 139 return Differencer.DELETION; 140 } 141 } 142 return Differencer.CHANGE; 143 } 144 145 153 protected IPath getStrippedPath(int strip, boolean reverse) { 154 IPath path= getPath(reverse); 155 if (strip > 0 && strip < path.segmentCount()) 156 path= path.removeFirstSegments(strip); 157 return path; 158 } 159 160 164 public int segmentCount() { 165 int length= 99; 168 if (fOldPath != null) 169 length= Math.min(length, fOldPath.segmentCount()); 170 if (fNewPath != null) 171 length= Math.min(length, fNewPath.segmentCount()); 172 return length; 173 } 174 175 public IFilePatchResult apply(IStorage contents, 176 PatchConfiguration configuration, IProgressMonitor monitor) { 177 FileDiffResult result = new FileDiffResult(this, configuration); 178 result.refresh(contents, monitor); 179 return result; 180 } 181 182 public IPath getTargetPath(PatchConfiguration configuration) { 183 return getStrippedPath(configuration.getPrefixSegmentStripCount(), configuration.isReversed()); 184 } 185 186 public FileDiff asRelativeDiff() { 187 if (fProject == null) 188 return this; 189 IPath adjustedOldPath = null; 190 if (fOldPath != null) { 191 adjustedOldPath = new Path(null, fProject.getName()).append(fOldPath); 192 } 193 IPath adjustedNewPath = null; 194 if (fNewPath != null) { 195 adjustedNewPath = new Path(null, fProject.getName()).append(fNewPath); 196 } 197 FileDiff diff = new FileDiff(adjustedOldPath, 0, adjustedNewPath, 0); 198 for (Iterator iterator = fHunks.iterator(); iterator.hasNext();) { 199 Hunk hunk = (Hunk) iterator.next(); 200 new Hunk(diff, hunk); 202 } 203 return diff; 204 } 205 206 public void setHeader(String header) { 207 this.header = header; 208 } 209 210 public String getHeader() { 211 return header; 212 } 213 } 214 | Popular Tags |