]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.eclipse.ui/src/org/argeo/eclipse/ui/dialogs/NonModalErrorFeedback.java
CMS wizard and error feedback
[lgpl/argeo-commons.git] / org.argeo.eclipse.ui / src / org / argeo / eclipse / ui / dialogs / NonModalErrorFeedback.java
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 error dialog to be used in try/catch blocks */
39 class NonModalErrorFeedback {
40 private final static Log log = LogFactory
41 .getLog(NonModalErrorFeedback.class);
42
43 private final String message;
44 private final Throwable exception;
45
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 NonModalErrorFeedback(getDisplay().getActiveShell(), message, e)
55 .open();
56 }
57
58 public static void show(String message) {
59 new NonModalErrorFeedback(getDisplay().getActiveShell(), message, null)
60 .open();
61 }
62
63 /** Tries to find a display */
64 private static Display getDisplay() {
65 try {
66 Display display = Display.getCurrent();
67 if (display != null)
68 return display;
69 else
70 return Display.getDefault();
71 } catch (Exception e) {
72 return Display.getCurrent();
73 }
74 }
75
76 public NonModalErrorFeedback(Shell parentShell, String message, Throwable e) {
77 this.message = message;
78 this.exception = e;
79 log.error(message, e);
80 }
81
82 public void open() {
83 if (shell != null)
84 throw new EclipseUiException("There is already a shell");
85 shell = new Shell(getDisplay(), SWT.NO_TRIM | SWT.BORDER | SWT.ON_TOP);
86 shell.setLayout(new GridLayout());
87 // shell.setText("Error");
88 shell.setSize(getInitialSize());
89 createDialogArea(shell);
90 // shell.pack();
91 // shell.layout();
92
93 Rectangle shellBounds = Display.getCurrent().getBounds();// RAP
94 Point dialogSize = shell.getSize();
95 int x = shellBounds.x + (shellBounds.width - dialogSize.x) / 2;
96 int y = shellBounds.y + (shellBounds.height - dialogSize.y) / 2;
97 shell.setLocation(x, y);
98
99 shell.addShellListener(new ShellAdapter() {
100 private static final long serialVersionUID = -2701270481953688763L;
101
102 @Override
103 public void shellDeactivated(ShellEvent e) {
104 closeShell();
105 }
106 });
107
108 shell.open();
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
142 | SWT.V_SCROLL | SWT.H_SCROLL);
143 stack.setEditable(false);
144 stack.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
145 StringWriter sw = new StringWriter();
146 exception.printStackTrace(new PrintWriter(sw));
147 stack.setText(sw.toString());
148 }
149
150 // parent.pack();
151 return composite;
152 }
153 }