Improve dialogs.
authorMathieu Baudier <mbaudier@argeo.org>
Fri, 23 Oct 2020 08:59:42 +0000 (10:59 +0200)
committerMathieu Baudier <mbaudier@argeo.org>
Fri, 23 Oct 2020 08:59:42 +0000 (10:59 +0200)
org.argeo.cms.ui/src/org/argeo/cms/ui/dialogs/CmsMessageDialog.java
org.argeo.cms.ui/src/org/argeo/cms/ui/dialogs/CmsWizardDialog.java
org.argeo.cms.ui/src/org/argeo/cms/ui/dialogs/SingleValueDialog.java [new file with mode: 0644]
org.argeo.eclipse.ui/src/org/argeo/eclipse/ui/dialogs/ErrorFeedback.java
org.argeo.eclipse.ui/src/org/argeo/eclipse/ui/dialogs/FeedbackDialog.java
org.argeo.eclipse.ui/src/org/argeo/eclipse/ui/dialogs/NonModalErrorFeedback.java [deleted file]
org.argeo.eclipse.ui/src/org/argeo/eclipse/ui/dialogs/SingleValue.java

index ba04b645baa4113a6e335e32a71e5809e06568f0..e076a11086d5270657ccca1a1615316bc8f8102c 100644 (file)
@@ -20,10 +20,13 @@ import org.eclipse.swt.widgets.Shell;
 
 /** Base class for dialogs displaying messages or small forms. */
 public class CmsMessageDialog extends LightweightDialog {
+       public final static int NONE = 0;
+       public final static int ERROR = 1;
        public final static int INFORMATION = 2;
        public final static int QUESTION = 3;
        public final static int WARNING = 4;
        public final static int CONFIRM = 5;
+       public final static int QUESTION_WITH_CANCEL = 6;
 
        private int kind;
        private String message;
@@ -68,7 +71,7 @@ public class CmsMessageDialog extends LightweightDialog {
                Composite buttons = new Composite(parent, SWT.NONE);
                buttons.addTraverseListener(traverseListener);
                buttons.setLayoutData(new GridData(SWT.END, SWT.FILL, true, false));
-               if (kind == INFORMATION || kind == WARNING) {
+               if (kind == INFORMATION || kind == WARNING || kind == ERROR || kind == ERROR) {
                        GridLayout layout = new GridLayout(1, true);
                        layout.marginWidth = 0;
                        layout.marginHeight = 0;
@@ -82,7 +85,7 @@ public class CmsMessageDialog extends LightweightDialog {
                        close.addTraverseListener(traverseListener);
 
                        buttons.setTabList(new Control[] { close });
-               } else if (kind == CONFIRM || kind == QUESTION) {
+               } else if (kind == CONFIRM || kind == QUESTION || kind == QUESTION_WITH_CANCEL) {
                        Control input = createInputArea(body);
                        if (input != null) {
                                input.addTraverseListener(traverseListener);
@@ -153,4 +156,8 @@ public class CmsMessageDialog extends LightweightDialog {
                open(WARNING, Display.getCurrent().getActiveShell(), message);
        }
 
+       public static void openError(String message) {
+               open(ERROR, Display.getCurrent().getActiveShell(), message);
+       }
+
 }
index 451287d64623778a93c8c00e211086fed6162541..93125be23478f406f023edb800b9d2ac68abd606 100644 (file)
@@ -2,7 +2,6 @@ package org.argeo.cms.ui.dialogs;
 
 import java.lang.reflect.InvocationTargetException;
 
-import org.argeo.cms.CmsException;
 import org.argeo.cms.CmsMsg;
 import org.argeo.cms.ui.util.CmsUiUtils;
 import org.argeo.eclipse.ui.EclipseUiUtils;
@@ -48,7 +47,7 @@ public class CmsWizardDialog extends LightweightDialog implements IWizardContain
                wizard.addPages();
                currentPage = wizard.getStartingPage();
                if (currentPage == null)
-                       throw new CmsException("At least one wizard page is required");
+                       throw new IllegalArgumentException("At least one wizard page is required");
        }
 
        @Override
@@ -138,7 +137,7 @@ public class CmsWizardDialog extends LightweightDialog implements IWizardContain
                        }
                }
                if (index < 0)
-                       throw new CmsException("Cannot find index of wizard page " + page);
+                       throw new IllegalArgumentException("Cannot find index of wizard page " + page);
                pageBodies[index].moveAbove(pageBodies[currentPageIndex]);
 
                // // clear
diff --git a/org.argeo.cms.ui/src/org/argeo/cms/ui/dialogs/SingleValueDialog.java b/org.argeo.cms.ui/src/org/argeo/cms/ui/dialogs/SingleValueDialog.java
new file mode 100644 (file)
index 0000000..45a227e
--- /dev/null
@@ -0,0 +1,82 @@
+package org.argeo.cms.ui.dialogs;
+
+import org.eclipse.jface.window.Window;
+import org.eclipse.swt.SWT;
+import org.eclipse.swt.layout.GridData;
+import org.eclipse.swt.widgets.Composite;
+import org.eclipse.swt.widgets.Control;
+import org.eclipse.swt.widgets.Display;
+import org.eclipse.swt.widgets.Shell;
+import org.eclipse.swt.widgets.Text;
+
+/** A dialog asking a for a single value. */
+public class SingleValueDialog extends CmsMessageDialog {
+       private Text valueT;
+       private String value;
+       private String defaultValue;
+
+       public SingleValueDialog(Shell parentShell, String message) {
+               super(parentShell, message, QUESTION);
+       }
+
+       public SingleValueDialog(Shell parentShell, String message, String defaultValue) {
+               super(parentShell, message, QUESTION);
+               this.defaultValue = defaultValue;
+       }
+
+       @Override
+       protected Control createInputArea(Composite parent) {
+               valueT = new Text(parent, SWT.LEAD | SWT.BORDER | SWT.SINGLE);
+               valueT.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, true));
+               if (defaultValue != null)
+                       valueT.setText(defaultValue);
+               return valueT;
+       }
+
+       @Override
+       protected void okPressed() {
+               value = valueT.getText();
+               super.okPressed();
+       }
+
+       public String getString() {
+               return value;
+       }
+
+       public Long getLong() {
+               return Long.valueOf(getString());
+       }
+
+       public Double getDouble() {
+               return Double.valueOf(getString());
+       }
+
+       public static String ask(String message) {
+               return ask(message, null);
+       }
+
+       public static String ask(String message, String defaultValue) {
+               SingleValueDialog svd = new SingleValueDialog(Display.getCurrent().getActiveShell(), message, defaultValue);
+               if (svd.open() == Window.OK)
+                       return svd.getString();
+               else
+                       return null;
+       }
+
+       public static Long askLong(String message) {
+               SingleValueDialog svd = new SingleValueDialog(Display.getCurrent().getActiveShell(), message);
+               if (svd.open() == Window.OK)
+                       return svd.getLong();
+               else
+                       return null;
+       }
+
+       public static Double askDouble(String message) {
+               SingleValueDialog svd = new SingleValueDialog(Display.getCurrent().getActiveShell(), message);
+               if (svd.open() == Window.OK)
+                       return svd.getDouble();
+               else
+                       return null;
+       }
+
+}
index cb1f00521c3024a3ce50e1ee0034be9eb1d18716..f3d0531e18bef4c619b12bac1779b336d3cf4a62 100644 (file)
@@ -17,7 +17,12 @@ import org.eclipse.swt.widgets.Display;
 import org.eclipse.swt.widgets.Shell;
 import org.eclipse.swt.widgets.Text;
 
