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