]>
git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.eclipse.ui/src/org/argeo/eclipse/ui/dialogs/LightweightDialog.java
6d45e8cf21bc35ecae4be693ff993efdf807a4bc
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
.argeo
.eclipse
.ui
.EclipseUiException
;
19 import org
.eclipse
.swt
.SWT
;
20 import org
.eclipse
.swt
.events
.FocusEvent
;
21 import org
.eclipse
.swt
.events
.FocusListener
;
22 import org
.eclipse
.swt
.events
.ShellAdapter
;
23 import org
.eclipse
.swt
.events
.ShellEvent
;
24 import org
.eclipse
.swt
.graphics
.Point
;
25 import org
.eclipse
.swt
.graphics
.Rectangle
;
26 import org
.eclipse
.swt
.layout
.GridData
;
27 import org
.eclipse
.swt
.layout
.GridLayout
;
28 import org
.eclipse
.swt
.widgets
.Composite
;
29 import org
.eclipse
.swt
.widgets
.Control
;
30 import org
.eclipse
.swt
.widgets
.Display
;
31 import org
.eclipse
.swt
.widgets
.Shell
;
33 /** Generic lightweight dialog, not based on JFace. */
34 public class LightweightDialog
{
35 // must be the same value as org.eclipse.jface.window.Window#OK
36 public final static int OK
= 0;
37 // must be the same value as org.eclipse.jface.window.Window#CANCEL
38 public final static int CANCEL
= 1;
40 private Shell parentShell
;
41 private Shell backgroundShell
;
42 private Shell foregoundShell
;
44 private Integer returnCode
= null;
45 private boolean block
= true;
49 /** Tries to find a display */
50 private static Display
getDisplay() {
52 Display display
= Display
.getCurrent();
56 return Display
.getDefault();
57 } catch (Exception e
) {
58 return Display
.getCurrent();
62 public LightweightDialog(Shell parentShell
) {
63 this.parentShell
= parentShell
;
67 if (foregoundShell
!= null)
68 throw new EclipseUiException("There is already a shell");
69 backgroundShell
= new Shell(parentShell
, SWT
.DIALOG_TRIM
| SWT
.ON_TOP
);
70 backgroundShell
.setFullScreen(true);
71 // backgroundShell.setMaximized(true);
72 backgroundShell
.setAlpha(128);
73 backgroundShell
.setBackground(getDisplay().getSystemColor(SWT
.COLOR_BLACK
));
74 backgroundShell
.open();
75 foregoundShell
= new Shell(backgroundShell
, SWT
.NO_TRIM
| SWT
.ON_TOP
);
78 foregoundShell
.setLayout(new GridLayout());
79 foregoundShell
.setSize(getInitialSize());
80 createDialogArea(foregoundShell
);
84 Rectangle shellBounds
= Display
.getCurrent().getBounds();// RAP
85 Point dialogSize
= foregoundShell
.getSize();
86 int x
= shellBounds
.x
+ (shellBounds
.width
- dialogSize
.x
) / 2;
87 int y
= shellBounds
.y
+ (shellBounds
.height
- dialogSize
.y
) / 2;
88 foregoundShell
.setLocation(x
, y
);
90 foregoundShell
.addShellListener(new ShellAdapter() {
91 private static final long serialVersionUID
= -2701270481953688763L;
94 public void shellDeactivated(ShellEvent e
) {
95 if (returnCode
== null)// not yet closed
100 public void shellClosed(ShellEvent e
) {
106 foregoundShell
.open();
107 // after the foreground shell has been opened
108 backgroundShell
.addFocusListener(new FocusListener() {
109 private static final long serialVersionUID
= 3137408447474661070L;
112 public void focusLost(FocusEvent event
) {
116 public void focusGained(FocusEvent event
) {
117 if (returnCode
== null)// not yet closed
123 runEventLoop(foregoundShell
);
125 if (returnCode
== null)
130 // public synchronized int openAndWait() {
132 // while (returnCode == null)
135 // } catch (InterruptedException e) {
138 // return returnCode;
141 private synchronized void notifyClose() {
142 if (returnCode
== null)
147 protected void closeShell(int returnCode
) {
148 this.returnCode
= returnCode
;
149 if (CANCEL
== returnCode
)
151 if (foregoundShell
!= null && !foregoundShell
.isDisposed()) {
152 foregoundShell
.close();
153 foregoundShell
.dispose();
154 foregoundShell
= null;
157 if (backgroundShell
!= null && !backgroundShell
.isDisposed()) {
158 backgroundShell
.close();
159 backgroundShell
.dispose();
163 protected Point
getInitialSize() {
164 // if (exception != null)
165 // return new Point(800, 600);
167 return new Point(600, 400);
170 protected Control
createDialogArea(Composite parent
) {
171 Composite dialogarea
= new Composite(parent
, SWT
.NONE
);
172 dialogarea
.setLayout(new GridLayout());
173 dialogarea
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
177 protected Shell
getBackgroundShell() {
178 return backgroundShell
;
181 protected Shell
getForegoundShell() {
182 return foregoundShell
;
185 public void setBlockOnOpen(boolean shouldBlock
) {
189 private void runEventLoop(Shell loopShell
) {
191 if (foregoundShell
== null) {
192 display
= Display
.getCurrent();
194 display
= loopShell
.getDisplay();
197 while (loopShell
!= null && !loopShell
.isDisposed()) {
199 if (!display
.readAndDispatch()) {
202 } catch (Throwable e
) {
206 if (!display
.isDisposed())
210 protected void handleException(Throwable t
) {
211 if (t
instanceof ThreadDeath
) {
212 // Don't catch ThreadDeath as this is a normal occurrence when
214 throw (ThreadDeath
) t
;
216 // Try to keep running.
220 /** @return false, if the dialog should not be closed. */
221 protected boolean onCancel() {
225 public void setTitle(String title
) {
227 if (title
!= null && getForegoundShell() != null)
228 getForegoundShell().setText(title
);