-/** Generic error dialog to be used in try/catch blocks */
+/**
+ * Generic error dialog to be used in try/catch blocks.
+ * 
+ * @deprecated Use CMS dialogs instead.
+ */
+@Deprecated
 public class ErrorFeedback extends TitleAreaDialog {
        private static final long serialVersionUID = -8918084784628179044L;
 
index f42bff55c08c98672c058bada1473452441fa386..825879cf985bf7ee32440e6be4f5af9ded4f499f 100644 (file)
@@ -20,14 +20,19 @@ import org.eclipse.swt.widgets.Label;
 import org.eclipse.swt.widgets.Shell;
 import org.eclipse.swt.widgets.Text;
 
-/** Generic lightweight dialog, not based on JFace. */
+/**
+ * Generic lightweight dialog, not based on JFace.
+ * 
+ * @deprecated Use CMS dialogs instead.
+ */
+@Deprecated
 public class FeedbackDialog extends LightweightDialog {
        private final static Log log = LogFactory.getLog(FeedbackDialog.class);
 
        private String message;
        private Throwable exception;
 
-       private Shell parentShell;
+//     private Shell parentShell;
        private Shell shell;
 
        public static void show(String message, Throwable e) {
diff --git a/org.argeo.eclipse.ui/src/org/argeo/eclipse/ui/dialogs/NonModalErrorFeedback.java b/org.argeo.eclipse.ui/src/org/argeo/eclipse/ui/dialogs/NonModalErrorFeedback.java
deleted file mode 100644 (file)
index f1b9d90..0000000
+++ /dev/null
@@ -1,138 +0,0 @@
-package org.argeo.eclipse.ui.dialogs;
-
-import java.io.PrintWriter;
-import java.io.StringWriter;
-
-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.ShellAdapter;
-import org.eclipse.swt.events.ShellEvent;
-import org.eclipse.swt.graphics.Point;
-import org.eclipse.swt.graphics.Rectangle;
-import org.eclipse.swt.layout.GridData;
-import org.eclipse.swt.layout.GridLayout;
-import org.eclipse.swt.widgets.Composite;
-import org.eclipse.swt.widgets.Control;
-import org.eclipse.swt.widgets.Display;
-import org.eclipse.swt.widgets.Label;
-import org.eclipse.swt.widgets.Shell;
-import org.eclipse.swt.widgets.Text;
-
-/** Generic error dialog to be used in try/catch blocks */
-class NonModalErrorFeedback {
-       private final static Log log = LogFactory
-                       .getLog(NonModalErrorFeedback.class);
-
-       private final String message;
-       private final Throwable exception;
-
-       private Shell shell;
-
-       public static void show(String message, Throwable e) {
-               // rethrow ThreaDeath in order to make sure that RAP will properly clean
-               // up the UI thread
-               if (e instanceof ThreadDeath)
-                       throw (ThreadDeath) e;
-
-               new NonModalErrorFeedback(getDisplay().getActiveShell(), message, e)
-                               .open();
-       }
-
-       public static void show(String message) {
-               new NonModalErrorFeedback(getDisplay().getActiveShell(), message, null)
-                               .open();
-       }
-
-       /** Tries to find a display */
-       private static Display getDisplay() {
-               try {
-                       Display display = Display.getCurrent();
-                       if (display != null)
-                               return display;
-                       else
-                               return Display.getDefault();
-               } catch (Exception e) {
-                       return Display.getCurrent();
-               }
-       }
-
-       public NonModalErrorFeedback(Shell parentShell, String message, Throwable e) {
-               this.message = message;
-               this.exception = e;
-               log.error(message, e);
-       }
-
-       public void open() {
-               if (shell != null)
-                       throw new EclipseUiException("There is already a shell");
-               shell = new Shell(getDisplay(), SWT.NO_TRIM | SWT.BORDER | SWT.ON_TOP);
-               shell.setLayout(new GridLayout());
-               // shell.setText("Error");
-               shell.setSize(getInitialSize());
-               createDialogArea(shell);
-               // shell.pack();
-               // shell.layout();
-
-               Rectangle shellBounds = Display.getCurrent().getBounds();// RAP
-               Point dialogSize = shell.getSize();
-               int x = shellBounds.x + (shellBounds.width - dialogSize.x) / 2;
-               int y = shellBounds.y + (shellBounds.height - dialogSize.y) / 2;
-               shell.setLocation(x, y);
-
-               shell.addShellListener(new ShellAdapter() {
-                       private static final long serialVersionUID = -2701270481953688763L;
-
-                       @Override
-                       public void shellDeactivated(ShellEvent e) {
-                               closeShell();
-                       }
-               });
-
-               shell.open();
-       }
-
-       protected void closeShell() {
-               shell.close();
-               shell.dispose();
-               shell = null;
-       }
-
-       protected Point getInitialSize() {
-               // if (exception != null)
-               // return new Point(800, 600);
-               // else
-               return new Point(400, 300);
-       }
-
-       protected Control createDialogArea(Composite parent) {
-               Composite dialogarea = new Composite(parent, SWT.NONE);
-               dialogarea.setLayout(new GridLayout());
-               // Composite dialogarea = (Composite) super.createDialogArea(parent);
-               dialogarea.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
-
-               Label messageLbl = new Label(dialogarea, SWT.NONE);
-               if (message != null)
-                       messageLbl.setText(message);
-               else if (exception != null)
-                       messageLbl.setText(exception.getLocalizedMessage());
-
-               Composite composite = new Composite(dialogarea, SWT.NONE);
-               composite.setLayout(new GridLayout(2, false));
-               composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
-
-               if (exception != null) {
-                       Text stack = new Text(composite, SWT.MULTI | SWT.LEAD | SWT.BORDER
-                                       | SWT.V_SCROLL | SWT.H_SCROLL);
-                       stack.setEditable(false);
-                       stack.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
-                       StringWriter sw = new StringWriter();
-                       exception.printStackTrace(new PrintWriter(sw));
-                       stack.setText(sw.toString());
-               }
-
-               // parent.pack();
-               return composite;
-       }
-}
\ No newline at end of file
index 3c03bcb7bad338f197b9763781b3cbc5ce039a5d..8ce9b44fb5bade306d868ceff17634fc8cf74934 100644 (file)
@@ -15,7 +15,12 @@ import org.eclipse.swt.widgets.Label;
 import org.eclipse.swt.widgets.Shell;
 import org.eclipse.swt.widgets.Text;
 
-/** Dialog to retrieve a single value. */
+/**
+ * Dialog to retrieve a single value.
+ * 
+ * @deprecated Use CMS dialogs instead.
+ */
+@Deprecated
 public class SingleValue extends TitleAreaDialog {
        private static final long serialVersionUID = 2843538207460082349L;
 
@@ -49,12 +54,10 @@ public class SingleValue extends TitleAreaDialog {
        }
 
        public SingleValue(String label, String message) {
-               this(Display.getDefault().getActiveShell(), label, message, label,
-                               false);
+               this(Display.getDefault().getActiveShell(), label, message, label, false);
        }
 
-       public SingleValue(Shell parentShell, String title, String message,
-                       String label, Boolean multiline) {
+       public SingleValue(Shell parentShell, String title, String message, String label, Boolean multiline) {
                super(parentShell);
                this.title = title;
                this.message = message;