KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jface > util > SafeRunnableDialog


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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.jface.util;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Collection JavaDoc;
15
16 import org.eclipse.core.runtime.IStatus;
17 import org.eclipse.jface.dialogs.ErrorDialog;
18 import org.eclipse.jface.dialogs.IDialogConstants;
19 import org.eclipse.jface.resource.JFaceResources;
20 import org.eclipse.jface.viewers.CellLabelProvider;
21 import org.eclipse.jface.viewers.ISelection;
22 import org.eclipse.jface.viewers.ISelectionChangedListener;
23 import org.eclipse.jface.viewers.IStructuredContentProvider;
24 import org.eclipse.jface.viewers.IStructuredSelection;
25 import org.eclipse.jface.viewers.SelectionChangedEvent;
26 import org.eclipse.jface.viewers.TableViewer;
27 import org.eclipse.jface.viewers.Viewer;
28 import org.eclipse.jface.viewers.ViewerCell;
29 import org.eclipse.jface.viewers.ViewerComparator;
30 import org.eclipse.swt.SWT;
31 import org.eclipse.swt.graphics.Point;
32 import org.eclipse.swt.layout.GridData;
33 import org.eclipse.swt.widgets.Button;
34 import org.eclipse.swt.widgets.Composite;
35 import org.eclipse.swt.widgets.Control;
36
37 /**
38  * SafeRunnableDialog is a dialog that can show the results of multiple safe
39  * runnable errors
40  *
41  */

