View Javadoc
1   package org.vaadin.aceeditor.client;
2   
3   import java.util.LinkedList;
4   import java.util.List;
5   
6   import com.google.gwt.core.client.JavaScriptObject;
7   import com.google.gwt.core.client.JsArray;
8   
9   public class GwtTextDiff{
10  	
11  	public static final int DIFF_DELETE = -1;
12  	public static final int DIFF_INSERT = 1;
13  	public static final int DIFF_EQUAL = 0;
14  	
15  	public static final class Patch extends JavaScriptObject {
16  		
17  		protected Patch() {
18  			
19  		}
20  		
21  		native int getStart1() /*-{
22  			return this.start1;
23  		}-*/;
24  		
25  		native public JsArray<Diff> getDiffsJsArray() /*-{
26  			return this.diffs;
27  		}-*/;
28  		
29  		public List<Diff> getDiffs() {
30  			JsArray<Diff> diffs = getDiffsJsArray();
31  			LinkedList<Diff> dili = new LinkedList<Diff>();
32  			for (int i=0; i<diffs.length(); ++i) {
33  				dili.add(diffs.get(i));
34  			}
35  			return dili;
36  		}
37  	}
38  	
39  	public static final class Diff extends JavaScriptObject {
40  		
41  		protected Diff() {
42  			
43  		}
44  		
45  		public native int getOperation() /*-{
46  			return this[0];
47  		}-*/;
48  		
49  		public native String getText() /*-{
50  			return this[1];
51  		}-*/;
52  	}
53  	
54  
55  	private static final DiffMatchPatchJSNI/../org/vaadin/aceeditor/client/DiffMatchPatchJSNI.html#DiffMatchPatchJSNI">DiffMatchPatchJSNI dmp = DiffMatchPatchJSNI
56  			.newInstance();
57  
58  	private JsArray<Patch> patches;
59  
60  //	public static final GwtTextDiff IDENTITY = new GwtTextDiff(Patch.createArray());
61  
62  	public static GwtTextDiff diff(String v1, String v2) {
63  		return new GwtTextDiff(v1, v2);
64  	}
65  
66  	private static GwtTextDiff fromPatches(JsArray<Patch> patches) {
67  		return new GwtTextDiff(patches);
68  	}
69  
70  	private GwtTextDiff(JsArray<Patch> patches) {
71  		this.patches = patches;
72  	}
73  
74  	private GwtTextDiff(String v1, String v2) {
75  		this.patches = dmp.patch_make_diff_main(v1, v2);
76  	}
77  
78  	public String applyTo(String value) {
79  		return dmp.patch_apply(patches, value);
80  	}
81  
82  	public boolean isIdentity() {
83  		return patches.length() == 0;
84  	}
85  
86  	public String getDiffString() {
87  		return getDMP().patch_toText(patches);
88  	}
89  
90  	public static int positionInNewText(String text1, int cursorPos,
91  			String text2) {
92  		// TODO: calculating the difference every time is a bit slow
93  		return dmp.diff_xIndex(dmp.diff_main(text1, text2), cursorPos);
94  	}
95  
96  	public static DiffMatchPatchJSNI getDMP() {
97  		return dmp;
98  	}
99  
100 	public static GwtTextDiff fromString(String s) {
101 		return fromPatches(dmp.patch_fromText(s));
102 	}
103 
104 	public int adjustPosition(int pos) {
105 		return dmp.diff_xIndex_patches(patches, pos);
106 	}
107 
108 	public List<Patch> getPatches() {
109 		LinkedList<Patch> pali = new LinkedList<Patch>();
110 		for (int i=0; i<patches.length(); ++i) {
111 			pali.add(patches.get(i));
112 		}
113 		return pali;
114 	}
115 	
116 
117 
118 }