]> gerrit.simantics Code Review - simantics/district.git/commitdiff
Manage drop-down menu contents by context in district finder dialog 50/4050/1
authorReino Ruusu <reino.ruusu@semantum.fi>
Fri, 27 Mar 2020 13:07:57 +0000 (15:07 +0200)
committerReino Ruusu <reino.ruusu@semantum.fi>
Fri, 27 Mar 2020 13:07:57 +0000 (15:07 +0200)
gitlab #84

Also fix failure in creation of new queries.

Change-Id: I122ec005119a7efd8878b24815dc09a226e8b6d9

org.simantics.district.selection.ui/src/org/simantics/district/selection/ui/parts/EditSelectorDialog.java
org.simantics.district.selection/src/org/simantics/district/selection/ElementSelector.java

index 414eb33998e7ada921f6a63de19b9bcc2879d290..b708d8e978695dc18919f05a80a824409d2b1564 100644 (file)
@@ -49,7 +49,6 @@ import org.simantics.db.layer0.request.ActiveModels;
 import org.simantics.db.layer0.request.PropertyInfo;
 import org.simantics.db.layer0.request.PropertyInfoRequest;
 import org.simantics.db.layer0.util.Layer0Utils;
-import org.simantics.db.request.Read;
 import org.simantics.diagram.stubs.DiagramResource;
 import org.simantics.district.network.ontology.DistrictNetworkResource;
 import org.simantics.district.region.ontology.DiagramRegionsResource;
@@ -192,71 +191,6 @@ public class EditSelectorDialog extends Dialog {
                                diagramNames.add(e.getValue());
                        });
                
-               final Map<Resource, String> regions = new HashMap<>();
-               final Map<Resource, String> routes = new HashMap<>();
-               
-               try {
-                       Simantics.getSession().syncRequest(new Read<Void>() {
-                               @Override
-                               public Void perform(ReadGraph graph) throws DatabaseException {
-                                       Resource model = ActiveModels.getPossibleActiveModel(graph, Simantics.getProjectResource());
-                                       List<Resource> regionCollection = QueryIndexUtils.searchByType(graph, model, DiagramRegionsResource.getInstance(graph).Region);
-                                       for (Resource r : regionCollection) {
-                                               String name = graph.getRelatedValue(r, Layer0.getInstance(graph).HasName);
-                                               regions.put(r, name);
-                                       }
-                                       
-                                       List<Resource> routeCollection = QueryIndexUtils.searchByType(graph, model, RouteResource.getInstance(graph).Route);
-                                       for (Resource r : routeCollection) {
-                                               String name = graph.getRelatedValue(r, Layer0.getInstance(graph).HasName);
-                                               routes.put(r, name);
-                                       }
-                                       return null;
-                               }
-                       });
-               } catch (DatabaseException e) {
-                       LOGGER.error("Failed to read routes and/or regions in the model", e);
-               }
-               
-               regionNames = regions.values().toArray(new String[regions.size()]);
-               regionResources = regions.keySet().toArray(new Resource[regions.size()]);
-               
-               routeNames = routes.values().toArray(new String[routes.size()]);
-               routeResources = routes.keySet().toArray(new Resource[routes.size()]);
-               
-               try {
-                       Simantics.getSession().syncRequest(new ReadRequest() {
-                               @Override
-                               public void run(ReadGraph graph) throws DatabaseException {
-                                       Layer0 L0 = Layer0.getInstance(graph);
-                                       List<Resource> types = findComponentTypes(graph);
-                                       
-                                       componentTypes = new ArrayList<>(types.size() + 1);
-                                       componentTypeNames = new ArrayList<>(types.size() + 1);
-                                       
-                                       componentTypes.add(null);
-                                       componentTypeNames.add("Any type");
-                                       componentTypes.addAll(types);
-                                       for (Resource t : types) {
-                                               componentTypeNames.add(graph.getValue2(t, L0.HasName));
-                                       }
-                               }
-                       });
-               } catch (DatabaseException e) {
-                       LOGGER.error("Failed to read district component types", e);
-               }
-               
-               componentType = elementSelector.getSelector().componentType;
-               
-               propertyNames = new ArrayList<>();
-               propertyLabels = new ArrayList<>();
-               
-               try {
-                       updatePropertyList();
-               } catch (DatabaseException e) {
-                       LOGGER.error("Failed to read district component properties", e);
-               }
-       
                name = elementSelector != null ? elementSelector.getName() : "";
                propertyName = "";
                numberOfItems = 1;
