Make layer switching more robust
[gpl/argeo-suite.git] / org.argeo.app.ui / src / org / argeo / app / ui / SuiteApp.java
index 1e71179d4f5f288f6a29384253490fc873cb3285..9ad2673fc1216d79746a5de580c2a6b7f56dc359 100644 (file)
@@ -2,6 +2,7 @@ package org.argeo.app.ui;
 
 import static org.argeo.api.cms.ux.CmsView.CMS_VIEW_UID_PROPERTY;
 
+import java.util.ArrayList;
 import java.util.Collections;
 import java.util.HashMap;
 import java.util.HashSet;
@@ -80,6 +81,7 @@ public class SuiteApp extends AbstractCmsApp implements CmsEventSubscriber {
        @Deprecated
        private String defaultThemeId = "org.argeo.app.theme.default";
 
+       // TODO use QName as key for byType
        private Map<String, RankedObject<SwtUiProvider>> uiProvidersByPid = Collections.synchronizedMap(new HashMap<>());
        private Map<String, RankedObject<SwtUiProvider>> uiProvidersByType = Collections.synchronizedMap(new HashMap<>());
        private Map<String, RankedObject<SuiteLayer>> layersByPid = Collections.synchronizedSortedMap(new TreeMap<>());
@@ -264,7 +266,7 @@ public class SuiteApp extends AbstractCmsApp implements CmsEventSubscriber {
                                setState(ui, state != null ? state : defaultLayerPid);
                        }
                } catch (Exception e) {
-                       CmsFeedback.show("Unexpected exception", e);
+                       CmsFeedback.error("Unexpected exception", e);
                }
        }
 
@@ -362,8 +364,9 @@ public class SuiteApp extends AbstractCmsApp implements CmsEventSubscriber {
                                if (byType.containsKey(type))
                                        types.add(type);
                        }
-                       if (types.size() == 0)
+                       if (types.size() == 0) {
                                throw new IllegalArgumentException("No type found for " + content + " (" + objectClasses + ")");
+                       }
                        String type = types.iterator().next();
                        if (!byType.containsKey(type))
                                throw new IllegalArgumentException("No component found for " + content + " with type " + type);
@@ -549,8 +552,8 @@ public class SuiteApp extends AbstractCmsApp implements CmsEventSubscriber {
                                        }
                                }
                        } catch (Exception e) {
-                               log.error("Cannot handle event " + event, e);
-//                     CmsView.getCmsView(ui).exception(e);
+                               CmsFeedback.error("Cannot handle event " + event, e);
+//                             log.error("Cannot handle event " + event, e);
                        }
                });
        }
@@ -629,8 +632,9 @@ public class SuiteApp extends AbstractCmsApp implements CmsEventSubscriber {
                }
                if (properties.containsKey(EntityConstants.TYPE)) {
                        List<String> types = LangUtils.toStringList(properties.get(EntityConstants.TYPE));
-                       for (String type : types)
+                       for (String type : types) {
                                RankedObject.putIfHigherRank(uiProvidersByType, type, uiProvider, properties);
+                       }
                }
        }
 
@@ -656,12 +660,26 @@ public class SuiteApp extends AbstractCmsApp implements CmsEventSubscriber {
        }
 
        public void addLayer(SuiteLayer layer, Map<String, Object> properties) {
-               if (properties.containsKey(Constants.SERVICE_PID)) {
-                       String pid = (String) properties.get(Constants.SERVICE_PID);
+               if (!properties.containsKey(Constants.SERVICE_PID))
+                       throw new IllegalArgumentException("A layer must have an ID");
+               String pid = (String) properties.get(Constants.SERVICE_PID);
+               List<String> types = properties.containsKey(EntityConstants.TYPE)
+                               ? LangUtils.toStringList(properties.get(EntityConstants.TYPE))
+                               : new ArrayList<>();
+               if (types.isEmpty()) {
+                       RankedObject.putIfHigherRank(layersByPid, pid, layer, properties);
+               } else {
+                       if (layersByPid.containsKey(pid)) {
+                               RankedObject<SuiteLayer> current = layersByPid.get(pid);
+                               List<String> currentTypes = current.getProperties().containsKey(EntityConstants.TYPE)
+                                               ? LangUtils.toStringList(current.getProperties().get(EntityConstants.TYPE))
+                                               : new ArrayList<>();
+                               if (!types.containsAll(currentTypes)) {
+                                       throw new IllegalArgumentException("Higher-ranked layer " + pid + " contains only types " + types
+                                                       + ", while it must override all " + currentTypes);
+                               }
+                       }
                        RankedObject.putIfHigherRank(layersByPid, pid, layer, properties);
-               }
-               if (properties.containsKey(EntityConstants.TYPE)) {
-                       List<String> types = LangUtils.toStringList(properties.get(EntityConstants.TYPE));
                        for (String type : types)
                                RankedObject.putIfHigherRank(layersByType, type, layer, properties);
                }