]>
git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.eclipse.ui/src/org/argeo/eclipse/ui/dialogs/LightweightDialog.java
a923b0134c6c0173eef2eb0dd5bb7ea4555a915f
2 * Copyright (C) 2007-2012 Argeo GmbH
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org
.argeo
.eclipse
.ui
.dialogs
;
18 import org
.apache
.commons
.logging
.Log
;
19 import org
.apache
.commons
.logging
.LogFactory
;
20 import org
.argeo
.eclipse
.ui
.EclipseUiException
;
21 import org
.eclipse
.swt
.SWT
;
22 import org
.eclipse
.swt
.events
.FocusEvent
;
23 import org
.eclipse
.swt
.events
.FocusListener
;
24 import org
.eclipse
.swt
.events
.ShellAdapter
;
25 import org
.eclipse
.swt
.events
.ShellEvent
;
26 import org
.eclipse
.swt
.graphics
.Point
;
27 import org
.eclipse
.swt
.graphics
.Rectangle
;
28 import org
.eclipse
.swt
.layout
.GridData
;
29 import org
.eclipse
.swt
.layout
.GridLayout
;
30 import org
.eclipse
.swt
.widgets
.Composite
;
31 import org
.eclipse
.swt
.widgets
.Control
;
32 import org
.eclipse
.swt
.widgets
.Display
;
33 import org
.eclipse
.swt
.widgets
.Shell
;
35 /** Generic lightweight dialog, not based on JFace. */
36 public class LightweightDialog
{
37 private final static Log log
= LogFactory
.getLog(LightweightDialog
.class);
39 // must be the same value as org.eclipse.jface.window.Window#OK
40 public final static int OK
= 0;
41 // must be the same value as org.eclipse.jface.window.Window#CANCEL
42 public final static int CANCEL
= 1;
44 private Shell parentShell
;
45 private Shell backgroundShell
;
46 private Shell foregoundShell
;
48 private Integer returnCode
= null;
49 private boolean block
= true;
53 /** Tries to find a display */
54 private static Display
getDisplay() {
56 Display display
= Display
.getCurrent();
60 return Display
.getDefault();
61 } catch (Exception e
) {
62 return Display
.getCurrent();
66 public LightweightDialog(Shell parentShell
) {
67 this.parentShell
= parentShell
;
71 if (foregoundShell
!= null)
72 throw new EclipseUiException("There is already a shell");
73 backgroundShell
= new Shell(parentShell
, SWT
.DIALOG_TRIM
| SWT
.ON_TOP
);
74 backgroundShell
.setFullScreen(true);
75 // backgroundShell.setMaximized(true);
76 backgroundShell
.setAlpha(128);
77 backgroundShell
.setBackground(getDisplay().getSystemColor(SWT
.COLOR_BLACK
));
78 foregoundShell
= new Shell(backgroundShell
, SWT
.NO_TRIM
| SWT
.ON_TOP
);
81 foregoundShell
.setLayout(new GridLayout());
82 foregoundShell
.setSize(getInitialSize());
83 createDialogArea(foregoundShell
);
87 Rectangle shellBounds
= Display
.getCurrent().getBounds();// RAP
88 Point dialogSize
= foregoundShell
.getSize();
89 int x
= shellBounds
.x
+ (shellBounds
.width
- dialogSize
.x
) / 2;
90 int y
= shellBounds
.y
+ (shellBounds
.height
- dialogSize
.y
) / 2;
91 foregoundShell
.setLocation(x
, y
);
93 foregoundShell
.addShellListener(new ShellAdapter() {
94 private static final long serialVersionUID
= -2701270481953688763L;
97 public void shellDeactivated(ShellEvent e
) {
98 if (returnCode
== null)// not yet closed
103 public void shellClosed(ShellEvent e
) {
109 backgroundShell
.open();
110 foregoundShell
.open();
111 // after the foreground shell has been opened
112 backgroundShell
.addFocusListener(new FocusListener() {
113 private static final long serialVersionUID
= 3137408447474661070L;
116 public void focusLost(FocusEvent event
) {
120 public void focusGained(FocusEvent event
) {
121 if (returnCode
== null)// not yet closed
128 runEventLoop(foregoundShell
);
129 } catch (Throwable t
) {
130 log
.error("Cannot open blocking lightweight dialog", t
);
133 if (returnCode
== null)
138 // public synchronized int openAndWait() {
140 // while (returnCode == null)
143 // } catch (InterruptedException e) {
146 // return returnCode;
149 private synchronized void notifyClose() {
150 if (returnCode
== null)
155 protected void closeShell(int returnCode
) {
156 this.returnCode
= returnCode
;
157 if (CANCEL
== returnCode
)
159 if (foregoundShell
!= null && !foregoundShell
.isDisposed()) {
160 foregoundShell
.close();
161 foregoundShell
.dispose();
162 foregoundShell
= null;
165 if (backgroundShell
!= null && !backgroundShell
.isDisposed()) {
166 backgroundShell
.close();
167 backgroundShell
.dispose();
171 protected Point
getInitialSize() {
172 // if (exception != null)
173 // return new Point(800, 600);
175 return new Point(600, 400);
178 protected Control
createDialogArea(Composite parent
) {
179 Composite dialogarea
= new Composite(parent
, SWT
.NONE
);
180 dialogarea
.setLayout(new GridLayout());
181 dialogarea
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
185 protected Shell
getBackgroundShell() {
186 return backgroundShell
;
189 protected Shell
getForegoundShell() {
190 return foregoundShell
;
193 public void setBlockOnOpen(boolean shouldBlock
) {
198 foregoundShell
.pack();
201 private void runEventLoop(Shell loopShell
) {
203 if (foregoundShell
== null) {
204 display
= Display
.getCurrent();
206 display
= loopShell
.getDisplay();
209 while (loopShell
!= null && !loopShell
.isDisposed()) {
211 if (!display
.readAndDispatch()) {
214 } catch (Throwable e
) {
218 if (!display
.isDisposed())
222 protected void handleException(Throwable t
) {
223 if (t
instanceof ThreadDeath
) {
224 // Don't catch ThreadDeath as this is a normal occurrence when
226 throw (ThreadDeath
) t
;
228 // Try to keep running.
232 /** @return false, if the dialog should not be closed. */
233 protected boolean onCancel() {
237 public void setTitle(String title
) {
239 if (title
!= null && getForegoundShell() != null)
240 getForegoundShell().setText(title
);