Append multiple exceptions in the same CmsFeedBack
authorMathieu Baudier <mbaudier@argeo.org>
Wed, 21 Sep 2022 09:22:01 +0000 (11:22 +0200)
committerMathieu Baudier <mbaudier@argeo.org>
Wed, 21 Sep 2022 09:22:01 +0000 (11:22 +0200)
swt/org.argeo.cms.swt/src/org/argeo/cms/swt/dialogs/CmsFeedback.java
swt/org.argeo.cms.swt/src/org/argeo/cms/swt/dialogs/LightweightDialog.java

index ddb8a7f95ff438067fb3242be261ebf64810e85e..69b117849949f33a01c3065b4bf2fc5f2751f3a9 100644 (file)
@@ -1,5 +1,6 @@
 package org.argeo.cms.swt.dialogs;
 
+import java.io.IOException;
 import java.io.PrintWriter;
 import java.io.StringWriter;
 
@@ -12,6 +13,7 @@ import org.eclipse.swt.layout.GridLayout;
 import org.eclipse.swt.widgets.Button;
 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;
@@ -23,6 +25,8 @@ public class CmsFeedback extends LightweightDialog {
        private String message;
        private Throwable exception;
 
+       private Text stack;
+
        private CmsFeedback(Shell parentShell, String message, Throwable e) {
                super(parentShell);
                this.message = message;
@@ -37,9 +41,23 @@ public class CmsFeedback extends LightweightDialog {
 
                log.error(message, e);
                try {
+                       Display display = LightweightDialog.findDisplay();
+
+                       CmsFeedback current = (CmsFeedback) display.getData(CmsFeedback.class.getName());
+                       if (current != null) {// already one open
+                               current.append("");
+                               if (message != null)
+                                       current.append(message);
+                               if (e != null)
+                                       current.append(e);
+                               // FIXME set a limit to the size of the text
+                               return current;
+                       }
+
                        CmsFeedback cmsFeedback = new CmsFeedback(null, message, e);
                        cmsFeedback.setBlockOnOpen(false);
                        cmsFeedback.open();
+                       cmsFeedback.getDisplay().setData(CmsFeedback.class.getName(), cmsFeedback);
                        return cmsFeedback;
                } catch (Throwable e1) {
                        log.error("Cannot open error feedback (" + e.getMessage() + "), original error below", e);
@@ -53,23 +71,11 @@ public class CmsFeedback extends LightweightDialog {
                return cmsFeedback;
        }
 
-       /** 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();
-       // }
-       // }
-
        protected Control createDialogArea(Composite parent) {
                parent.setLayout(new GridLayout(2, false));
 
                Label messageLbl = new Label(parent, SWT.WRAP);
+               messageLbl.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
                if (message != null)
                        messageLbl.setText(message);
                else if (exception != null)
@@ -80,21 +86,33 @@ public class CmsFeedback extends LightweightDialog {
                close.setLayoutData(new GridData(SWT.END, SWT.TOP, false, false));
                close.addSelectionListener((Selected) (e) -> closeShell(OK));
 
-               // 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(parent, SWT.MULTI | SWT.LEAD | SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL);
+                       stack = new Text(parent, 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, 2, 1));
-                       StringWriter sw = new StringWriter();
+                       append(exception);
+               }
+               return messageLbl;
+       }
+
+       protected void append(String message) {
+               stack.append(message);
+               stack.append("\n");
+       }
+
+       protected void append(Throwable exception) {
+               try (StringWriter sw = new StringWriter()) {
                        exception.printStackTrace(new PrintWriter(sw));
-                       stack.setText(sw.toString());
+                       stack.append(sw.toString());
+               } catch (IOException e) {
+                       // ignore
                }
 
-               // parent.pack();
-               return messageLbl;
+       }
+
+       @Override
+       protected void onClose() {
+               getDisplay().setData(CmsFeedback.class.getName(), null);
        }
 
 }
index bf6417beadd3baf911407cd57e6665c946649698..9e6a8d5b04013b13d4ecb361d7919ae5cdd3670a 100644 (file)
@@ -29,13 +29,15 @@ public class LightweightDialog {
        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 +54,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 +64,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 +74,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;
@@ -113,6 +116,7 @@ public class LightweightDialog {
                                        closeShell(CANCEL);
                        }
                });
+               backgroundShell.addDisposeListener((event) -> onClose());
 
                if (block) {
                        block();
@@ -141,6 +145,10 @@ public class LightweightDialog {
                return foregoundShell.getShells().length != 0;
        }
 
+       protected void onClose() {
+
+       }
+
        // public synchronized int openAndWait() {
        // open();
        // while (returnCode == null)
@@ -252,4 +260,8 @@ public class LightweightDialog {
                return returnCode;
        }
 
+       Display getDisplay() {
+               return display;
+       }
+
 }
\ No newline at end of file