1 /*******************************************************************************
\r
2 * Copyright (c) 2007, 2012 Association for Decentralized Information Management in
\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
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.sysdyn.ui.browser.actions.newActions;
\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
28 * Creates a new function library
\r
29 * @author Teemu Lempinen
\r
32 public class NewFunctionLibraryAction implements ActionFactory{
\r
35 public Runnable create(Object target) {
\r
36 if(!(target instanceof Resource))
\r
38 final Resource resource = (Resource)target;
\r
40 return new Runnable() {
\r
43 createLibrary(resource, false);
\r
49 * Create a new Library to the selected root or to SharedOntologies
\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
55 protected static void createLibrary(final Resource libraryLocation, final boolean shared) {
\r
56 SimanticsUI.getSession().asyncRequest(new WriteRequest() {
\r
59 public void perform(WriteGraph g) throws DatabaseException {
\r
60 Layer0 l0 = Layer0.getInstance(g);
\r
61 SysdynResource sr = SysdynResource.getInstance(g);
\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
69 Resource root = libraryLocation;
\r
71 String name = "FunctionLibrary";
\r
72 Resource libraryType = sr.SysdynModelicaFunctionLibrary;
\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
85 name = "Shared" + name;
\r
86 libraryType = sr.SharedFunctionOntology;
\r
89 name = NameUtils.findFreshName(g, name, root, l0.ConsistsOf, "%s%d");
\r
91 Resource functionLibrary = GraphUtils.create2(g, libraryType,
\r
93 l0.HasDescription, "",
\r
97 g.claim(libraryLocation, l0.IsLinkedTo, functionLibrary);
\r
99 FunctionUtils.updateFunctionFileForLibrary(g, functionLibrary);
\r