1 11 package org.eclipse.ltk.core.refactoring; 12 13 import java.util.ArrayList ; 14 import java.util.Iterator ; 15 import java.util.List ; 16 17 import org.eclipse.core.runtime.Assert; 18 import org.eclipse.core.runtime.IProgressMonitor; 19 import org.eclipse.core.runtime.IStatus; 20 21 43 public class RefactoringStatus { 44 45 48 public static final int OK= 0; 49 50 53 public static final int INFO= 1; 54 55 58 public static final int WARNING= 2; 59 60 63 public static final int ERROR= 3; 64 65 69 public static final int FATAL= 4; 70 71 74 private List fEntries; 75 76 81 private int fSeverity= OK; 82 83 87 public RefactoringStatus() { 88 fEntries= new ArrayList (0); 89 } 90 91 96 public int getSeverity() { 97 return fSeverity; 98 } 99 100 106 public RefactoringStatusEntry[] getEntries() { 107 return (RefactoringStatusEntry[])fEntries.toArray(new RefactoringStatusEntry[fEntries.size()]); 108 } 109 110 124 public RefactoringStatusEntry[] getEntries(IRefactoringStatusEntryComparator comparator, RefactoringStatusEntry entry) { 125 final List matches= new ArrayList (fEntries.size()); 126 RefactoringStatusEntry current= null; 127 for (Iterator iterator= fEntries.iterator(); iterator.hasNext();) { 128 current= (RefactoringStatusEntry) iterator.next(); 129 if (comparator.compare(current, entry) == 0) 130 matches.add(current); 131 } 132 return (RefactoringStatusEntry[]) matches.toArray(new RefactoringStatusEntry[matches.size()]); 133 } 134 135 141 public boolean hasEntries() { 142 return !fEntries.isEmpty(); 143 } 144 145 153 public RefactoringStatusEntry getEntryAt(int index) { 154 return (RefactoringStatusEntry)fEntries.get(index); 155 } 156 157 169 public RefactoringStatusEntry getEntryMatchingCode(String pluginId, int code) { 170 Assert.isTrue(pluginId != null); 171 for (Iterator iter= fEntries.iterator(); iter.hasNext(); ) { 172 RefactoringStatusEntry entry= (RefactoringStatusEntry)iter.next(); 173 if (pluginId.equals(entry.getPluginId()) && entry.getCode() == code) 174 return entry; 175 } 176 return null; 177 } 178 179 189 public RefactoringStatusEntry getEntryMatchingSeverity(int severity) { 190 Assert.isTrue(severity >= OK && severity <= FATAL); 191 if (severity > fSeverity) 192 return null; 193 Iterator iter= fEntries.iterator(); 194 while (iter.hasNext()) { 195 RefactoringStatusEntry entry= (RefactoringStatusEntry)iter.next(); 196 if (entry.getSeverity() >= severity) 197 return entry; 198 } 199 return null; 200 } 201 202 211 public RefactoringStatusEntry getEntryWithHighestSeverity() { 212 if (fEntries == null || fEntries.size() == 0) 213 return null; 214 RefactoringStatusEntry result= (RefactoringStatusEntry)fEntries.get(0); 215 for (int i= 1; i < fEntries.size(); i++) { 216 RefactoringStatusEntry entry= (RefactoringStatusEntry)fEntries.get(i); 217 if (result.getSeverity() < entry.getSeverity()) 218 result= entry; 219 } 220 return result; 221 } 222 223 232 public String getMessageMatchingSeverity(int severity) { 233 RefactoringStatusEntry entry= getEntryMatchingSeverity(severity); 234 if (entry == null) 235 return null; 236 return entry.getMessage(); 237 } 238 239 255 public static RefactoringStatus createStatus(int severity, String msg, RefactoringStatusContext context, String pluginId, int code, Object data) { 256 RefactoringStatus result= new RefactoringStatus(); 257 result.fEntries.add(new RefactoringStatusEntry(severity, msg, context, pluginId, code, data)); 258 result.fSeverity= severity; 259 return result; 260 } 261 262 271 public static RefactoringStatus createInfoStatus(String msg) { 272 return createStatus(INFO, msg, null, null, RefactoringStatusEntry.NO_CODE, null); 273 } 274 275 285 public static RefactoringStatus createInfoStatus(String msg, RefactoringStatusContext context) { 286 return createStatus(INFO, msg, context, null, RefactoringStatusEntry.NO_CODE, null); 287 } 288 289 298 public static RefactoringStatus createWarningStatus(String msg) { 299 return createStatus(WARNING, msg, null, null, RefactoringStatusEntry.NO_CODE, null); 300 } 301 302 312 public static RefactoringStatus createWarningStatus(String msg, RefactoringStatusContext context) { 313 return createStatus(WARNING, msg, context, null, RefactoringStatusEntry.NO_CODE, null); 314 } 315 316 325 public static RefactoringStatus createErrorStatus(String msg) { 326 return createStatus(ERROR, msg, null, null, RefactoringStatusEntry.NO_CODE, null); 327 } 328 329 339 public static RefactoringStatus createErrorStatus(String msg, RefactoringStatusContext context) { 340 return createStatus(ERROR, msg, context, null, RefactoringStatusEntry.NO_CODE, null); 341 } 342 343 352 public static RefactoringStatus createFatalErrorStatus(String msg) { 353 return createStatus(FATAL, msg, null, null, RefactoringStatusEntry.NO_CODE, null); 354 } 355 356 366 public static RefactoringStatus createFatalErrorStatus(String msg, RefactoringStatusContext context) { 367 return createStatus(FATAL, msg, context, null, RefactoringStatusEntry.NO_CODE, null); 368 } 369 370 385 public static RefactoringStatus create(IStatus status) { 386 if (status.isOK()) 387 return new RefactoringStatus(); 388 389 if (!status.isMultiStatus()) { 390 switch (status.getSeverity()) { 391 case IStatus.OK : 392 return new RefactoringStatus(); 393 case IStatus.INFO : 394 return RefactoringStatus.createWarningStatus(status.getMessage()); 395 case IStatus.WARNING : 396 return RefactoringStatus.createErrorStatus(status.getMessage()); 397 case IStatus.ERROR : 398 return RefactoringStatus.createFatalErrorStatus(status.getMessage()); 399 case IStatus.CANCEL : 400 return RefactoringStatus.createFatalErrorStatus(status.getMessage()); 401 default : 402 return RefactoringStatus.createFatalErrorStatus(status.getMessage()); 403 } 404 } else { 405 IStatus[] children= status.getChildren(); 406 RefactoringStatus result= new RefactoringStatus(); 407 for (int i= 0; i < children.length; i++) { 408 result.merge(RefactoringStatus.create(children[i])); 409 } 410 return result; 411 } 412 } 413 414 423 public void merge(RefactoringStatus other) { 424 if (other == null) 425 return; 426 fEntries.addAll(other.fEntries); 427 fSeverity= Math.max(fSeverity, other.getSeverity()); 428 } 429 430 439 public void addInfo(String msg) { 440 addInfo(msg, null); 441 } 442 443 453 public void addInfo(String msg, RefactoringStatusContext context) { 454 fEntries.add(new RefactoringStatusEntry(RefactoringStatus.INFO, msg, context)); 455 fSeverity= Math.max(fSeverity, INFO); 456 } 457 458 467 public void addWarning(String msg) { 468 addWarning(msg, null); 469 } 470 471 481 public void addWarning(String msg, RefactoringStatusContext context) { 482 fEntries.add(new RefactoringStatusEntry(RefactoringStatus.WARNING, msg, context)); 483 fSeverity= Math.max(fSeverity, WARNING); 484 } 485 486 496 public void addError(String msg) { 497 addError(msg, null); 498 } 499 500 511 public void addError(String msg, RefactoringStatusContext context) { 512 fEntries.add(new RefactoringStatusEntry(RefactoringStatus.ERROR, msg, context)); 513 fSeverity= Math.max(fSeverity, ERROR); 514 } 515 516 524 public void addFatalError(String msg) { 525 addFatalError(msg, null); 526 } 527 528 537 public void addFatalError(String msg, RefactoringStatusContext context) { 538 fEntries.add(new RefactoringStatusEntry(RefactoringStatus.FATAL, msg, context)); 539 fSeverity= Math.max(fSeverity, FATAL); 540 } 541 542 555 public void addEntry(int severity, String msg, RefactoringStatusContext context, String pluginId, int code) { 556 fEntries.add(new RefactoringStatusEntry(severity, msg, context, pluginId, code)); 557 fSeverity= Math.max(fSeverity, severity); 558 } 559 560 574 public void addEntry(int severity, String msg, RefactoringStatusContext context, String pluginId, int code, Object data) { 575 fEntries.add(new RefactoringStatusEntry(severity, msg, context, pluginId, code, data)); 576 fSeverity= Math.max(fSeverity, severity); 577 } 578 579 586 public void addEntry(RefactoringStatusEntry entry) { 587 Assert.isNotNull(entry); 588 fEntries.add(entry); 589 fSeverity= Math.max(fSeverity, entry.getSeverity()); 590 } 591 592 598 public boolean isOK() { 599 return fSeverity == OK; 600 } 601 602 609 public boolean hasFatalError() { 610 return fSeverity == FATAL; 611 } 612 613 621 public boolean hasError() { 622 return fSeverity == FATAL || fSeverity == ERROR; 623 } 624 625 633 public boolean hasWarning() { 634 return fSeverity == FATAL || fSeverity == ERROR || fSeverity == WARNING; 635 } 636 637 646 public boolean hasInfo() { 647 return fSeverity == FATAL || fSeverity == ERROR || fSeverity == WARNING || fSeverity == INFO; 648 } 649 650 654 public String toString() { 655 StringBuffer buff= new StringBuffer (); 656 buff.append("<") .append(getSeverityString(fSeverity)).append("\n"); if (!isOK()) { 659 for (Iterator iter= fEntries.iterator(); iter.hasNext(); ) { 660 buff.append("\t") .append(iter.next()).append("\n"); } 663 } 664 buff.append(">"); return buff.toString(); 666 } 667 668 672 static String getSeverityString(int severity) { 673 Assert.isTrue(severity >= OK && severity <= FATAL); 674 if (severity == RefactoringStatus.OK) 675 return "OK"; if (severity == RefactoringStatus.INFO) 677 return "INFO"; if (severity == RefactoringStatus.WARNING) 679 return "WARNING"; if (severity == RefactoringStatus.ERROR) 681 return "ERROR"; if (severity == RefactoringStatus.FATAL) 683 return "FATALERROR"; return null; 685 } 686 } 687 | Popular Tags |