Simplify CMS App deployment
[lgpl/argeo-commons.git] / rcp / org.argeo.cms.ui.rcp / src / org / argeo / cms / ui / rcp / CmsRcpApp.java
index fb56773b86bb8bac3744d09e71766045fea377e5..664d49d2b54896d99f0038b861cdd09bd4b5562a 100644 (file)
@@ -13,7 +13,6 @@ import javax.security.auth.login.LoginException;
 
 import org.argeo.api.cms.CmsApp;
 import org.argeo.api.cms.CmsAuth;
-import org.argeo.api.cms.CmsContext;
 import org.argeo.api.cms.CmsImageManager;
 import org.argeo.api.cms.CmsLog;
 import org.argeo.api.cms.CmsSession;
@@ -26,7 +25,6 @@ import org.argeo.cms.swt.CmsSwtUtils;
 import org.eclipse.e4.ui.css.core.engine.CSSEngine;
 import org.eclipse.e4.ui.css.core.engine.CSSErrorHandler;
 import org.eclipse.e4.ui.css.swt.engine.CSSSWTEngineImpl;
-import org.eclipse.swt.layout.GridLayout;
 import org.eclipse.swt.widgets.Composite;
 import org.eclipse.swt.widgets.Display;
 import org.eclipse.swt.widgets.Shell;
