Improve CMS dialogs and localisation.
[lgpl/argeo-commons.git] / org.argeo.cms.ui / src / org / argeo / cms / ui / dialogs / CmsWizardDialog.java
index cafde7e2739f5505eeb43e173492384a3258ccaa..e4fe35d3426c72880e9d20afb968a5f45c90860c 100644 (file)
@@ -2,16 +2,18 @@ package org.argeo.cms.ui.dialogs;
 
 import java.lang.reflect.InvocationTargetException;
 
-import org.argeo.cms.CmsException;
-import org.argeo.cms.util.CmsUtils;
+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.jface.operation.IRunnableWithProgress;
 import org.eclipse.jface.wizard.IWizard;
 import org.eclipse.jface.wizard.IWizardContainer2;
 import org.eclipse.jface.wizard.IWizardPage;
 import org.eclipse.swt.SWT;
+import org.eclipse.swt.layout.FormAttachment;
+import org.eclipse.swt.layout.FormData;
+import org.eclipse.swt.layout.FormLayout;
 import org.eclipse.swt.layout.GridData;
 import org.eclipse.swt.layout.GridLayout;
 import org.eclipse.swt.widgets.Button;
@@ -20,15 +22,17 @@ import org.eclipse.swt.widgets.Control;
 import org.eclipse.swt.widgets.Label;
 import org.eclipse.swt.widgets.Shell;
 
+/** A wizard dialog based on {@link LightweightDialog}. */
 public class CmsWizardDialog extends LightweightDialog implements IWizardContainer2 {
        private static final long serialVersionUID = -2123153353654812154L;
 
        private IWizard wizard;
        private IWizardPage currentPage;
+       private int currentPageIndex;
 
        private Label titleBar;
        private Label message;
-       private Composite body;
+       private Composite[] pageBodies;
        private Composite buttons;
        private Button back;
        private Button next;
@@ -42,7 +46,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
@@ -52,13 +56,13 @@ public class CmsWizardDialog extends LightweightDialog implements IWizardContain
                Composite messageArea = new Composite(parent, SWT.NONE);
                messageArea.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
                {
-                       messageArea.setLayout(CmsUtils.noSpaceGridLayout(new GridLayout(2, false)));
+                       messageArea.setLayout(CmsUiUtils.noSpaceGridLayout(new GridLayout(2, false)));
                        titleBar = new Label(messageArea, SWT.WRAP);
                        titleBar.setFont(EclipseUiUtils.getBoldFont(parent));
                        titleBar.setLayoutData(new GridData(SWT.BEGINNING, SWT.FILL, true, false));
                        updateTitleBar();
                        Button cancelButton = new Button(messageArea, SWT.FLAT);
-                       cancelButton.setText("Cancel");
+                       cancelButton.setText(CmsMsg.cancel.lead());
                        cancelButton.setLayoutData(new GridData(SWT.END, SWT.TOP, false, false, 1, 3));
                        cancelButton.addSelectionListener((Selected) (e) -> closeShell(CANCEL));
                        message = new Label(messageArea, SWT.WRAP);
@@ -66,15 +70,24 @@ public class CmsWizardDialog extends LightweightDialog implements IWizardContain
                        updateMessage();
                }
 
-               body = new Composite(parent, SWT.BORDER);
-               body.setLayout(CmsUtils.noSpaceGridLayout());
+               Composite body = new Composite(parent, SWT.BORDER);
+               body.setLayout(new FormLayout());
                body.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
+               pageBodies = new Composite[wizard.getPageCount()];
+               IWizardPage[] pages = wizard.getPages();
+               for (int i = 0; i < pages.length; i++) {
+                       pageBodies[i] = new Composite(body, SWT.NONE);
+                       pageBodies[i].setLayout(CmsUiUtils.noSpaceGridLayout());
+                       setSwitchingFormData(pageBodies[i]);
+                       pages[i].createControl(pageBodies[i]);
+               }
                showPage(currentPage);
 
                buttons = new Composite(parent, SWT.NONE);
                buttons.setLayoutData(new GridData(SWT.END, SWT.FILL, true, false));
                {
                        boolean singlePage = wizard.getPageCount() == 1;
+                       // singlePage = false;// dev
                        GridLayout layout = new GridLayout(singlePage ? 1 : 3, true);
                        layout.marginWidth = 0;
                        layout.marginHeight = 0;
@@ -83,17 +96,17 @@ public class CmsWizardDialog extends LightweightDialog implements IWizardContain
 
                        if (!singlePage) {
                                back = new Button(buttons, SWT.PUSH);
-                               back.setText("Back");
+                               back.setText(CmsMsg.wizardBack.lead());
                                back.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false));
                                back.addSelectionListener((Selected) (e) -> backPressed());
 
                                next = new Button(buttons, SWT.PUSH);
-                               next.setText("Next");
+                               next.setText(CmsMsg.wizardNext.lead());
                                next.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false));
                                next.addSelectionListener((Selected) (e) -> nextPressed());
                        }
                        finish = new Button(buttons, SWT.PUSH);
