]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/actions/NewProceduralComponentType.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.modeling.ui / src / org / simantics / modeling / ui / actions / NewProceduralComponentType.java
diff --git a/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/actions/NewProceduralComponentType.java b/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/actions/NewProceduralComponentType.java
new file mode 100644 (file)
index 0000000..d7fcad4
--- /dev/null
@@ -0,0 +1,133 @@
+/*******************************************************************************\r
+ * Copyright (c) 2012 Association for Decentralized Information Management in\r
+ * Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ *     VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.modeling.ui.actions;\r
+\r
+import org.eclipse.core.runtime.IProgressMonitor;\r
+import org.eclipse.core.runtime.IStatus;\r
+import org.eclipse.core.runtime.Status;\r
+import org.eclipse.core.runtime.jobs.Job;\r
+import org.simantics.DatabaseJob;\r
+import org.simantics.Simantics;\r
+import org.simantics.databoard.Bindings;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.WriteGraph;\r
+import org.simantics.db.common.request.PossibleIndexRoot;\r
+import org.simantics.db.common.request.WriteRequest;\r
+import org.simantics.db.common.utils.CommonDBUtils;\r
+import org.simantics.db.common.utils.NameUtils;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.layer0.adapter.ActionFactory;\r
+import org.simantics.db.layer0.util.Layer0Utils;\r
+import org.simantics.diagram.stubs.DiagramResource;\r
+import org.simantics.layer0.Layer0;\r
+import org.simantics.modeling.ModelingResources;\r
+import org.simantics.modeling.ModelingUtils;\r
+import org.simantics.modeling.ui.Activator;\r
+import org.simantics.operation.Layer0X;\r
+import org.simantics.structural.stubs.StructuralResource2;\r
+\r
+public class NewProceduralComponentType implements ActionFactory {\r
+\r
+    public static Resource create(WriteGraph graph, Resource library) throws DatabaseException {\r
+        ModelingResources MOD = ModelingResources.getInstance(graph);\r
+        Layer0 L0 = Layer0.getInstance(graph);\r
+        Layer0X L0X = Layer0X.getInstance(graph);\r
+        StructuralResource2 STR = StructuralResource2.getInstance(graph);\r
+        DiagramResource DIA = DiagramResource.getInstance(graph);\r
+\r
+        // New component type\r
+        CommonDBUtils.selectClusterSet(graph, library);\r
+        Resource componentType = graph.newResource();\r
+        graph.newClusterSet(componentType);\r
+        CommonDBUtils.selectClusterSet(graph, componentType);\r
+        \r
+        graph.claim(componentType, L0.PartOf, library);\r
+        \r
+        Resource indexRoot = graph.sync(new PossibleIndexRoot(library));\r
+\r
+        // Supertype\r
+        Resource supertype = graph.getSingleObject(indexRoot, MOD.StructuralModel_HasComponentTypeSupertype);\r
+        graph.claim(componentType, L0.Inherits, null, supertype);\r
+        \r
+        graph.claim(componentType, L0.InstanceOf, STR.ProceduralComponentType);\r
+\r
+        // Name\r
+        String defaultName = graph.getRelatedValue(indexRoot, MOD.StructuralModel_HasDefaultComponentTypeName, Bindings.STRING);\r
+        String name = NameUtils.findFreshName(graph, defaultName, library);\r
+        graph.claimLiteral(componentType, L0.HasName, name + "@1");\r
+        graph.claimLiteral(componentType, L0X.HasGeneratedNamePrefix, "");\r
+\r
+        // Substructure\r
+//        Resource substructureType = graph.getSingleObject(indexRoot, MOD.StructuralModel_HasComponentTypeSubstructureType);\r
+//        Resource composite = new InstantiateRequest(substructureType).perform(graph);\r
+//        graph.claim(componentType, STR.IsDefinedBy, composite);\r
+        // These are added to give the configuration a proper URI.\r
+//        graph.claimLiteral(composite, L0.HasName, "Configuration");\r
+//        graph.claim(componentType, L0.ConsistsOf, composite);\r
+\r
+        // Attach trigger to keep component type interface structures up-to-date\r
+//        Resource diagram = graph.getPossibleObject(composite, MOD.CompositeToDiagram);\r
+//        if (diagram != null) {\r
+//            Resource componentTypeUpdater = graph.newResource();\r
+//            graph.claim(componentTypeUpdater, L0.InstanceOf, null, MOD.ComponentTypeUpdater);\r
+//            graph.claim(diagram, L0X.HasTrigger, componentTypeUpdater);\r
+//        }\r
+\r
+        graph.addLiteral(componentType, STR.ProceduralComponentType_code, STR.ProceduralComponentType_code_Inverse, \r
+                STR.ProceduralComponentTypeCode, "[]", Bindings.STRING);\r
+        \r
+        Resource symbolDiagramType = graph.getPossibleObject(indexRoot, MOD.StructuralModel_HasSymbolDiagramType);\r
+        if(symbolDiagramType == null) symbolDiagramType = DIA.Composite;\r
+        \r
+        // Symbol\r
+        Resource symbol = new ModelingUtils(graph).createSymbol2("Symbol", symbolDiagramType);\r
+        graph.claim(componentType, MOD.ComponentTypeToSymbol, symbol);\r
+        graph.claim(componentType, L0.ConsistsOf, symbol);\r
+        \r
+//        CommentMetadata cm = graph.getMetadata(CommentMetadata.class);\r
+//        graph.addMetadata(cm.add("Created new user component " + name + ", resource " + componentType));\r
+        \r
+        return componentType;\r
+    }\r
+\r
+    @Override\r
+    public Runnable create(Object target) {\r
+        if(!(target instanceof Resource))\r
+            return null;\r
+        final Resource library = (Resource) target;\r
+        return new Runnable() {\r
+            @Override\r
+            public void run() {\r
+                Job job = new DatabaseJob("New User Component") {\r
+                    @Override\r
+                    protected IStatus run(IProgressMonitor monitor) {\r
+                        try {\r
+                            Simantics.getSession().syncRequest(new WriteRequest() {\r
+                                @Override\r
+                                public void perform(WriteGraph graph) throws DatabaseException {\r
+                                    graph.markUndoPoint();\r
+                                    Resource r = NewProceduralComponentType.create(graph, library);\r
+                                    Layer0Utils.addCommentMetadata(graph, "Created new Procedural Component Type " + graph.getPossibleRelatedValue2(r, Layer0.getInstance(graph).HasName, Bindings.STRING));\r
+                                }\r
+                            });\r
+                            return Status.OK_STATUS;\r
+                        } catch (DatabaseException e) {\r
+                            return new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Failed to create new user component.", e); \r
+                        }\r
+                    }\r
+                };\r
+                job.schedule();\r
+            }\r
+        };\r
+    }\r
+\r
+}\r