]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
ffc74ba175a32e4cf0b82bab05287d0c13f6a8db
[simantics/sysdyn.git] /
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2012 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.browser.actions.newActions;\r
13 \r
14 import java.util.Collections;\r
15 \r
16 import org.simantics.db.Resource;\r
17 import org.simantics.db.WriteGraph;\r
18 import org.simantics.db.common.request.WriteRequest;\r
19 import org.simantics.db.common.utils.ListUtils;\r
20 import org.simantics.db.common.utils.NameUtils;\r
21 import org.simantics.db.exception.DatabaseException;\r
22 import org.simantics.db.layer0.adapter.ActionFactory;\r
23 import org.simantics.layer0.Layer0;\r
24 import org.simantics.layer0.utils.direct.GraphUtils;\r
25 import org.simantics.modeling.ModelingResources;\r
26 import org.simantics.structural.stubs.StructuralResource2;\r
27 import org.simantics.sysdyn.SysdynResource;\r
28 import org.simantics.ui.SimanticsUI;\r
29 \r
30 /**\r
31  * Creates a new enumeration to a model or module\r
32  * @author Teemu Lempinen\r
33  *\r
34  */\r
35 public class NewEnumerationAction implements ActionFactory{\r
36 \r
37     @Override\r
38     public Runnable create(Object target) {\r
39         if(!(target instanceof Resource))\r
40             return null;\r
41         final Resource resource = (Resource)target;\r
42 \r
43         return new Runnable() {\r
44             @Override\r
45             public void run() {\r
46 \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 configuration from...\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                             // Module symbol\r
61                             Resource module = g.getPossibleObject(resource,ModelingResources.getInstance(g).SymbolToComponentType);\r
62                             configuration = g.getPossibleObject(module, StructuralResource2.getInstance(g).IsDefinedBy);\r
63                         } else {\r
64                             // Module instance\r
65                             Resource instanceOf = g.getSingleObject(resource, l0.InstanceOf);\r
66                             if(g.isInheritedFrom(instanceOf, sr.Module)) {\r
67                                 configuration = g.getPossibleObject(instanceOf, StructuralResource2.getInstance(g).IsDefinedBy);\r
68                             } else {\r
69                                 return;\r
70                             }\r
71                         }\r
72 \r
73                         // Find unique name\r
74                         String name = NameUtils.findFreshName(g, "Enum", configuration, l0.ConsistsOf, "%s%d");\r
75                         \r
76                         // Create enumeration\r
77                         GraphUtils.create2(g, \r
78                                 sr.Enumeration,\r
79                                 l0.HasName, name,\r
80                                 sr.Enumeration_enumerationIndexList, ListUtils.create(g, Collections.<Resource>emptyList()),\r
81                                 l0.PartOf, configuration);\r
82                     }\r
83                 });\r
84             }\r
85         };\r
86     }\r
87 \r
88 }\r