]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
978137a5a247f95d7a3f3368927f1276a6ceb59c
[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.exception.ResourceNotFoundException;\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.ui.utils.AdaptionUtils;\r
31 \r
32 /**\r
33  * Creates a new function library to a model or other library.\r
34  * \r
35  * @author Teemu Lempinen\r
36  *\r
37  */\r
38 public class NewFunctionLibraryHandler extends AbstractHandler {\r
39 \r
40         @Override\r
41         public Object execute(ExecutionEvent event) throws ExecutionException {\r
42 \r
43         ISelection sel = HandlerUtil.getCurrentSelection(event);\r
44         \r
45         @SuppressWarnings("unchecked")\r
46                 AbstractNode<Resource> node = AdaptionUtils.adaptToSingle(sel, AbstractNode.class);\r
47         if (node == null)\r
48             return null;\r
49 \r
50         createLibrary(node.data, false);\r
51         return null;\r
52         }\r
53         \r
54         /**\r
55          * Create function library. Shared libraries are created to root, local libraries to the model. \r
56          * Shared libraries can be used in other models in the project.\r
57          * \r
58          * @param model The initial location of the command\r
59          * @param shared Shared libraries are created to root, local libraries to the model.\r
60          */\r
61         protected void createLibrary(final Resource model, final boolean shared) {\r
62         SimanticsUI.getSession().asyncRequest(new WriteRequest() {\r
63 \r
64             @Override\r
65             public void perform(WriteGraph g) throws DatabaseException {\r
66                 Layer0 l0 = Layer0.getInstance(g);\r
67                 SysdynResource sr = SysdynResource.getInstance(g);\r
68 \r
69                 if(!(g.isInstanceOf(model, sr.SysdynModel) ||\r
70                                 g.isInstanceOf(model, sr.SysdynModelicaFunctionLibrary) ||\r
71                                                 g.isInstanceOf(model, sr.SharedFunctionOntology)))\r
72                         return;\r
73 \r
74                 Resource root = model;\r
75 \r
76                 String name = "FunctionLibrary";\r
77                 Resource libraryType = sr.SysdynModelicaFunctionLibrary;\r
78                 \r
79                 if(shared) {\r
80                         \r
81                         try {\r
82                                 root = g.getResource("http://SharedOntologies");\r
83                         } catch (ResourceNotFoundException e) {\r
84                                 root = g.getResource("http:/");\r
85                                 root = GraphUtils.create2(g, l0.Library, \r
86                                                 l0.HasName, "SharedOntologies",\r
87                                                 l0.PartOf, root);\r
88                         }\r
89                         \r
90                         name = "Shared" + name;\r
91                     libraryType = sr.SharedFunctionOntology;\r
92                 }\r
93 \r
94                 name = NameUtils.findFreshName(g, name, root, l0.ConsistsOf, "%s%d");\r
95                 \r
96                 Resource functionLibrary = GraphUtils.create2(g, libraryType,\r
97                         l0.HasName, name,\r
98                         l0.HasDescription, "",\r
99                         l0.PartOf, root);\r
100                 \r
101                 if(shared)\r
102                         g.claim(model, l0.IsLinkedTo, functionLibrary);\r
103             }\r
104         });\r
105         }\r
106 }\r