KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > core > builder > BuildNotifier


1 /*******************************************************************************
2  * Copyright (c) 2000, 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.jdt.internal.core.builder;
12
13 import org.eclipse.core.resources.*;
14 import org.eclipse.core.runtime.*;
15
16 import org.eclipse.jdt.core.compiler.CategorizedProblem;
17 import org.eclipse.jdt.core.compiler.IProblem;
18 import org.eclipse.jdt.internal.compiler.problem.AbortCompilation;
19 import org.eclipse.jdt.internal.core.util.Messages;
20
21 public class BuildNotifier {
22
23 protected IProgressMonitor monitor;
24 protected boolean cancelling;
25 protected float percentComplete;
26 protected float progressPerCompilationUnit;
27 protected int newErrorCount;
28 protected int fixedErrorCount;
29 protected int newWarningCount;
30 protected int fixedWarningCount;
31 protected int workDone;
32 protected int totalWork;
33 protected String JavaDoc previousSubtask;
34
35 public static int NewErrorCount = 0;
36 public static int FixedErrorCount = 0;
37 public static int NewWarningCount = 0;
38 public static int FixedWarningCount = 0;
39
40 public static void resetProblemCounters() {
41     NewErrorCount = 0;
42     FixedErrorCount = 0;
43     NewWarningCount = 0;
44     FixedWarningCount = 0;
45 }
46
47 public BuildNotifier(IProgressMonitor monitor, IProject project) {
48     this.monitor = monitor;
49     this.cancelling = false;
50     this.newErrorCount = NewErrorCount;
51     this.fixedErrorCount = FixedErrorCount;
52     this.newWarningCount = NewWarningCount;
53     this.fixedWarningCount = FixedWarningCount;
54     this.workDone = 0;
55     this.totalWork = 1000000;
56 }
57
58 /**
59  * Notification before a compile that a unit is about to be compiled.
60  */

61 public void aboutToCompile(SourceFile unit) {
62     String JavaDoc message = Messages.bind(Messages.build_compiling, unit.resource.getFullPath().removeLastSegments(1).makeRelative().toString());
63     subTask(message);
64 }
65
66 public void begin() {
67     if (monitor != null)
68         monitor.beginTask("", totalWork); //$NON-NLS-1$
69
this.previousSubtask = null;
70 }
71
72 /**
73  * Check whether the build has been canceled.
74  */

75 public void checkCancel() {
76     if (monitor != null && monitor.isCanceled())
77         throw new OperationCanceledException();
78 }
79
80 /**
81  * Check whether the build has been canceled.
82  * Must use this call instead of checkCancel() when within the compiler.
83  */

84 public void checkCancelWithinCompiler() {
85     if (monitor != null && monitor.isCanceled() && !cancelling) {
86         // Once the compiler has been canceled, don't check again.
87
setCancelling(true);
88         // Only AbortCompilation can stop the compiler cleanly.
89
// We check cancelation again following the call to compile.
90
throw new AbortCompilation(true, null);
91     }
92 }
93
94 /**
95  * Notification while within a compile that a unit has finished being compiled.
96  */

97 public void compiled(SourceFile unit) {
98     String JavaDoc message = Messages.bind(Messages.build_compiling, unit.resource.getFullPath().removeLastSegments(1).makeRelative().toString());
99     subTask(message);
100     updateProgressDelta(progressPerCompilationUnit);
101     checkCancelWithinCompiler();
102 }
103
104 public void done() {
105     NewErrorCount = this.newErrorCount;
106     FixedErrorCount = this.fixedErrorCount;
107     NewWarningCount = this.newWarningCount;
108     FixedWarningCount = this.fixedWarningCount;
109
110     updateProgress(1.0f);
111     subTask(Messages.build_done);
112     if (monitor != null)
113         monitor.done();
114     this.previousSubtask = null;
115 }
116
117 /**
118  * Returns a string describing the problems.
119  */

120 protected String JavaDoc problemsMessage() {
121     int numNew = newErrorCount + newWarningCount;
122     int numFixed = fixedErrorCount + fixedWarningCount;
123     if (numNew == 0 && numFixed == 0) return ""; //$NON-NLS-1$
124

125     boolean displayBoth = numNew > 0 && numFixed > 0;
126     StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
127     buffer.append('(');
128     if (numNew > 0) {
129         // (Found x errors + y warnings)
130
buffer.append(Messages.build_foundHeader);
131         buffer.append(' ');
132         if (displayBoth || newErrorCount > 0) {
133             if (newErrorCount == 1)
134                 buffer.append(Messages.build_oneError);
135             else
136                 buffer.append(Messages.bind(Messages.build_multipleErrors, String.valueOf(newErrorCount)));
137             if (displayBoth || newWarningCount > 0)
138                 buffer.append(" + "); //$NON-NLS-1$
139
}
140         if (displayBoth || newWarningCount > 0) {
141             if (newWarningCount == 1)
142                 buffer.append(Messages.build_oneWarning);
143             else
144                 buffer.append(Messages.bind(Messages.build_multipleWarnings, String.valueOf(newWarningCount)));
145         }
146         if (numFixed > 0)
147             buffer.append(", "); //$NON-NLS-1$
148
}
149     if (numFixed > 0) {
150         // (Fixed x errors + y warnings) or (Found x errors + y warnings, Fixed x + y)
151
buffer.append(Messages.build_fixedHeader);
152         buffer.append(' ');
153         if (displayBoth) {
154             buffer.append(String.valueOf(fixedErrorCount));
155             buffer.append(" + "); //$NON-NLS-1$
156
buffer.append(String.valueOf(fixedWarningCount));
157         } else {
158             if (fixedErrorCount > 0) {
159                 if (fixedErrorCount == 1)
160                     buffer.append(Messages.build_oneError);
161                 else
162                     buffer.append(Messages.bind(Messages.build_multipleErrors, String.valueOf(fixedErrorCount)));
163                 if (fixedWarningCount > 0)
164                     buffer.append(" + "); //$NON-NLS-1$
165
}
166             if (fixedWarningCount > 0) {
167                 if (fixedWarningCount == 1)
168                     buffer.append(Messages.build_oneWarning);
169                 else
170                     buffer.append(Messages.bind(Messages.build_multipleWarnings, String.valueOf(fixedWarningCount)));
171             }
172         }
173     }
174     buffer.append(')');
175     return buffer.toString();
176 }
177
178 /**
179  * Sets the cancelling flag, which indicates we are in the middle
180  * of being cancelled. Certain places (those callable indirectly from the compiler)
181  * should not check cancel again while this is true, to avoid OperationCanceledException
182  * being thrown at an inopportune time.
183  */

