]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
60a45356415bb1ad2c206ecdec11625cb90ead65
[simantics/sysdyn.git] /
1 package org.simantics.sysdyn.ui.handlers;\r
2 \r
3 import org.eclipse.core.commands.AbstractHandler;\r
4 import org.eclipse.core.commands.ExecutionEvent;\r
5 import org.eclipse.core.commands.ExecutionException;\r
6 import org.eclipse.jface.viewers.ISelection;\r
7 import org.eclipse.ui.handlers.HandlerUtil;\r
8 import org.simantics.db.Resource;\r
9 import org.simantics.db.WriteGraph;\r
10 import org.simantics.db.common.request.WriteRequest;\r
11 import org.simantics.db.common.utils.OrderedSetUtils;\r
12 import org.simantics.db.exception.DatabaseException;\r
13 import org.simantics.layer0.Layer0;\r
14 import org.simantics.layer0.utils.direct.GraphUtils;\r
15 import org.simantics.modeling.ModelingResources;\r
16 import org.simantics.structural.stubs.StructuralResource2;\r
17 import org.simantics.sysdyn.SysdynResource;\r
18 import org.simantics.ui.SimanticsUI;\r
19 import org.simantics.ui.utils.AdaptionUtils;\r
20 \r
21 public class NewEnumerationNodeHandler extends AbstractHandler {\r
22 \r
23         @Override\r
24         public Object execute(ExecutionEvent event) throws ExecutionException {\r
25 \r
26         ISelection sel = HandlerUtil.getCurrentSelection(event);\r
27         \r
28                 final Resource resource = AdaptionUtils.adaptToSingle(sel, Resource.class);\r
29         \r
30         SimanticsUI.getSession().asyncRequest(new WriteRequest() {\r
31 \r
32             @Override\r
33             public void perform(WriteGraph g) throws DatabaseException {\r
34                 SysdynResource sr = SysdynResource.getInstance(g);\r
35                 Layer0 l0 = Layer0.getInstance(g);\r
36                 \r
37                 Resource configuration = null;\r
38                 if(g.isInstanceOf(resource, sr.Configuration)) {\r
39                         configuration = resource;\r
40                 } else if(g.isInheritedFrom(resource, sr.ModuleSymbol)) {\r
41                         Resource module = g.getPossibleObject(resource,ModelingResources.getInstance(g).SymbolToComponentType);\r
42                         configuration = g.getPossibleObject(module, StructuralResource2.getInstance(g).IsDefinedBy);\r
43                 } else {\r
44                         Resource instanceOf = g.getSingleObject(resource, l0.InstanceOf);\r
45                         if(g.isInheritedFrom(instanceOf, sr.Module)) {\r
46                                 configuration = g.getPossibleObject(instanceOf, StructuralResource2.getInstance(g).IsDefinedBy);\r
47                         } else {\r
48                                 return;\r
49                         }\r
50                 }\r
51 \r
52                                 Resource enumerationIndexes = OrderedSetUtils.create(g, sr.EnumerationIndexes);\r
53                                 \r
54                                 Resource enumeration = GraphUtils.create2(g, \r
55                                                 sr.Enumeration,\r
56                                                 l0.HasName, "Enumeration",\r
57                                                 sr.HasEnumerationIndexes, enumerationIndexes);\r
58                 \r
59                 g.claim(configuration, l0.ConsistsOf, enumeration);\r
60                 \r
61             }\r
62         });\r
63                 return null;\r
64         }\r
65 \r
66 }\r