1 /*******************************************************************************
2 * Copyright (c) 2014 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
11 *******************************************************************************/
12 package org.simantics.charts.ui;
14 import java.util.Collections;
15 import java.util.HashSet;
16 import java.util.List;
17 import java.util.Optional;
20 import org.simantics.Simantics;
21 import org.simantics.browsing.ui.common.ErrorLogger;
22 import org.simantics.charts.internal.JsonUtils;
23 import org.simantics.databoard.type.Datatype;
24 import org.simantics.databoard.util.ObjectUtils;
25 import org.simantics.db.ReadGraph;
26 import org.simantics.db.Resource;
27 import org.simantics.db.WriteGraph;
28 import org.simantics.db.common.request.WriteRequest;
29 import org.simantics.db.exception.DatabaseException;
30 import org.simantics.db.layer0.SelectionHints;
31 import org.simantics.db.layer0.adapter.DropActionFactory;
32 import org.simantics.db.layer0.request.PossibleActiveVariableFromVariable;
33 import org.simantics.db.layer0.request.PossibleModel;
34 import org.simantics.db.layer0.variable.RVI;
35 import org.simantics.db.layer0.variable.Variable;
36 import org.simantics.db.layer0.variable.VariableReference;
37 import org.simantics.db.layer0.variable.Variables;
38 import org.simantics.layer0.Layer0;
39 import org.simantics.modeling.ModelingResources;
40 import org.simantics.modeling.PropertyVariables;
41 import org.simantics.modeling.PropertyVariablesImpl;
42 import org.simantics.modeling.utils.VariableReferences;
43 import org.simantics.utils.ui.ISelectionUtils;
46 * @author Tuukka Lehtonen
48 public class SubscriptionDropActionFactory implements DropActionFactory {
51 public Runnable create(ReadGraph g, Object target, Object source, int operation) throws DatabaseException {
52 //System.out.println("DROP: " + source + " -> " + target);
54 final Resource subscription = ISelectionUtils.getSinglePossibleKey(target, SelectionHints.KEY_MAIN, Resource.class);
55 if (subscription == null)
57 Resource targetModel = g.syncRequest(new PossibleModel(subscription));
59 if(source instanceof RVI) {
60 RVI rvi = (RVI)source;
61 List<VariableReference> refs = Collections.singletonList(new VariableReference(rvi, getDatatype(g, targetModel, rvi), null));
62 return addSubscriptions(g, subscription, refs, Collections.<Resource>emptySet());
65 List<PropertyVariables> vars = ISelectionUtils.getPossibleKeys(source, SelectionHints.KEY_MAIN, PropertyVariables.class);
66 if (!vars.isEmpty()) {
67 // FIXME: this is a hack for indexed value support
68 vars = PropertyVariablesImpl.resolve(g, vars);
69 List<VariableReference> references = g.syncRequest(VariableReferences.toReferences(targetModel, vars));
70 if (!references.isEmpty())
71 return addSubscriptions(g, subscription, references, Collections.<Resource>emptySet());
73 List<Resource> srcs = ISelectionUtils.getPossibleKeys(source, SelectionHints.KEY_MAIN, Resource.class);
74 ModelingResources MOD = ModelingResources.getInstance(g);
75 Set<Resource> movedItems = new HashSet<>();
76 for (Resource src : srcs) {
77 if (g.isInstanceOf(src, MOD.Subscription_Item)) {
78 Resource model = g.syncRequest(new PossibleModel(src));
79 if (ObjectUtils.objectEquals(targetModel, model))
83 if (!movedItems.isEmpty())
84 return addSubscriptions(g, subscription, Collections.emptyList(), movedItems);
87 if (source instanceof String) {
89 Optional<Variable> v = JsonUtils.tryParseJsonPropertyVariable(g, (String) source);
91 List<VariableReference> references = g.syncRequest(VariableReferences.variablesToReferences(targetModel, Collections.singletonList(v.get())));
92 return addSubscriptions(g, subscription, references, Collections.emptySet());
99 private Runnable addSubscriptions(ReadGraph graph, Resource subscription, List<VariableReference> references,
100 Set<Resource> movedSubscriptionItems) throws DatabaseException {
101 AddVariableToChartAction action = new AddVariableToChartAction(null, subscription, references).init(graph);
104 if(!movedSubscriptionItems.isEmpty()) {
105 Simantics.getSession().asyncRequest(new WriteRequest() {
107 public void perform(WriteGraph graph) throws DatabaseException {
108 Layer0 L0 = Layer0.getInstance(graph);
109 for (Resource item : movedSubscriptionItems) {
110 graph.deny(item, L0.PartOf);
111 graph.claim(subscription, L0.ConsistsOf, item);
116 ErrorLogger.defaultLogError(e);
122 static Datatype getDatatype(ReadGraph graph, Resource resource, RVI rvi) throws DatabaseException {
123 Variable configuration = Variables.getConfigurationContext(graph, resource);
124 Variable active = graph.syncRequest(new PossibleActiveVariableFromVariable(configuration));
125 Variable var = rvi.resolve(graph, active != null ? active : configuration);
126 return var.getDatatype(graph);