]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
e048657cf762ae801d725503c2bce2807558cfcb
[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.db.layer0.util.Layer0Utils;\r
26 import org.simantics.layer0.Layer0;\r
27 import org.simantics.layer0.utils.direct.GraphUtils;\r
28 import org.simantics.sysdyn.SysdynResource;\r
29 import org.simantics.ui.SimanticsUI;\r
30 import org.simantics.utils.ui.AdaptionUtils;\r
31 \r
32 /**\r
33  * Creates a new function to a SysdynModel or Library.\r
34  * \r
35  * @author Teemu Lempinen\r
36  *\r
37  */\r
38 public class NewFunctionHandler extends AbstractHandler {\r
39 \r
40     /**\r
41      * Assumes to be called from a node with SysdynModel, Library or SysdynModelicaFunction as its resource\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         @SuppressWarnings("unchecked")\r
49                 AbstractNode<Resource> node = AdaptionUtils.adaptToSingle(sel, AbstractNode.class);\r
50         if (node == null)\r
51             return null;\r
52 \r
53         final Resource data = node.data;\r
54 \r
55         SimanticsUI.getSession().asyncRequest(new WriteRequest() {\r
56 \r
57             @Override\r
58             public void perform(WriteGraph g) throws DatabaseException {\r
59                 g.markUndoPoint();\r
60                 Layer0 l0 = Layer0.getInstance(g);\r
61                 SysdynResource sr = SysdynResource.getInstance(g);\r
62 \r
63                 // Library can be either SysdynModel or L0.Library. \r
64                 Resource library = null;\r
65                 if(g.isInstanceOf(data, sr.SysdynModel) || g.isInstanceOf(data, l0.Library))\r
66                         library = data;\r
67                 else if (g.isInstanceOf(data, sr.SysdynModelicaFunction))\r
68                         library = g.getPossibleObject(data, l0.PartOf);\r
69                 \r
70                 if(library == null)\r
71                         return;\r
72                         \r
73                 \r
74                 String name = NameUtils.findFreshName(g, "Function", library, l0.ConsistsOf, "%s%d");\r
75 \r
76                 Resource func = GraphUtils.create2(g, sr.SysdynModelicaFunction,\r
77                         l0.HasName, name,\r
78                         l0.HasDescription, "",\r
79                         sr.SysdynModelicaFunction_modelicaFunctionCode, "",\r
80                         l0.PartOf, library);\r
81                 \r
82                 Layer0Utils.addCommentMetadata(g, "Created new Function " + name + " " + func.toString());\r
83             }\r
84         });\r
85         \r
86         return null;\r
87         }\r
88 \r
89 \r
90 }\r