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.drop;
\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
26 * Action for moving functions and function libraries in model browser
\r
27 * @author Teemu Lempinen
\r
30 public class FunctionDropAction implements DropActionFactory {
\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
37 if(t == null || s == null)
\r
40 SysdynResource sr = SysdynResource.getInstance(g);
\r
41 Layer0 L0 = Layer0.getInstance(g);
\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
47 final Resource library = t;
\r
48 final Resource tobemoved = s;
\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
56 // Functions and function libraries can be dropped
\r
57 if(!(g.isInstanceOf(tobemoved, sr.SysdynModelicaFunction) ||
\r
58 g.isInstanceOf(tobemoved, sr.SysdynModelicaFunctionLibrary)))
\r
62 return new Runnable() {
\r
66 SimanticsUI.getSession().asyncRequest(new WriteRequest() {
\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
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