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