]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.diagram.profile/src/org/simantics/diagram/profile/view/ProfileEntrySelectionListener.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.diagram.profile / src / org / simantics / diagram / profile / view / ProfileEntrySelectionListener.java
diff --git a/bundles/org.simantics.diagram.profile/src/org/simantics/diagram/profile/view/ProfileEntrySelectionListener.java b/bundles/org.simantics.diagram.profile/src/org/simantics/diagram/profile/view/ProfileEntrySelectionListener.java
new file mode 100644 (file)
index 0000000..789a18e
--- /dev/null
@@ -0,0 +1,78 @@
+package org.simantics.diagram.profile.view;\r
+\r
+import org.eclipse.swt.SWT;\r
+import org.eclipse.swt.widgets.Event;\r
+import org.eclipse.swt.widgets.Listener;\r
+import org.eclipse.swt.widgets.Tree;\r
+import org.eclipse.swt.widgets.TreeItem;\r
+import org.simantics.Simantics;\r
+import org.simantics.browsing.ui.BuiltinKeys;\r
+import org.simantics.browsing.ui.GraphExplorer;\r
+import org.simantics.browsing.ui.NodeContext;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.WriteGraph;\r
+import org.simantics.db.common.request.WriteRequest;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.service.VirtualGraphSupport;\r
+import org.simantics.diagram.stubs.DiagramResource;\r
+import org.simantics.scenegraph.profile.ProfileUtils;\r
+import org.simantics.simulation.ontology.SimulationResource;\r
+\r
+public class ProfileEntrySelectionListener implements Listener {\r
+\r
+       public void processRecursively(WriteGraph graph, Resource runtimeProfile, Resource entry, boolean checked) throws DatabaseException {\r
+\r
+       DiagramResource DIA = DiagramResource.getInstance(graph);\r
+               \r
+       if(graph.isInstanceOf(entry, DIA.Profile)) {\r
+               \r
+               for(Resource child : ProfileUtils.getProfileChildren(graph, entry)) {\r
+                       processRecursively(graph, runtimeProfile, child, checked);\r
+               }\r
+               \r
+       } else if(graph.isInstanceOf(entry, DIA.ProfileEntry)) {\r
+\r
+            if(checked) {\r
+                graph.claim(runtimeProfile, SimulationResource.getInstance(graph).IsActive, null, entry);\r
+            } else {\r
+                graph.denyStatement(runtimeProfile, SimulationResource.getInstance(graph).IsActive, entry);\r
+            }\r
+\r
+               }\r
+               \r
+       }\r
+       \r
+    @Override\r
+    public void handleEvent (Event event) {\r
+       \r
+        if(event.detail == SWT.CHECK) {\r
+               \r
+            final TreeItem item = (TreeItem)event.item;\r
+            Tree tree = item.getParent();\r
+            GraphExplorer explorer = (GraphExplorer)tree.getData(GraphExplorer.KEY_GRAPH_EXPLORER);\r
+            final Resource runtimeDiagram = (Resource)explorer.getRoot().getConstant(BuiltinKeys.INPUT);\r
+            final boolean checked = item.getChecked();\r
+            NodeContext context = (NodeContext)item.getData();\r
+            final ResourcePair entry = (ResourcePair)context.getConstant(BuiltinKeys.INPUT);\r
+            try {\r
+               \r
+                VirtualGraphSupport support = Simantics.getSession().getService(VirtualGraphSupport.class);\r
+                Simantics.getSession().syncRequest(new WriteRequest(support.getWorkspacePersistent("profiles")) {\r
+\r
+                    @Override\r
+                    public void perform(WriteGraph graph) throws DatabaseException {\r
+                       \r
+                       DiagramResource DIA = DiagramResource.getInstance(graph);\r
+                       Resource runtimeProfile = graph.getPossibleObject(runtimeDiagram, DIA.RuntimeDiagram_HasRuntimeProfile);\r
+                       processRecursively(graph, runtimeProfile, entry.getSecond(), checked);\r
+                        \r
+                    }\r
+\r
+                });\r
+            } catch (DatabaseException e) {\r
+                e.printStackTrace();\r
+            }\r
+        }\r
+    }\r
+\r
+}\r