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.layer0.Layer0;
\r
26 import org.simantics.layer0.utils.direct.GraphUtils;
\r
27 import org.simantics.sysdyn.SysdynResource;
\r
28 import org.simantics.ui.SimanticsUI;
\r
29 import org.simantics.utils.ui.AdaptionUtils;
\r
32 * Creates a new function to a SysdynModel or Library.
\r
34 * @author Teemu Lempinen
\r
37 public class NewFunctionHandler extends AbstractHandler {
\r
40 * Assumes to be called from a node with SysdynModel, Library or SysdynModelicaFunction as its resource
\r
43 public Object execute(ExecutionEvent event) throws ExecutionException {
\r
45 ISelection sel = HandlerUtil.getCurrentSelection(event);
\r
47 @SuppressWarnings("unchecked")
\r
48 AbstractNode<Resource> node = AdaptionUtils.adaptToSingle(sel, AbstractNode.class);
\r
52 final Resource data = node.data;
\r
54 SimanticsUI.getSession().asyncRequest(new WriteRequest() {
\r
57 public void perform(WriteGraph g) throws DatabaseException {
\r
58 Layer0 l0 = Layer0.getInstance(g);
\r
59 SysdynResource sr = SysdynResource.getInstance(g);
\r
61 // Library can be either SysdynModel or L0.Library.
\r
62 Resource library = null;
\r
63 if(g.isInstanceOf(data, sr.SysdynModel) || g.isInstanceOf(data, l0.Library))
\r
65 else if (g.isInstanceOf(data, sr.SysdynModelicaFunction))
\r
66 library = g.getPossibleObject(data, l0.PartOf);
\r
72 String name = NameUtils.findFreshName(g, "Function", library, l0.ConsistsOf, "%s%d");
\r
74 GraphUtils.create2(g, sr.SysdynModelicaFunction,
\r
76 l0.HasDescription, "",
\r
77 sr.SysdynModelicaFunction_modelicaFunctionCode, "",
\r
78 l0.PartOf, library);
\r