KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > lib > cvsclient > command > diff > DiffInformation


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.lib.cvsclient.command.diff;
21
22 import java.io.*;
23 import java.util.*;
24
25 import org.netbeans.lib.cvsclient.command.*;
26
27 /**
28  * Describes diff information for 2 fversions of a file. This is the result of doing a
29  * cvs diff command. The fields in instances of this object are populated
30  * by response handlers.
31  * @author Milos Kleint
32  */

33 public class DiffInformation extends FileInfoContainer {
34     private File file;
35
36     private String JavaDoc repositoryFileName;
37
38     private String JavaDoc rightRevision;
39
40     private String JavaDoc leftRevision;
41
42     private String JavaDoc parameters;
43
44     /**
45      * List of changes stored here
46      */

47     private final List changesList = new ArrayList();
48
49     private Iterator iterator;
50
51     public DiffInformation() {
52     }
53
54     /**
55      * Getter for property file.
56      * @return Value of property file.
57      */

58     public File getFile() {
59         return file;
60     }
61
62     /**
63      * Setter for property file.
64      * @param file New value of property file.
65      */

66     public void setFile(File file) {
67         this.file = file;
68     }
69
70     /**
71      * Getter for property repositoryFileName.
72      * @return Value of property repositoryFileName.
73      */

74     public String JavaDoc getRepositoryFileName() {
75         return repositoryFileName;
76     }
77
78     /**
79      * Setter for property repositoryFileName.
80      * @param repositoryRevision New value of property repositoryFileName.
81      */

82     public void setRepositoryFileName(String JavaDoc repositoryFileName) {
83         this.repositoryFileName = repositoryFileName;
84     }
85
86     /**
87      * Return a string representation of this object. Useful for debugging.
88      */

89     public String JavaDoc toString() {
90         StringBuffer JavaDoc buf = new StringBuffer JavaDoc(30);
91         buf.append("\nFile: " + ((file != null)?file.getAbsolutePath():"null")); //NOI18N
92
buf.append("\nRCS file: " + repositoryFileName); //NOI18N
93
buf.append("\nRevision: " + leftRevision); //NOI18N
94
if (rightRevision != null) {
95             buf.append("\nRevision: " + rightRevision); //NOI18N
96
}
97         buf.append("\nParameters: " + parameters); //NOI18N
98
// buf.append(differences.toString());
99
return buf.toString();
100     }
101
102     /** Getter for property rightRevision.
103      * @return Value of property rightRevision.
104      */

105     public String JavaDoc getRightRevision() {
106         return rightRevision;
107     }
108
109     /** Setter for property rightRevision.
110      * @param rightRevision New value of property rightRevision.
111      */

112     public void setRightRevision(String JavaDoc rightRevision) {
113         this.rightRevision = rightRevision;
114     }
115
116     /** Getter for property leftRevision.
117      * @return Value of property leftRevision.
118      */

119     public String JavaDoc getLeftRevision() {
120         return leftRevision;
121     }
122
123     /** Setter for property leftRevision.
124      * @param leftRevision New value of property leftRevision.
125      */

126     public void setLeftRevision(String JavaDoc leftRevision) {
127         this.leftRevision = leftRevision;
128     }
129
130     public String JavaDoc getParameters() {
131         return parameters;
132     }
133
134     public void setParameters(String JavaDoc parameters) {
135         this.parameters = parameters;
136     }
137
138     public DiffChange createDiffChange() {
139         return new DiffChange();
140     }
141
142     public void addChange(DiffChange change) {
143         changesList.add(change);
144     }
145
146     public DiffChange getFirstChange() {
147         iterator = changesList.iterator();
148         return getNextChange();
149     }
150
151     public DiffChange getNextChange() {
152         if (iterator == null) {
153             return null;
154         }
155         if (!iterator.hasNext()) {
156             return null;
157         }
158         return (DiffChange)iterator.next();
159     }
160
161     public class DiffChange {
162         public static final int ADD = 0;
163         public static final int DELETE = 1;
164         public static final int CHANGE = 2;
165
166         protected int type;
167         private int leftBeginning = -1;
168         private int leftEnd = -1;
169         private final List leftDiff = new ArrayList();
170         private int rightBeginning = -1;
171         private int rightEnd = -1;
172         private final List rightDiff = new ArrayList();
173
174         public DiffChange() {
175         }
176
177         public void setType(int typeChange) {
178 // System.out.println("type=" + typeChange);
179
type = typeChange;
180         }
181
182         public int getType() {
183             return type;
184         }
185
186         public void setLeftRange(int min, int max) {
187 // System.out.println("setLeftRange() min=" + min + " max=" +max);
188
leftBeginning = min;
189             leftEnd = max;
190         }
191
192         public void setRightRange(int min, int max) {
193 // System.out.println("setRightRange() min=" + min + " max=" +max);
194
rightBeginning = min;
195             rightEnd = max;
196         }
197
198         public int getMainBeginning() {
199             return rightBeginning;
200         }
201
202         public int getRightMin() {
203             return rightBeginning;
204         }
205
206         public int getRightMax() {
207             return rightEnd;
208         }
209
210         public int getLeftMin() {
211             return leftBeginning;
212         }
213
214         public int getLeftMax() {
215             return leftEnd;
216         }
217
218         public boolean isInRange(int number, boolean left) {
219             if (left) {
220                 return (number >= leftBeginning && number <= leftEnd);
221             }
222
223             return (number >= rightBeginning && number <= rightEnd);
224         }
225
226         public String JavaDoc getLine(int number, boolean left) {
227             if (left) {
228                 int index = number - leftBeginning;
229                 if (index < 0 || index >= leftDiff.size()) {
230                     return null;
231                 }
232                 String JavaDoc line = (String JavaDoc)leftDiff.get(index);
233                 return line;
234             }
235             else {
236                 int index = number - rightBeginning;
237                 if (index < 0 || index >= rightDiff.size()) {
238                     return null;
239                 }
240                 String JavaDoc line = (String JavaDoc)rightDiff.get(index);
241                 return line;
242             }
243         }
244
245         public void appendLeftLine(String JavaDoc diffLine) {
246             leftDiff.add(diffLine);
247         }
248
249         public void appendRightLine(String JavaDoc diffLine) {
250             rightDiff.add(diffLine);
251         }
252     }
253 }
Popular Tags