]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.charts/src/org/simantics/charts/ui/SubscriptionDropActionFactory.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.charts / src / org / simantics / charts / ui / SubscriptionDropActionFactory.java
1 /*******************************************************************************
2  * Copyright (c) 2014 Association for Decentralized Information Management in
3  * Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.charts.ui;
13
14 import java.util.Collections;
15 import java.util.HashSet;
16 import java.util.List;
17 import java.util.Optional;
18 import java.util.Set;
19
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;
44
45 /**
46  * @author Tuukka Lehtonen
47  */
48 public class SubscriptionDropActionFactory implements DropActionFactory {
49
50     @Override
51     public Runnable create(ReadGraph g, Object target, Object source, int operation) throws DatabaseException {
52         //System.out.println("DROP: " + source + " -> " + target);
53
54         final Resource subscription = ISelectionUtils.getSinglePossibleKey(target, SelectionHints.KEY_MAIN, Resource.class);
55         if (subscription == null)
56             return null;
57         Resource targetModel = g.syncRequest(new PossibleModel(subscription));
58
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());
63         }
64
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());
72         } else {
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))
80                         movedItems.add(src);
81                 }
82             }
83             if (!movedItems.isEmpty())
84                 return addSubscriptions(g, subscription, Collections.emptyList(), movedItems);
85         }
86
87         if (source instanceof String) {
88             // JSON ?
89             Optional<Variable> v = JsonUtils.tryParseJsonPropertyVariable(g, (String) source);
90             if (v.isPresent()) {
91                 List<VariableReference> references = g.syncRequest(VariableReferences.variablesToReferences(targetModel, Collections.singletonList(v.get())));
92                 return addSubscriptions(g, subscription, references, Collections.emptySet());
93             }
94         }
95
96         return null;
97     }
98
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);
102         return () -> {
103             action.run();
104             if(!movedSubscriptionItems.isEmpty()) {
105                 Simantics.getSession().asyncRequest(new WriteRequest() {
106                     @Override
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);
112                         }
113                     }
114                 }, e -> {
115                     if (e != null)
116                         ErrorLogger.defaultLogError(e);
117                 });
118             }
119         };
120     }
121
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);
127     }
128
129 }