Improve CMS dialogs and localisation.
[lgpl/argeo-commons.git] / org.argeo.cms.ui / src / org / argeo / cms / ui / dialogs / CmsMessageDialog.java
index 6e1fc261c2eddb1201a47146f092720011d742c7..eb881c6bd03cf60fb7e9b7181465f57055b24228 100644 (file)
@@ -1,10 +1,12 @@
 package org.argeo.cms.ui.dialogs;
 
 import org.argeo.cms.CmsMsg;
+import org.argeo.cms.ui.util.CmsUiUtils;
 import org.argeo.eclipse.ui.EclipseUiUtils;
 import org.argeo.eclipse.ui.Selected;
-import org.argeo.eclipse.ui.dialogs.LightweightDialog;
 import org.eclipse.swt.SWT;
+import org.eclipse.swt.events.TraverseEvent;
+import org.eclipse.swt.events.TraverseListener;
 import org.eclipse.swt.graphics.Point;
 import org.eclipse.swt.layout.GridData;
 import org.eclipse.swt.layout.GridLayout;
@@ -15,11 +17,15 @@ import org.eclipse.swt.widgets.Display;
 import org.eclipse.swt.widgets.Label;
 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;
@@ -33,23 +39,39 @@ public class CmsMessageDialog extends LightweightDialog {
        protected Control createDialogArea(Composite parent) {
                parent.setLayout(new GridLayout());
 
+               TraverseListener traverseListener = new TraverseListener() {
+                       private static final long serialVersionUID = -1158892811534971856L;
+
+                       public void keyTraversed(TraverseEvent e) {
+                               if (e.detail == SWT.TRAVERSE_RETURN)
+                                       okPressed();
+                               else if (e.detail == SWT.TRAVERSE_ESCAPE)
+                                       cancelPressed();
+                       }
+               };
+
                // message
                Composite body = new Composite(parent, SWT.NONE);
+               body.addTraverseListener(traverseListener);
                GridLayout bodyGridLayout = new GridLayout();
                bodyGridLayout.marginHeight = 20;
                bodyGridLayout.marginWidth = 20;
                body.setLayout(bodyGridLayout);
                body.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
 
-               Label messageLbl = new Label(body, SWT.WRAP);
-               messageLbl.setFont(EclipseUiUtils.getBoldFont(parent));
-               if (message != null)
+               if (message != null) {
+                       Label messageLbl = new Label(body, SWT.WRAP);
+                       CmsUiUtils.markup(messageLbl);
+                       messageLbl.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
+                       messageLbl.setFont(EclipseUiUtils.getBoldFont(parent));
                        messageLbl.setText(message);
+               }
 
                // buttons
                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;
@@ -59,7 +81,16 @@ public class CmsMessageDialog extends LightweightDialog {
                        close.setText(CmsMsg.close.lead());
                        close.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false));
                        close.addSelectionListener((Selected) (e) -> closeShell(OK));
-               } else if (kind == CONFIRM || kind == QUESTION) {
+                       close.setFocus();
+                       close.addTraverseListener(traverseListener);
+
+                       buttons.setTabList(new Control[] { close });
+               } else if (kind == CONFIRM || kind == QUESTION || kind == QUESTION_WITH_CANCEL) {
+                       Control input = createInputArea(body);
+                       if (input != null) {
+                               input.addTraverseListener(traverseListener);
+                               body.setTabList(new Control[] { input });
+                       }
                        GridLayout layout = new GridLayout(2, true);
                        layout.marginWidth = 0;
                        layout.marginHeight = 0;
@@ -68,15 +99,40 @@ public class CmsMessageDialog extends LightweightDialog {
                        Button cancel = new Button(buttons, SWT.FLAT);
                        cancel.setText(CmsMsg.cancel.lead());
                        cancel.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false));
-                       cancel.addSelectionListener((Selected) (e) -> closeShell(CANCEL));
+                       cancel.addSelectionListener((Selected) (e) -> cancelPressed());
+                       cancel.addTraverseListener(traverseListener);
 
                        Button ok = new Button(buttons, SWT.FLAT);
                        ok.setText(CmsMsg.ok.lead());
                        ok.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false));
-                       ok.addSelectionListener((Selected) (e) -> closeShell(OK));
+                       ok.addSelectionListener((Selected) (e) -> okPressed());
+                       ok.addTraverseListener(traverseListener);
+                       if (input == null)
+                               ok.setFocus();
+                       else
+                               input.setFocus();
+
+                       buttons.setTabList(new Control[] { ok, cancel });
                }
                // pack();
-               return messageLbl;
+               parent.setTabList(new Control[] { body, buttons });
+               return body;
+       }
+
+       protected Control createInputArea(Composite parent) {
+               return null;
+       }
+
+       protected void okPressed() {
+               closeShell(OK);
+       }
+
+       protected void cancelPressed() {
+               closeShell(CANCEL);
+       }
+
+       protected void cancel() {
+               closeShell(CANCEL);
        }
 
        protected Point getInitialSize() {
@@ -104,4 +160,8 @@ public class CmsMessageDialog extends LightweightDialog {
                open(WARNING, Display.getCurrent().getActiveShell(), message);
        }
 
+       public static void openError(String message) {
+               open(ERROR, Display.getCurrent().getActiveShell(), message);
+       }
+
 }