KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > progress > ErrorNotificationDialog


1 /*******************************************************************************
2  * Copyright (c) 2004 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.ui.internal.progress;
12 import java.util.ArrayList JavaDoc;
13 import java.util.Collection JavaDoc;
14
15 import org.eclipse.core.runtime.IStatus;
16 import org.eclipse.jface.dialogs.Dialog;
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.IContentProvider;
21 import org.eclipse.jface.viewers.ILabelProviderListener;
22 import org.eclipse.jface.viewers.ISelection;
23 import org.eclipse.jface.viewers.ISelectionChangedListener;
24 import org.eclipse.jface.viewers.IStructuredContentProvider;
25 import org.eclipse.jface.viewers.IStructuredSelection;
26 import org.eclipse.jface.viewers.ITableLabelProvider;
27 import org.eclipse.jface.viewers.SelectionChangedEvent;
28 import org.eclipse.jface.viewers.TableViewer;
29 import org.eclipse.jface.viewers.Viewer;
30 import org.eclipse.jface.viewers.ViewerSorter;
31 import org.eclipse.swt.SWT;
32 import org.eclipse.swt.dnd.Clipboard;
33 import org.eclipse.swt.dnd.TextTransfer;
34 import org.eclipse.swt.dnd.Transfer;
35 import org.eclipse.swt.events.DisposeEvent;
36 import org.eclipse.swt.events.DisposeListener;
37 import org.eclipse.swt.events.MouseAdapter;
38 import org.eclipse.swt.events.MouseEvent;
39 import org.eclipse.swt.events.SelectionAdapter;
40 import org.eclipse.swt.events.SelectionEvent;
41 import org.eclipse.swt.events.SelectionListener;
42 import org.eclipse.swt.graphics.Image;
43 import org.eclipse.swt.graphics.Rectangle;
44 import org.eclipse.swt.layout.GridData;
45 import org.eclipse.swt.widgets.Button;
46 import org.eclipse.swt.widgets.Composite;
47 import org.eclipse.swt.widgets.Control;
48 import org.eclipse.swt.widgets.List;
49 import org.eclipse.swt.widgets.Menu;
50 import org.eclipse.swt.widgets.MenuItem;
51 import org.eclipse.swt.widgets.Shell;
52 /**
53  * The ErrorNotificationDialog is is the dialog that comes up when an error has
54  * occured.
55  */

