Make CMS login UI more extensible
authorMathieu Baudier <mbaudier@argeo.org>
Wed, 14 Oct 2015 09:02:35 +0000 (09:02 +0000)
committerMathieu Baudier <mbaudier@argeo.org>
Wed, 14 Oct 2015 09:02:35 +0000 (09:02 +0000)
git-svn-id: https://svn.argeo.org/commons/trunk@8479 4cfe0d0a-d680-48aa-b62c-e0a02a3f76cc

org.argeo.cms/src/org/argeo/cms/util/LoginEntryPoint.java
org.argeo.cms/src/org/argeo/cms/util/OpenUserMenu.java
org.argeo.cms/src/org/argeo/cms/util/UserMenu.java
org.argeo.cms/src/org/argeo/cms/util/UserMenuLink.java
org.argeo.cms/src/org/argeo/cms/widgets/auth/CmsLogin.java
org.argeo.cms/src/org/argeo/cms/widgets/auth/CmsLoginShell.java [new file with mode: 0644]
org.argeo.security.ui/src/org/argeo/security/ui/login/WorkbenchLogin.java

index f3597de776adc760e028fd95a04b5af7229efb42..e917525b74d46a84e2d5e64bc3f655128f5115e7 100644 (file)
@@ -1,14 +1,6 @@
 package org.argeo.cms.util;
 
-import javax.security.auth.Subject;
-import javax.security.auth.login.LoginContext;
-import javax.security.auth.login.LoginException;
-
-import org.argeo.cms.CmsException;
-import org.argeo.cms.auth.AuthConstants;
-import org.argeo.cms.widgets.auth.CompositeCallbackHandler;
 import org.eclipse.rap.rwt.application.AbstractEntryPoint;
-import org.eclipse.swt.SWT;
 import org.eclipse.swt.layout.GridLayout;
 import org.eclipse.swt.widgets.Composite;
 
@@ -18,16 +10,16 @@ public class LoginEntryPoint extends AbstractEntryPoint {
                parent.setLayout(new GridLayout());
                // CompositeCallbackHandler cch = new CompositeCallbackHandler(parent,
                // SWT.NONE);
-               UserMenu userMenu = new UserMenu(parent, false);
+               // UserMenu userMenu = new UserMenu(null);
 
-//             Subject subject = new Subject();
-//             try {
-//                     LoginContext lc = new LoginContext(
-//                                     AuthConstants.LOGIN_CONTEXT_USER, subject, userMenu);
-//                     lc.login();
-//             } catch (LoginException e1) {
-//                     throw new CmsException("Cannot logint", e1);
-//             }
+               // Subject subject = new Subject();
+               // try {
+               // LoginContext lc = new LoginContext(
+               // AuthConstants.LOGIN_CONTEXT_USER, subject, userMenu);
+               // lc.login();
+               // } catch (LoginException e1) {
+               // throw new CmsException("Cannot logint", e1);
+               // }
        }
 
 }
index 61ecebd196f8a61b13491e6ebd8d80898338217c..bb2bb3990f6937f946514505058d0edf686e53e2 100644 (file)
@@ -11,7 +11,7 @@ public class OpenUserMenu extends MouseAdapter {
        @Override
        public void mouseDown(MouseEvent e) {
                if (e.button == 1) {
-                       new UserMenu((Control) e.getSource(), true);
+                       new UserMenu((Control) e.getSource());
                }
        }
 }
\ No newline at end of file
index 95c7b21597a8b1aed1a9ce8b42132c5ecdad18f1..a654dddd4054f7aa27b318352afe65d892a0ec29 100644 (file)
@@ -1,84 +1,48 @@
 package org.argeo.cms.util;
 
-import org.argeo.cms.widgets.auth.CmsLogin;
-import org.eclipse.rap.rwt.RWT;
+import org.argeo.cms.CmsException;
+import org.argeo.cms.widgets.auth.CmsLoginShell;
 import org.eclipse.swt.SWT;
 import org.eclipse.swt.events.ShellAdapter;
 import org.eclipse.swt.events.ShellEvent;
-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;
 
 /** The site-related user menu */
