]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.diagram.profile/src/org/simantics/diagram/profile/view/CreateEntryAction.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.diagram.profile / src / org / simantics / diagram / profile / view / CreateEntryAction.java
diff --git a/bundles/org.simantics.diagram.profile/src/org/simantics/diagram/profile/view/CreateEntryAction.java b/bundles/org.simantics.diagram.profile/src/org/simantics/diagram/profile/view/CreateEntryAction.java
new file mode 100644 (file)
index 0000000..acdb9b2
--- /dev/null
@@ -0,0 +1,66 @@
+package org.simantics.diagram.profile.view;\r
+\r
+import org.eclipse.jface.viewers.ISelection;\r
+import org.simantics.Simantics;\r
+import org.simantics.browsing.ui.swt.ModelledActionImpl;\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.common.utils.NameUtils;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.layer0.SelectionHints;\r
+import org.simantics.diagram.stubs.DiagramResource;\r
+import org.simantics.layer0.Layer0;\r
+import org.simantics.utils.ui.ISelectionUtils;\r
+\r
+public class CreateEntryAction extends ModelledActionImpl<Resource> {\r
+\r
+       public CreateEntryAction(Resource configuration) {\r
+               super(configuration);\r
+       }\r
+       \r
+       public void run(final Resource runtimeDiagram) {\r
+\r
+               ISelection groupSelection = getParameter(DiagramResource.URIs.ProfilesView_NewEntryGroup);\r
+        final Resource selectedGroup = ISelectionUtils.getSinglePossibleKey(groupSelection, SelectionHints.KEY_MAIN, Resource.class);\r
+        if(selectedGroup == null) return;\r
+\r
+               ISelection styleSelection = getParameter(DiagramResource.URIs.ProfilesView_NewEntryStyle);\r
+        final Resource selectedStyle = ISelectionUtils.getSinglePossibleKey(styleSelection, SelectionHints.KEY_MAIN, Resource.class);\r
+        if(selectedStyle == null) return;\r
+\r
+        try {\r
+               \r
+                       Simantics.getSession().syncRequest(new WriteRequest() {\r
+                               \r
+                               @Override\r
+                               public void perform(WriteGraph graph) throws DatabaseException {\r
+       \r
+                                       Layer0 L0 = Layer0.getInstance(graph);\r
+                                       DiagramResource DIA = DiagramResource.getInstance(graph);\r
+                               String modelURI = graph.getPossibleRelatedValue((Resource)runtimeDiagram, DIA.RuntimeDiagram_HasModelURI);\r
+                               if (modelURI == null) return;\r
+                               \r
+                               Resource model = graph.getPossibleResource(modelURI);\r
+                               if(model == null) return;\r
+                               \r
+                               String name = NameUtils.findFreshEscapedName(graph, "Entry", model, L0.ConsistsOf);\r
+                               \r
+                               Resource newGroup = graph.newResource();\r
+                               graph.claim(newGroup, L0.InstanceOf, DIA.GroupStyleProfileEntry);\r
+                               graph.claimLiteral(newGroup, L0.HasName, name);\r
+                               graph.claim(newGroup, DIA.ProfileEntry_HasGroup, selectedGroup);\r
+                               graph.claim(newGroup, DIA.ProfileEntry_HasStyle, selectedStyle);\r
+                               graph.claim(model, L0.ConsistsOf, newGroup);\r
+       \r
+                               }\r
+       \r
+                       });\r
+               \r
+        } catch (DatabaseException e) {\r
+               e.printStackTrace();\r
+        }\r
+\r
+       }\r
+\r
+}\r