Eclipse 4 internationalisation
authorMathieu Baudier <mbaudier@argeo.org>
Wed, 11 Apr 2018 19:09:44 +0000 (21:09 +0200)
committerMathieu Baudier <mbaudier@argeo.org>
Wed, 11 Apr 2018 19:09:44 +0000 (21:09 +0200)
org.argeo.cms.e4.rap/bnd.bnd
org.argeo.cms.e4.rap/src/org/argeo/cms/e4/rap/AbstractRapE4App.java
org.argeo.cms.e4.rap/src/org/argeo/cms/e4/rap/CmsE4EntryPointFactory.java
org.argeo.cms.e4.rap/src/org/argeo/cms/e4/rap/CmsLoginLifecycle.java
org.argeo.cms.e4/src/org/argeo/cms/e4/handlers/ChangeLanguage.java [new file with mode: 0644]
org.argeo.cms.e4/src/org/argeo/cms/e4/handlers/CloseAllParts.java [new file with mode: 0644]
org.argeo.cms.e4/src/org/argeo/cms/e4/handlers/DoNothing.java [new file with mode: 0644]
org.argeo.cms.e4/src/org/argeo/cms/e4/handlers/LocaleAddon.java [new file with mode: 0644]
org.argeo.cms.e4/src/org/argeo/cms/e4/handlers/SaveAllParts.java [new file with mode: 0644]
org.argeo.cms.e4/src/org/argeo/cms/e4/handlers/SavePart.java [new file with mode: 0644]
org.argeo.cms/src/org/argeo/cms/auth/UserAdminLoginModule.java

index 1dc383b48757dc844f204f78710fb5fa0b9b5451..87191bbeb1343011651f4a3e3f2971948e1e3f0c 100644 (file)
@@ -3,5 +3,6 @@ Service-Component: OSGI-INF/cms-admin-rap.xml
 
 Import-Package: org.argeo.node,\
 org.eclipse.swt,\
+org.eclipse.swt.graphics,\
 org.eclipse.rap.rwt.client,\
 *
index 68415f31adbabc9479936c6349a9508423e75ce0..da71e90ad43cad4d98466a0570c98c55eed0b859 100644 (file)
@@ -3,8 +3,6 @@ package org.argeo.cms.e4.rap;
 import java.util.HashMap;
 import java.util.Map;
 