@@ -296,7 +230,97 @@ public class EditSelectorDialog extends Dialog {
                        }
                        
                        condition = elementSelector.getCondition();
+                       
+                       componentType = elementSelector.getSelector().componentType;
+               }
+               
+               readRegions(diagram);
+               readRoutes();
+               readComponentTypes();
+               updatePropertyList();
+       }
+
+       private void readComponentTypes() {
+               try {
+                       Simantics.getSession().syncRequest(new ReadRequest() {
+                               @Override
+                               public void run(ReadGraph graph) throws DatabaseException {
+                                       Layer0 L0 = Layer0.getInstance(graph);
+                                       List<Resource> types = findComponentTypes(graph);
+                                       
+                                       componentTypes = new ArrayList<>(types.size() + 1);
+                                       componentTypeNames = new ArrayList<>(types.size() + 1);
+                                       
+                                       componentTypes.add(null);
+                                       componentTypeNames.add("Any type");
+                                       componentTypes.addAll(types);
+                                       for (Resource t : types) {
+                                               componentTypeNames.add(graph.getValue2(t, L0.HasName));
+                                       }
+                               }
+                       });
+               } catch (DatabaseException e) {
+                       LOGGER.error("Failed to read district component types", e);
+               }
+       }
+
+       private void readRoutes() {
+               final Map<Resource, String> routes = new HashMap<>();
+               
+               try {
+                       Simantics.getSession().syncRequest(new ReadRequest() {
+                               @Override
+                               public void run(ReadGraph graph) throws DatabaseException {
+                                       Layer0 L0 = Layer0.getInstance(graph);
+                                       RouteResource ROUTE = RouteResource.getInstance(graph);
+                                       
+                                       Resource model = ActiveModels.getPossibleActiveModel(graph, Simantics.getProjectResource());
+                                       List<Resource> routeCollection = QueryIndexUtils.searchByType(graph, model, ROUTE.Route);
+                                       for (Resource r : routeCollection) {
+                                               String name = graph.getRelatedValue(r, L0.HasLabel);
+                                               routes.put(r, name);
+                                       }
+                               }
+                       });
+               } catch (DatabaseException e) {
+                       LOGGER.error("Failed to read routes in the model", e);
+               }
+               
+               routeNames = routes.values().toArray(new String[routes.size()]);
+               routeResources = routes.keySet().toArray(new Resource[routes.size()]);
+       }
+
+       private void readRegions(Resource diagram) {
+               final Map<Resource, String> regions = new HashMap<>();
+               
+               try {
+                       Simantics.getSession().syncRequest(new ReadRequest() {
+                               @Override
+                               public void run(ReadGraph graph) throws DatabaseException {
+                                       Layer0 L0 = Layer0.getInstance(graph);
+                                       ModelingResources MOD = ModelingResources.getInstance(graph);
+                                       DiagramRegionsResource DR = DiagramRegionsResource.getInstance(graph);
+                                       
+                                       // If a specific diagram is given, use that
+                                       Collection<Resource> ds = diagram != null ? Collections.singleton(diagram) : diagrams;
+                                       
+                                       for (Resource composite : ds) {
+                                               Resource diagram = graph.getSingleObject(composite, MOD.CompositeToDiagram);
+                                               for (Resource r : graph.getObjects(diagram, DR.hasRegion)) {
+                                                       if (!graph.isInstanceOf(r, DR.Region))
+                                                               continue;
+                                                       String name = graph.getRelatedValue(r, L0.HasLabel);
+                                                       regions.put(r, name);
+                                               }
+                                       }
+                               }
+                       });
+               } catch (DatabaseException e) {
+                       LOGGER.error("Failed to read regions in the model", e);
                }
+               
+               regionNames = regions.values().toArray(new String[regions.size()]);
+               regionResources = regions.keySet().toArray(new Resource[regions.size()]);
        }
        
        private void updateDialog() {
@@ -512,6 +536,19 @@ public class EditSelectorDialog extends Dialog {
                        componentTypeField.select(index >= 0 ? index : 0);
                }
                
+               // Update property selection controls when component type changes
+               componentTypeField.addSelectionListener(new SelectionAdapter() {
+                       @Override
+                       public void widgetSelected(SelectionEvent e) {
+                               int index = componentTypeField.getSelectionIndex();
+                               componentType = index >= 0 ? componentTypes.get(index) : null;
+                               updatePropertyList();
+                               propertyField.setItems(propertyLabels.toArray(new String[] {}));
+                               
+                               updateDialog();
+                       }
+               });
+               
                new Label(selectorComposite, SWT.NONE).setText("with");
                
                selectorField = new Combo(selectorComposite, SWT.BORDER | SWT.READ_ONLY);
@@ -581,6 +618,11 @@ public class EditSelectorDialog extends Dialog {
                                                diagramField.clearSelection();
                                }
                                diagramField.setEnabled(enabled);
+
+                               // Refresh list of regions for current diagram
+                               diagram = enabled ? (diagramIndex >= 0 ? diagrams.get(diagramIndex) : null) : null;
+                               readRegions(diagram);
+                               updateDialog();
                        }
                });
                
