]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
1064e0190807beec3eaa5a355c0a12a07015b711
[simantics/sysdyn.git] /
1 package org.simantics.sysdyn.ui.browser.actions;\r
2 \r
3 import org.simantics.db.Resource;\r
4 import org.simantics.db.WriteGraph;\r
5 import org.simantics.db.common.request.WriteRequest;\r
6 import org.simantics.db.common.utils.NameUtils;\r
7 import org.simantics.db.exception.DatabaseException;\r
8 import org.simantics.db.exception.ResourceNotFoundException;\r
9 import org.simantics.db.layer0.adapter.ActionFactory;\r
10 import org.simantics.layer0.Layer0;\r
11 import org.simantics.layer0.utils.direct.GraphUtils;\r
12 import org.simantics.sysdyn.SysdynResource;\r
13 import org.simantics.sysdyn.manager.FunctionUtils;\r
14 import org.simantics.ui.SimanticsUI;\r
15 \r
16 public class NewFunctionLibraryAction implements ActionFactory{\r
17 \r
18     @Override\r
19     public Runnable create(Object target) {\r
20         if(!(target instanceof Resource))\r
21             return null;\r
22         final Resource resource = (Resource)target;\r
23 \r
24         return new Runnable() {\r
25             @Override\r
26             public void run() {\r
27                 createLibrary(resource, false);\r
28             }\r
29         };\r
30     }\r
31     \r
32     /**\r
33      * Create a new Library to the selected root or to SharedOntologies\r
34      * \r
35      * @param libraryLocation Resource of the model or other \r
36      * library where the new library will be added.\r
37      * @param shared is the library a shared library\r
38      */\r
39     protected static void createLibrary(final Resource libraryLocation, final boolean shared) {\r
40         SimanticsUI.getSession().asyncRequest(new WriteRequest() {\r
41 \r
42             @Override\r
43             public void perform(WriteGraph g) throws DatabaseException {\r
44                 Layer0 l0 = Layer0.getInstance(g);\r
45                 SysdynResource sr = SysdynResource.getInstance(g);\r
46 \r
47                 if(!(g.isInstanceOf(libraryLocation, sr.SysdynModel) ||\r
48                         g.isInstanceOf(libraryLocation, sr.SysdynModelicaFunctionLibrary) ||\r
49                                 g.isInstanceOf(libraryLocation, sr.SharedFunctionOntology)))\r
50                     return;\r
51 \r
52                 Resource root = libraryLocation;\r
53 \r
54                 String name = "FunctionLibrary";\r
55                 Resource libraryType = sr.SysdynModelicaFunctionLibrary;\r
56                 \r
57                 if(shared) {\r
58                     \r
59                     try {\r
60                         root = g.getResource("http://SharedOntologies");\r
61                     } catch (ResourceNotFoundException e) {\r
62                         root = g.getResource("http:/");\r
63                         root = GraphUtils.create2(g, l0.Library, \r
64                                 l0.HasName, "SharedOntologies",\r
65                                 l0.PartOf, root);\r
66                     }\r
67                     \r
68                     name = "Shared" + name;\r
69                     libraryType = sr.SharedFunctionOntology;\r
70                 }\r
71 \r
72                 name = NameUtils.findFreshName(g, name, root, l0.ConsistsOf, "%s%d");\r
73                 \r
74                 Resource functionLibrary = GraphUtils.create2(g, libraryType,\r
75                         l0.HasName, name,\r
76                         l0.HasDescription, "",\r
77                         l0.PartOf, root);\r
78                 \r
79                 if(shared)\r
80                     g.claim(libraryLocation, l0.IsLinkedTo, functionLibrary);\r
81 \r
82                 FunctionUtils.updateFunctionFileForLibrary(g, functionLibrary);\r
83             }\r
84         });\r
85     }\r
86 }\r