1 11 package org.eclipse.ui.internal.ide.dialogs; 12 13 import java.io.ByteArrayOutputStream ; 14 import java.io.IOException ; 15 import java.io.PrintStream ; 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 32 public class InternalErrorDialog extends MessageDialog { 33 34 private Throwable detail; 35 36 private int detailButtonID = -1; 37 38 private Text text; 39 40 private int defaultButtonIndex = 0; 43 44 47 private static final int TEXT_LINE_COUNT = 15; 48 49 61 public InternalErrorDialog(Shell parentShell, String dialogTitle, 62 Image dialogTitleImage, String dialogMessage, Throwable detail, 63 int dialogImageType, String [] 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 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 85 public void setDetailButton(int index) { 86 detailButtonID = index; 87 } 88 89 92 protected void buttonPressed(int buttonId) { 93 if (buttonId == detailButtonID) { 94 toggleDetailsArea(); 95 } else { 96 setReturnCode(buttonId); 97 close(); 98 } 99 } 100 101 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 132 protected void createDropDownText(Composite parent) { 133 text = new Text(parent, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL); 135 text.setFont(parent.getFont()); 136 137 try { 139 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 140 PrintStream ps = new PrintStream (baos); 141 detail.printStackTrace(ps); 142 ps.flush(); 143 baos.flush(); 144 text.setText(baos.toString()); 145 } catch (IOException 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 167 public static boolean openQuestion(Shell parent, String title, 168 String message, Throwable detail, int defaultIndex) { 169 String [] labels; 170 if (detail == null) { 171 labels = new String [] { IDialogConstants.YES_LABEL, 172 IDialogConstants.NO_LABEL }; 173 } else { 174 labels = new String [] { IDialogConstants.YES_LABEL, 175 IDialogConstants.NO_LABEL, 176 IDialogConstants.SHOW_DETAILS_LABEL }; 177 } 178 179 InternalErrorDialog dialog = new InternalErrorDialog(parent, title, 180 null, 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 |