@@ -908,49 +950,56 @@ public class EditSelectorDialog extends Dialog {
                return QueryIndexUtils.searchByType(graph, model, DN.Mapping_Base);
        }
        
-       void updatePropertyList() throws DatabaseException {
+       void updatePropertyList() {
+               propertyNames = new ArrayList<>();
+               propertyLabels = new ArrayList<>();
+               
                Collection<Resource> types = componentType != null ? Collections.singleton(componentType) : componentTypes;
                Set<Pair<String, String>> properties = new HashSet<>();
                
-               Simantics.getSession().syncRequest(new ReadRequest() {
-                       @Override
-                       public void run(ReadGraph graph) throws DatabaseException {
-                               Layer0 L0 = Layer0.getInstance(graph);
-                               
-                               for (Resource type : types) {
-                                       if (type == null)
-                                               continue;
-                                       
-                                       Resource ct = graph.getPossibleObject(type, DistrictNetworkResource.getInstance(graph).Mapping_ComponentType);
-                                       if (ct == null)
-                                               continue;
-                                       
-                                       if (graph.isInstanceOf(ct, L0.String)) {
-                                               Resource indexRoot = graph.syncRequest(new IndexRoot(type));
-                                               String name = graph.getValue(ct);
-                                               ct = GraphUtils.getPossibleChild(graph, indexRoot, name);
-                                               if (ct == null)
-                                                       continue;
-                                       }
+               try {
+                       Simantics.getSession().syncRequest(new ReadRequest() {
+                               @Override
+                               public void run(ReadGraph graph) throws DatabaseException {
+                                       Layer0 L0 = Layer0.getInstance(graph);
                                        
-                                       for (Resource prop : graph.getObjects(ct, L0.DomainOf)) {
-                                               if (!graph.isInstanceOf(prop, StructuralResource2.getInstance(graph).Property))
+                                       for (Resource type : types) {
+                                               if (type == null)
                                                        continue;
                                                
-                                               // Filter only numeric properties
-                                               PropertyInfo info = graph.syncRequest(new PropertyInfoRequest(prop));
-                                               if (info != null && info.requiredValueType != null && !isNumericValueType(info.requiredValueType))
+                                               Resource ct = graph.getPossibleObject(type, DistrictNetworkResource.getInstance(graph).Mapping_ComponentType);
+                                               if (ct == null)
                                                        continue;
                                                
-                                               String name = graph.getRelatedValue2(prop, L0.HasName);
-                                               String label = graph.getPossibleRelatedValue2(prop, L0.HasLabel);
-                                               if (label == null) label = name;
+                                               if (graph.isInstanceOf(ct, L0.String)) {
+                                                       Resource indexRoot = graph.syncRequest(new IndexRoot(type));
+                                                       String name = graph.getValue(ct);
+                                                       ct = GraphUtils.getPossibleChild(graph, indexRoot, name);
+                                                       if (ct == null)
+                                                               continue;
+                                               }
                                                
-                                               properties.add(Pair.make(label, name));
+                                               for (Resource prop : graph.getObjects(ct, L0.DomainOf)) {
+                                                       if (!graph.isInstanceOf(prop, StructuralResource2.getInstance(graph).Property))
+                                                               continue;
+                                                       
+                                                       // Filter only numeric properties
+                                                       PropertyInfo info = graph.syncRequest(new PropertyInfoRequest(prop));
+                                                       if (info != null && info.requiredValueType != null && !isNumericValueType(info.requiredValueType))
+                                                               continue;
+                                                       
+                                                       String name = graph.getRelatedValue2(prop, L0.HasName);
+                                                       String label = graph.getPossibleRelatedValue2(prop, L0.HasLabel);
+                                                       if (label == null) label = name;
+                                                       
+                                                       properties.add(Pair.make(label, name));
+                                               }
                                        }
                                }
-                       }
-               });
+                       });
+               } catch (DatabaseException e) {
+                       LOGGER.error("Failed to read district component properties", e);
+               }
                
                propertyNames.clear();
                propertyLabels.clear();
index 4143248214b97786661ae6465bf160dafec42a77..f015bddf0f2ccc0814788c956d59ff05064f90dd 100644 (file)
@@ -170,20 +170,26 @@ public class ElementSelector {
                        return Simantics.getSession().syncRequest(new Read<Map<Resource, String>>() {
                                @Override
                                public Map<Resource, String> perform(ReadGraph graph) throws DatabaseException {
+                                       Layer0 L0 = Layer0.getInstance(graph);
+                                       StructuralResource2 STR = StructuralResource2.getInstance(graph);
+                                       DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
+                                       ModelingResources MOD = ModelingResources.getInstance(graph);
+                                       
                                        Map<Resource, String> result = new HashMap<>();
                                        Resource model = graph.syncRequest(new PossibleActiveModel(Simantics.getProjectResource()));
-                                       List<Resource> composites = QueryIndexUtils.searchByType(graph, model, StructuralResource2.getInstance(graph).Composite);
+                                       List<Resource> composites = QueryIndexUtils.searchByType(graph, model, STR.Composite);
                                        for (Resource r : composites) {
                                                // Get diagram
-                                               Resource diagram = graph.getPossibleObject(r, ModelingResources.getInstance(graph).CompositeToDiagram);
-                                               if (diagram == null) continue;
+                                               Resource diagram = graph.getPossibleObject(r, MOD.CompositeToDiagram);
+                                               if (diagram == null || !graph.isInstanceOf(diagram, DN.Diagram))
+                                                       continue;
                                                
                                                // Filter out user component diagrams
-                                               Resource parent = graph.getPossibleObject(r, Layer0.getInstance(graph).PartOf);
-                                               if (parent == null || graph.isInheritedFrom(parent, StructuralResource2.getInstance(graph).Component))
+                                               Resource parent = graph.getPossibleObject(r, L0.PartOf);
+                                               if (parent == null || graph.isInheritedFrom(parent, STR.Component))
                                                        continue;
                                                
-                                               result.put(r, graph.getRelatedValue(r, Layer0.getInstance(graph).HasName));
+                                               result.put(r, graph.getRelatedValue(r, L0.HasName));
                                        }
                                        
                                        return result;