1 package org
.argeo
.cms
.swt
.dialogs
;
3 import org
.argeo
.api
.cms
.CmsLog
;
4 import org
.argeo
.eclipse
.ui
.EclipseUiException
;
5 import org
.eclipse
.swt
.SWT
;
6 import org
.eclipse
.swt
.events
.FocusEvent
;
7 import org
.eclipse
.swt
.events
.FocusListener
;
8 import org
.eclipse
.swt
.events
.ShellAdapter
;
9 import org
.eclipse
.swt
.events
.ShellEvent
;
10 import org
.eclipse
.swt
.graphics
.Point
;
11 import org
.eclipse
.swt
.graphics
.Rectangle
;
12 import org
.eclipse
.swt
.layout
.GridData
;
13 import org
.eclipse
.swt
.layout
.GridLayout
;
14 import org
.eclipse
.swt
.widgets
.Composite
;
15 import org
.eclipse
.swt
.widgets
.Control
;
16 import org
.eclipse
.swt
.widgets
.Display
;
17 import org
.eclipse
.swt
.widgets
.Shell
;
19 /** Generic lightweight dialog, not based on JFace. */
20 public class LightweightDialog
{
21 private final static CmsLog log
= CmsLog
.getLog(LightweightDialog
.class);
23 // must be the same value as org.eclipse.jface.window.Window#OK
24 public final static int OK
= 0;
25 // must be the same value as org.eclipse.jface.window.Window#CANCEL
26 public final static int CANCEL
= 1;
28 private Shell parentShell
;
29 private Shell backgroundShell
;
30 private Shell foregoundShell
;
32 private Display display
;
34 private Integer returnCode
= null;
35 private boolean block
= true;
39 /** Tries to find a display */
40 static Display
findDisplay() {
42 Display display
= Display
.getCurrent();
46 return Display
.getDefault();
47 } catch (Exception e
) {
48 return Display
.getCurrent();
52 public LightweightDialog(Shell parentShell
) {
53 this.parentShell
= parentShell
;
57 display
= findDisplay();
58 if (foregoundShell
!= null)
59 throw new EclipseUiException("There is already a shell");
60 backgroundShell
= new Shell(parentShell
, SWT
.ON_TOP
);
61 backgroundShell
.setFullScreen(true);
62 // if (parentShell != null) {
63 // backgroundShell.setBounds(parentShell.getBounds());
65 // backgroundShell.setMaximized(true);
66 backgroundShell
.setAlpha(128);
67 backgroundShell
.setBackground(display
.getSystemColor(SWT
.COLOR_BLACK
));
68 foregoundShell
= new Shell(backgroundShell
, SWT
.NO_TRIM
| SWT
.ON_TOP
);
71 foregoundShell
.setLayout(new GridLayout());
72 foregoundShell
.setSize(getInitialSize());
73 createDialogArea(foregoundShell
);
77 Rectangle shellBounds
= parentShell
!= null ? parentShell
.getBounds() : display
.getBounds();// RAP
78 Point dialogSize
= foregoundShell
.getSize();
79 int x
= shellBounds
.x
+ (shellBounds
.width
- dialogSize
.x
) / 2;
80 int y
= shellBounds
.y
+ (shellBounds
.height
- dialogSize
.y
) / 2;
81 foregoundShell
.setLocation(x
, y
);
83 foregoundShell
.addShellListener(new ShellAdapter() {
84 private static final long serialVersionUID
= -2701270481953688763L;
87 public void shellDeactivated(ShellEvent e
) {
90 if (returnCode
== null)// not yet closed
95 public void shellClosed(ShellEvent e
) {
101 backgroundShell
.open();
102 foregoundShell
.open();
103 // after the foreground shell has been opened
104 backgroundShell
.addFocusListener(new FocusListener() {
105 private static final long serialVersionUID
= 3137408447474661070L;
108 public void focusLost(FocusEvent event
) {
112 public void focusGained(FocusEvent event
) {
113 if (hasChildShells())
115 if (returnCode
== null)// not yet closed
119 backgroundShell
.addDisposeListener((event
) -> onClose());
124 if (returnCode
== null)
129 public void block() {
131 runEventLoop(foregoundShell
);
132 } catch (ThreadDeath t
) {
134 if (log
.isTraceEnabled())
135 log
.error("Thread death, canceling dialog", t
);
136 } catch (Throwable t
) {
138 log
.error("Cannot open blocking lightweight dialog", t
);
142 private boolean hasChildShells() {
143 if (foregoundShell
== null)
145 return foregoundShell
.getShells().length
!= 0;
148 protected void onClose() {
152 // public synchronized int openAndWait() {
154 // while (returnCode == null)
157 // } catch (InterruptedException e) {
160 // return returnCode;
163 private synchronized void notifyClose() {
164 if (returnCode
== null)
169 protected void closeShell(int returnCode
) {
170 this.returnCode
= returnCode
;
171 if (CANCEL
== returnCode
)
173 if (foregoundShell
!= null && !foregoundShell
.isDisposed()) {
174 foregoundShell
.close();
175 foregoundShell
.dispose();
176 foregoundShell
= null;
179 if (backgroundShell
!= null && !backgroundShell
.isDisposed()) {
180 backgroundShell
.close();
181 backgroundShell
.dispose();
185 protected Point
getInitialSize() {
186 return new Point(600, 400);
189 protected Control
createDialogArea(Composite parent
) {
190 Composite dialogarea
= new Composite(parent
, SWT
.NONE
);
191 dialogarea
.setLayout(new GridLayout());
192 dialogarea
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
196 protected Shell
getBackgroundShell() {
197 return backgroundShell
;
200 protected Shell
getForegoundShell() {
201 return foregoundShell
;
204 public void setBlockOnOpen(boolean shouldBlock
) {
209 foregoundShell
.pack();
212 private void runEventLoop(Shell loopShell
) {
214 if (foregoundShell
== null) {
215 display
= Display
.getCurrent();
217 display
= loopShell
.getDisplay();
220 while (loopShell
!= null && !loopShell
.isDisposed()) {
222 if (!display
.readAndDispatch()) {
225 } catch (UnsupportedOperationException e
) {
227 } catch (Throwable e
) {
231 if (!display
.isDisposed())
235 protected void handleException(Throwable t
) {
236 if (t
instanceof ThreadDeath
) {
237 // Don't catch ThreadDeath as this is a normal occurrence when
239 throw (ThreadDeath
) t
;
241 // Try to keep running.
245 /** @return false, if the dialog should not be closed. */
246 protected boolean onCancel() {
250 public void setTitle(String title
) {
252 if (title
!= null && getForegoundShell() != null)
253 getForegoundShell().setText(title
);
256 public Integer
getReturnCode() {
260 Display
getDisplay() {