1 /*******************************************************************************
2 * Copyright (c) 2012 Association for Decentralized Information Management in
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.modeling.ui.actions;
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;
38 public class NewProceduralComponentType implements ActionFactory {
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);
48 CommonDBUtils.selectClusterSet(graph, library);
49 Resource componentType = graph.newResource();
50 graph.newClusterSet(componentType);
51 CommonDBUtils.selectClusterSet(graph, componentType);
53 graph.claim(componentType, L0.PartOf, library);
55 Resource indexRoot = graph.sync(new PossibleIndexRoot(library));
58 Resource supertype = graph.getSingleObject(indexRoot, MOD.StructuralModel_HasComponentTypeSupertype);
59 graph.claim(componentType, L0.Inherits, null, supertype);
61 graph.claim(componentType, L0.InstanceOf, STR.ProceduralComponentType);
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");
67 graph.claimLiteral(componentType, L0X.HasGeneratedNamePrefix, "");
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);
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);
85 graph.addLiteral(componentType, STR.ProceduralComponentType_code, STR.ProceduralComponentType_code_Inverse,
86 STR.ProceduralComponentTypeCode, "[]", Bindings.STRING);
88 Resource symbolDiagramType = graph.getPossibleObject(indexRoot, MOD.StructuralModel_HasSymbolDiagramType);
89 if(symbolDiagramType == null) symbolDiagramType = DIA.Composite;
92 Resource symbol = new ModelingUtils(graph).createSymbol2("Symbol", symbolDiagramType);
93 graph.claim(componentType, MOD.ComponentTypeToSymbol, symbol);
94 graph.claim(componentType, L0.ConsistsOf, symbol);
96 // CommentMetadata cm = graph.getMetadata(CommentMetadata.class);
97 // graph.addMetadata(cm.add("Created new user component " + name + ", resource " + componentType));
103 public Runnable create(Object target) {
104 if(!(target instanceof Resource))
106 final Resource library = (Resource) target;
107 return new Runnable() {
110 Job job = new DatabaseJob("New User Component") {
112 protected IStatus run(IProgressMonitor monitor) {
114 Simantics.getSession().syncRequest(new WriteRequest() {
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));
122 return Status.OK_STATUS;
123 } catch (DatabaseException e) {
124 return new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Failed to create new user component.", e);