Store UI context data in CMS View.
[lgpl/argeo-commons.git] / org.argeo.cms.ui.rap / src / org / argeo / cms / web / CmsWebEntryPoint.java
index 4f2521fd4fa718f0a99cc3a73cd56f5f2b4561ad..288069bd9de464400c934da1aab14d68a51bd760 100644 (file)
@@ -35,6 +35,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;
@@ -116,6 +117,9 @@ public class CmsWebEntryPoint implements EntryPoint, CmsView, BrowserNavigationL
                                        ui = cmsWebApp.getCmsApp().initUi(parent);
                                        ui.setData(CmsApp.UI_NAME_PROPERTY, uiName);
                                        ui.setLayoutData(CmsUiUtils.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 +173,13 @@ public class CmsWebEntryPoint implements EntryPoint, CmsView, BrowserNavigationL
 
        @Override
        public void exception(final Throwable e) {
+               if (e instanceof SWTError) {
+                       SWTError swtError = (SWTError) e;
+                       if (swtError.code == SWT.ERROR_FUNCTION_DISPOSED)
+                               return;
+               }
                ui.getDisplay().syncExec(() -> {
-                       CmsFeedback.show("Unexpected exception in CMS", e);
+//                     CmsFeedback.show("Unexpected exception in CMS", e);
                        exception = e;
 //             log.error("Unexpected exception in CMS", e);
                        doRefresh();
@@ -254,6 +263,24 @@ public class CmsWebEntryPoint implements EntryPoint, CmsView, BrowserNavigationL
                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
         */
@@ -273,12 +300,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;
        }