]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
38670b8c4cca7512f88967793b9f5033f920663d
[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.exception.ResourceNotFoundException;\r
20 import org.simantics.db.layer0.adapter.ActionFactory;\r
21 import org.simantics.layer0.Layer0;\r
22 import org.simantics.layer0.utils.direct.GraphUtils;\r
23 import org.simantics.sysdyn.SysdynResource;\r
24 import org.simantics.sysdyn.manager.FunctionUtils;\r
25 import org.simantics.ui.SimanticsUI;\r
26 \r
27 /**\r
28  * Creates a new function library\r
29  * @author Teemu Lempinen\r
30  *\r
31  */\r
32 public class NewFunctionLibraryAction implements ActionFactory{\r
33 \r
34     @Override\r
35     public Runnable create(Object target) {\r
36         if(!(target instanceof Resource))\r
37             return null;\r
38         final Resource resource = (Resource)target;\r
39 \r
40         return new Runnable() {\r
41             @Override\r
42             public void run() {\r
43                 createLibrary(resource, false);\r
44             }\r
45         };\r
46     }\r
47     \r
48     /**\r
49      * Create a new Library to the selected root or to SharedOntologies\r
50      * \r
51      * @param libraryLocation Resource of the model or other \r
52      * library where the new library will be added.\r
53      * @param shared is the library a shared library\r
54      */\r
55     protected static void createLibrary(final Resource libraryLocation, final boolean shared) {\r
56         SimanticsUI.getSession().asyncRequest(new WriteRequest() {\r
57 \r
58             @Override\r
59             public void perform(WriteGraph g) throws DatabaseException {\r
60                 Layer0 l0 = Layer0.getInstance(g);\r
61                 SysdynResource sr = SysdynResource.getInstance(g);\r
62 \r
63                 // Libraries can be created to model, function library and shared function library\r
64                 if(!(g.isInstanceOf(libraryLocation, sr.SysdynModel) ||\r
65                         g.isInstanceOf(libraryLocation, sr.SysdynModelicaFunctionLibrary) ||\r
66                                 g.isInstanceOf(libraryLocation, sr.SharedFunctionOntology)))\r
67                     return;\r
68 \r
69                 Resource root = libraryLocation;\r
70 \r
71                 String name = "FunctionLibrary";\r
72                 Resource libraryType = sr.SysdynModelicaFunctionLibrary;\r
73                 \r
74                 if(shared) {\r
75                     \r
76                     try {\r
77                         root = g.getResource("http://SharedOntologies");\r
78                     } catch (ResourceNotFoundException e) {\r
79                         root = g.getResource("http:/");\r
80                         root = GraphUtils.create2(g, l0.Library, \r
81                                 l0.HasName, "SharedOntologies",\r
82                                 l0.PartOf, root);\r
83                     }\r
84                     \r
85                     name = "Shared" + name;\r
86                     libraryType = sr.SharedFunctionOntology;\r
87                 }\r
88 \r
89                 name = NameUtils.findFreshName(g, name, root, l0.ConsistsOf, "%s%d");\r
90                 \r
91                 Resource functionLibrary = GraphUtils.create2(g, libraryType,\r
92                         l0.HasName, name,\r
93                         l0.HasDescription, "",\r
94                         l0.PartOf, root);\r
95                 \r
96                 if(shared)\r
97                     g.claim(libraryLocation, l0.IsLinkedTo, functionLibrary);\r
98 \r
99                 FunctionUtils.updateFunctionFileForLibrary(g, functionLibrary);\r
100             }\r
101         });\r
102     }\r
103 }\r