]> gerrit.simantics Code Review - simantics/platform.git/blob - 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
1 /*******************************************************************************\r
2  * Copyright (c) 2012 Association for Decentralized Information Management in\r
3  * Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.modeling.ui.actions;\r
13 \r
14 import org.eclipse.core.runtime.IProgressMonitor;\r
15 import org.eclipse.core.runtime.IStatus;\r
16 import org.eclipse.core.runtime.Status;\r
17 import org.eclipse.core.runtime.jobs.Job;\r
18 import org.simantics.DatabaseJob;\r
19 import org.simantics.Simantics;\r
20 import org.simantics.databoard.Bindings;\r
21 import org.simantics.db.Resource;\r
22 import org.simantics.db.WriteGraph;\r
23 import org.simantics.db.common.request.PossibleIndexRoot;\r
24 import org.simantics.db.common.request.WriteRequest;\r
25 import org.simantics.db.common.utils.CommonDBUtils;\r
26 import org.simantics.db.common.utils.NameUtils;\r
27 import org.simantics.db.exception.DatabaseException;\r
28 import org.simantics.db.layer0.adapter.ActionFactory;\r
29 import org.simantics.db.layer0.util.Layer0Utils;\r
30 import org.simantics.diagram.stubs.DiagramResource;\r
31 import org.simantics.layer0.Layer0;\r
32 import org.simantics.modeling.ModelingResources;\r
33 import org.simantics.modeling.ModelingUtils;\r
34 import org.simantics.modeling.ui.Activator;\r
35 import org.simantics.operation.Layer0X;\r
36 import org.simantics.structural.stubs.StructuralResource2;\r
37 \r
38 public class NewProceduralComponentType implements ActionFactory {\r
39 \r
40     public static Resource create(WriteGraph graph, Resource library) throws DatabaseException {\r
41         ModelingResources MOD = ModelingResources.getInstance(graph);\r
42         Layer0 L0 = Layer0.getInstance(graph);\r
43         Layer0X L0X = Layer0X.getInstance(graph);\r
44         StructuralResource2 STR = StructuralResource2.getInstance(graph);\r
45         DiagramResource DIA = DiagramResource.getInstance(graph);\r
46 \r
47         // New component type\r
48         CommonDBUtils.selectClusterSet(graph, library);\r
49         Resource componentType = graph.newResource();\r
50         graph.newClusterSet(componentType);\r
51         CommonDBUtils.selectClusterSet(graph, componentType);\r
52         \r
53         graph.claim(componentType, L0.PartOf, library);\r
54         \r
55         Resource indexRoot = graph.sync(new PossibleIndexRoot(library));\r
56 \r
57         // Supertype\r
58         Resource supertype = graph.getSingleObject(indexRoot, MOD.StructuralModel_HasComponentTypeSupertype);\r
59         graph.claim(componentType, L0.Inherits, null, supertype);\r
60         \r
61         graph.claim(componentType, L0.InstanceOf, STR.ProceduralComponentType);\r
62 \r
63         // Name\r
64         String defaultName = graph.getRelatedValue(indexRoot, MOD.StructuralModel_HasDefaultComponentTypeName, Bindings.STRING);\r
65         String name = NameUtils.findFreshName(graph, defaultName, library);\r
66         graph.claimLiteral(componentType, L0.HasName, name + "@1");\r
67         graph.claimLiteral(componentType, L0X.HasGeneratedNamePrefix, "");\r
68 \r
69         // Substructure\r
70 //        Resource substructureType = graph.getSingleObject(indexRoot, MOD.StructuralModel_HasComponentTypeSubstructureType);\r
71 //        Resource composite = new InstantiateRequest(substructureType).perform(graph);\r
72 //        graph.claim(componentType, STR.IsDefinedBy, composite);\r
73         // These are added to give the configuration a proper URI.\r
74 //        graph.claimLiteral(composite, L0.HasName, "Configuration");\r
75 //        graph.claim(componentType, L0.ConsistsOf, composite);\r
76 \r
77         // Attach trigger to keep component type interface structures up-to-date\r
78 //        Resource diagram = graph.getPossibleObject(composite, MOD.CompositeToDiagram);\r
79 //        if (diagram != null) {\r
80 //            Resource componentTypeUpdater = graph.newResource();\r
81 //            graph.claim(componentTypeUpdater, L0.InstanceOf, null, MOD.ComponentTypeUpdater);\r
82 //            graph.claim(diagram, L0X.HasTrigger, componentTypeUpdater);\r
83 //        }\r
84 \r
85         graph.addLiteral(componentType, STR.ProceduralComponentType_code, STR.ProceduralComponentType_code_Inverse, \r
86                 STR.ProceduralComponentTypeCode, "[]", Bindings.STRING);\r
87         \r
88         Resource symbolDiagramType = graph.getPossibleObject(indexRoot, MOD.StructuralModel_HasSymbolDiagramType);\r
89         if(symbolDiagramType == null) symbolDiagramType = DIA.Composite;\r
90         \r
91         // Symbol\r
92         Resource symbol = new ModelingUtils(graph).createSymbol2("Symbol", symbolDiagramType);\r
93         graph.claim(componentType, MOD.ComponentTypeToSymbol, symbol);\r
94         graph.claim(componentType, L0.ConsistsOf, symbol);\r
95         \r
96 //        CommentMetadata cm = graph.getMetadata(CommentMetadata.class);\r
97 //        graph.addMetadata(cm.add("Created new user component " + name + ", resource " + componentType));\r
98         \r
99         return componentType;\r
100     }\r
101 \r
102     @Override\r
103     public Runnable create(Object target) {\r
104         if(!(target instanceof Resource))\r
105             return null;\r
106         final Resource library = (Resource) target;\r
107         return new Runnable() {\r
108             @Override\r
109             public void run() {\r
110                 Job job = new DatabaseJob("New User Component") {\r
111                     @Override\r
112                     protected IStatus run(IProgressMonitor monitor) {\r
113                         try {\r
114                             Simantics.getSession().syncRequest(new WriteRequest() {\r
115                                 @Override\r
116                                 public void perform(WriteGraph graph) throws DatabaseException {\r
117                                     graph.markUndoPoint();\r
118                                     Resource r = NewProceduralComponentType.create(graph, library);\r
119                                     Layer0Utils.addCommentMetadata(graph, "Created new Procedural Component Type " + graph.getPossibleRelatedValue2(r, Layer0.getInstance(graph).HasName, Bindings.STRING));\r
120                                 }\r
121                             });\r
122                             return Status.OK_STATUS;\r
123                         } catch (DatabaseException e) {\r
124                             return new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Failed to create new user component.", e); \r
125                         }\r
126                     }\r
127                 };\r
128                 job.schedule();\r
129             }\r
130         };\r
131     }\r
132 \r
133 }\r