1 /*******************************************************************************
2 * Copyright (c) 2014, 2017 Association for Decentralized Information Management in
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
12 *******************************************************************************/
13 package org.simantics.charts.ui;
15 import java.util.Collections;
16 import java.util.HashSet;
17 import java.util.List;
20 import org.eclipse.core.runtime.IStatus;
21 import org.eclipse.core.runtime.Status;
22 import org.simantics.Simantics;
23 import org.simantics.browsing.ui.common.ErrorLogger;
24 import org.simantics.charts.Activator;
25 import org.simantics.charts.internal.VariableUtils;
26 import org.simantics.databoard.util.ObjectUtils;
27 import org.simantics.db.ReadGraph;
28 import org.simantics.db.Resource;
29 import org.simantics.db.WriteGraph;
30 import org.simantics.db.common.request.WriteRequest;
31 import org.simantics.db.exception.DatabaseException;
32 import org.simantics.db.layer0.SelectionHints;
33 import org.simantics.db.layer0.adapter.DropActionFactory;
34 import org.simantics.db.layer0.request.PossibleModel;
35 import org.simantics.db.layer0.variable.RVI;
36 import org.simantics.db.layer0.variable.VariableReference;
37 import org.simantics.layer0.Layer0;
38 import org.simantics.modeling.ModelingResources;
39 import org.simantics.modeling.PropertyVariables;
40 import org.simantics.modeling.PropertyVariablesImpl;
41 import org.simantics.modeling.utils.VariableReferences;
42 import org.simantics.utils.ui.ISelectionUtils;
45 * @author Tuukka Lehtonen
47 public class SubscriptionDropActionFactory implements DropActionFactory {
50 public Runnable create(ReadGraph g, Object target, Object source, int operation) throws DatabaseException {
51 //System.out.println("SUBSCRIPTION DROP: " + source + " -> " + target);
53 final Resource subscription = ISelectionUtils.getSinglePossibleKey(target, SelectionHints.KEY_MAIN, Resource.class);
54 if (subscription == null)
56 Resource targetModel = g.syncRequest(new PossibleModel(subscription));
58 if(source instanceof RVI) {
59 RVI rvi = (RVI)source;
60 List<VariableReference> refs = Collections.singletonList(new VariableReference(rvi, VariableUtils.getDatatype(g, targetModel, rvi), null));
61 return addSubscriptions(g, subscription, refs, Collections.<Resource>emptySet());
64 List<PropertyVariables> vars = ISelectionUtils.getPossibleKeys(source, SelectionHints.KEY_MAIN, PropertyVariables.class);
65 if (!vars.isEmpty()) {
66 // FIXME: this is a hack for indexed value support
67 vars = PropertyVariablesImpl.resolve(g, vars);
68 List<VariableReference> references = g.syncRequest(VariableReferences.toReferences(targetModel, vars));
69 if (!references.isEmpty())
70 return addSubscriptions(g, subscription, references, Collections.<Resource>emptySet());
72 List<Resource> srcs = ISelectionUtils.getPossibleKeys(source, SelectionHints.KEY_MAIN, Resource.class);
73 ModelingResources MOD = ModelingResources.getInstance(g);
74 Set<Resource> movedItems = new HashSet<>();
75 for (Resource src : srcs) {
76 if (g.isInstanceOf(src, MOD.Subscription_Item)) {
77 Resource model = g.syncRequest(new PossibleModel(src));
78 if (ObjectUtils.objectEquals(targetModel, model))
82 if (!movedItems.isEmpty())
83 return addSubscriptions(g, subscription, Collections.emptyList(), movedItems);
86 if (source instanceof String) {
87 return handleStringDrop(g, subscription, targetModel, (String) source);
93 private Runnable handleStringDrop(ReadGraph graph, Resource subscription, Resource targetModel, String source) throws DatabaseException {
95 List<VariableReference> refs = VariableUtils.getVariableReferencesFromString(graph, targetModel, source);
96 return addSubscriptions(graph, subscription, refs, Collections.emptySet());
97 } catch (DatabaseException e) {
98 Activator.getDefault().getLog().log(new Status(IStatus.WARNING, Activator.PLUGIN_ID, getClass().getSimpleName() + ": Unrecognized String input: " + source));
103 private Runnable addSubscriptions(ReadGraph graph, Resource subscription, List<VariableReference> references,
104 Set<Resource> movedSubscriptionItems) throws DatabaseException {
105 AddVariableToChartAction action = new AddVariableToChartAction(null, subscription, references).init(graph);
108 if(!movedSubscriptionItems.isEmpty()) {
109 Simantics.getSession().asyncRequest(new WriteRequest() {
111 public void perform(WriteGraph graph) throws DatabaseException {
112 Layer0 L0 = Layer0.getInstance(graph);
113 for (Resource item : movedSubscriptionItems) {
114 graph.deny(item, L0.PartOf);
115 graph.claim(subscription, L0.ConsistsOf, item);
120 ErrorLogger.defaultLogError(e);