-                       finish.setText("Finish");
+                       finish.setText(CmsMsg.wizardFinish.lead());
                        finish.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false));
                        finish.addSelectionListener((Selected) (e) -> finishPressed());
 
@@ -114,10 +127,24 @@ public class CmsWizardDialog extends LightweightDialog implements IWizardContain
 
        @Override
        public void showPage(IWizardPage page) {
-               // clear
-               for (Control c : body.getChildren())
-                       c.dispose();
-               page.createControl(body);
+               IWizardPage[] pages = wizard.getPages();
+               int index = -1;
+               for (int i = 0; i < pages.length; i++) {
+                       if (page == pages[i]) {
+                               index = i;
+                               break;
+                       }
+               }
+               if (index < 0)
+                       throw new IllegalArgumentException("Cannot find index of wizard page " + page);
+               pageBodies[index].moveAbove(pageBodies[currentPageIndex]);
+
+               // // clear
+               // for (Control c : body.getChildren())
+               // c.dispose();
+               // page.createControl(body);
+               // body.layout(true, true);
+               currentPageIndex = index;
                currentPage = page;
        }
 
@@ -127,17 +154,21 @@ public class CmsWizardDialog extends LightweightDialog implements IWizardContain
                        back.setEnabled(wizard.getPreviousPage(currentPage) != null);
                if (next != null)
                        next.setEnabled(wizard.getNextPage(currentPage) != null && currentPage.canFlipToNextPage());
-               finish.setEnabled(wizard.canFinish());
+               if (finish != null) {
+                       finish.setEnabled(wizard.canFinish());
+               }
        }
 
        @Override
        public void updateMessage() {
-               message.setText(currentPage.getMessage());
+               if (currentPage.getMessage() != null)
+                       message.setText(currentPage.getMessage());
        }
 
        @Override
        public void updateTitleBar() {
-               titleBar.setText(currentPage.getTitle());
+               if (currentPage.getTitle() != null)
+                       titleBar.setText(currentPage.getTitle());
        }
 
        @Override
@@ -163,15 +194,27 @@ public class CmsWizardDialog extends LightweightDialog implements IWizardContain
        protected void nextPressed() {
                IWizardPage page = wizard.getNextPage(currentPage);
                showPage(page);
+               updateButtons();
        }
 
        protected void backPressed() {
                IWizardPage page = wizard.getPreviousPage(currentPage);
                showPage(page);
+               updateButtons();
        }
 
        protected void finishPressed() {
                if (wizard.performFinish())
                        closeShell(OK);
        }
+
+       private static void setSwitchingFormData(Composite composite) {
+               FormData fdLabel = new FormData();
+               fdLabel.top = new FormAttachment(0, 0);
+               fdLabel.left = new FormAttachment(0, 0);
+               fdLabel.right = new FormAttachment(100, 0);
+               fdLabel.bottom = new FormAttachment(100, 0);
+               composite.setLayoutData(fdLabel);
+       }
+
 }