X-Git-Url: https://git.argeo.org/?a=blobdiff_plain;f=org.argeo.eclipse.ui%2Fsrc%2Forg%2Fargeo%2Feclipse%2Fui%2Fdialogs%2FLightweightDialog.java;h=d00365bf2c7e8ad1aa6547ffae2a326a6d8ecfd8;hb=c53fec78daddb69c489686844188036b04e1615a;hp=33a0d2781d7f968943bd9542e87ab25bc0020f16;hpb=0553f7fa3e80d37b5a0c17945a0816dff4a47e8e;p=lgpl%2Fargeo-commons.git diff --git a/org.argeo.eclipse.ui/src/org/argeo/eclipse/ui/dialogs/LightweightDialog.java b/org.argeo.eclipse.ui/src/org/argeo/eclipse/ui/dialogs/LightweightDialog.java index 33a0d2781..d00365bf2 100644 --- a/org.argeo.eclipse.ui/src/org/argeo/eclipse/ui/dialogs/LightweightDialog.java +++ b/org.argeo.eclipse.ui/src/org/argeo/eclipse/ui/dialogs/LightweightDialog.java @@ -1,20 +1,7 @@ -/* - * Copyright (C) 2007-2012 Argeo GmbH - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ 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; @@ -31,12 +18,23 @@ import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell; /** Generic lightweight dialog, not based on JFace. */ +@Deprecated public class LightweightDialog { - // private final static Log log = LogFactory.getLog(LightweightDialog.class); + 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 final static int CANCEL = 1; private Shell parentShell; private Shell backgroundShell; - private Shell shell; + private Shell foregoundShell; + + private Integer returnCode = null; + private boolean block = true; + + private String title; /** Tries to find a display */ private static Display getDisplay() { @@ -55,38 +53,52 @@ public class LightweightDialog { this.parentShell = parentShell; } - public void open() { - if (shell != null) + public int open() { + if (foregoundShell != null) throw new EclipseUiException("There is already a shell"); - backgroundShell = new Shell(parentShell, SWT.NO_TRIM | SWT.BORDER | SWT.ON_TOP); - backgroundShell.setMaximized(true); + 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(); - shell = new Shell(backgroundShell, SWT.NO_TRIM | SWT.BORDER | SWT.ON_TOP); - shell.setLayout(new GridLayout()); - // shell.setText("Error"); - shell.setSize(getInitialSize()); - createDialogArea(shell); + foregoundShell = new Shell(backgroundShell, SWT.NO_TRIM | SWT.ON_TOP); + if (title != null) + setTitle(title); + foregoundShell.setLayout(new GridLayout()); + foregoundShell.setSize(getInitialSize()); + createDialogArea(foregoundShell); // shell.pack(); // shell.layout(); - Rectangle shellBounds = Display.getCurrent().getBounds();// RAP - Point dialogSize = shell.getSize(); + 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; - shell.setLocation(x, y); + foregoundShell.setLocation(x, y); - shell.addShellListener(new ShellAdapter() { + foregoundShell.addShellListener(new ShellAdapter() { private static final long serialVersionUID = -2701270481953688763L; @Override public void shellDeactivated(ShellEvent e) { - closeShell(); + if (hasChildShells()) + return; + if (returnCode == null)// not yet closed + closeShell(CANCEL); } + + @Override + public void shellClosed(ShellEvent e) { + notifyClose(); + } + }); - shell.open(); + backgroundShell.open(); + foregoundShell.open(); // after the foreground shell has been opened backgroundShell.addFocusListener(new FocusListener() { private static final long serialVersionUID = 3137408447474661070L; @@ -97,19 +109,68 @@ public class LightweightDialog { @Override public void focusGained(FocusEvent event) { - closeShell(); + if (hasChildShells()) + return; + if (returnCode == null)// not yet closed + closeShell(CANCEL); } }); + + if (block) { + block(); + } + if (returnCode == null) + returnCode = OK; + return returnCode; } - protected void closeShell() { - if (shell != null) { - shell.close(); - shell.dispose(); - shell = null; + public void block() { + 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); } + } + + private boolean hasChildShells() { + if (foregoundShell == null) + return false; + return foregoundShell.getShells().length != 0; + } + + // public synchronized int openAndWait() { + // open(); + // while (returnCode == null) + // try { + // wait(100); + // } catch (InterruptedException e) { + // // silent + // } + // return returnCode; + // } - if (backgroundShell != null) { + private synchronized void notifyClose() { + if (returnCode == null) + returnCode = CANCEL; + notifyAll(); + } + + protected void closeShell(int returnCode) { + this.returnCode = returnCode; + if (CANCEL == returnCode) + onCancel(); + if (foregoundShell != null && !foregoundShell.isDisposed()) { + foregoundShell.close(); + foregoundShell.dispose(); + foregoundShell = null; + } + + if (backgroundShell != null && !backgroundShell.isDisposed()) { backgroundShell.close(); backgroundShell.dispose(); } @@ -119,7 +180,7 @@ public class LightweightDialog { // if (exception != null) // return new Point(800, 600); // else - return new Point(400, 400); + return new Point(600, 400); } protected Control createDialogArea(Composite parent) { @@ -128,4 +189,69 @@ public class LightweightDialog { dialogarea.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); return dialogarea; } + + protected Shell getBackgroundShell() { + return backgroundShell; + } + + protected Shell getForegoundShell() { + return foregoundShell; + } + + public void setBlockOnOpen(boolean shouldBlock) { + block = shouldBlock; + } + + public void pack() { + foregoundShell.pack(); + } + + private void runEventLoop(Shell loopShell) { + Display display; + if (foregoundShell == null) { + display = Display.getCurrent(); + } else { + display = loopShell.getDisplay(); + } + + while (loopShell != null && !loopShell.isDisposed()) { + try { + if (!display.readAndDispatch()) { + display.sleep(); + } + } catch (UnsupportedOperationException e) { + throw e; + } catch (Throwable e) { + handleException(e); + } + } + if (!display.isDisposed()) + display.update(); + } + + protected void handleException(Throwable t) { + if (t instanceof ThreadDeath) { + // Don't catch ThreadDeath as this is a normal occurrence when + // the thread dies + throw (ThreadDeath) t; + } + // Try to keep running. + t.printStackTrace(); + } + + /** @return false, if the dialog should not be closed. */ + protected boolean onCancel() { + return true; + } + + public void setTitle(String title) { + this.title = title; + if (title != null && getForegoundShell() != null) + getForegoundShell().setText(title); + } + + public Integer getReturnCode() { + return returnCode; + } + } \ No newline at end of file