1 package org.simantics.sysdyn.ui.browser.actions;
\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
16 public class NewFunctionLibraryAction implements ActionFactory{
\r
19 public Runnable create(Object target) {
\r
20 if(!(target instanceof Resource))
\r
22 final Resource resource = (Resource)target;
\r
24 return new Runnable() {
\r
27 createLibrary(resource, false);
\r
33 * Create a new Library to the selected root or to SharedOntologies
\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
39 protected static void createLibrary(final Resource libraryLocation, final boolean shared) {
\r
40 SimanticsUI.getSession().asyncRequest(new WriteRequest() {
\r
43 public void perform(WriteGraph g) throws DatabaseException {
\r
44 Layer0 l0 = Layer0.getInstance(g);
\r
45 SysdynResource sr = SysdynResource.getInstance(g);
\r
47 if(!(g.isInstanceOf(libraryLocation, sr.SysdynModel) ||
\r
48 g.isInstanceOf(libraryLocation, sr.SysdynModelicaFunctionLibrary) ||
\r
49 g.isInstanceOf(libraryLocation, sr.SharedFunctionOntology)))
\r
52 Resource root = libraryLocation;
\r
54 String name = "FunctionLibrary";
\r
55 Resource libraryType = sr.SysdynModelicaFunctionLibrary;
\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
68 name = "Shared" + name;
\r
69 libraryType = sr.SharedFunctionOntology;
\r
72 name = NameUtils.findFreshName(g, name, root, l0.ConsistsOf, "%s%d");
\r
74 Resource functionLibrary = GraphUtils.create2(g, libraryType,
\r
76 l0.HasDescription, "",
\r
80 g.claim(libraryLocation, l0.IsLinkedTo, functionLibrary);
\r
82 FunctionUtils.updateFunctionFileForLibrary(g, functionLibrary);
\r