KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > ide > dialogs > InternalErrorDialog


1 /*******************************************************************************
2  * Copyright (c) 2000, 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.ide.dialogs;
12
13 import java.io.ByteArrayOutputStream JavaDoc;
14 import java.io.IOException JavaDoc;
15 import java.io.PrintStream JavaDoc;
16
17 import org.eclipse.jface.dialogs.IDialogConstants;
18 import org.eclipse.jface.dialogs.MessageDialog;
19 import org.eclipse.swt.SWT;
20 import org.eclipse.swt.graphics.Image;
21 import org.eclipse.swt.graphics.Point;
22 import org.eclipse.swt.layout.GridData;
23 import org.eclipse.swt.widgets.Button;
24 import org.eclipse.swt.widgets.Composite;
25 import org.eclipse.swt.widgets.Shell;
26 import org.eclipse.swt.widgets.Text;
27
28 /**
29  * Added a Details button to the MessageDialog to show the exception
30  * stack trace.
31  */

32 public class InternalErrorDialog extends MessageDialog {
33
34     private Throwable JavaDoc detail;
35
36     private int detailButtonID = -1;
37
38     private Text text;
39
40     //Workaround. SWT does not seem to set the default button if
41
//there is not control with focus. Bug: 14668
42
private int defaultButtonIndex = 0;
43
44     /**
45      * Size of the text in lines.
46      */

47     private static final int TEXT_LINE_COUNT = 15;
48
49     /**
50      * Create a new dialog.
51      *
52      * @param parentShell the parent shell
53      * @param dialogTitle the title
54      * @param dialogTitleImage the title image
55      * @param dialogMessage the message
56      * @param detail the error to display
57      * @param dialogImageType the type of image
58      * @param dialogButtonLabels the button labels
59      * @param defaultIndex the default selected button index
60      */

61     public InternalErrorDialog(Shell parentShell, String JavaDoc dialogTitle,
62             Image dialogTitleImage, String JavaDoc dialogMessage, Throwable JavaDoc detail,
63             int dialogImageType, String JavaDoc[] dialogButtonLabels, int defaultIndex) {
64         super(parentShell, dialogTitle, dialogTitleImage, dialogMessage,
65                 dialogImageType, dialogButtonLabels, defaultIndex);
66         defaultButtonIndex = defaultIndex;
67         this.detail = detail;
68         setShellStyle(getShellStyle() | SWT.APPLICATION_MODAL);
69     }
70
71     //Workaround. SWT does not seem to set rigth the default button if
72
//there is not control with focus. Bug: 14668
73
public int open() {
74         create();
75         Button b = getButton(defaultButtonIndex);
76         b.setFocus();
77         b.getShell().setDefaultButton(b);
78         return super.open();
79     }
80
81     /**
82      * Set the detail button;
83      * @param index the detail button index
84      */

85     public void setDetailButton(int index) {
86         detailButtonID = index;
87     }
88
89     /* (non-Javadoc)
90      * Method declared on Dialog.
91      */

92     protected void buttonPressed(int buttonId) {
93         if (buttonId == detailButtonID) {
94             toggleDetailsArea();
95         } else {
96             setReturnCode(buttonId);
97             close();
98         }
99     }
100
101     /**
102      * Toggles the unfolding of the details area. This is triggered by
103      * the user pressing the details button.
104      */

105     private void toggleDetailsArea() {
106         Point windowSize = getShell().getSize();
107         Point oldSize = getContents().computeSize(SWT.DEFAULT, SWT.DEFAULT);
108
109         if (text != null) {
110             text.dispose();
111             text = null;
112             getButton(detailButtonID).setText(
113                     IDialogConstants.SHOW_DETAILS_LABEL);
114         } else {
115             createDropDownText((Composite) getContents());
116             getButton(detailButtonID).setText(
117                     IDialogConstants.HIDE_DETAILS_LABEL);
118         }
119
120         Point newSize = getContents().computeSize(SWT.DEFAULT, SWT.DEFAULT);
121         getShell()
122                 .setSize(
123                         new Point(windowSize.x, windowSize.y
124                                 + (newSize.y - oldSize.y)));
125     }
126
127     /**
128      * Create this dialog's drop-down list component.
129      *
130      * @param parent the parent composite
131      */

132     protected void createDropDownText(Composite parent) {
133         // create the list
134
text = new Text(parent, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
135         text.setFont(parent.getFont());
136
137         // print the stacktrace in the text field
138
try {
139             ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
140             PrintStream JavaDoc ps = new PrintStream JavaDoc(baos);
141             detail.printStackTrace(ps);
142             ps.flush();
143             baos.flush();
144             text.setText(baos.toString());
145         } catch (IOException JavaDoc e) {
146         }
147
148         GridData data = new GridData(GridData.HORIZONTAL_ALIGN_FILL
149                 | GridData.GRAB_HORIZONTAL | GridData.VERTICAL_ALIGN_FILL
150                 | GridData.GRAB_VERTICAL);
151         data.heightHint = text.getLineHeight() * TEXT_LINE_COUNT;
152         data.horizontalSpan = 2;
153         text.setLayoutData(data);
154     }
155
156     /**
157      * Convenience method to open a simple Yes/No question dialog.
158      *
159      * @param parent the parent shell of the dialog, or <code>null</code> if none
160      * @param title the dialog's title, or <code>null</code> if none
161      * @param message the message
162      * @param detail the error
163      * @param defaultIndex the default index of the button to select
164      * @return <code>true</code> if the user presses the OK button,
165      * <code>false</code> otherwise
166      */

167     public static boolean openQuestion(Shell parent, String JavaDoc title,
168             String JavaDoc message, Throwable JavaDoc detail, int defaultIndex) {
169         String JavaDoc[] labels;
170         if (detail == null) {
171             labels = new String JavaDoc[] { IDialogConstants.YES_LABEL,
172                     IDialogConstants.NO_LABEL };
173         } else {
174             labels = new String JavaDoc[] { IDialogConstants.YES_LABEL,
175                     IDialogConstants.NO_LABEL,
176                     IDialogConstants.SHOW_DETAILS_LABEL };
177         }
178
179         InternalErrorDialog dialog = new InternalErrorDialog(parent, title,
180                 null, // accept the default window icon
181
message, detail, QUESTION, labels, defaultIndex);
182         if (detail != null) {
183             dialog.setDetailButton(2);
184         }
185         return dialog.open() == 0;
186     }
187
188 }
189
Popular Tags