]> git.argeo.org Git - lgpl/argeo-commons.git/blobdiff - org.argeo.cms.ui.rap/src/org/argeo/cms/web/CmsWebEntryPoint.java
Major refactoring of Argeo CMS UI
[lgpl/argeo-commons.git] / org.argeo.cms.ui.rap / src / org / argeo / cms / web / CmsWebEntryPoint.java
index 4f2521fd4fa718f0a99cc3a73cd56f5f2b4561ad..4bdc5a0aa4fd554c2581767ba64d6658c9925c06 100644 (file)
@@ -15,18 +15,20 @@ import javax.security.auth.login.LoginException;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.argeo.api.NodeConstants;
+import org.argeo.api.cms.CmsApp;
+import org.argeo.api.cms.CmsImageManager;
+import org.argeo.api.cms.CmsSession;
+import org.argeo.api.cms.CmsUi;
+import org.argeo.api.cms.CmsView;
+import org.argeo.api.cms.UxContext;
 import org.argeo.cms.LocaleUtils;
-import org.argeo.cms.auth.CmsSession;
 import org.argeo.cms.auth.CurrentUser;
 import org.argeo.cms.auth.HttpRequestCallbackHandler;
-import org.argeo.cms.ui.CmsApp;
-import org.argeo.cms.ui.CmsImageManager;
-import org.argeo.cms.ui.CmsView;
-import org.argeo.cms.ui.UxContext;
-import org.argeo.cms.ui.dialogs.CmsFeedback;
-import org.argeo.cms.ui.util.CmsUiUtils;
+import org.argeo.cms.osgi.CmsOsgiUtils;
+import org.argeo.cms.swt.CmsSwtUtils;
+import org.argeo.cms.swt.SimpleSwtUxContext;
+import org.argeo.cms.swt.dialogs.CmsFeedback;
 import org.argeo.cms.ui.util.DefaultImageManager;
-import org.argeo.cms.ui.util.SimpleUxContext;
 import org.argeo.eclipse.ui.specific.UiContext;
 import org.eclipse.rap.rwt.RWT;
 import org.eclipse.rap.rwt.application.EntryPoint;
@@ -35,6 +37,7 @@ import org.eclipse.rap.rwt.client.service.BrowserNavigationEvent;
 import org.eclipse.rap.rwt.client.service.BrowserNavigationListener;
 import org.eclipse.rap.rwt.internal.lifecycle.RWTLifeCycle;
 import org.eclipse.swt.SWT;
+import org.eclipse.swt.SWTError;
 import org.eclipse.swt.widgets.Composite;
 import org.eclipse.swt.widgets.Display;
 import org.eclipse.swt.widgets.Shell;
@@ -58,7 +61,8 @@ public class CmsWebEntryPoint implements EntryPoint, CmsView, BrowserNavigationL
        private UxContext uxContext;
        private CmsImageManager imageManager;
 
-       private Composite ui;
+       private Display display;
+       private CmsUi ui;
 
        private String uid;
 
