*/
package org.argeo.eclipse.ui.dialogs;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
import org.argeo.eclipse.ui.EclipseUiException;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.FocusEvent;
/** Generic lightweight dialog, not based on JFace. */
public class LightweightDialog {
+ private final static Log log = LogFactory.getLog(LightweightDialog.class);
+
// must be the same value as org.eclipse.jface.window.Window#OK
public final static int OK = 0;
// must be the same value as org.eclipse.jface.window.Window#CANCEL
public int open() {
if (foregoundShell != null)
throw new EclipseUiException("There is already a shell");
- backgroundShell = new Shell(parentShell, SWT.DIALOG_TRIM | SWT.ON_TOP);
+ backgroundShell = new Shell(parentShell, SWT.ON_TOP);
backgroundShell.setFullScreen(true);
+ // if (parentShell != null) {
+ // backgroundShell.setBounds(parentShell.getBounds());
+ // } else
// backgroundShell.setMaximized(true);
backgroundShell.setAlpha(128);
backgroundShell.setBackground(getDisplay().getSystemColor(SWT.COLOR_BLACK));
- backgroundShell.open();
foregoundShell = new Shell(backgroundShell, SWT.NO_TRIM | SWT.ON_TOP);
if (title != null)
setTitle(title);
// shell.pack();
// shell.layout();
- Rectangle shellBounds = Display.getCurrent().getBounds();// RAP
+ Rectangle shellBounds = parentShell != null ? parentShell.getBounds() : Display.getCurrent().getBounds();// RAP
Point dialogSize = foregoundShell.getSize();
int x = shellBounds.x + (shellBounds.width - dialogSize.x) / 2;
int y = shellBounds.y + (shellBounds.height - dialogSize.y) / 2;
@Override
public void shellDeactivated(ShellEvent e) {
+ if (hasChildShells())
+ return;
if (returnCode == null)// not yet closed
closeShell(CANCEL);
}
});
+ backgroundShell.open();
foregoundShell.open();
// after the foreground shell has been opened
backgroundShell.addFocusListener(new FocusListener() {
@Override
public void focusGained(FocusEvent event) {
+ if (hasChildShells())
+ return;
if (returnCode == null)// not yet closed
closeShell(CANCEL);
}
});
if (block) {
- runEventLoop(foregoundShell);
+ try {
+ runEventLoop(foregoundShell);
+ } catch (ThreadDeath t) {
+ returnCode = CANCEL;
+ if (log.isTraceEnabled())
+ log.error("Thread death, canceling dialog", t);
+ } catch (Throwable t) {
+ returnCode = CANCEL;
+ log.error("Cannot open blocking lightweight dialog", t);
+ }
}
if (returnCode == null)
returnCode = OK;
return returnCode;
}
+ private boolean hasChildShells() {
+ if (foregoundShell == null)
+ return false;
+ return foregoundShell.getShells().length != 0;
+ }
+
// public synchronized int openAndWait() {
// open();
// while (returnCode == null)
block = shouldBlock;
}
+ public void pack() {
+ foregoundShell.pack();
+ }
+
private void runEventLoop(Shell loopShell) {
Display display;
if (foregoundShell == null) {
public void setTitle(String title) {
this.title = title;
- if (getForegoundShell() != null)
+ if (title != null && getForegoundShell() != null)
getForegoundShell().setText(title);
}
+ public Integer getReturnCode() {
+ return returnCode;
+ }
+
}
\ No newline at end of file