]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
da452b2f3d95db93ceb5a3e71aa70c1be8ca8b47
[simantics/sysdyn.git] /
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2012 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.actions.drop;\r
13 \r
14 import org.simantics.db.ReadGraph;\r
15 import org.simantics.db.Resource;\r
16 import org.simantics.db.WriteGraph;\r
17 import org.simantics.db.common.request.WriteRequest;\r
18 import org.simantics.db.exception.DatabaseException;\r
19 import org.simantics.db.layer0.adapter.DropActionFactory;\r
20 import org.simantics.layer0.Layer0;\r
21 import org.simantics.sysdyn.SysdynResource;\r
22 import org.simantics.ui.SimanticsUI;\r
23 import org.simantics.ui.utils.AdaptionUtils;\r
24 \r
25 /**\r
26  * Action for moving functions and function libraries in model browser \r
27  * @author Teemu Lempinen\r
28  *\r
29  */\r
30 public class FunctionDropAction implements DropActionFactory {\r
31 \r
32     @Override\r
33     public Runnable create(ReadGraph g, Object target, Object source) throws DatabaseException {\r
34         Resource t = AdaptionUtils.adaptToSingle(target, Resource.class);\r
35         Resource s = AdaptionUtils.adaptToSingle(source, Resource.class);\r
36 \r
37         if(t == null || s == null)\r
38             return null;\r
39 \r
40         SysdynResource sr = SysdynResource.getInstance(g);\r
41         Layer0 L0 = Layer0.getInstance(g);\r
42 \r
43         // If target is a function, find functions parent to be the drop target\r
44         if(g.isInstanceOf(t, sr.SysdynModelicaFunction))\r
45             t = g.getSingleObject(t, L0.PartOf);\r
46 \r
47         final Resource library = t;\r
48         final Resource tobemoved = s;\r
49 \r
50         // Libraries and model accept drops\r
51         if(!(g.isInstanceOf(library, sr.SysdynModelicaFunctionLibrary) || \r
52                 g.isInstanceOf(library, sr.SysdynModel) ||\r
53                 g.isInstanceOf(library, sr.SharedFunctionOntology)))\r
54             return null;\r
55 \r
56         // Functions and function libraries can be dropped\r
57         if(!(g.isInstanceOf(tobemoved, sr.SysdynModelicaFunction) || \r
58                 g.isInstanceOf(tobemoved, sr.SysdynModelicaFunctionLibrary)))\r
59             return null;\r
60 \r
61 \r
62         return new Runnable() {\r
63 \r
64             @Override\r
65             public void run() {\r
66                 SimanticsUI.getSession().asyncRequest(new WriteRequest() {\r
67 \r
68                     @Override\r
69                     public void perform(WriteGraph graph) throws DatabaseException {\r
70                         Layer0 L0 = Layer0.getInstance(graph);\r
71                         Resource oldLib = graph.getSingleObject(tobemoved, L0.PartOf);\r
72 \r
73                         // Remove dragged entity from its parent and add it to the new parent\r
74                         graph.claim(tobemoved, L0.PartOf, library);\r
75                         graph.deny(tobemoved, L0.PartOf, oldLib);\r
76                     }\r
77 \r
78                 });\r
79             }\r
80         };\r
81     }\r
82 \r
83 }\r