View Javadoc
1   /*
2    * Copyright (C) 2007-2012 Argeo GmbH
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *         http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.argeo.eclipse.ui.dialogs;
17  
18  import java.io.PrintWriter;
19  import java.io.StringWriter;
20  
21  import org.apache.commons.logging.Log;
22  import org.apache.commons.logging.LogFactory;
23  import org.argeo.eclipse.ui.EclipseUiException;
24  import org.eclipse.swt.SWT;
25  import org.eclipse.swt.events.ShellAdapter;
26  import org.eclipse.swt.events.ShellEvent;
27  import org.eclipse.swt.graphics.Point;
28  import org.eclipse.swt.graphics.Rectangle;
29  import org.eclipse.swt.layout.GridData;
30  import org.eclipse.swt.layout.GridLayout;
31  import org.eclipse.swt.widgets.Composite;
32  import org.eclipse.swt.widgets.Control;
33  import org.eclipse.swt.widgets.Display;
34  import org.eclipse.swt.widgets.Label;
35  import org.eclipse.swt.widgets.Shell;
36  import org.eclipse.swt.widgets.Text;
37  
38  /** Generic lightweight dialog, not based on JFace. */
39  public class FeedbackDialog extends LightweightDialog {
40  	private final static Log log = LogFactory.getLog(FeedbackDialog.class);
41  
42  	private String message;
43  	private Throwable exception;
44  
45  	private Shell parentShell;
46  	private Shell shell;
47  
48  	public static void show(String message, Throwable e) {
49  		// rethrow ThreaDeath in order to make sure that RAP will properly clean
50  		// up the UI thread
51  		if (e instanceof ThreadDeath)
52  			throw (ThreadDeath) e;
53  
54  		new FeedbackDialog(getDisplay().getActiveShell(), message, e).open();
55  	}
56  
57  	public static void show(String message) {
58  		new FeedbackDialog(getDisplay().getActiveShell(), message, null).open();
59  	}
60  
61  	/** Tries to find a display */
62  	private static Display getDisplay() {
63  		try {
64  			Display display = Display.getCurrent();
65  			if (display != null)
66  				return display;
67  			else
68  				return Display.getDefault();
69  		} catch (Exception e) {
70  			return Display.getCurrent();
71  		}
72  	}
73  
74  	public FeedbackDialog(Shell parentShell, String message, Throwable e) {
75  		super(parentShell);
76  		this.message = message;
77  		this.exception = e;
78  		log.error(message, e);
79  	}
80  
81  	public int open() {
82  		if (shell != null)
83  			throw new EclipseUiException("There is already a shell");
84  		shell = new Shell(getDisplay(), SWT.NO_TRIM | SWT.BORDER | SWT.ON_TOP);
85  		shell.setLayout(new GridLayout());
86  		// shell.setText("Error");
87  		shell.setSize(getInitialSize());
88  		createDialogArea(shell);
89  		// shell.pack();
90  		// shell.layout();
91  
92  		Rectangle shellBounds = Display.getCurrent().getBounds();// RAP
93  		Point dialogSize = shell.getSize();
94  		int x = shellBounds.x + (shellBounds.width - dialogSize.x) / 2;
95  		int y = shellBounds.y + (shellBounds.height - dialogSize.y) / 2;
96  		shell.setLocation(x, y);
97  
98  		shell.addShellListener(new ShellAdapter() {
99  			private static final long serialVersionUID = -2701270481953688763L;
100 
101 			@Override
102 			public void shellDeactivated(ShellEvent e) {
103 				closeShell();
104 			}
105 		});
106 
107 		shell.open();
108 		return OK;
109 	}
110 
111 	protected void closeShell() {
112 		shell.close();
113 		shell.dispose();
114 		shell = null;
115 	}
116 
117 	protected Point getInitialSize() {
118 		// if (exception != null)
119 		// return new Point(800, 600);
120 		// else
121 		return new Point(400, 300);
122 	}
123 
124 	protected Control createDialogArea(Composite parent) {
125 		Composite dialogarea = new Composite(parent, SWT.NONE);
126 		dialogarea.setLayout(new GridLayout());
127 		// Composite dialogarea = (Composite) super.createDialogArea(parent);
128 		dialogarea.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
129 
130 		Label messageLbl = new Label(dialogarea, SWT.NONE);
131 		if (message != null)
132 			messageLbl.setText(message);
133 		else if (exception != null)
134 			messageLbl.setText(exception.getLocalizedMessage());
135 
136 		Composite composite = new Composite(dialogarea, SWT.NONE);
137 		composite.setLayout(new GridLayout(2, false));
138 		composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
139 
140 		if (exception != null) {
141 			Text stack = new Text(composite, SWT.MULTI | SWT.LEAD | SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL);
142 			stack.setEditable(false);
143 			stack.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
144 			StringWriter sw = new StringWriter();
145 			exception.printStackTrace(new PrintWriter(sw));
146 			stack.setText(sw.toString());
147 		}
148 
149 		// parent.pack();
150 		return composite;
151 	}
152 }