001: /*******************************************************************************
002: * Copyright (c) 2002, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.internal.cheatsheets.views;
011:
012: import java.util.ArrayList;
013: import java.util.Iterator;
014: import java.util.List;
015:
016: import org.eclipse.core.runtime.IStatus;
017: import org.eclipse.swt.SWT;
018: import org.eclipse.swt.graphics.Image;
019: import org.eclipse.swt.widgets.Composite;
020: import org.eclipse.swt.widgets.Label;
021: import org.eclipse.ui.forms.widgets.TableWrapData;
022: import org.eclipse.ui.internal.cheatsheets.CheatSheetPlugin;
023: import org.eclipse.ui.internal.cheatsheets.ICheatSheetResource;
024: import org.eclipse.ui.internal.cheatsheets.Messages;
025:
026: public class ErrorPage extends Page {
027:
028: /*
029: * Class used to sort status with errors first, then warnings
030: */
031: private class StatusSorter {
032: private List errors = new ArrayList();
033: private List warnings = new ArrayList();
034: private List info = new ArrayList();
035:
036: public StatusSorter(IStatus status) {
037: sortStatus(status);
038: }
039:
040: private void sortStatus(IStatus status) {
041: if (status.isMultiStatus()) {
042: IStatus[] children = status.getChildren();
043: for (int i = 0; i < children.length; i++) {
044: sortStatus(children[i]);
045: }
046: } else {
047: switch (status.getSeverity()) {
048: case IStatus.ERROR:
049: errors.add(status);
050: break;
051: case IStatus.WARNING:
052: warnings.add(status);
053: break;
054: default:
055: info.add(status);
056: }
057: }
058: }
059:
060: public List getSortedStatus() {
061: List result = new ArrayList();
062: result.addAll(errors);
063: result.addAll(warnings);
064: result.addAll(info);
065: return result;
066: }
067: }
068:
069: private String message;
070: private IStatus status;
071:
072: public ErrorPage() {
073: }
074:
075: public ErrorPage(String errorMessage) {
076: this .message = errorMessage;
077: }
078:
079: public ErrorPage(IStatus status) {
080: this .status = status;
081: }
082:
083: public void createPart(Composite parent) {
084: super .createPart(parent);
085: if (status != null) {
086: showStatus(status);
087: } else {
088: String errorString = null;
089: if (message == null) {
090: errorString = Messages.ERROR_PAGE_MESSAGE;
091: } else {
092: errorString = message;
093: }
094: Label errorLabel = toolkit.createLabel(form.getBody(),
095: errorString, SWT.WRAP);
096: errorLabel.setLayoutData(new TableWrapData(
097: TableWrapData.FILL_GRAB));
098: }
099: }
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(), ""); //$NON-NLS-1$
107: imageLabel.setImage(getImage(nextStatus.getSeverity()));
108: Label messageLabel = toolkit.createLabel(form.getBody(),
109: nextStatus.getMessage(), SWT.WRAP);
110: messageLabel.setLayoutData(new TableWrapData(
111: TableWrapData.FILL_GRAB));
112: }
113: }
114:
115: /**
116: * Return the image for a status message
117: *
118: * @return
119: */
120: private Image getImage(int severity) {
121: switch (severity) {
122: case IStatus.ERROR:
123: return CheatSheetPlugin.getPlugin().getImage(
124: ICheatSheetResource.ERROR);
125: case IStatus.WARNING:
126: return CheatSheetPlugin.getPlugin().getImage(
127: ICheatSheetResource.WARNING);
128: default:
129: return CheatSheetPlugin.getPlugin().getImage(
130: ICheatSheetResource.INFORMATION);
131: }
132: }
133:
134: /**
135: * Creates the cheatsheet's title areawhich will consists
136: * of a title and image.
137: *
138: * @param parent the SWT parent for the title area composite
139: */
140: protected String getTitle() {
141: return Messages.ERROR_LOADING_CHEATSHEET_CONTENT;
142: }
143:
144: public void initialized() {
145: // No initialization required
146: }
147: }
|