184 public void setCancelling(boolean cancelling) {
185     this.cancelling = cancelling;
186 }
187
188 /**
189  * Sets the amount of progress to report for compiling each compilation unit.
190  */

191 public void setProgressPerCompilationUnit(float progress) {
192     this.progressPerCompilationUnit = progress;
193 }
194
195 public void subTask(String JavaDoc message) {
196     String JavaDoc pm = problemsMessage();
197     String JavaDoc msg = pm.length() == 0 ? message : pm + " " + message; //$NON-NLS-1$
198

199     if (msg.equals(this.previousSubtask)) return; // avoid refreshing with same one
200
//if (JavaBuilder.DEBUG) System.out.println(msg);
201
if (monitor != null)
202         monitor.subTask(msg);
203
204     this.previousSubtask = msg;
205 }
206
207 protected void updateProblemCounts(CategorizedProblem[] newProblems) {
208     for (int i = 0, l = newProblems.length; i < l; i++)
209         if (newProblems[i].isError()) newErrorCount++; else newWarningCount++;
210 }
211
212 /**
213  * Update the problem counts from one compilation result given the old and new problems,
214  * either of which may be null.
215  */

216 protected void updateProblemCounts(IMarker[] oldProblems, CategorizedProblem[] newProblems) {
217     if (newProblems != null) {
218         next : for (int i = 0, l = newProblems.length; i < l; i++) {
219             CategorizedProblem newProblem = newProblems[i];
220             if (newProblem.getID() == IProblem.Task) continue; // skip task
221
boolean isError = newProblem.isError();
222             String JavaDoc message = newProblem.getMessage();
223
224             if (oldProblems != null) {
225                 for (int j = 0, m = oldProblems.length; j < m; j++) {
226                     IMarker pb = oldProblems[j];
227                     if (pb == null) continue; // already matched up with a new problem
228
boolean wasError = IMarker.SEVERITY_ERROR
229                         == pb.getAttribute(IMarker.SEVERITY, IMarker.SEVERITY_ERROR);
230                     if (isError == wasError && message.equals(pb.getAttribute(IMarker.MESSAGE, ""))) { //$NON-NLS-1$
231
oldProblems[j] = null;
232                         continue next;
233                     }
234                 }
235             }
236             if (isError) newErrorCount++; else newWarningCount++;
237         }
238     }
239     if (oldProblems != null) {
240         next : for (int i = 0, l = oldProblems.length; i < l; i++) {
241             IMarker oldProblem = oldProblems[i];
242             if (oldProblem == null) continue next; // already matched up with a new problem
243
boolean wasError = IMarker.SEVERITY_ERROR
244                 == oldProblem.getAttribute(IMarker.SEVERITY, IMarker.SEVERITY_ERROR);
245             String JavaDoc message = oldProblem.getAttribute(IMarker.MESSAGE, ""); //$NON-NLS-1$
246

247             if (newProblems != null) {
248                 for (int j = 0, m = newProblems.length; j < m; j++) {
249                     CategorizedProblem pb = newProblems[j];
250                     if (pb.getID() == IProblem.Task) continue; // skip task
251
if (wasError == pb.isError() && message.equals(pb.getMessage()))
252                         continue next;
253                 }
254             }
255             if (wasError) fixedErrorCount++; else fixedWarningCount++;
256         }
257     }
258 }
259
260 public void updateProgress(float newPercentComplete) {
261     if (newPercentComplete > this.percentComplete) {
262         this.percentComplete = Math.min(newPercentComplete, 1.0f);
263         int work = Math.round(this.percentComplete * this.totalWork);
264         if (work > this.workDone) {
265             if (monitor != null)
266                 monitor.worked(work - this.workDone);
267             //if (JavaBuilder.DEBUG)
268
//System.out.println(java.text.NumberFormat.getPercentInstance().format(this.percentComplete));
269
this.workDone = work;
270         }
271     }
272 }
273
274 public void updateProgressDelta(float percentWorked) {
275     updateProgress(percentComplete + percentWorked);
276 }
277 }
278
Popular Tags