]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
67ddc56b98cc323b578d72acef3e50a98d02d5b9
[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 org.eclipse.core.commands.AbstractHandler;\r
15 import org.eclipse.core.commands.ExecutionEvent;\r
16 import org.eclipse.core.commands.ExecutionException;\r
17 import org.eclipse.jface.viewers.ISelection;\r
18 import org.eclipse.ui.handlers.HandlerUtil;\r
19 import org.simantics.browsing.ui.common.node.AbstractNode;\r
20 import org.simantics.db.Resource;\r
21 import org.simantics.db.WriteGraph;\r
22 import org.simantics.db.common.request.WriteRequest;\r
23 import org.simantics.db.common.utils.NameUtils;\r
24 import org.simantics.db.exception.DatabaseException;\r
25 import org.simantics.layer0.Layer0;\r
26 import org.simantics.layer0.utils.direct.GraphUtils;\r
27 import org.simantics.sysdyn.SysdynResource;\r
28 import org.simantics.ui.SimanticsUI;\r
29 import org.simantics.ui.utils.AdaptionUtils;\r
30 \r
31 /**\r
32  * Creates a new function to a SysdynModel or Library.\r
33  * \r
34  * @author Teemu Lempinen\r
35  *\r
36  */\r
37 public class NewFunctionHandler extends AbstractHandler {\r
38 \r
39     /**\r
40      * Assumes to be called from a node with SysdynModel, Library or SysdynModelicaFunction as its resource\r
41      */\r
42         @Override\r
43         public Object execute(ExecutionEvent event) throws ExecutionException {\r
44 \r
45         ISelection sel = HandlerUtil.getCurrentSelection(event);\r
46         \r
47         @SuppressWarnings("unchecked")\r
48                 AbstractNode<Resource> node = AdaptionUtils.adaptToSingle(sel, AbstractNode.class);\r
49         if (node == null)\r
50             return null;\r
51 \r
52         final Resource data = node.data;\r
53 \r
54         SimanticsUI.getSession().asyncRequest(new WriteRequest() {\r
55 \r
56             @Override\r
57             public void perform(WriteGraph g) throws DatabaseException {\r
58                 Layer0 l0 = Layer0.getInstance(g);\r
59                 SysdynResource sr = SysdynResource.getInstance(g);\r
60 \r
61                 // Library can be either SysdynModel or L0.Library. \r
62                 Resource library = null;\r
63                 if(g.isInstanceOf(data, sr.SysdynModel) || g.isInstanceOf(data, l0.Library))\r
64                         library = data;\r
65                 else if (g.isInstanceOf(data, sr.SysdynModelicaFunction))\r
66                         library = g.getPossibleObject(data, l0.PartOf);\r
67                 \r
68                 if(library == null)\r
69                         return;\r
70                         \r
71                 \r
72                 String name = NameUtils.findFreshName(g, "Function", library, l0.ConsistsOf, "%s%d");\r
73 \r
74                 GraphUtils.create2(g, sr.SysdynModelicaFunction,\r
75                         l0.HasName, name,\r
76                         l0.HasDescription, "",\r
77                         sr.SysdynModelicaFunction_modelicaFunctionCode, "",\r
78                         l0.PartOf, library);\r
79             }\r
80         });\r
81         \r
82         return null;\r
83         }\r
84 \r
85 \r
86 }\r