42 class SafeRunnableDialog extends ErrorDialog {
43
44     private TableViewer statusListViewer;
45
46     private Collection JavaDoc statuses = new ArrayList JavaDoc();
47
48     /**
49      * Create a new instance of the receiver on a status.
50      *
51      * @param status
52      * The status to display.
53      */

54     SafeRunnableDialog(IStatus status) {
55
56         super(null, JFaceResources.getString("error"), status.getMessage(), //$NON-NLS-1$
57
status, IStatus.ERROR);
58
59         setShellStyle(SWT.DIALOG_TRIM | SWT.MODELESS | SWT.RESIZE | SWT.MIN
60                 | getDefaultOrientation());
61
62         setStatus(status);
63         statuses.add(status);
64         
65         setBlockOnOpen(false);
66
67         String JavaDoc reason = JFaceResources
68                 .getString("SafeRunnableDialog_checkDetailsMessage"); //$NON-NLS-1$
69
if (status.getException() != null) {
70             reason = status.getException().getMessage() == null ? status
71                     .getException().toString() : status.getException()
72                     .getMessage();
73         }
74         this.message = JFaceResources.format(JFaceResources
75                 .getString("SafeRunnableDialog_reason"), new Object JavaDoc[] { //$NON-NLS-1$
76
status.getMessage(), reason });
77     }
78
79     /**
80      * Method which should be invoked when new errors become available for
81      * display
82      */

83     void refresh() {
84
85         if (AUTOMATED_MODE)
86             return;
87
88         createStatusList((Composite) dialogArea);
89         updateEnablements();
90     }
91
92     /*
93      * (non-Javadoc)
94      *
95      * @see org.eclipse.jface.dialogs.ErrorDialog#createDialogArea(org.eclipse.swt.widgets.Composite)
96      */

97     protected Control createDialogArea(Composite parent) {
98         Control area = super.createDialogArea(parent);
99         createStatusList((Composite) area);
100         return area;
101     }
102
103     /**
104      * Create the status list if required.
105      *
106      * @param parent
107      * the Control to create it in.
108      */

109     private void createStatusList(Composite parent) {
110         if (isMultipleStatusDialog()) {
111             if (statusListViewer == null) {
112                 // The job list doesn't exist so create it.
113
setMessage(JFaceResources
114                         .getString("SafeRunnableDialog_MultipleErrorsMessage")); //$NON-NLS-1$
115
getShell()
116                         .setText(
117                                 JFaceResources
118                                         .getString("SafeRunnableDialog_MultipleErrorsTitle")); //$NON-NLS-1$
119
createStatusListArea(parent);
120                 showDetailsArea();
121             }
122             refreshStatusList();
123         }
124     }
125
126     /*
127      * Update the button enablements
128      */

129     private void updateEnablements() {
130         Button details = getButton(IDialogConstants.DETAILS_ID);
131         if (details != null) {
132             details.setEnabled(true);
133         }
134     }
135
136     /**
137      * This method sets the message in the message label.
138      *
139      * @param messageString -
140      * the String for the message area
141      */

142     private void setMessage(String JavaDoc messageString) {
143         // must not set null text in a label
144
message = messageString == null ? "" : messageString; //$NON-NLS-1$
145
if (messageLabel == null || messageLabel.isDisposed()) {
146             return;
147         }
148         messageLabel.setText(message);
149     }
150
151     /**
152      * Create an area that allow the user to select one of multiple jobs that
153      * have reported errors
154      *
155      * @param parent -
156      * the parent of the area
157      */

158     private void createStatusListArea(Composite parent) {
159         // Display a list of jobs that have reported errors
160
statusListViewer = new TableViewer(parent, SWT.SINGLE | SWT.H_SCROLL
161                 | SWT.V_SCROLL | SWT.BORDER);
162         statusListViewer.setComparator(getViewerComparator());
163         Control control = statusListViewer.getControl();
164         GridData data = new GridData(GridData.FILL_BOTH
165                 | GridData.GRAB_HORIZONTAL | GridData.GRAB_VERTICAL);
166         data.heightHint = convertHeightInCharsToPixels(10);
167         control.setLayoutData(data);
168         statusListViewer.setContentProvider(getStatusContentProvider());
169         statusListViewer.setLabelProvider(getStatusListLabelProvider());
170         statusListViewer
171                 .addSelectionChangedListener(new ISelectionChangedListener() {
172                     public void selectionChanged(SelectionChangedEvent event) {
173                         handleSelectionChange();
174                     }
175                 });
176         applyDialogFont(parent);
177         statusListViewer.setInput(this);
178     }
179
180     /**
181      * Return the label provider for the status list.
182      *
183      * @return CellLabelProvider
184      */

185     private CellLabelProvider getStatusListLabelProvider() {
186         return new CellLabelProvider() {
187             /*
188              * (non-Javadoc)
189              *
190              * @see org.eclipse.jface.viewers.CellLabelProvider#update(org.eclipse.jface.viewers.ViewerCell)
191              */

192             public void update(ViewerCell cell) {
193                 cell.setText(((IStatus) cell.getElement()).getMessage());
194
195             }
196         };
197     }
198
199     /**
200      * Return the content provider for the statuses.
201      *
202      * @return IStructuredContentProvider
203      */

204     private IStructuredContentProvider getStatusContentProvider() {
205         return new IStructuredContentProvider() {
206             /*
207              * (non-Javadoc)
208              *
209              * @see org.eclipse.jface.viewers.IStructuredContentProvider#getElements(java.lang.Object)
210              */

211             public Object JavaDoc[] getElements(Object JavaDoc inputElement) {
212                 return statuses.toArray();
213             }
214
215             /*
216              * (non-Javadoc)
217              *
218              * @see org.eclipse.jface.viewers.IContentProvider#dispose()
219              */

220             public void dispose() {
221
222             }
223
224             /*
225              * (non-Javadoc)
226              *
227              * @see org.eclipse.jface.viewers.IContentProvider#inputChanged(org.eclipse.jface.viewers.Viewer,
228              * java.lang.Object, java.lang.Object)
229              */

230             public void inputChanged(Viewer viewer, Object JavaDoc oldInput,
231                     Object JavaDoc newInput) {
232
233             }
234         };
235     }
236
237     /*
238      * Return whether there are multiple errors to be displayed
239      */

240     private boolean isMultipleStatusDialog() {
241         return statuses.size() > 1;
242     }
243
244     /**
245      * Return a viewer sorter for looking at the jobs.
246      *
247      * @return ViewerSorter
248      */

249     private ViewerComparator getViewerComparator() {
250         return new ViewerComparator() {
251             /*
252              * (non-Javadoc)
253              *
254              * @see org.eclipse.jface.viewers.ViewerComparator#compare(org.eclipse.jface.viewers.Viewer,
255              * java.lang.Object, java.lang.Object)
256              */

257             public int compare(Viewer testViewer, Object JavaDoc e1, Object JavaDoc e2) {
258                 String JavaDoc message1 = ((IStatus) e1).getMessage();
259                 String JavaDoc message2 = ((IStatus) e2).getMessage();
260                 if (message1 == null)
261                     return 1;
262                 if (message2 == null)
263                     return -1;
264
265                 return message1.compareTo(message2);
266             }
267         };
268     }
269
270     /**
271      * Refresh the contents of the viewer.
272      */

273     void refreshStatusList() {
274         if (statusListViewer != null
275                 && !statusListViewer.getControl().isDisposed()) {
276             statusListViewer.refresh();
277             Point newSize = getShell().computeSize(SWT.DEFAULT, SWT.DEFAULT);
278             getShell().setSize(newSize);
279         }
280     }
281
282     /**
283      * Get the single selection. Return null if the selection is not just one
284      * element.
285      *
286      * @return IStatus or <code>null</code>.
287      */

288     private IStatus getSingleSelection() {
289         ISelection rawSelection = statusListViewer.getSelection();
290         if (rawSelection != null
291                 && rawSelection instanceof IStructuredSelection) {
292             IStructuredSelection selection = (IStructuredSelection) rawSelection;
293             if (selection.size() == 1) {
294                 return (IStatus) selection.getFirstElement();
295             }
296         }
297         return null;
298     }
299
300     /**
301      * The selection in the multiple job list has changed. Update widget
302      * enablements and repopulate the list.
303      */

304     void handleSelectionChange() {
305         IStatus newSelection = getSingleSelection();
306         setStatus(newSelection);
307         updateEnablements();
308         showDetailsArea();
309     }
310
311     /*
312      * (non-Javadoc)
313      *
314      * @see org.eclipse.jface.dialogs.ErrorDialog#shouldShowDetailsButton()
315      */

316     protected boolean shouldShowDetailsButton() {
317         return true;
318     }
319
320     /**
321      * Add the status to the receiver.
322      * @param status
323      */

324     public void addStatus(IStatus status) {
325         statuses.add(status);
326         refresh();
327         
328     }
329
330     
331 }
332
Popular Tags