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