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