1 package com.quantum.ui.dialog;
3 import com.quantum.Messages;
5 import org.eclipse.jface.dialogs.Dialog;
6 import org.eclipse.jface.dialogs.IDialogConstants;
7 import org.eclipse.swt.SWT;
8 import org.eclipse.swt.graphics.Point;
9 import org.eclipse.swt.layout.GridData;
10 import org.eclipse.swt.layout.GridLayout;
11 import org.eclipse.swt.widgets.Button;
12 import org.eclipse.swt.widgets.Composite;
13 import org.eclipse.swt.widgets.Control;
14 import org.eclipse.swt.widgets.Label;
15 import org.eclipse.swt.widgets.Shell;
16 import org.eclipse.swt.widgets.Text;
21 public class ExceptionDisplayDialog extends Dialog {
23 private Control detailsArea;
24 private Throwable throwable;
25 private String message;
30 public ExceptionDisplayDialog(Shell parentShell, Throwable throwable) {
32 this.throwable = throwable;
38 private Button detailsButton;
41 * The title of the dialog.
46 * Indicates whether the error details viewer is currently created.
48 private boolean detailsShown = false;
51 * Creates an error dialog.
52 * Note that the dialog will have no visual representation (no widgets)
53 * until it is told to open.
55 * Normally one should use <code>openError</code> to create and open one of these.
56 * This constructor is useful only if the error object being displayed contains child
57 * items <it>and</it> you need to specify a mask which will be used to filter the
58 * displaying of these children.
61 * @param parentShell the shell under which to create this dialog
62 * @param dialogTitle the title to use for this dialog,
63 * or <code>null</code> to indicate that the default title should be used
64 * @param message the message to show in this dialog,
65 * or <code>null</code> to indicate that the error's message should be shown
66 * as the primary message
67 * @param status the error to show to the user
68 * @param displayMask the mask to use to filter the displaying of child items,
69 * as per <code>IStatus.matches</code>
70 * @see org.eclipse.core.runtime.IStatus#matches(int)
72 public ExceptionDisplayDialog(
76 Throwable throwable) {
79 this.title = dialogTitle;
80 this.message = message;
81 this.throwable = throwable;
83 setShellStyle(SWT.DIALOG_TRIM | SWT.RESIZE | SWT.APPLICATION_MODAL);
87 * Method declared on Dialog.
88 * Handles the pressing of the Ok or Details button in this dialog.
89 * If the Ok button was pressed then close this dialog. If the Details
90 * button was pressed then toggle the displaying of the error details area.
91 * Note that the Details button will only be visible if the error being
92 * displayed specifies child details.
94 protected void buttonPressed(int id) {
95 if (id == IDialogConstants.DETAILS_ID) {
96 // was the details button pressed?
99 super.buttonPressed(id);
103 * Method declared in Window.
105 protected void configureShell(Shell shell) {
106 super.configureShell(shell);
107 shell.setText(title);
110 * Method declared on Dialog.
112 protected void createButtonsForButtonBar(Composite parent) {
113 // create OK and Details buttons
116 IDialogConstants.OK_ID,
117 IDialogConstants.OK_LABEL,
122 IDialogConstants.DETAILS_ID,
123 IDialogConstants.SHOW_DETAILS_LABEL,
127 protected Control createDialogArea(Composite parent) {
129 // create a composite with standard margins and spacing
130 Composite composite = new Composite(parent, SWT.NONE);
131 GridLayout layout = new GridLayout();
132 layout.marginHeight =
133 convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_MARGIN);
135 convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_MARGIN);
136 layout.verticalSpacing =
137 convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_SPACING);
138 layout.horizontalSpacing =
139 convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_SPACING);
140 composite.setLayout(layout);
141 GridData childData = new GridData(GridData.FILL_BOTH);
142 childData.horizontalSpan = 2;
143 composite.setLayoutData(childData);
144 composite.setFont(parent.getFont());
146 Label label = new Label(composite, 0);
147 label.setText(this.message);
148 label.setFont(parent.getFont());
151 GridData full = new GridData();
152 full.horizontalAlignment = GridData.FILL;
153 full.verticalAlignment = GridData.FILL;
154 full.heightHint = convertHeightInCharsToPixels(3);
155 full.widthHint = convertWidthInCharsToPixels(60);
156 label.setLayoutData(full);
163 * Create the expandable details arae.
165 * @param parent the parent composite
166 * @return the details text control
168 protected Control createDetailsArea(Composite parent) {
170 // create a composite with standard margins and spacing
171 Composite composite = new Composite(parent, SWT.NONE);
172 GridLayout layout = new GridLayout();
173 layout.marginHeight =
174 convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_MARGIN);
176 convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_MARGIN);
177 layout.verticalSpacing =
178 convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_SPACING);
179 layout.horizontalSpacing =
180 convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_SPACING);
181 composite.setLayout(layout);
182 GridData childData = new GridData(GridData.FILL_BOTH);
183 childData.horizontalSpan = 2;
184 composite.setLayoutData(childData);
185 composite.setFont(parent.getFont());
187 Label label = new Label(composite, 0);
188 label.setText(Messages.getString("ExceptionDisplayDialog.stackTrace"));
189 label.setFont(parent.getFont());
191 GridData full = new GridData();
192 full.horizontalAlignment = GridData.FILL;
193 full.verticalAlignment = GridData.FILL;
194 full.heightHint = convertHeightInCharsToPixels(3);
195 full.widthHint = convertWidthInCharsToPixels(60);
196 label.setLayoutData(full);
201 SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL | SWT.MULTI);
203 text.setText(this.throwable.getLocalizedMessage());
207 GridData.HORIZONTAL_ALIGN_FILL
208 | GridData.GRAB_HORIZONTAL
209 | GridData.VERTICAL_ALIGN_FILL
210 | GridData.GRAB_VERTICAL);
211 data.heightHint = convertHeightInCharsToPixels(8);
212 text.setLayoutData(data);
213 text.setFont(parent.getFont());
215 this.detailsShown = true;
219 * Opens an error dialog to display the given error. Use this method if the
220 * error object being displayed contains child items <it>and</it> you wish to
221 * specify a mask which will be used to filter the displaying of these
222 * children. The error dialog will only be displayed if there is at
223 * least one child status matching the mask.
225 * @param parentShell -
226 * the parent shell of the dialog, or <code>null</code> if none
227 * @param dialogTitle the title to use for this dialog,
228 * or <code>null</code> to indicate that the default title should be used
229 * @param message the message to show in this dialog,
230 * or <code>null</code> to indicate that the error's message should be shown
231 * as the primary message
232 * as per <code>IStatus.matches</code>
233 * @return the code of the button that was pressed that resulted in this dialog
234 * closing. This will be <code>Dialog.OK</code> if the OK button was
235 * pressed, or <code>Dialog.CANCEL</code> if this dialog's close window
236 * decoration or the ESC key was used.
237 * @see org.eclipse.core.runtime.IStatus#matches(int)
239 public static int openError(
243 Throwable throwable) {
246 title = Messages.getString(ExceptionDisplayDialog.class.getName() +
247 "." + throwable.getClass().getName() + ".title");
249 if (message == null) {
250 message = Messages.getString(ExceptionDisplayDialog.class.getName() +
251 "." + throwable.getClass().getName() + ".message");
253 ExceptionDisplayDialog dialog =
254 new ExceptionDisplayDialog(parentShell, title, message, throwable);
255 return dialog.open();
259 * Toggles the unfolding of the details area. This is triggered by
260 * the user pressing the details button.
262 private void toggleDetailsArea() {
263 Point windowSize = getShell().getSize();
264 Point oldSize = getShell().computeSize(SWT.DEFAULT, SWT.DEFAULT);
266 if (this.detailsShown) {
267 this.detailsArea.dispose();
268 this.detailsShown = false;
269 detailsButton.setText(IDialogConstants.SHOW_DETAILS_LABEL);
271 this.detailsArea = createDetailsArea((Composite) getContents());
272 detailsButton.setText(IDialogConstants.HIDE_DETAILS_LABEL);
275 Point newSize = getShell().computeSize(SWT.DEFAULT, SWT.DEFAULT);
278 new Point(windowSize.x, windowSize.y + (newSize.y - oldSize.y)));