]> gerrit.simantics Code Review - simantics/district.git/blobdiff - org.simantics.district.selection.ui/src/org/simantics/district/selection/ui/parts/EditSelectorDialog.java
Selection queries from explicit sets of elements.
[simantics/district.git] / org.simantics.district.selection.ui / src / org / simantics / district / selection / ui / parts / EditSelectorDialog.java
index 42270da9253fee7d6726b2b8b16257fcffb8b437..b68c3fe11148504c43348c540b0b21ea51f2cb1b 100644 (file)
@@ -1,6 +1,7 @@
 package org.simantics.district.selection.ui.parts;
 
 import java.util.ArrayList;
+import java.util.Collection;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
@@ -38,8 +39,10 @@ import org.simantics.db.exception.DatabaseException;
 import org.simantics.db.exception.RuntimeDatabaseException;
 import org.simantics.db.layer0.QueryIndexUtils;
 import org.simantics.db.layer0.request.ActiveModels;
+import org.simantics.db.layer0.util.Layer0Utils;
 import org.simantics.db.request.Read;
 import org.simantics.db.request.WriteResult;
+import org.simantics.diagram.stubs.DiagramResource;
 import org.simantics.district.region.ontology.DiagramRegionsResource;
 import org.simantics.district.route.ontology.RouteResource;
 import org.simantics.district.selection.ElementSelectionResource;
@@ -69,6 +72,9 @@ public class EditSelectorDialog extends Dialog {
        private static Logger LOGGER = LoggerFactory.getLogger(EditSelectorDialog.class);
        
        private ElementSelector elementSelector;
+       
+       // Currently selected elements
+       Collection<Resource> currentSelection;
 
        // Data for comboboxes
        Map<Resource, String> diagrams;
@@ -106,7 +112,7 @@ public class EditSelectorDialog extends Dialog {
        private Composite content;
 
        @Inject
-       public EditSelectorDialog(Shell shell, ElementSelector elementSelector) {
+       public EditSelectorDialog(Shell shell, ElementSelector elementSelector, Collection<Resource> currentSelection) {
                super(shell);
                
                this.elementSelector = elementSelector;
@@ -124,6 +130,8 @@ public class EditSelectorDialog extends Dialog {
                        }
                }
                
+               this.currentSelection = currentSelection;
+               
                final Map<Resource, String> regions = new HashMap<>();
                final Map<Resource, String> routes = new HashMap<>();
                
@@ -229,8 +237,11 @@ public class EditSelectorDialog extends Dialog {
                        public void perform(WriteGraph graph) throws DatabaseException {
                                Layer0 L0 = Layer0.getInstance(graph);
                                ElementSelectionResource ES = ElementSelectionResource.getInstance(graph);
+                               ModelingResources MOD = ModelingResources.getInstance(graph);
+                               DiagramResource DIA = DiagramResource.getInstance(graph);
                                
                                graph.markUndoPoint();
+                               Layer0Utils.addCommentMetadata(graph, "Created new element selection");
                                
                                Resource lib = ElementSelectionUtils.ensureSelectionLibrary(graph);
                                
@@ -258,12 +269,26 @@ public class EditSelectorDialog extends Dialog {
                                        break;
                                case 1:
                                        generatorType = ES.Generator_Diagram;
-                                       Resource composite = graph.getPossibleObject(diagram, ModelingResources.getInstance(graph).DiagramToComposite);
+                                       Resource composite = graph.getPossibleObject(diagram, MOD.DiagramToComposite);
                                        graph.claim(generator, ES.Generator_HasDiagram, composite != null ? composite : diagram);
                                        break;
                                case 2:
                                        generatorType = ES.Generator_Explicit;
-                                       // TODO: Claim relations to current selection elements
+                                       for (Resource r : currentSelection) {
+                                               // No connections
+                                               if (graph.isInstanceOf(r, DIA.Connection))
+                                                       continue;
+                                               if (!graph.isInstanceOf(r, DIA.Element)) {
+                                                       if (!graph.hasStatement(r, MOD.ComponentToElement))
+                                                               continue;
+                                                       
+                                                       r = graph.getPossibleObject(r, MOD.ComponentToElement);
+                                                       if (r == null)
+                                                               continue;
+                                               }
+                                                       
+                                               graph.claim(generator, ES.Generator_HasSelectedElement, r);
+                                       }
                                        break;
                                default: throw new IllegalStateException("Invalid source index " + generatorIndex);
                                }