-public class UserMenu extends CmsLogin {
-       private final Shell shell;
+public class UserMenu extends CmsLoginShell {
+       private final Control source;
 
-       public UserMenu(Control source, boolean autoclose) {
+       public UserMenu(Control source) {
                super(CmsUtils.getCmsView());
-               if (source != null) {
-                       shell = new Shell(Display.getCurrent(), SWT.NO_TRIM | SWT.BORDER
-                                       | SWT.ON_TOP);
-               } else {
-                       shell = new Shell(Display.getCurrent(), SWT.NO_TRIM);
-                       shell.setMaximized(true);
-                       shell.setLayout(CmsUtils.noSpaceGridLayout());
-               }
-               shell.setData(RWT.CUSTOM_VARIANT, CMS_USER_MENU);
-
-               if (isAnonymous()) {
-                       anonymousUi(shell);
-               } else {
-                       userUi(shell);
-               }
-
-               if (source != null) {// popup
-                       shell.pack();
-                       shell.layout();
-                       shell.setLocation(source.toDisplay(
-                                       source.getSize().x - shell.getSize().x, source.getSize().y));
-               } else { // centered
-                       Composite parent = getCredentialsBlock();
-                       parent.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true,
-                                       true));
-                       // Rectangle shellBounds = shell.getBounds();// RAP
-                       // Point dialogSize = parent.getSize();
-                       // int x = shellBounds.x + (shellBounds.width - dialogSize.x) / 2;
-                       // int y = shellBounds.y + (shellBounds.height - dialogSize.y) / 2;
-                       // parent.setLocation(x, y);
-               }
-               if (autoclose)
-                       shell.addShellListener(new ShellAdapter() {
-                               private static final long serialVersionUID = 5178980294808435833L;
-
-                               @Override
-                               public void shellDeactivated(ShellEvent e) {
-                                       closeShell();
-                               }
-                       });
-               shell.open();
-
+               if (source == null)
+                       throw new CmsException("Source control cannot be null.");
+               this.source = source;
+               open();
        }
 
        @Override
-       protected void login() {
-               super.login();
-               closeShell();
+       protected Shell createShell() {
+               return new Shell(Display.getCurrent(), SWT.NO_TRIM | SWT.BORDER
+                               | SWT.ON_TOP);
        }
 
        @Override
-       protected void logout() {
-               closeShell();
-               super.logout();
+       public void open() {
+               Shell shell = getShell();
+               shell.pack();
+               shell.layout();
+               shell.setLocation(source.toDisplay(source.getSize().x
+                               - shell.getSize().x, source.getSize().y));
+               shell.addShellListener(new ShellAdapter() {
+                       private static final long serialVersionUID = 5178980294808435833L;
+
+                       @Override
+                       public void shellDeactivated(ShellEvent e) {
+                               closeShell();
+                       }
+               });
+               super.open();
        }
 
-       protected void closeShell() {
-               shell.close();
-               shell.dispose();
-       }
-
-       public Shell getShell() {
-               return shell;
-       }
 }
index e8fd556bfe7f71302f16e40c20f7fe4751c239ff..1b7ca03f44f2fcc937498a208d6274840a9947b5 100644 (file)
@@ -38,7 +38,7 @@ public class UserMenuLink extends MenuLink {
        }
 
        protected UserMenu createUserMenu(Control source) {
-               return new UserMenu(source.getParent(), true);
+               return new UserMenu(source.getParent());
        }
 
        private class UserMenuLinkController implements MouseListener,
index d3a14dc8038bd680bbebfb8ff1d1f1b271f64ac9..366e80c2b9e1ba5663360ed343b76980b4fd523e 100644 (file)
@@ -32,6 +32,7 @@ import org.eclipse.swt.layout.GridLayout;
 import org.eclipse.swt.widgets.Composite;
 import org.eclipse.swt.widgets.Control;
 import org.eclipse.swt.widgets.Label;
+import org.eclipse.swt.widgets.Shell;
 import org.eclipse.swt.widgets.Text;
 
 public class CmsLogin implements CmsStyles, CallbackHandler {
@@ -48,12 +49,28 @@ public class CmsLogin implements CmsStyles, CallbackHandler {
                return CurrentUser.isAnonymous(cmsView.getSubject());
        }
 
+       protected void createContents(Composite parent) {
+               parent.setLayout(CmsUtils.noSpaceGridLayout());
+               Composite credentialsBlock = createCredentialsBlock(parent);
+               if (parent instanceof Shell) {
+                       credentialsBlock.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER,
+                                       true, true));
+               }
+       }
+
+       protected final Composite createCredentialsBlock(Composite parent) {
+               if (isAnonymous()) {
+                       return anonymousUi(parent);
+               } else {
+                       return userUi(parent);
+               }
+       }
+
        protected Composite getCredentialsBlock() {
                return credentialsBlock;
        }
 
-       protected void userUi(Composite parent) {
-               parent.setLayout(CmsUtils.noSpaceGridLayout());
+       protected Composite userUi(Composite parent) {
                credentialsBlock = new Composite(parent, SWT.NONE);
                credentialsBlock.setLayout(new GridLayout());
                credentialsBlock.setLayoutData(CmsUtils.fillAll());
@@ -74,6 +91,7 @@ public class CmsLogin implements CmsStyles, CallbackHandler {
                                logout();
                        }
                });
+               return credentialsBlock;
        }
 
        /** To be overridden */
@@ -81,9 +99,7 @@ public class CmsLogin implements CmsStyles, CallbackHandler {
 
        }
 