@@ -42,13 +40,9 @@ public class CmsRcpApp implements CmsView {
 
        private BundleContext bundleContext = FrameworkUtil.getBundle(CmsRcpApp.class).getBundleContext();
 
-       private Display display;
        private Shell shell;
        private CmsApp cmsApp;
-       private CmsUiThread uiThread;
 
-       private CmsContext cmsContext;
-       
        // CMS View
        private String uid;
        private LoginContext loginContext;
@@ -61,123 +55,68 @@ public class CmsRcpApp implements CmsView {
        // TODO make it configurable
        private String uiName = "desktop";
 
-       public CmsRcpApp() {
+       public CmsRcpApp(String uiName) {
                uid = UUID.randomUUID().toString();
+               this.uiName = uiName;
        }
 
-       public void init(Map<String, String> properties) {
-               try {
-                       Thread.sleep(5000);
-               } catch (InterruptedException e) {
-                       // silent
-               }
-               uiThread = new CmsUiThread();
-               uiThread.start();
-
-       }
+       public void initRcpApp() {
+               Display display = Display.getCurrent();
+               shell = new Shell(display);
+               shell.setText("Argeo CMS");
+               Composite parent = shell;
+               parent.setLayout(CmsSwtUtils.noSpaceGridLayout());
+               CmsSwtUtils.registerCmsView(shell, CmsRcpApp.this);
 
-       public void destroy(Map<String, String> properties) {
-               if (!shell.isDisposed())
-                       shell.dispose();
                try {
-                       uiThread.join();
-               } catch (InterruptedException e) {
-                       // silent
-               } finally {
-                       uiThread = null;
+                       loginContext = new LoginContext(CmsAuth.SINGLE_USER.getLoginContextName());
+                       loginContext.login();
+               } catch (LoginException e) {
+                       throw new IllegalStateException("Could not log in.", e);
                }
-       }
-
-       class CmsUiThread extends Thread {
-
-               public CmsUiThread() {
-                       super("CMS UI");
-               }
-
-               @Override
-               public void run() {
-                       display = Display.getDefault();
-                       shell = new Shell(display);
-                       shell.setText("Argeo CMS");
-                       Composite parent = shell;
-                       parent.setLayout(new GridLayout());
-                       CmsSwtUtils.registerCmsView(shell, CmsRcpApp.this);
-
-//                     Subject subject = new Subject();
-//                     CmsLoginShell loginShell = new CmsLoginShell(CmsRcpApp.this);
-//                     loginShell.setSubject(subject);
-                       try {
-                               // try pre-auth
-//                             loginContext = new LoginContext(NodeConstants.LOGIN_CONTEXT_USER, subject, loginShell);
-                               loginContext = new LoginContext(CmsAuth.LOGIN_CONTEXT_SINGLE_USER);
-                               loginContext.login();
-                       } catch (LoginException e) {
-                               throw new IllegalStateException("Could not log in.", e);
-//                             loginShell.createUi();
-//                             loginShell.open();
-//
-//                             while (!loginShell.getShell().isDisposed()) {
-//                                     if (!display.readAndDispatch())
-//                                             display.sleep();
-//                             }
-                       }
-                       if (log.isDebugEnabled())
-                               log.debug("Logged in to desktop: " + loginContext.getSubject());
-
-                       Subject.doAs(loginContext.getSubject(), (PrivilegedAction<Void>) () -> {
-
-                               // TODO factorise with web app
-                               parent.setData(CmsApp.UI_NAME_PROPERTY, uiName);
-                               ui = cmsApp.initUi(parent);
-                               if (ui instanceof Composite)
-                                       ((Composite) ui).setLayoutData(CmsSwtUtils.fillAll());
-                               // ui.setLayoutData(CmsUiUtils.fillAll());
-                               // we need ui to be set before refresh so that CmsView can store UI context data
-                               // in it.
-                               cmsApp.refreshUi(ui, null);
-
-                               // Styling
-                               CmsTheme theme = CmsSwtUtils.getCmsTheme(parent);
-                               if (theme != null) {
-                                       cssEngine = new CSSSWTEngineImpl(display);
-                                       for (String path : theme.getSwtCssPaths()) {
-                                               try (InputStream in = theme.loadPath(path)) {
-                                                       cssEngine.parseStyleSheet(in);
-                                               } catch (IOException e) {
-                                                       throw new IllegalStateException("Cannot load stylesheet " + path, e);
-                                               }
+               if (log.isDebugEnabled())
+                       log.debug("Logged in to desktop: " + loginContext.getSubject());
+
+               Subject.doAs(loginContext.getSubject(), (PrivilegedAction<Void>) () -> {
+
+                       // TODO factorise with web app
+                       parent.setData(CmsApp.UI_NAME_PROPERTY, uiName);
+                       ui = cmsApp.initUi(parent);
+                       if (ui instanceof Composite)
+                               ((Composite) ui).setLayoutData(CmsSwtUtils.fillAll());
+                       // we need ui to be set before refresh so that CmsView can store UI context data
+                       // in it.
+                       cmsApp.refreshUi(ui, null);
+
+                       // Styling
+                       CmsTheme theme = CmsSwtUtils.getCmsTheme(parent);
+                       if (theme != null) {
+                               cssEngine = new CSSSWTEngineImpl(display);
+                               for (String path : theme.getSwtCssPaths()) {
+                                       try (InputStream in = theme.loadPath(path)) {
+                                               cssEngine.parseStyleSheet(in);
+                                       } catch (IOException e) {
+                                               throw new IllegalStateException("Cannot load stylesheet " + path, e);
                                        }
-                                       cssEngine.setErrorHandler(new CSSErrorHandler() {
-                                               public void error(Exception e) {
-                                                       log.error("SWT styling error: ", e);
-                                               }
-                                       });
-                                       applyStyles(shell);
-                               }
-                               shell.layout(true, true);
-
-                               shell.open();
-                               while (!shell.isDisposed()) {
-                                       if (!display.readAndDispatch())
-                                               display.sleep();
                                }
-                               display.dispose();
-                               return null;
-                       });
-               }
+                               cssEngine.setErrorHandler(new CSSErrorHandler() {
+                                       public void error(Exception e) {
+                                               log.error("SWT styling error: ", e);
+                                       }
+                               });
+                               applyStyles(shell);
+                       }
+                       shell.layout(true, true);
 
+                       shell.open();
+                       return null;
+               });
        }
 
-       
-       
        /*
         * CMS VIEW
         */
 
-       public void setCmsContext(CmsContext cmsContext) {
-               this.cmsContext = cmsContext;
-       }
-
        @Override
        public String getUid() {
                return uid;
@@ -274,10 +213,14 @@ public class CmsRcpApp implements CmsView {
        /*
         * DEPENDENCY INJECTION
         */
-       public void setCmsApp(CmsApp cmsApp) {
+       public void setCmsApp(CmsApp cmsApp, Map<String, String> properties) {
                this.cmsApp = cmsApp;
        }
 
+       public void unsetCmsApp(CmsApp cmsApp, Map<String, String> properties) {
+               this.cmsApp = null;
+       }
+
        public void setEventAdmin(EventAdmin eventAdmin) {
                this.eventAdmin = eventAdmin;
        }