]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
c4e0b3dc03fd8679206b58bb598fb4c790eadb9c
[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 org.simantics.db.Resource;\r
15 import org.simantics.db.WriteGraph;\r
16 import org.simantics.db.common.request.WriteRequest;\r
17 import org.simantics.db.common.utils.NameUtils;\r
18 import org.simantics.db.exception.DatabaseException;\r
19 import org.simantics.db.layer0.adapter.ActionFactory;\r
20 import org.simantics.layer0.Layer0;\r
21 import org.simantics.layer0.utils.direct.GraphUtils;\r
22 import org.simantics.sysdyn.SysdynResource;\r
23 import org.simantics.sysdyn.manager.FunctionUtils;\r
24 import org.simantics.ui.SimanticsUI;\r
25 \r
26 /**\r
27  * Creates a new modelica function to a model, function library or shared function library.\r
28  * @author Teemu Lempinen\r
29  *\r
30  */\r
31 public class NewFunctionAction implements ActionFactory{\r
32 \r
33     @Override\r
34     public Runnable create(Object target) {\r
35         if(!(target instanceof Resource))\r
36             return null;\r
37         final Resource resource = (Resource)target;\r
38 \r
39         return new Runnable() {\r
40             @Override\r
41             public void run() {\r
42 \r
43                 SimanticsUI.getSession().asyncRequest(new WriteRequest() {\r
44 \r
45                     @Override\r
46                     public void perform(WriteGraph g) throws DatabaseException {\r
47                         Layer0 l0 = Layer0.getInstance(g);\r
48                         SysdynResource sr = SysdynResource.getInstance(g);\r
49 \r
50                         Resource library = null;\r
51                         if(g.isInstanceOf(resource, sr.SysdynModel) || g.isInstanceOf(resource, l0.Library))\r
52                             library = resource;\r
53                         else if (g.isInstanceOf(resource, sr.SysdynModelicaFunction))\r
54                             library = g.getPossibleObject(resource, l0.PartOf);\r
55 \r
56                         if(library == null)\r
57                             return;\r
58 \r
59 \r
60                         String name = NameUtils.findFreshName(g, "Function", library, l0.ConsistsOf, "%s%d");\r
61 \r
62                         GraphUtils.create2(g, sr.SysdynModelicaFunction,\r
63                                 l0.HasName, name,\r
64                                 l0.HasDescription, "",\r
65                                 sr.SysdynModelicaFunction_modelicaFunctionCode, "",\r
66                                 l0.PartOf, library);\r
67 \r
68                          FunctionUtils.updateFunctionFileForLibrary(g, library);\r
69                     }\r
70                 });\r
71 \r
72             }\r
73         };\r
74     }\r
75 }\r