]> git.argeo.org Git - lgpl/argeo-commons.git/blobdiff - swt/org.argeo.cms.swt/src/org/argeo/cms/swt/dialogs/LightweightDialog.java
Prepare next development cycle
[lgpl/argeo-commons.git] / swt / org.argeo.cms.swt / src / org / argeo / cms / swt / dialogs / LightweightDialog.java
index bf6417beadd3baf911407cd57e6665c946649698..3aec22a14eb791843c912c11057acb810081dacc 100644 (file)
@@ -1,6 +1,7 @@
 package org.argeo.cms.swt.dialogs;
 
 import org.argeo.api.cms.CmsLog;
+import org.argeo.cms.ux.widgets.CmsDialog;
 import org.argeo.eclipse.ui.EclipseUiException;
 import org.eclipse.swt.SWT;
 import org.eclipse.swt.events.FocusEvent;
@@ -20,22 +21,19 @@ import org.eclipse.swt.widgets.Shell;
 public class LightweightDialog {
        private final static CmsLog log = CmsLog.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 foregoundShell;
 
+       private Display display;
+
        private Integer returnCode = null;
        private boolean block = true;
 
        private String title;
 
        /** Tries to find a display */
-       private static Display getDisplay() {
+       static Display findDisplay() {
                try {
                        Display display = Display.getCurrent();
                        if (display != null)
@@ -52,6 +50,7 @@ public class LightweightDialog {
        }
 
        public int open() {
+               display = findDisplay();
                if (foregoundShell != null)
                        throw new EclipseUiException("There is already a shell");
                backgroundShell = new Shell(parentShell, SWT.ON_TOP);
@@ -61,7 +60,7 @@ public class LightweightDialog {
                // } else
                // backgroundShell.setMaximized(true);
                backgroundShell.setAlpha(128);
-               backgroundShell.setBackground(getDisplay().getSystemColor(SWT.COLOR_BLACK));
+               backgroundShell.setBackground(display.getSystemColor(SWT.COLOR_BLACK));
                foregoundShell = new Shell(backgroundShell, SWT.NO_TRIM | SWT.ON_TOP);
                if (title != null)
                        setTitle(title);
@@ -71,7 +70,7 @@ public class LightweightDialog {
                // shell.pack();
                // shell.layout();
 
-               Rectangle shellBounds = parentShell != null ? parentShell.getBounds() : Display.getCurrent().getBounds();// RAP
+               Rectangle shellBounds = parentShell != null ? parentShell.getBounds() : display.getBounds();// RAP
                Point dialogSize = foregoundShell.getSize();
                int x = shellBounds.x + (shellBounds.width - dialogSize.x) / 2;
                int y = shellBounds.y + (shellBounds.height - dialogSize.y) / 2;
@@ -85,7 +84,7 @@ public class LightweightDialog {
                                if (hasChildShells())
                                        return;
                                if (returnCode == null)// not yet closed
-                                       closeShell(CANCEL);
+                                       closeShell(CmsDialog.CANCEL);
                        }
 
                        @Override
@@ -110,15 +109,16 @@ public class LightweightDialog {
                                if (hasChildShells())
                                        return;
                                if (returnCode == null)// not yet closed
-                                       closeShell(CANCEL);
+                                       closeShell(CmsDialog.CANCEL);
                        }
                });
+               backgroundShell.addDisposeListener((event) -> onClose());
 
                if (block) {
                        block();
                }
                if (returnCode == null)
-                       returnCode = OK;
+                       returnCode = CmsDialog.OK;
                return returnCode;
        }
 
@@ -126,11 +126,11 @@ public class LightweightDialog {
                try {
                        runEventLoop(foregoundShell);
                } catch (ThreadDeath t) {
-                       returnCode = CANCEL;
+                       returnCode = CmsDialog.CANCEL;
                        if (log.isTraceEnabled())
                                log.error("Thread death, canceling dialog", t);
                } catch (Throwable t) {
-                       returnCode = CANCEL;
+                       returnCode = CmsDialog.CANCEL;
                        log.error("Cannot open blocking lightweight dialog", t);
                }
        }
@@ -141,6 +141,10 @@ public class LightweightDialog {
                return foregoundShell.getShells().length != 0;
        }
 
+       protected void onClose() {
+
+       }
+
        // public synchronized int openAndWait() {
        // open();
        // while (returnCode == null)
@@ -154,13 +158,13 @@ public class LightweightDialog {
 
        private synchronized void notifyClose() {
                if (returnCode == null)
-                       returnCode = CANCEL;
+                       returnCode = CmsDialog.CANCEL;
                notifyAll();
        }
 
        protected void closeShell(int returnCode) {
                this.returnCode = returnCode;
-               if (CANCEL == returnCode)
+               if (CmsDialog.CANCEL == returnCode)
                        onCancel();
                if (foregoundShell != null && !foregoundShell.isDisposed()) {
                        foregoundShell.close();
@@ -175,9 +179,6 @@ public class LightweightDialog {
        }
 
        protected Point getInitialSize() {
-               // if (exception != null)
-               // return new Point(800, 600);
-               // else
                return new Point(600, 400);
        }
 
@@ -252,4 +253,8 @@ public class LightweightDialog {
                return returnCode;
        }
 
+       Display getDisplay() {
+               return display;
+       }
+
 }
\ No newline at end of file