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.layer0.adapter.ActionFactory;
\r
20 import org.simantics.layer0.Layer0;
\r
21 import org.simantics.layer0.utils.direct.GraphUtils;
\r
22 import org.simantics.sysdyn.SysdynResource;
\r
23 import org.simantics.sysdyn.manager.FunctionUtils;
\r
24 import org.simantics.ui.SimanticsUI;
\r
27 * Creates a new modelica function to a model, function library or shared function library.
\r
28 * @author Teemu Lempinen
\r
31 public class NewFunctionAction implements ActionFactory{
\r
34 public Runnable create(Object target) {
\r
35 if(!(target instanceof Resource))
\r
37 final Resource resource = (Resource)target;
\r
39 return new Runnable() {
\r
43 SimanticsUI.getSession().asyncRequest(new WriteRequest() {
\r
46 public void perform(WriteGraph g) throws DatabaseException {
\r
47 Layer0 l0 = Layer0.getInstance(g);
\r
48 SysdynResource sr = SysdynResource.getInstance(g);
\r
50 Resource library = null;
\r
51 if(g.isInstanceOf(resource, sr.SysdynModel) || g.isInstanceOf(resource, l0.Library))
\r
53 else if (g.isInstanceOf(resource, sr.SysdynModelicaFunction))
\r
54 library = g.getPossibleObject(resource, l0.PartOf);
\r
60 String name = NameUtils.findFreshName(g, "Function", library, l0.ConsistsOf, "%s%d");
\r
62 GraphUtils.create2(g, sr.SysdynModelicaFunction,
\r
64 l0.HasDescription, "",
\r
65 sr.SysdynModelicaFunction_modelicaFunctionCode, "",
\r
66 l0.PartOf, library);
\r
68 FunctionUtils.updateFunctionFileForLibrary(g, library);
\r