1 /*******************************************************************************
\r
2 * Copyright (c) 2007, 2011 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.handlers.newComponents;
\r
14 import org.eclipse.core.commands.AbstractHandler;
\r
15 import org.eclipse.core.commands.ExecutionEvent;
\r
16 import org.eclipse.core.commands.ExecutionException;
\r
17 import org.eclipse.jface.viewers.ISelection;
\r
18 import org.eclipse.ui.handlers.HandlerUtil;
\r
19 import org.simantics.browsing.ui.common.node.AbstractNode;
\r
20 import org.simantics.db.Resource;
\r
21 import org.simantics.db.WriteGraph;
\r
22 import org.simantics.db.common.request.WriteRequest;
\r
23 import org.simantics.db.common.utils.NameUtils;
\r
24 import org.simantics.db.exception.DatabaseException;
\r
25 import org.simantics.db.exception.ResourceNotFoundException;
\r
26 import org.simantics.db.layer0.util.Layer0Utils;
\r
27 import org.simantics.layer0.Layer0;
\r
28 import org.simantics.layer0.utils.direct.GraphUtils;
\r
29 import org.simantics.sysdyn.SysdynResource;
\r
30 import org.simantics.ui.SimanticsUI;
\r
31 import org.simantics.utils.ui.AdaptionUtils;
\r
34 * Creates a new function library to a model or other library.
\r
36 * @author Teemu Lempinen
\r
39 public class NewFunctionLibraryHandler extends AbstractHandler {
\r
42 public Object execute(ExecutionEvent event) throws ExecutionException {
\r
44 ISelection sel = HandlerUtil.getCurrentSelection(event);
\r
46 @SuppressWarnings("unchecked")
\r
47 AbstractNode<Resource> node = AdaptionUtils.adaptToSingle(sel, AbstractNode.class);
\r
51 createLibrary(node.data, false);
\r
56 * Create function library. Shared libraries are created to root, local libraries to the model.
\r
57 * Shared libraries can be used in other models in the project.
\r
59 * @param model The initial location of the command
\r
60 * @param shared Shared libraries are created to root, local libraries to the model.
\r
62 protected void createLibrary(final Resource model, final boolean shared) {
\r
63 SimanticsUI.getSession().asyncRequest(new WriteRequest() {
\r
66 public void perform(WriteGraph g) throws DatabaseException {
\r
68 Layer0 l0 = Layer0.getInstance(g);
\r
69 SysdynResource sr = SysdynResource.getInstance(g);
\r
71 if(!(g.isInstanceOf(model, sr.SysdynModel) ||
\r
72 g.isInstanceOf(model, sr.SysdynModelicaFunctionLibrary) ||
\r
73 g.isInstanceOf(model, sr.SharedFunctionOntology)))
\r
76 Resource root = model;
\r
78 String name = "FunctionLibrary";
\r
79 Resource libraryType = sr.SysdynModelicaFunctionLibrary;
\r
84 root = g.getResource("http://SharedOntologies");
\r
85 } catch (ResourceNotFoundException e) {
\r
86 root = g.getResource("http:/");
\r
87 root = GraphUtils.create2(g, l0.Library,
\r
88 l0.HasName, "SharedOntologies",
\r
92 name = "Shared" + name;
\r
93 libraryType = sr.SharedFunctionOntology;
\r
96 name = NameUtils.findFreshName(g, name, root, l0.ConsistsOf, "%s%d");
\r
98 Resource functionLibrary = GraphUtils.create2(g, libraryType,
\r
100 l0.HasDescription, "",
\r
104 g.claim(model, l0.IsLinkedTo, functionLibrary);
\r
106 Layer0Utils.addCommentMetadata(g, "Created new Function Library " + name + " " + functionLibrary.toString());
\r