1 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 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 61 public void aboutToCompile(SourceFile unit) { 62 String 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); this.previousSubtask = null; 70 } 71 72 75 public void checkCancel() { 76 if (monitor != null && monitor.isCanceled()) 77 throw new OperationCanceledException(); 78 } 79 80 84 public void checkCancelWithinCompiler() { 85 if (monitor != null && monitor.isCanceled() && !cancelling) { 86 setCancelling(true); 88 throw new AbortCompilation(true, null); 91 } 92 } 93 94 97 public void compiled(SourceFile unit) { 98 String 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 120 protected String problemsMessage() { 121 int numNew = newErrorCount + newWarningCount; 122 int numFixed = fixedErrorCount + fixedWarningCount; 123 if (numNew == 0 && numFixed == 0) return ""; 125 boolean displayBoth = numNew > 0 && numFixed > 0; 126 StringBuffer buffer = new StringBuffer (); 127 buffer.append('('); 128 if (numNew > 0) { 129 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(" + "); } 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(", "); } 149 if (numFixed > 0) { 150 buffer.append(Messages.build_fixedHeader); 152 buffer.append(' '); 153 if (displayBoth) { 154 buffer.append(String.valueOf(fixedErrorCount)); 155 buffer.append(" + "); 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(" + "); } 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 184 public void setCancelling(boolean cancelling) { 185 this.cancelling = cancelling; 186 } 187 188 191 public void setProgressPerCompilationUnit(float progress) { 192 this.progressPerCompilationUnit = progress; 193 } 194 195 public void subTask(String message) { 196 String pm = problemsMessage(); 197 String msg = pm.length() == 0 ? message : pm + " " + message; 199 if (msg.equals(this.previousSubtask)) return; 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 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; boolean isError = newProblem.isError(); 222 String 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; boolean wasError = IMarker.SEVERITY_ERROR 229 == pb.getAttribute(IMarker.SEVERITY, IMarker.SEVERITY_ERROR); 230 if (isError == wasError && message.equals(pb.getAttribute(IMarker.MESSAGE, ""))) { 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; boolean wasError = IMarker.SEVERITY_ERROR 244 == oldProblem.getAttribute(IMarker.SEVERITY, IMarker.SEVERITY_ERROR); 245 String message = oldProblem.getAttribute(IMarker.MESSAGE, ""); 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; 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 this.workDone = work; 270 } 271 } 272 } 273 274 public void updateProgressDelta(float percentWorked) { 275 updateProgress(percentComplete + percentWorked); 276 } 277 } 278 | Popular Tags |