@@ -103,7 +107,7 @@ public class CmsWebEntryPoint implements EntryPoint, CmsView, BrowserNavigationL
                        @Override
                        public Void run() {
                                try {
-                                       uxContext = new SimpleUxContext();
+                                       uxContext = new SimpleSwtUxContext();
                                        imageManager = new DefaultImageManager();
                                        CmsSession cmsSession = getCmsSession();
                                        if (cmsSession != null) {
@@ -113,9 +117,14 @@ public class CmsWebEntryPoint implements EntryPoint, CmsView, BrowserNavigationL
                                                Locale rwtLocale = RWT.getUISession().getLocale();
                                                LocaleUtils.setThreadLocale(rwtLocale);
                                        }
+                                       parent.setData(CmsApp.UI_NAME_PROPERTY, uiName);
+                                       display = parent.getDisplay();
                                        ui = cmsWebApp.getCmsApp().initUi(parent);
-                                       ui.setData(CmsApp.UI_NAME_PROPERTY, uiName);
-                                       ui.setLayoutData(CmsUiUtils.fillAll());
+                                       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.
+                                       cmsWebApp.getCmsApp().refreshUi(ui, null);
                                } catch (Exception e) {
                                        throw new IllegalStateException("Cannot create entrypoint contents", e);
                                }
@@ -169,8 +178,13 @@ public class CmsWebEntryPoint implements EntryPoint, CmsView, BrowserNavigationL
 
        @Override
        public void exception(final Throwable e) {
-               ui.getDisplay().syncExec(() -> {
-                       CmsFeedback.show("Unexpected exception in CMS", e);
+               if (e instanceof SWTError) {
+                       SWTError swtError = (SWTError) e;
+                       if (swtError.code == SWT.ERROR_FUNCTION_DISPOSED)
+                               return;
+               }
+               display.syncExec(() -> {
+//                     CmsFeedback.show("Unexpected exception in CMS", e);
                        exception = e;
 //             log.error("Unexpected exception in CMS", e);
                        doRefresh();
@@ -221,7 +235,6 @@ public class CmsWebEntryPoint implements EntryPoint, CmsView, BrowserNavigationL
                        browserNavigation.pushState(state, title);
        }
 
-       @Override
        public CmsImageManager getImageManager() {
                return imageManager;
        }
@@ -250,10 +263,28 @@ public class CmsWebEntryPoint implements EntryPoint, CmsView, BrowserNavigationL
 
        @Override
        public CmsSession getCmsSession() {
-               CmsSession cmsSession = CmsSession.getCmsSession(cmsWebApp.getBundleContext(), getSubject());
+               CmsSession cmsSession = CmsOsgiUtils.getCmsSession(cmsWebApp.getBundleContext(), getSubject());
                return cmsSession;
        }
 
+       @Override
+       public Object getData(String key) {
+               if (ui != null) {
+                       return ui.getData(key);
+               } else {
+                       throw new IllegalStateException("UI is not initialized");
+               }
+       }
+
+       @Override
+       public void setData(String key, Object value) {
+               if (ui != null) {
+                       ui.setData(key, value);
+               } else {
+                       throw new IllegalStateException("UI is not initialized");
+               }
+       }
+
        /*
         * EntryPoint IMPLEMENTATION
         */
@@ -262,8 +293,8 @@ public class CmsWebEntryPoint implements EntryPoint, CmsView, BrowserNavigationL
        public int createUI() {
                Display display = new Display();
                Shell shell = createShell(display);
-               shell.setLayout(CmsUiUtils.noSpaceGridLayout());
-               CmsView.registerCmsView(shell, this);
+               shell.setLayout(CmsSwtUtils.noSpaceGridLayout());
+               CmsSwtUtils.registerCmsView(shell, this);
                createContents(shell);
                shell.layout();
 //             if (shell.getMaximized()) {
@@ -273,12 +304,34 @@ public class CmsWebEntryPoint implements EntryPoint, CmsView, BrowserNavigationL
 //             }
                shell.open();
                if (getApplicationContext().getLifeCycleFactory().getLifeCycle() instanceof RWTLifeCycle) {
-                       while (!shell.isDisposed()) {
-                               if (!display.readAndDispatch()) {
-                                       display.sleep();
+                       eventLoop: while (!shell.isDisposed()) {
+                               try {
+                                       if (!display.readAndDispatch()) {
+                                               display.sleep();
+                                       }
+                               } catch (Throwable e) {
+                                       if (e instanceof SWTError) {
+                                               SWTError swtError = (SWTError) e;
+                                               if (swtError.code == SWT.ERROR_FUNCTION_DISPOSED) {
+                                                       log.error("Unexpected SWT error in event loop, ignoring it. " + e.getMessage());
+                                                       continue eventLoop;
+                                               } else {
+                                                       log.error("Unexpected SWT error in event loop, shutting down...", e);
+                                                       break eventLoop;
+                                               }
+                                       } else if (e instanceof ThreadDeath) {
+                                               throw (ThreadDeath) e;
+                                       } else if (e instanceof Error) {
+                                               log.error("Unexpected error in event loop, shutting down...", e);
+                                               break eventLoop;
+                                       } else {
+                                               log.error("Unexpected exception in event loop, ignoring it. " + e.getMessage());
+                                               continue eventLoop;
+                                       }
                                }
                        }
-                       display.dispose();
+                       if (!display.isDisposed())
+                               display.dispose();
                }
                return 0;
        }