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