001: /*
002: * Copyright (c) 2004-2006, Jean-François Brazeau. All rights reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions are met:
006: *
007: * 1. Redistributions of source code must retain the above copyright notice,
008: * this list of conditions and the following disclaimer.
009: *
010: * 2. Redistributions in binary form must reproduce the above copyright
011: * notice, this list of conditions and the following disclaimer in the
012: * documentation and/or other materials provided with the distribution.
013: *
014: * 3. The name of the author may not be used to endorse or promote products
015: * derived from this software without specific prior written permission.
016: *
017: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
018: * IMPLIEDWARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
019: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
020: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
021: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
022: * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
023: * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
024: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
025: * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
026: * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
027: */
028: package jfb.tools.activitymgr.ui.dialogs;
029:
030: import java.io.ByteArrayInputStream;
031: import java.io.ByteArrayOutputStream;
032: import java.io.IOException;
033: import java.io.InputStreamReader;
034: import java.io.LineNumberReader;
035: import java.io.PrintStream;
036:
037: import org.apache.log4j.Logger;
038: import org.eclipse.jface.dialogs.IDialogConstants;
039: import org.eclipse.jface.dialogs.IconAndMessageDialog;
040: import org.eclipse.swt.SWT;
041: import org.eclipse.swt.graphics.Image;
042: import org.eclipse.swt.graphics.Point;
043: import org.eclipse.swt.layout.GridData;
044: import org.eclipse.swt.layout.GridLayout;
045: import org.eclipse.swt.widgets.Button;
046: import org.eclipse.swt.widgets.Composite;
047: import org.eclipse.swt.widgets.Control;
048: import org.eclipse.swt.widgets.List;
049: import org.eclipse.swt.widgets.Shell;
050:
051: /**
052: * Dialogue affichant une erreur avec la pile associée.
053: */
054: public class ErrorDialog extends IconAndMessageDialog {
055:
056: /** Logger */
057: private static Logger log = Logger.getLogger(ErrorDialog.class);
058:
059: /** Bouton d'affichage du détail */
060: private Button detailsButton;
061:
062: /** Booléen indiquant si le détail est affiché */
063: private boolean detailsShown = false;
064:
065: /** Liste contenant la pile d'erreur */
066: private List list;
067:
068: /** Exception associée à l'erreur */
069: private Throwable throwable;
070:
071: /**
072: * Constructeur par défaut.
073: * @param parentShell shell parent.
074: * @param message le message d'erreur.
075: * @param throwable l'exception.
076: */
077: public ErrorDialog(Shell parentShell, String message,
078: Throwable throwable) {
079: super (parentShell);
080: this .throwable = throwable;
081: this .message = message;
082: }
083:
084: /*
085: * (non-Javadoc) Method declared in Window.
086: */
087: protected void configureShell(Shell shell) {
088: super .configureShell(shell);
089: shell.setText("Error");
090: shell.setImage(getImage());
091: }
092:
093: /* (non-Javadoc)
094: * @see org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets.Composite)
095: */
096: protected Control createDialogArea(Composite parent) {
097: Composite c = (Composite) super .createDialogArea(parent);
098: GridLayout layout = (GridLayout) c.getLayout();
099: // Changement du Nb de colonnes du Layout
100: layout.numColumns = 2;
101: createMessageArea(c);
102: return c;
103: }
104:
105: /* (non-Javadoc)
106: * @see org.eclipse.jface.dialogs.Dialog#createButtonsForButtonBar(org.eclipse.swt.widgets.Composite)
107: */
108: protected void createButtonsForButtonBar(Composite parent) {
109: createButton(parent, IDialogConstants.OK_ID,
110: IDialogConstants.OK_LABEL, true);
111: detailsButton = createButton(parent,
112: IDialogConstants.DETAILS_ID,
113: IDialogConstants.SHOW_DETAILS_LABEL, false);
114: }
115:
116: /* (non-Javadoc)
117: * @see org.eclipse.jface.dialogs.Dialog#buttonPressed(int)
118: */
119: protected void buttonPressed(int buttonId) {
120: switch (buttonId) {
121: case IDialogConstants.DETAILS_ID:
122: Point windowSize = getShell().getSize();
123: Point oldSize = getShell().computeSize(SWT.DEFAULT,
124: SWT.DEFAULT);
125:
126: detailsShown = !detailsShown;
127: detailsButton
128: .setText(detailsShown ? IDialogConstants.HIDE_DETAILS_LABEL
129: : IDialogConstants.SHOW_DETAILS_LABEL);
130: if (detailsShown) {
131: list = new List((Composite) getContents(), SWT.BORDER
132: | SWT.H_SCROLL | SWT.V_SCROLL | SWT.MULTI);
133: GridData data = new GridData(
134: GridData.HORIZONTAL_ALIGN_FILL
135: | GridData.GRAB_HORIZONTAL
136: | GridData.VERTICAL_ALIGN_FILL
137: | GridData.GRAB_VERTICAL);
138: data.heightHint = list.getItemHeight() * 10;
139: data.horizontalSpan = 2;
140: list.setLayoutData(data);
141:
142: try {
143: ByteArrayOutputStream out = new ByteArrayOutputStream();
144: throwable.printStackTrace(new PrintStream(out));
145: out.close();
146: ByteArrayInputStream in = new ByteArrayInputStream(
147: out.toByteArray());
148: LineNumberReader lin = new LineNumberReader(
149: new InputStreamReader(in));
150: String line = null;
151: while ((line = lin.readLine()) != null) {
152: list.add(line.replaceFirst("\t", " "));
153: }
154: } catch (IOException e) {
155: log
156: .error(
157: "Unexpected I/O error while printing stack trace.",
158: e);
159: list
160: .add("Unexpected I/O error while printing stack trace.");
161: list.add("See logs for more details.");
162: }
163: } else {
164: list.dispose();
165: }
166: Point newSize = getShell().computeSize(SWT.DEFAULT,
167: SWT.DEFAULT);
168: getShell().setSize(
169: new Point(windowSize.x, windowSize.y
170: + (newSize.y - oldSize.y)));
171: break;
172: default:
173: super .buttonPressed(buttonId);
174: }
175: }
176:
177: /* (non-Javadoc)
178: * @see org.eclipse.jface.dialogs.IconAndMessageDialog#getImage()
179: */
180: protected Image getImage() {
181: return getErrorImage();
182: }
183:
184: }
|