]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
00a3dcb5ff87d2240f7211993b6f8c0b798bc00e
[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.browser.nodes;\r
13 \r
14 import org.simantics.browsing.ui.common.node.AbstractNode;\r
15 import org.simantics.browsing.ui.common.node.DeleteException;\r
16 import org.simantics.browsing.ui.common.node.IDeletableNode;\r
17 import org.simantics.browsing.ui.common.node.IDropTargetNode;\r
18 import org.simantics.browsing.ui.common.node.IModifiableNode;\r
19 import org.simantics.browsing.ui.content.Labeler.Modifier;\r
20 import org.simantics.browsing.ui.graph.impl.LabelModifier;\r
21 import org.simantics.db.Resource;\r
22 import org.simantics.db.WriteGraph;\r
23 import org.simantics.db.common.request.WriteRequest;\r
24 import org.simantics.db.exception.DatabaseException;\r
25 import org.simantics.db.layer0.util.RemoverUtil;\r
26 import org.simantics.layer0.Layer0;\r
27 import org.simantics.sysdyn.SysdynResource;\r
28 import org.simantics.sysdyn.ui.utils.FunctionLibraryNameValidator;\r
29 import org.simantics.ui.SimanticsUI;\r
30 import org.simantics.ui.utils.ResourceAdaptionUtils;\r
31 \r
32 public class FunctionLibraryNode<T> extends AbstractNode<Resource> implements IDeletableNode, IModifiableNode, IDropTargetNode {\r
33 \r
34         \r
35     public FunctionLibraryNode(Resource resource) {\r
36         super(resource);\r
37     }\r
38 \r
39         @Override\r
40         public Modifier getModifier(String columnId) {\r
41                 try {\r
42                         Resource hasName = Layer0.getInstance(SimanticsUI.getSession()).HasName;\r
43                         LabelModifier modifier = new LabelModifier(SimanticsUI.getSession(), data, hasName) {\r
44                                 @Override\r
45                     public String isValid(String label) {\r
46                         if (!new FunctionLibraryNameValidator().isValid(data, label))\r
47                             return "Not valid";\r
48                         else\r
49                             return null;\r
50                     }\r
51                         };\r
52                         return modifier;\r
53                 } catch (DatabaseException e) {\r
54                         e.printStackTrace();\r
55                 }\r
56                 return null;\r
57         }\r
58 \r
59         @Override\r
60         public void delete() throws DeleteException {\r
61             SimanticsUI.getSession().asyncRequest(new WriteRequest() {\r
62 \r
63                 @Override\r
64                 public void perform(WriteGraph graph) throws DatabaseException {\r
65                     RemoverUtil.remove(graph, data);\r
66                 }\r
67             });\r
68         }\r
69 \r
70         @Override\r
71         public void drop(Object data) {\r
72                 final Resource[] resources = ResourceAdaptionUtils.toResources(data);\r
73                 final Resource library = this.data;\r
74                 if(resources.length > 0) {\r
75                         SimanticsUI.getSession().asyncRequest(new WriteRequest() {\r
76                                 \r
77                                 @Override\r
78                                 public void perform(WriteGraph graph) throws DatabaseException {\r
79                                         SysdynResource sr = SysdynResource.getInstance(graph);\r
80                                         Layer0 l0 = Layer0.getInstance(graph);\r
81                                         for(Resource tobeMoved : resources) {\r
82                                                 if(graph.isInstanceOf(tobeMoved, sr.SysdynModelicaFunction) ||\r
83                                                                 graph.isInstanceOf(tobeMoved, sr.SysdynModelicaFunctionLibrary)) {\r
84                                                         graph.deny(tobeMoved, l0.PartOf);\r
85                                                         graph.claim(tobeMoved, l0.PartOf, library);\r
86                                                 }\r
87                                         }\r
88                                 }\r
89                         });\r
90                 }\r
91         }\r
92         \r
93 }\r