]> gerrit.simantics Code Review - simantics/district.git/blobdiff - org.simantics.district.selection/src/org/simantics/district/selection/ElementSelectionUtils.java
UI for diagram element selection
[simantics/district.git] / org.simantics.district.selection / src / org / simantics / district / selection / ElementSelectionUtils.java
diff --git a/org.simantics.district.selection/src/org/simantics/district/selection/ElementSelectionUtils.java b/org.simantics.district.selection/src/org/simantics/district/selection/ElementSelectionUtils.java
new file mode 100644 (file)
index 0000000..4075e9b
--- /dev/null
@@ -0,0 +1,43 @@
+package org.simantics.district.selection;
+
+import java.util.List;
+import java.util.UUID;
+
+import org.simantics.Simantics;
+import org.simantics.databoard.Bindings;
+import org.simantics.db.RequestProcessor;
+import org.simantics.db.Resource;
+import org.simantics.db.WriteGraph;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.layer0.QueryIndexUtils;
+import org.simantics.db.layer0.request.PossibleActiveModel;
+import org.simantics.db.request.WriteResult;
+import org.simantics.layer0.Layer0;
+
+public class ElementSelectionUtils {
+       public static Resource ensureSelectionLibrary(RequestProcessor graph) throws DatabaseException {
+               return graph.syncRequest(new WriteResult<Resource>() {
+                       @Override
+                       public Resource perform(WriteGraph graph) throws DatabaseException {
+                               ElementSelectionResource ES = ElementSelectionResource.getInstance(graph);
+                               
+                               Resource model = graph.sync(new PossibleActiveModel(Simantics.getProjectResource()));
+                               if (model == null) return null;
+                               
+                               List<Resource> libs = QueryIndexUtils.searchByTypeShallow(graph, model, ES.SelectionLibrary);
+                               if (libs.isEmpty()) {
+                                       Resource lib = graph.newResource();
+                                       Layer0 L0 = Layer0.getInstance(graph);
+                                       graph.claim(lib, L0.InstanceOf, ES.SelectionLibrary);
+                                       graph.claim(model, L0.ConsistsOf, lib);
+                                       graph.claimLiteral(lib, L0.HasName, L0.String, UUID.randomUUID().toString(), Bindings.STRING);
+                                       return lib;
+                               }
+                               else {
+                                       return libs.get(0);
+                               }
+                       }
+               });
+       }
+
+}