KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jface > internal > text > revisions > DiffApplier


1 /*******************************************************************************
2  * Copyright (c) 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jface.internal.text.revisions;
12
13 import java.util.Iterator JavaDoc;
14 import java.util.List JavaDoc;
15
16 import org.eclipse.jface.text.source.ILineDiffInfo;
17 import org.eclipse.jface.text.source.ILineDiffer;
18
19
20 /**
21  * Applies diff information to a list of change regions.
22  *
23  * @since 3.2
24  */

25 public final class DiffApplier {
26     /**
27      * Adjusts the {@link ChangeRegion}s in <code>regions</code> to the diff information provided
28      * by <code>lineDiffer</code>.
29      *
30      * @param regions the list of {@link ChangeRegion}s to adjust
31      * @param lineDiffer the differ
32      * @param numberOfLines the number of lines to adjust
33      */

34     public void applyDiff(List JavaDoc regions, ILineDiffer lineDiffer, int numberOfLines) {
35         clearDiffs(regions);
36         
37         int added= 0;
38         int changed= 0;
39         ILineDiffInfo info= null;
40         for (int line= 0; line < numberOfLines; line++) {
41             info= lineDiffer.getLineInfo(line);
42             if (info == null)
43                 continue;
44             
45             int changeType= info.getChangeType();
46             switch (changeType) {
47                 case ILineDiffInfo.ADDED:
48                     added++;
49                     continue;
50                 case ILineDiffInfo.CHANGED:
51                     changed++;
52                     continue;
53                 case ILineDiffInfo.UNCHANGED:
54                     added -= info.getRemovedLinesAbove();
55                     if (added != 0 || changed != 0) {
56                         applyDiff(regions, new Hunk(line - changed - Math.max(0, added), added, changed));
57                         added= 0;
58                         changed= 0;
59                         info= null;
60                     }
61             }
62         }
63         
64         // last hunk
65
if (info != null) {
66             added -= info.getRemovedLinesAbove();
67             if (added != 0 || changed != 0) {
68                 applyDiff(regions, new Hunk(numberOfLines - changed, added, changed));
69                 added= 0;
70                 changed= 0;
71             }
72             
73         }
74         
75     }
76
77     private void clearDiffs(List JavaDoc regions) {
78         for (Iterator JavaDoc it= regions.iterator(); it.hasNext();) {
79             ChangeRegion region= (ChangeRegion) it.next();
80             region.clearDiff();
81         }
82     }
83
84     /**
85      * Adjusts the change regions to one diff hunk.
86      *
87      * @param regions the list of {@link ChangeRegion}s
88      * @param hunk the diff hunk to apply
89      */

90     private void applyDiff(List JavaDoc regions, Hunk hunk) {
91         for (Iterator JavaDoc it= regions.iterator(); it.hasNext();) {
92             ChangeRegion region= (ChangeRegion) it.next();
93             region.adjustTo(hunk);
94         }
95     }
96 }
Popular Tags