]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
81128020d790d378897b92cfc81c8a6ae49f0f55
[simantics/sysdyn.git] /
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2011 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.sysdyn.ui.handlers.newComponents;\r
13 \r
14 import java.util.ArrayList;\r
15 \r
16 import org.eclipse.core.commands.AbstractHandler;\r
17 import org.eclipse.core.commands.ExecutionEvent;\r
18 import org.eclipse.core.commands.ExecutionException;\r
19 import org.eclipse.jface.viewers.ISelection;\r
20 import org.eclipse.ui.handlers.HandlerUtil;\r
21 import org.simantics.databoard.Bindings;\r
22 import org.simantics.db.Resource;\r
23 import org.simantics.db.WriteGraph;\r
24 import org.simantics.db.common.request.WriteRequest;\r
25 import org.simantics.db.common.utils.ListUtils;\r
26 import org.simantics.db.common.utils.NameUtils;\r
27 import org.simantics.db.exception.DatabaseException;\r
28 import org.simantics.db.layer0.util.Layer0Utils;\r
29 import org.simantics.layer0.Layer0;\r
30 import org.simantics.layer0.utils.direct.GraphUtils;\r
31 import org.simantics.modeling.ModelingResources;\r
32 import org.simantics.structural.stubs.StructuralResource2;\r
33 import org.simantics.sysdyn.SysdynResource;\r
34 import org.simantics.ui.SimanticsUI;\r
35 import org.simantics.utils.ui.AdaptionUtils;\r
36 \r
37 /**\r
38  * Creates a new Enumeration node to a configuration or module\r
39  * \r
40  * @author Teemu Lempinen\r
41  *\r
42  */\r
43 public class NewEnumerationNodeHandler extends AbstractHandler {\r
44 \r
45         @Override\r
46         public Object execute(ExecutionEvent event) throws ExecutionException {\r
47 \r
48         ISelection sel = HandlerUtil.getCurrentSelection(event);\r
49         \r
50                 final Resource resource = AdaptionUtils.adaptToSingle(sel, Resource.class);\r
51         \r
52         SimanticsUI.getSession().asyncRequest(new WriteRequest() {\r
53 \r
54             @Override\r
55             public void perform(WriteGraph g) throws DatabaseException {\r
56                 g.markUndoPoint();\r
57                 SysdynResource sr = SysdynResource.getInstance(g);\r
58                 Layer0 l0 = Layer0.getInstance(g);\r
59                 \r
60                 // Find the actual configuration. Possible cases for resource are sr.Configuration, sr.ModuleSymbol or sr.Module.\r
61                 Resource configuration = null;\r
62                 if(g.isInstanceOf(resource, sr.Configuration)) {\r
63                         configuration = resource;\r
64                 } else if(g.isInheritedFrom(resource, sr.ModuleSymbol)) {\r
65                         Resource module = g.getPossibleObject(resource,ModelingResources.getInstance(g).SymbolToComponentType);\r
66                         configuration = g.getPossibleObject(module, StructuralResource2.getInstance(g).IsDefinedBy);\r
67                 } else {\r
68                         Resource instanceOf = g.getSingleObject(resource, l0.InstanceOf);\r
69                         if(g.isInheritedFrom(instanceOf, sr.Module)) {\r
70                                 configuration = g.getPossibleObject(instanceOf, StructuralResource2.getInstance(g).IsDefinedBy);\r
71                         } else {\r
72                                 return;\r
73                         }\r
74                 }\r
75 \r
76                 // Create the enumeartion\r
77                 \r
78                 String name = NameUtils.findFreshName(g, "Enum", configuration, l0.ConsistsOf, "%s%d");\r
79                 \r
80                 GraphUtils.create2(g, \r
81                                 sr.Enumeration,\r
82                                 l0.HasName, name,\r
83                                 sr.Enumeration_enumerationIndexList, ListUtils.create(g, new ArrayList<Resource>()),\r
84                                 l0.PartOf, configuration);\r
85                 Layer0Utils.addCommentMetadata(g, "Created new Enumeration " + name + " to " + g.getRelatedValue2(configuration, l0.HasLabel, Bindings.STRING));\r
86             }\r
87         });\r
88                 return null;\r
89         }\r
90 \r
91 }\r