1 11 package org.eclipse.ui.internal.cheatsheets.views; 12 13 import java.util.ArrayList ; 14 import java.util.Iterator ; 15 import java.util.List ; 16 17 import org.eclipse.core.runtime.IStatus; 18 import org.eclipse.swt.SWT; 19 import org.eclipse.swt.graphics.Image; 20 import org.eclipse.swt.widgets.Composite; 21 import org.eclipse.swt.widgets.Label; 22 import org.eclipse.ui.forms.widgets.TableWrapData; 23 import org.eclipse.ui.internal.cheatsheets.CheatSheetPlugin; 24 import org.eclipse.ui.internal.cheatsheets.ICheatSheetResource; 25 import org.eclipse.ui.internal.cheatsheets.Messages; 26 27 28 public class ErrorPage extends Page { 29 30 33 private class StatusSorter { 34 private List errors = new ArrayList (); 35 private List warnings = new ArrayList (); 36 private List info = new ArrayList (); 37 38 public StatusSorter(IStatus status) { 39 sortStatus(status); 40 } 41 42 private void sortStatus(IStatus status) { 43 if (status.isMultiStatus()) { 44 IStatus[] children = status.getChildren(); 45 for (int i = 0; i< children.length; i++) { 46 sortStatus(children[i]); 47 } 48 } else { 49 switch(status.getSeverity()) { 50 case IStatus.ERROR: 51 errors.add(status); 52 break; 53 case IStatus.WARNING: 54 warnings.add(status); 55 break; 56 default: 57 info.add(status); 58 } 59 } 60 } 61 62 public List getSortedStatus() { 63 List result = new ArrayList (); 64 result.addAll(errors); 65 result.addAll(warnings); 66 result.addAll(info); 67 return result; 68 } 69 } 70 71 private String message; 72 private IStatus status; 73 74 public ErrorPage() { 75 } 76 77 public ErrorPage(String errorMessage) { 78 this.message = errorMessage; 79 } 80 81 public ErrorPage(IStatus status) { 82 this.status = status; 83 } 84 85 public void createPart(Composite parent) { 86 super.createPart(parent); 87 if (status != null) { 88 showStatus(status); 89 } else { 90 String errorString = null; 91 if(message == null) { 92 errorString = Messages.ERROR_PAGE_MESSAGE; 93 } else { 94 errorString = message; 95 } 96 Label errorLabel = toolkit.createLabel(form.getBody(), errorString, SWT.WRAP); 97 errorLabel.setLayoutData(new TableWrapData(TableWrapData.FILL_GRAB)); 98 } 99 } 100 101 private void showStatus(IStatus status) { 102 StatusSorter sorter = new StatusSorter(status); 103 List sorted = sorter.getSortedStatus(); 104 for (Iterator iter = sorted.iterator(); iter.hasNext();) { 105 IStatus nextStatus = (IStatus)iter.next(); 106 Label imageLabel = toolkit.createLabel(form.getBody(), ""); imageLabel.setImage(getImage(nextStatus.getSeverity())); 108 Label messageLabel = toolkit.createLabel(form.getBody(), nextStatus.getMessage(), SWT.WRAP); 109 messageLabel.setLayoutData(new TableWrapData(TableWrapData.FILL_GRAB)); 110 } 111 } 112 113 118 private Image getImage(int severity) { 119 switch(severity) { 120 case IStatus.ERROR: 121 return CheatSheetPlugin.getPlugin().getImage(ICheatSheetResource.ERROR); 122 case IStatus.WARNING: 123 return CheatSheetPlugin.getPlugin().getImage(ICheatSheetResource.WARNING); 124 default: 125 return CheatSheetPlugin.getPlugin().getImage(ICheatSheetResource.INFORMATION); 126 } 127 } 128 129 135 protected String getTitle() { 136 return Messages.ERROR_LOADING_CHEATSHEET_CONTENT; 137 } 138 139 public void initialized() { 140 } 142 } 143 | Popular Tags |