]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.eclipse.ui/src/org/argeo/eclipse/ui/dialogs/LightweightDialog.java
Start working on pseudo Eclipse Forms in order to ease transition to
[lgpl/argeo-commons.git] / org.argeo.eclipse.ui / src / org / argeo / eclipse / ui / dialogs / LightweightDialog.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 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;
32
33 /** Generic lightweight dialog, not based on JFace. */
34 public class LightweightDialog {
35 // private final static Log log = LogFactory.getLog(LightweightDialog.class);
36
37 private Shell parentShell;
38 private Shell backgroundShell;
39 private Shell shell;
40
41 /** Tries to find a display */
42 private static Display getDisplay() {
43 try {
44 Display display = Display.getCurrent();
45 if (display != null)
46 return display;
47 else
48 return Display.getDefault();
49 } catch (Exception e) {
50 return Display.getCurrent();
51 }
52 }
53
54 public LightweightDialog(Shell parentShell) {
55 this.parentShell = parentShell;
56 }
57
58 public void open() {
59 if (shell != null)
60 throw new EclipseUiException("There is already a shell");
61 backgroundShell = new Shell(parentShell, SWT.NO_TRIM | SWT.BORDER | SWT.ON_TOP);
62 backgroundShell.setMaximized(true);
63 backgroundShell.setAlpha(128);
64 backgroundShell.setBackground(getDisplay().getSystemColor(SWT.COLOR_BLACK));
65 backgroundShell.open();
66 shell = new Shell(backgroundShell, SWT.NO_TRIM | SWT.BORDER | SWT.ON_TOP);
67 shell.setLayout(new GridLayout());
68 // shell.setText("Error");
69 shell.setSize(getInitialSize());
70 createDialogArea(shell);
71 // shell.pack();
72 // shell.layout();
73
74 Rectangle shellBounds = Display.getCurrent().getBounds();// RAP
75 Point dialogSize = shell.getSize();
76 int x = shellBounds.x + (shellBounds.width - dialogSize.x) / 2;
77 int y = shellBounds.y + (shellBounds.height - dialogSize.y) / 2;
78 shell.setLocation(x, y);
79
80 shell.addShellListener(new ShellAdapter() {
81 private static final long serialVersionUID = -2701270481953688763L;
82
83 @Override
84 public void shellDeactivated(ShellEvent e) {
85 closeShell();
86 }
87 });
88
89 shell.open();
90 // after the foreground shell has been opened
91 backgroundShell.addFocusListener(new FocusListener() {
92 private static final long serialVersionUID = 3137408447474661070L;
93
94 @Override
95 public void focusLost(FocusEvent event) {
96 }
97
98 @Override
99 public void focusGained(FocusEvent event) {
100 closeShell();
101 }
102 });
103 }
104
105 protected void closeShell() {
106 if (shell != null) {
107 shell.close();
108 shell.dispose();
109 shell = null;
110 }
111
112 if (backgroundShell != null) {
113 backgroundShell.close();
114 backgroundShell.dispose();
115 }
116 }
117
118 protected Point getInitialSize() {
119 // if (exception != null)
120 // return new Point(800, 600);
121 // else
122 return new Point(400, 400);
123 }
124
125 protected Control createDialogArea(Composite parent) {
126 Composite dialogarea = new Composite(parent, SWT.NONE);
127 dialogarea.setLayout(new GridLayout());
128 dialogarea.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
129 return dialogarea;
130 }
131 }