Make CMS login more robust.
[lgpl/argeo-commons.git] / org.argeo.cms.ui / src / org / argeo / cms / widgets / auth / CmsLogin.java
index e450bfa075040bb0f30926dfb1cd6225faa42711..cc0e766d4c35d162217c736aa6ea809141e7e3f2 100644 (file)
@@ -20,14 +20,15 @@ import javax.security.auth.login.LoginException;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.argeo.cms.CmsMsg;
+import org.argeo.cms.LocaleUtils;
 import org.argeo.cms.auth.HttpRequestCallback;
-import org.argeo.cms.i18n.LocaleUtils;
 import org.argeo.cms.ui.CmsStyles;
 import org.argeo.cms.ui.CmsView;
 import org.argeo.cms.ui.internal.Activator;
 import org.argeo.cms.util.CmsUtils;
 import org.argeo.eclipse.ui.specific.UiContext;
 import org.argeo.node.NodeConstants;
+import org.argeo.node.NodeState;
 import org.eclipse.swt.SWT;
 import org.eclipse.swt.events.MouseAdapter;
 import org.eclipse.swt.events.MouseEvent;
@@ -63,10 +64,15 @@ public class CmsLogin implements CmsStyles, CallbackHandler {
 
        public CmsLogin(CmsView cmsView) {
                this.cmsView = cmsView;
-               defaultLocale = Activator.getNodeState().getDefaultLocale();
-               List<Locale> locales = Activator.getNodeState().getLocales();
-               if (locales != null)
-                       localeChoice = new LocaleChoice(locales, defaultLocale);
+               NodeState nodeState = Activator.getNodeState();
+               if (nodeState != null) {
+                       defaultLocale = nodeState.getDefaultLocale();
+                       List<Locale> locales = nodeState.getLocales();
+                       if (locales != null)
+                               localeChoice = new LocaleChoice(locales, defaultLocale);
+               } else {
+                       defaultLocale = Locale.getDefault();
+               }
                loginSelectionListener = new SelectionListener() {
                        private static final long serialVersionUID = -8832133363830973578L;
 
@@ -118,7 +124,7 @@ public class CmsLogin implements CmsStyles, CallbackHandler {
                Locale locale = localeChoice == null ? this.defaultLocale : localeChoice.getSelectedLocale();
                credentialsBlock = new Composite(parent, SWT.NONE);
                credentialsBlock.setLayout(new GridLayout());
-               credentialsBlock.setLayoutData(CmsUtils.fillAll());
+               // credentialsBlock.setLayoutData(CmsUtils.fillAll());
 
                specificUserUi(credentialsBlock);
 
@@ -149,10 +155,12 @@ public class CmsLogin implements CmsStyles, CallbackHandler {
                // We need a composite for the traversal
                credentialsBlock = new Composite(parent, SWT.NONE);
                credentialsBlock.setLayout(new GridLayout());
-               credentialsBlock.setLayoutData(CmsUtils.fillAll());
+               // credentialsBlock.setLayoutData(CmsUtils.fillAll());
+               CmsUtils.style(credentialsBlock, CMS_LOGIN_DIALOG);
 
                Integer textWidth = 120;
-               CmsUtils.style(parent, CMS_USER_MENU);
+               if (parent instanceof Shell)
+                       CmsUtils.style(parent, CMS_USER_MENU);
                // new Label(this, SWT.NONE).setText(CmsMsg.username.lead());
                usernameT = new Text(credentialsBlock, SWT.BORDER);
                usernameT.setMessage(username.lead(locale));
@@ -182,7 +190,12 @@ public class CmsLogin implements CmsStyles, CallbackHandler {
                passwordT.addTraverseListener(tl);
                parent.setTabList(new Control[] { credentialsBlock });
                credentialsBlock.setTabList(new Control[] { usernameT, passwordT });
-               // credentialsBlock.setFocus();
+
+               // Button
+               Button loginButton = new Button(credentialsBlock, SWT.PUSH);
+               loginButton.setText(CmsMsg.login.lead(locale));
+               loginButton.setLayoutData(CmsUtils.fillWidth());
+               loginButton.addSelectionListener(loginSelectionListener);
 
                extendsCredentialsBlock(credentialsBlock, locale, loginSelectionListener);
                if (localeChoice != null)
@@ -216,6 +229,7 @@ public class CmsLogin implements CmsStyles, CallbackHandler {
 
        protected Composite createLocalesBlock(final Composite parent) {
                Composite c = new Composite(parent, SWT.NONE);
+               CmsUtils.style(c, CMS_USER_MENU_ITEM);
                c.setLayout(CmsUtils.noSpaceGridLayout());
                c.setLayoutData(CmsUtils.fillAll());
 
@@ -235,6 +249,7 @@ public class CmsLogin implements CmsStyles, CallbackHandler {
                for (Integer i = 0; i < locales.size(); i++) {
                        Locale locale = locales.get(i);
                        Button button = new Button(c, SWT.RADIO);
+                       CmsUtils.style(button, CMS_USER_MENU_ITEM);
                        button.setData(i);
                        button.setText(LocaleUtils.lead(locale.getDisplayName(locale), locale) + " (" + locale + ")");
                        // button.addListener(SWT.Selection, listener);
@@ -254,7 +269,11 @@ public class CmsLogin implements CmsStyles, CallbackHandler {
                        // LOGIN
                        //
                        // loginContext.logout();
-                       LoginContext loginContext = new LoginContext(NodeConstants.LOGIN_CONTEXT_USER, subject, this);
+                       LoginContext loginContext;
+                       if (subject == null)
+                               loginContext = new LoginContext(NodeConstants.LOGIN_CONTEXT_USER, this);
+                       else
+                               loginContext = new LoginContext(NodeConstants.LOGIN_CONTEXT_USER, subject, this);
                        loginContext.login();
                        cmsView.authChange(loginContext);
                        return true;