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