]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
4ac2459dadb5e309de2a4bc187dd2c55c1cde685
[simantics/sysdyn.git] /
1 package org.simantics.sysdyn.ui.browser.actions;\r
2 \r
3 import org.simantics.db.Resource;\r
4 import org.simantics.db.WriteGraph;\r
5 import org.simantics.db.common.request.WriteRequest;\r
6 import org.simantics.db.common.utils.NameUtils;\r
7 import org.simantics.db.common.utils.OrderedSetUtils;\r
8 import org.simantics.db.exception.DatabaseException;\r
9 import org.simantics.db.layer0.adapter.ActionFactory;\r
10 import org.simantics.layer0.Layer0;\r
11 import org.simantics.layer0.utils.direct.GraphUtils;\r
12 import org.simantics.modeling.ModelingResources;\r
13 import org.simantics.structural.stubs.StructuralResource2;\r
14 import org.simantics.sysdyn.SysdynResource;\r
15 import org.simantics.ui.SimanticsUI;\r
16 \r
17 public class NewEnumerationAction implements ActionFactory{\r
18 \r
19     @Override\r
20     public Runnable create(Object target) {\r
21         if(!(target instanceof Resource))\r
22             return null;\r
23         final Resource resource = (Resource)target;\r
24 \r
25         return new Runnable() {\r
26             @Override\r
27             public void run() {\r
28 \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                         String name = NameUtils.findFreshName(g, "Enum", configuration, l0.ConsistsOf, "%s%d");\r
55 \r
56                         GraphUtils.create2(g, \r
57                                 sr.Enumeration,\r
58                                 l0.HasName, name,\r
59                                 sr.HasEnumerationIndexes, enumerationIndexes,\r
60                                 l0.PartOf, configuration);\r
61                     }\r
62                 });\r
63             }\r
64         };\r
65     }\r
66 \r
67 }\r