-       protected void anonymousUi(Composite parent) {
-               parent.setLayout(CmsUtils.noSpaceGridLayout());
-
+       protected Composite anonymousUi(Composite parent) {
                // We need a composite for the traversal
                credentialsBlock = new Composite(parent, SWT.NONE);
                credentialsBlock.setLayout(new GridLayout());
@@ -122,6 +138,7 @@ public class CmsLogin implements CmsStyles, CallbackHandler {
                parent.setTabList(new Control[] { credentialsBlock });
                credentialsBlock.setTabList(new Control[] { username, password });
                credentialsBlock.setFocus();
+               return credentialsBlock;
        }
 
        protected void login() {
diff --git a/org.argeo.cms/src/org/argeo/cms/widgets/auth/CmsLoginShell.java b/org.argeo.cms/src/org/argeo/cms/widgets/auth/CmsLoginShell.java
new file mode 100644 (file)
index 0000000..9f89f12
--- /dev/null
@@ -0,0 +1,52 @@
+package org.argeo.cms.widgets.auth;
+
+import org.argeo.cms.CmsView;
+import org.eclipse.rap.rwt.RWT;
+import org.eclipse.swt.SWT;
+import org.eclipse.swt.widgets.Display;
+import org.eclipse.swt.widgets.Shell;
+
+/** The site-related user menu */
+public class CmsLoginShell extends CmsLogin {
+       private final Shell shell;
+
+       public CmsLoginShell(CmsView cmsView) {
+               super(cmsView);
+               shell = createShell();
+               shell.setData(RWT.CUSTOM_VARIANT, CMS_USER_MENU);
+               createContents(shell);
+       }
+
+       /** To be overridden. */
+       protected Shell createShell() {
+               Shell shell = new Shell(Display.getCurrent(), SWT.NO_TRIM);
+               shell.setMaximized(true);
+               return shell;
+       }
+
+       /** To be overridden. */
+       public void open() {
+               shell.open();
+       }
+
+       @Override
+       protected void login() {
+               super.login();
+               closeShell();
+       }
+
+       @Override
+       protected void logout() {
+               closeShell();
+               super.logout();
+       }
+
+       protected void closeShell() {
+               shell.close();
+               shell.dispose();
+       }
+
+       public Shell getShell() {
+               return shell;
+       }
+}
index e36d1d1ae85266da4c58fc6c5ce68f0b8d61af80..2baedaf7731a24210d057814e381b85079cac889 100644 (file)
@@ -15,14 +15,10 @@ import org.argeo.cms.CmsView;
 import org.argeo.cms.auth.AuthConstants;
 import org.argeo.cms.auth.CurrentUser;
 import org.argeo.cms.auth.HttpRequestCallbackHandler;
-import org.argeo.cms.util.UserMenu;
-import org.argeo.cms.widgets.auth.CmsLogin;
+import org.argeo.cms.widgets.auth.CmsLoginShell;
 import org.argeo.eclipse.ui.specific.UiContext;
 import org.eclipse.rap.rwt.application.EntryPoint;
-import org.eclipse.swt.SWT;
-import org.eclipse.swt.widgets.Composite;
 import org.eclipse.swt.widgets.Display;
-import org.eclipse.swt.widgets.Shell;
 import org.eclipse.ui.PlatformUI;
 
 public abstract class WorkbenchLogin implements EntryPoint, CmsView {
@@ -35,16 +31,14 @@ public abstract class WorkbenchLogin implements EntryPoint, CmsView {
                final Display display = PlatformUI.createDisplay();
                UiContext.setData(CmsView.KEY, this);
                try {
+                       // try pre-auth
                        loginContext = new LoginContext(AuthConstants.LOGIN_CONTEXT_USER,
                                        subject, new HttpRequestCallbackHandler(getRequest()));
                        loginContext.login();
                } catch (CredentialNotFoundException e) {
-                       // Shell shell = new Shell(display, SWT.NO_TRIM);
-                       // shell.setMaximized(true);
-                       // shell.setBackground(display.getSystemColor(SWT.COLOR_CYAN));
-                       UserMenu userMenu = new UserMenu(null, false);
-                       // shell.open();
-                       while (!userMenu.getShell().isDisposed()) {
+                       CmsLoginShell loginShell = createCmsLoginShell();
+                       loginShell.open();
+                       while (!loginShell.getShell().isDisposed()) {
                                if (!display.readAndDispatch()) {
                                        display.sleep();
                                }
@@ -79,6 +73,10 @@ public abstract class WorkbenchLogin implements EntryPoint, CmsView {
                return UiContext.getHttpRequest();
        }
 
+       protected CmsLoginShell createCmsLoginShell() {
+               return new CmsLoginShell(this);
+       }
+
        @Override
        public void navigateTo(String state) {
                // TODO Auto-generated method stub
@@ -117,17 +115,4 @@ public abstract class WorkbenchLogin implements EntryPoint, CmsView {
                // TODO Auto-generated method stub
                return null;
        }
-
-       // private class WorbenchCmsLogin extends CmsLogin {
-       // private final Shell shell;
-       //
-       // public WorbenchCmsLogin(CmsView cmsView, Shell shell) {
-       // super(cmsView);
-       // this.shell = shell;
-       // shell
-       // Composite parent = new Composite(shell, SWT.NONE);
-       // anonymousUi(parent);
-       // }
-       //
-       // }
 }