56 public class ErrorNotificationDialog extends Dialog {
57     TableViewer errorViewer;
58     Button clearButton;
59     List JavaDoc detailsList;
60     private Clipboard clipboard;
61     private ErrorInfo selectedError = null;
62
63     /**
64      * Reserve room for this many details list items.
65      */

66     private static final int DETAILS_LIST_ITEM_COUNT = 7;
67
68     /**
69      * The nesting indent.
70      */

71     private static final String JavaDoc NESTING_INDENT = " "; //$NON-NLS-1$
72

73     /**
74      * Create a new instance of the receiver.
75      *
76      * @param parentShell
77      */

78     public ErrorNotificationDialog(Shell parentShell) {
79         super(parentShell == null
80                 ? ProgressManagerUtil.getDefaultParent()
81                 : parentShell);
82         setBlockOnOpen(false);
83         setShellStyle(SWT.CLOSE | SWT.MODELESS | SWT.BORDER | SWT.TITLE
84                 | SWT.RESIZE);
85     }
86     /*
87      * (non-Javadoc)
88      *
89      * @see org.eclipse.jface.window.Window#configureShell(org.eclipse.swt.widgets.Shell)
90      */

91     protected void configureShell(Shell newShell) {
92         super.configureShell(newShell);
93         newShell.setText(ProgressMessages
94                 .getString("ErrorNotificationDialog.ErrorNotificationTitle")); //$NON-NLS-1$
95
newShell.addDisposeListener(new DisposeListener() {
96             /*
97              * (non-Javadoc)
98              *
99              * @see org.eclipse.swt.events.DisposeListener#widgetDisposed(org.eclipse.swt.events.DisposeEvent)
100              */

101             public void widgetDisposed(DisposeEvent e) {
102                 getManager().clearDialog();
103             }
104         });
105     }
106
107     /*
108      * (non-Javadoc)
109      *
110      * @see org.eclipse.jface.window.Window#getShellStyle()
111      */

112     protected int getShellStyle() {
113         return super.getShellStyle() | SWT.MIN;
114     }
115     /*
116      * (non-Javadoc)
117      *
118      * @see org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets.Composite)
119      */

120     protected Control createDialogArea(Composite parent) {
121         initializeDialogUnits(parent);
122         Composite topArea = (Composite) super.createDialogArea(parent);
123         errorViewer = new TableViewer(topArea, SWT.MULTI | SWT.H_SCROLL
124                 | SWT.V_SCROLL | SWT.BORDER);
125         errorViewer.setSorter(getViewerSorter());
126         errorViewer.getControl().addMouseListener(new MouseAdapter() {
127             /*
128              * (non-Javadoc)
129              *
130              * @see org.eclipse.swt.events.MouseAdapter#mouseDoubleClick(org.eclipse.swt.events.MouseEvent)
131              */

132             public void mouseDoubleClick(MouseEvent e) {
133                 openErrorDialog();
134             }
135         });
136         errorViewer
137                 .addSelectionChangedListener(new ISelectionChangedListener() {
138                     public void selectionChanged(SelectionChangedEvent event) {
139
140                         clearButton.setEnabled(!errorViewer.getSelection()
141                                 .isEmpty());
142                         setDetailsContents();
143                     }
144                 });
145         Control control = errorViewer.getControl();
146         GridData data = new GridData(GridData.FILL_BOTH
147                 | GridData.GRAB_HORIZONTAL | GridData.GRAB_VERTICAL);
148         data.widthHint = convertWidthInCharsToPixels(60);
149         data.heightHint = convertHeightInCharsToPixels(10);
150         control.setLayoutData(data);
151         initContentProvider();
152         initLabelProvider();
153         applyDialogFont(parent);
154
155         createDetailsList(topArea);
156         return topArea;
157     }
158     /*
159      * (non-Javadoc)
160      *
161      * @see org.eclipse.jface.dialogs.Dialog#createButtonsForButtonBar(org.eclipse.swt.widgets.Composite)
162      */

163     protected void createButtonsForButtonBar(Composite parent) {
164
165         clearButton = createButton(
166                 parent,
167                 IDialogConstants.CLIENT_ID + 2,
168                 ProgressMessages
169                         .getString("ErrorNotificationDialog.ClearButtonTitle"), false); //$NON-NLS-1$
170
clearButton.setEnabled(false);
171         clearButton.addSelectionListener(new SelectionAdapter() {
172             public void widgetSelected(SelectionEvent e) {
173                 ISelection rawSelection = errorViewer.getSelection();
174                 if (rawSelection != null
175                         && rawSelection instanceof IStructuredSelection) {
176                     IStructuredSelection selection = (IStructuredSelection) rawSelection;
177                     getManager().removeErrors(selection.toList());
178                 }
179                 refresh();
180             }
181         });
182         Button button = createButton(parent, IDialogConstants.CLOSE_ID,
183                 IDialogConstants.CLOSE_LABEL, true);
184         button.addSelectionListener(new SelectionListener() {
185             /*
186              * (non-Javadoc)
187              *
188              * @see org.eclipse.swt.events.SelectionListener#widgetSelected(org.eclipse.swt.events.SelectionEvent)
189              */

190             public void widgetSelected(SelectionEvent e) {
191                 close();
192             }
193             /*
194              * (non-Javadoc)
195              *
196              * @see org.eclipse.swt.events.SelectionListener#widgetDefaultSelected(org.eclipse.swt.events.SelectionEvent)
197              */

198             public void widgetDefaultSelected(SelectionEvent e) {
199                 close();
200             }
201         });
202     }
203     /**
204      * Return a viewer sorter for looking at the jobs.
205      *
206      * @return
207      */

208     private ViewerSorter getViewerSorter() {
209         return new ViewerSorter() {
210             /*
211              * (non-Javadoc)
212              *
213              * @see org.eclipse.jface.viewers.ViewerSorter#compare(org.eclipse.jface.viewers.Viewer,
214              * java.lang.Object, java.lang.Object)
215              */

216             public int compare(Viewer testViewer, Object JavaDoc e1, Object JavaDoc e2) {
217                 return ((Comparable JavaDoc) e1).compareTo(e2);
218             }
219         };
220     }
221     /**
222      * Sets the content provider for the viewer.
223      */

224     protected void initContentProvider() {
225         IContentProvider provider = new IStructuredContentProvider() {
226             /*
227              * (non-Javadoc)
228              *
229              * @see org.eclipse.jface.viewers.IContentProvider#dispose()
230              */

231             public void dispose() {
232                 //Nothing of interest here
233
}
234             /*
235              * (non-Javadoc)
236              *
237              * @see org.eclipse.jface.viewers.IStructuredContentProvider#getElements(java.lang.Object)
238              */

239             public Object JavaDoc[] getElements(Object JavaDoc inputElement) {
240                 return getManager().getErrors().toArray();
241             }
242             /*
243              * (non-Javadoc)
244              *
245              * @see org.eclipse.jface.viewers.IContentProvider#inputChanged(org.eclipse.jface.viewers.Viewer,
246              * java.lang.Object, java.lang.Object)
247              */

248             public void inputChanged(Viewer viewer, Object JavaDoc oldInput,
249                     Object JavaDoc newInput) {
250                 if (newInput != null)
251                     refresh();
252             }
253         };
254         errorViewer.setContentProvider(provider);
255         errorViewer.setInput(getManager());
256     }
257     /**
258      * Get the notificationManager that this is being created for.
259      *
260      * @return
261      */

262     private ErrorNotificationManager getManager() {
263         return ProgressManager.getInstance().errorManager;
264     }
265     /**
266      * Refresh the contents of the viewer.
267      */

268     void refresh() {
269         errorViewer.refresh();
270     }
271     private void initLabelProvider() {
272         ITableLabelProvider provider = new ITableLabelProvider() {
273             /*
274              * (non-Javadoc)
275              *
276              * @see org.eclipse.jface.viewers.IBaseLabelProvider#addListener(org.eclipse.jface.viewers.ILabelProviderListener)
277              */

278             public void addListener(ILabelProviderListener listener) {
279                 //Do nothing
280
}
281             /*
282              * (non-Javadoc)
283              *
284              * @see org.eclipse.jface.viewers.IBaseLabelProvider#dispose()
285              */

286             public void dispose() {
287                 //Do nothing
288
}
289             /*
290              * (non-Javadoc)
291              *
292              * @see org.eclipse.jface.viewers.ITableLabelProvider#getColumnImage(java.lang.Object,
293              * int)
294              */

295             public Image getColumnImage(Object JavaDoc element, int columnIndex) {
296                 return JFaceResources.getImageRegistry().get(
297                         ErrorNotificationManager.ERROR_JOB_KEY);
298             }
299             /*
300              * (non-Javadoc)
301              *
302              * @see org.eclipse.jface.viewers.ITableLabelProvider#getColumnText(java.lang.Object,
303              * int)
304              */

305             public String JavaDoc getColumnText(Object JavaDoc element, int columnIndex) {
306                 return ((ErrorInfo) element).getDisplayString();
307             }
308             /*
309              * (non-Javadoc)
310              *
311              * @see org.eclipse.jface.viewers.IBaseLabelProvider#isLabelProperty(java.lang.Object,
312              * java.lang.String)
313              */

314             public boolean isLabelProperty(Object JavaDoc element, String JavaDoc property) {
315                 return false;
316             }
317             /*
318              * (non-Javadoc)
319              *
320              * @see org.eclipse.jface.viewers.IBaseLabelProvider#removeListener(org.eclipse.jface.viewers.ILabelProviderListener)
321              */

322             public void removeListener(ILabelProviderListener listener) {
323                 //Do nothing
324
}
325         };
326         errorViewer.setLabelProvider(provider);
327     }
328     /**
329      * Open the error dialog on the current selection.
330      */

331     private void openErrorDialog() {
332         ErrorInfo element = getSingleSelection();
333         if (element == null)
334             return;
335         ErrorDialog.openError(getShell(), element.getDisplayString(), null,
336                 element.getErrorStatus());
337     }
338     /**
339      * Get the single selection. Return null if the selection is not just one
340      * element.
341      *
342      * @return ErrorInfo or <code>null</code>.
343      */

344     private ErrorInfo getSingleSelection() {
345         ISelection rawSelection = errorViewer.getSelection();
346         if (rawSelection != null
347                 && rawSelection instanceof IStructuredSelection) {
348             IStructuredSelection selection = (IStructuredSelection) rawSelection;
349             if (selection.size() == 1)
350                 return (ErrorInfo) selection.getFirstElement();
351         }
352         return null;
353     }
354     /*
355      * (non-Javadoc)
356      *
357      * @see org.eclipse.jface.dialogs.Dialog#close()
358      */

359     public boolean close() {
360         getManager().clearAllErrors();
361         Rectangle shellPosition = getShell().getBounds();
362         boolean result = super.close();
363         ProgressManagerUtil.animateDown(shellPosition);
364         return result;
365     }
366
367     /*
368      * (non-Javadoc)
369      *
370      * @see org.eclipse.jface.dialogs.Dialog#initializeBounds()
371      */

372     protected void initializeBounds() {
373         super.initializeBounds();
374         Rectangle shellPosition = getShell().getBounds();
375         ProgressManagerUtil.animateUp(shellPosition);
376     }
377
378     /**
379      * Create this dialog's drop-down list component.
380      *
381      * @param detailsParent
382      * the parent composite
383      */

384     private void createDetailsList(Composite detailsParent) {
385         // create the list
386
detailsList = new List JavaDoc(detailsParent, SWT.BORDER | SWT.H_SCROLL
387                 | SWT.V_SCROLL | SWT.MULTI);
388
389         GridData data = new GridData(GridData.HORIZONTAL_ALIGN_FILL
390                 | GridData.GRAB_HORIZONTAL | GridData.VERTICAL_ALIGN_FILL
391                 | GridData.GRAB_VERTICAL);
392         data.heightHint = detailsList.getItemHeight() * DETAILS_LIST_ITEM_COUNT;
393         data.horizontalSpan = 2;
394         detailsList.setLayoutData(data);
395         Menu copyMenu = new Menu(detailsList);
396         MenuItem copyItem = new MenuItem(copyMenu, SWT.NONE);
397         copyItem.addSelectionListener(new SelectionListener() {
398             /*
399              * @see SelectionListener.widgetSelected (SelectionEvent)
400              */

401             public void widgetSelected(SelectionEvent e) {
402                 copyToClipboard();
403             }
404             /*
405              * @see SelectionListener.widgetDefaultSelected(SelectionEvent)
406              */

407             public void widgetDefaultSelected(SelectionEvent e) {
408                 copyToClipboard();
409             }
410         });
411         copyItem.setText(JFaceResources.getString("copy")); //$NON-NLS-1$
412
detailsList.setMenu(copyMenu);
413     }
414
415     /**
416      * Set the contents of the details list to be the status from the selected
417      * error.
418      */

419     private void setDetailsContents() {
420
421         Collection JavaDoc statusList = new ArrayList JavaDoc();
422
423         ErrorInfo info = getSingleSelection();
424
425         if (info != null) {
426             selectedError = info;
427             statusList.add(selectedError.getErrorStatus().getMessage());
428             if (selectedError.getErrorStatus().getException() != null) {
429                 Throwable JavaDoc exception = selectedError.getErrorStatus()
430                         .getException();
431                 statusList.add(exception.toString());
432                 StackTraceElement JavaDoc[] elements = exception.getStackTrace();
433                 for (int i = 0; i < elements.length; i++) {
434                     statusList.add(elements[i].toString());
435                 }
436             }
437             IStatus[] statuses = (selectedError.getErrorStatus().getChildren());
438             for (int i = 0; i < statuses.length; i++) {
439                 statusList.add(NESTING_INDENT + statuses[i].getMessage());
440             }
441         }
442
443         String JavaDoc[] items = new String JavaDoc[statusList.size()];
444         statusList.toArray(items);
445
446         detailsList.setItems(items);
447
448     }
449
450     /**
451      * Copy the contents of the statuses to the clipboard.
452      */

453     private void copyToClipboard() {
454
455         if (selectedError == null)
456             return;
457
458         if (clipboard != null)
459             clipboard.dispose();
460
461         StringBuffer JavaDoc statusBuffer = new StringBuffer JavaDoc();
462         populateCopyBuffer(selectedError.getErrorStatus(), statusBuffer, 0);
463         clipboard = new Clipboard(detailsList.getDisplay());
464         clipboard.setContents(new Object JavaDoc[]{statusBuffer.toString()},
465                 new Transfer[]{TextTransfer.getInstance()});
466     }
467
468     /**
469      * Put the details of the status of the error onto the stream.
470      *
471      * @param buildingStatus
472      * @param buffer
473      * @param nesting
474      */

475     private void populateCopyBuffer(IStatus buildingStatus,
476             StringBuffer JavaDoc buffer, int nesting) {
477
478         for (int i = 0; i < nesting; i++) {
479             buffer.append(NESTING_INDENT); //$NON-NLS-1$
480
}
481         buffer.append(buildingStatus.getMessage());
482
483         if (buildingStatus.getException() != null) {
484             Throwable JavaDoc exception = buildingStatus.getException();
485             buffer.append("\n"); //$NON-NLS-1$
486
buffer.append(exception.toString());
487             StackTraceElement JavaDoc[] elements = exception.getStackTrace();
488             for (int i = 0; i < elements.length; i++) {
489                 buffer.append("\n"); //$NON-NLS-1$
490
buffer.append(elements[i].toString());
491
492             }
493         }
494
495         buffer.append("\n"); //$NON-NLS-1$
496
IStatus[] children = buildingStatus.getChildren();
497         for (int i = 0; i < children.length; i++) {
498             populateCopyBuffer(children[i], buffer, nesting + 1);
499         }
500     }
501 }
Popular Tags