-import javax.security.auth.Subject;
-
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.argeo.cms.ui.dialogs.CmsFeedback;
@@ -43,8 +41,7 @@ public abstract class AbstractRapE4App implements ApplicationConfiguration {
                Map<String, String> properties = new HashMap<String, String>();
                properties.put(WebClient.PAGE_TITLE, pageTitle);
                E4ApplicationConfig config = new E4ApplicationConfig(e4Xmi, lifeCycleUri, null, false, true, true);
-               Subject subject = new Subject();
-               addEntryPoint(application, subject, config, properties);
+               addEntryPoint(application, config, properties);
                // config.isClearPersistedState();
                // E4EntryPointFactory entryPointFactory = new E4EntryPointFactory(config) {
                //
@@ -76,9 +73,8 @@ public abstract class AbstractRapE4App implements ApplicationConfiguration {
                // };
        }
 
-       protected void addEntryPoint(Application application, Subject subject, E4ApplicationConfig config,
-                       Map<String, String> properties) {
-               CmsE4EntryPointFactory entryPointFactory = new CmsE4EntryPointFactory(subject, config);
+       protected void addEntryPoint(Application application, E4ApplicationConfig config, Map<String, String> properties) {
+               CmsE4EntryPointFactory entryPointFactory = new CmsE4EntryPointFactory(config);
                application.addEntryPoint(path, entryPointFactory, properties);
                application.setOperationMode(OperationMode.SWT_COMPATIBILITY);
        }
index 6b974770eb7d03eabfa4de6d572644e13fac2e22..05eb7c3d03fb64fddf4366ea186ee67ab42cf077 100644 (file)
@@ -3,17 +3,26 @@ package org.argeo.cms.e4.rap;
 import java.security.PrivilegedAction;
 
 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.CurrentUser;
+import org.argeo.cms.ui.CmsImageManager;
+import org.argeo.cms.ui.CmsView;
+import org.argeo.cms.ui.UxContext;
+import org.argeo.cms.util.SimpleUxContext;
+import org.argeo.cms.widgets.auth.CmsLoginShell;
+import org.argeo.node.NodeConstants;
 import org.eclipse.rap.e4.E4ApplicationConfig;
 import org.eclipse.rap.e4.E4EntryPointFactory;
 import org.eclipse.rap.rwt.application.EntryPoint;
+import org.eclipse.swt.widgets.Display;
 
 public class CmsE4EntryPointFactory extends E4EntryPointFactory {
-       private Subject subject;
 
-       public CmsE4EntryPointFactory(Subject subject, E4ApplicationConfig config) {
+       public CmsE4EntryPointFactory(E4ApplicationConfig config) {
                super(config);
-               this.subject = subject;
        }
 
        @Override
@@ -24,14 +33,21 @@ public class CmsE4EntryPointFactory extends E4EntryPointFactory {
 
                        @Override
                        public int createUI() {
-                               return Subject.doAs(subject, new PrivilegedAction<Integer>() {
+                               Subject subject = new Subject();
+//                             boolean success = login(subject);
+//                             if (success)
+                                       return Subject.doAs(subject, new PrivilegedAction<Integer>() {
 
-                                       @Override
-                                       public Integer run() {
-                                               return ep.createUI();
-                                       }
+                                               @Override
+                                               public Integer run() {
+//                                                     RWT.setLocale(Locale.FRENCH);
+                                                       System.out.println("createUI");
+                                                       return ep.createUI();
+                                               }
 
-                               });
+                                       });
+//                             else
+//                                     return 1;
                        }
                };
                return authEp;
@@ -41,4 +57,98 @@ public class CmsE4EntryPointFactory extends E4EntryPointFactory {
                return super.create();
        }
 
+       boolean login(Subject subject) {
+               // Subject subject = Subject.getSubject(AccessController.getContext());
+               Display display = new Display();
+               CmsView cmsView = new E4CmsView(subject);
+               CmsLoginShell loginShell = new CmsLoginShell(cmsView);
+               loginShell.setSubject(subject);
+               try {
+                       // try pre-auth
+                       LoginContext loginContext = new LoginContext(NodeConstants.LOGIN_CONTEXT_USER, subject, loginShell);
+                       loginContext.login();
+               } catch (LoginException e) {
+                       loginShell.createUi();
+                       loginShell.open();
+
+                       while (!loginShell.getShell().isDisposed()) {
+                               if (!display.readAndDispatch())
+                                       display.sleep();
+                       }
+               } finally {
+                       display.dispose();
+               }
+               if (CurrentUser.getUsername(subject) == null)
+                       return false;
+               return true;
+       }
+
+       class E4CmsView implements CmsView {
+               private LoginContext loginContext;
+               private UxContext uxContext;
+               private Subject subject;
+
+               public E4CmsView(Subject subject) {
+                       this.subject = subject;
+                       uxContext = new SimpleUxContext();
+               }
+
+               @Override
+               public UxContext getUxContext() {
+                       return uxContext;
+               }
+
+               @Override
+               public void navigateTo(String state) {
+                       // TODO Auto-generated method stub
+
+               }
+
+               @Override
+               public void authChange(LoginContext loginContext) {
+                       if (loginContext == null)
+                               throw new CmsException("Login context cannot be null");
+                       // logout previous login context
+                       // if (this.loginContext != null)
+                       // try {
+                       // this.loginContext.logout();
+                       // } catch (LoginException e1) {
+                       // System.err.println("Could not log out: " + e1);
+                       // }
+                       this.loginContext = loginContext;
+               }
+
+               @Override
+               public void logout() {
+                       if (loginContext == null)
+                               throw new CmsException("Login context should not bet null");
+                       try {
+                               CurrentUser.logoutCmsSession(loginContext.getSubject());
+                               loginContext.logout();
+                       } catch (LoginException e) {
+                               throw new CmsException("Cannot log out", e);
+                       }
+               }
+
+               @Override
+               public void exception(Throwable e) {
+                       // TODO Auto-generated method stub
+
+               }
+
+               @Override
+               public CmsImageManager getImageManager() {
+                       // TODO Auto-generated method stub
+                       return null;
+               }
+
+               protected Subject getSubject() {
+                       return subject;
+               }
+
+               @Override
+               public boolean isAnonymous() {
+                       return CurrentUser.isAnonymous(getSubject());
+               }
+       }
 }
index 8b437a0e7988889b0cc77ac381c81e2989bc0d9b..ed5c20985683cae6897f6dbe098dfa9f9701e17f 100644 (file)
@@ -23,19 +23,7 @@ public class CmsLoginLifecycle implements CmsView {
        private LoginContext loginContext;
 
        @PostContextCreate
-       boolean login(Display d) {// , IEclipseContext eclipseContext) {
-               // RepositoryFactory repositoryFactory =
-               // eclipseContext.get(RepositoryFactory.class);
-               // Map<String, String> params = new HashMap<>();
-               // params.put("cn", "home");
-               // Repository homeRepository;
-               // try {
-               // homeRepository = repositoryFactory.getRepository(params);
-               // } catch (RepositoryException e1) {
-               // throw new CmsException("Cannot get home repository", e1);
-               // }
-               // eclipseContext.set("(cn=home)", homeRepository);
-
+       boolean login() {
                Subject subject = Subject.getSubject(AccessController.getContext());
                Display display = Display.getCurrent();
                CmsLoginShell loginShell = new CmsLoginShell(this);
@@ -56,6 +44,8 @@ public class CmsLoginLifecycle implements CmsView {
                if (CurrentUser.getUsername(getSubject()) == null)
                        return false;
                uxContext = new SimpleUxContext();
+               
+               //lcs.changeApplicationLocale(Locale.FRENCH);
                return true;
        }
 
diff --git a/org.argeo.cms.e4/src/org/argeo/cms/e4/handlers/ChangeLanguage.java b/org.argeo.cms.e4/src/org/argeo/cms/e4/handlers/ChangeLanguage.java
new file mode 100644 (file)
index 0000000..416df7d
--- /dev/null
@@ -0,0 +1,13 @@
+package org.argeo.cms.e4.handlers;
+
+import java.util.Locale;
+
+import org.eclipse.e4.core.di.annotations.Execute;
+import org.eclipse.e4.core.services.nls.ILocaleChangeService;
+
+public class ChangeLanguage {
+       @Execute
+       public void execute(ILocaleChangeService localeChangeService) {
+               localeChangeService.changeApplicationLocale(Locale.FRENCH);
+       }
+}
diff --git a/org.argeo.cms.e4/src/org/argeo/cms/e4/handlers/CloseAllParts.java b/org.argeo.cms.e4/src/org/argeo/cms/e4/handlers/CloseAllParts.java
new file mode 100644 (file)
index 0000000..850c0e2
--- /dev/null
@@ -0,0 +1,37 @@
+package org.argeo.cms.e4.handlers;
+
+import org.eclipse.e4.core.di.annotations.CanExecute;
+import org.eclipse.e4.core.di.annotations.Execute;
+import org.eclipse.e4.ui.model.application.ui.basic.MPart;
+import org.eclipse.e4.ui.workbench.modeling.EPartService;
+
+public class CloseAllParts {
+
+       @Execute
+       void execute(EPartService partService) {
+               for (MPart part : partService.getParts()) {
+                       if (part.isCloseable()) {
+                               if (part.isDirty()) {
+                                       if (partService.savePart(part, true)) {
+                                               partService.hidePart(part);
+                                       }
+                               } else {
+                                       partService.hidePart(part);
+                               }
+                       }
+               }
+       }
+
+       @CanExecute
+       boolean canExecute(EPartService partService) {
+               boolean atLeastOnePart = false;
+               for (MPart part : partService.getParts()) {
+                       if (part.isVisible()) {
+                               atLeastOnePart = true;
+                               break;
+                       }
+               }
+               return atLeastOnePart;
+       }
+
+}
\ No newline at end of file
diff --git a/org.argeo.cms.e4/src/org/argeo/cms/e4/handlers/DoNothing.java b/org.argeo.cms.e4/src/org/argeo/cms/e4/handlers/DoNothing.java
new file mode 100644 (file)
index 0000000..358494c
--- /dev/null
@@ -0,0 +1,10 @@
+package org.argeo.cms.e4.handlers;
+
+import org.eclipse.e4.core.di.annotations.Execute;
+
+public class DoNothing {
+       @Execute
+       public void execute() {
+
+       }
+}
diff --git a/org.argeo.cms.e4/src/org/argeo/cms/e4/handlers/LocaleAddon.java b/org.argeo.cms.e4/src/org/argeo/cms/e4/handlers/LocaleAddon.java
new file mode 100644 (file)
index 0000000..265b213
--- /dev/null
@@ -0,0 +1,18 @@
+package org.argeo.cms.e4.handlers;
+
+import java.security.AccessController;
+import java.util.Locale;
+
+import javax.annotation.PostConstruct;
+import javax.security.auth.Subject;
+
+import org.eclipse.e4.core.services.nls.ILocaleChangeService;
+
+public class LocaleAddon {
+       @PostConstruct
+       public void init(ILocaleChangeService localeChangeService) {
+               Subject subject = Subject.getSubject(AccessController.getContext());
+               Locale locale = subject.getPublicCredentials(Locale.class).iterator().next();
+               localeChangeService.changeApplicationLocale(locale);
+       }
+}
diff --git a/org.argeo.cms.e4/src/org/argeo/cms/e4/handlers/SaveAllParts.java b/org.argeo.cms.e4/src/org/argeo/cms/e4/handlers/SaveAllParts.java
new file mode 100644 (file)
index 0000000..3b60abd
--- /dev/null
@@ -0,0 +1,19 @@
+package org.argeo.cms.e4.handlers;
+
+import org.eclipse.e4.core.di.annotations.CanExecute;
+import org.eclipse.e4.core.di.annotations.Execute;
+import org.eclipse.e4.ui.workbench.modeling.EPartService;
+
+public class SaveAllParts {
+
+       @Execute
+       void execute(EPartService partService) {
+               partService.saveAll(false);
+       }
+
+       @CanExecute
+       boolean canExecute(EPartService partService) {
+               return partService.getDirtyParts().size() > 0;
+       }
+
+}
\ No newline at end of file
diff --git a/org.argeo.cms.e4/src/org/argeo/cms/e4/handlers/SavePart.java b/org.argeo.cms.e4/src/org/argeo/cms/e4/handlers/SavePart.java
new file mode 100644 (file)
index 0000000..73486f3
--- /dev/null
@@ -0,0 +1,18 @@
+package org.argeo.cms.e4.handlers;
+
+import org.eclipse.e4.core.di.annotations.CanExecute;
+import org.eclipse.e4.core.di.annotations.Execute;
+import org.eclipse.e4.ui.model.application.ui.basic.MPart;
+import org.eclipse.e4.ui.workbench.modeling.EPartService;
+
+public class SavePart {
+       @Execute
+       void execute(EPartService partService, MPart part) {
+               partService.savePart(part, false);
+       }
+
+       @CanExecute
+       boolean canExecute(MPart part) {
+               return part.isDirty();
+       }
+}
\ No newline at end of file
index de2d8bf75fe9e21dbd2141f3b9e5229dc61f9d23..b95d0577000c8591dd8c0c700c68cb9ce72c5b90 100644 (file)
@@ -167,6 +167,8 @@ public class UserAdminLoginModule implements LoginModule {
 
        @Override
        public boolean commit() throws LoginException {
+               subject.getPublicCredentials().add(locale);
+
                if (singleUser) {
                        OsUserUtils.loginAsSystemUser(subject);
                }
@@ -236,7 +238,7 @@ public class UserAdminLoginModule implements LoginModule {
                                });
                        }
                }
-               
+
                // Register CmsSession with initial subject
                CmsAuthUtils.registerSessionAuthorization(request, subject, authorization, locale);