KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > cheatsheets > views > ErrorPage


1 /*******************************************************************************
2  * Copyright (c) 2002, 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.ui.internal.cheatsheets.views;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.List JavaDoc;
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     /*
31      * Class used to sort status with errors first, then warnings
32      */

33     private class StatusSorter {
34         private List JavaDoc errors = new ArrayList JavaDoc();
35         private List JavaDoc warnings = new ArrayList JavaDoc();
36         private List JavaDoc info = new ArrayList JavaDoc();
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 JavaDoc getSortedStatus() {
63             List JavaDoc result = new ArrayList JavaDoc();
64             result.addAll(errors);
65             result.addAll(warnings);
66             result.addAll(info);
67             return result;
68         }
69     }
70
71     private String JavaDoc message;
72     private IStatus status;
73     
74     public ErrorPage() {
75     }
76
77     public ErrorPage(String JavaDoc 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 JavaDoc 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 JavaDoc sorted = sorter.getSortedStatus();
104         for (Iterator JavaDoc 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(), nextStatus.getMessage(), SWT.WRAP);
109             messageLabel.setLayoutData(new TableWrapData(TableWrapData.FILL_GRAB));
110         }
111     }
112     
113     /**
114      * Return the image for a status message
115      *
116      * @return
117      */

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     /**
130      * Creates the cheatsheet's title areawhich will consists
131      * of a title and image.
132      *
133      * @param parent the SWT parent for the title area composite
134      */

135     protected String JavaDoc getTitle() {
136         return Messages.ERROR_LOADING_CHEATSHEET_CONTENT;
137     }
138
139     public void initialized() {
140         // No initialization required
141
}
142 }
143
Popular Tags