]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
62d88a4ad474287d704bb3ab476d3f94830ff630
[simantics/sysdyn.git] /
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2011 Association for Decentralized Information Management in\r
3  * Industry THTH ry.\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
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.sysdyn.ui.handlers.newComponents;\r
13 \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
32 \r
33 /**\r
34  * Creates a new function library to a model or other library.\r
35  * \r
36  * @author Teemu Lempinen\r
37  *\r
38  */\r
39 public class NewFunctionLibraryHandler extends AbstractHandler {\r
40 \r
41         @Override\r
42         public Object execute(ExecutionEvent event) throws ExecutionException {\r
43 \r
44         ISelection sel = HandlerUtil.getCurrentSelection(event);\r
45         \r
46         @SuppressWarnings("unchecked")\r
47                 AbstractNode<Resource> node = AdaptionUtils.adaptToSingle(sel, AbstractNode.class);\r
48         if (node == null)\r
49             return null;\r
50 \r
51         createLibrary(node.data, false);\r
52         return null;\r
53         }\r
54         \r
55         /**\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
58          * \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
61          */\r
62         protected void createLibrary(final Resource model, final boolean shared) {\r
63         SimanticsUI.getSession().asyncRequest(new WriteRequest() {\r
64 \r
65             @Override\r
66             public void perform(WriteGraph g) throws DatabaseException {\r
67                 g.markUndoPoint();\r
68                 Layer0 l0 = Layer0.getInstance(g);\r
69                 SysdynResource sr = SysdynResource.getInstance(g);\r
70 \r
71                 if(!(g.isInstanceOf(model, sr.SysdynModel) ||\r
72                                 g.isInstanceOf(model, sr.SysdynModelicaFunctionLibrary) ||\r
73                                                 g.isInstanceOf(model, sr.SharedFunctionOntology)))\r
74                         return;\r
75 \r
76                 Resource root = model;\r
77 \r
78                 String name = "FunctionLibrary";\r
79                 Resource libraryType = sr.SysdynModelicaFunctionLibrary;\r
80                 \r
81                 if(shared) {\r
82                         \r
83                         try {\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
89                                                 l0.PartOf, root);\r
90                         }\r
91                         \r
92                         name = "Shared" + name;\r
93                     libraryType = sr.SharedFunctionOntology;\r
94                 }\r
95 \r
96                 name = NameUtils.findFreshName(g, name, root, l0.ConsistsOf, "%s%d");\r
97                 \r
98                 Resource functionLibrary = GraphUtils.create2(g, libraryType,\r
99                         l0.HasName, name,\r
100                         l0.HasDescription, "",\r
101                         l0.PartOf, root);\r
102                 \r
103                 if(shared)\r
104                         g.claim(model, l0.IsLinkedTo, functionLibrary);\r
105                 \r
106                 Layer0Utils.addCommentMetadata(g, "Created new Function Library " + name + " " + functionLibrary.toString());\r
107             }\r
108         });\r
109         }\r
110 }\r