Sync git svn branch with SVN repository r33406.
[simantics/platform.git] / bundles / org.simantics.charts / src / org / simantics / charts / ui / ChartDropActionFactory.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2014 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.charts.ui;\r
13 \r
14 import java.util.ArrayList;\r
15 import java.util.Collection;\r
16 import java.util.Collections;\r
17 import java.util.HashSet;\r
18 import java.util.List;\r
19 import java.util.Optional;\r
20 import java.util.Set;\r
21 import java.util.stream.Collectors;\r
22 \r
23 import org.eclipse.jface.viewers.ISelection;\r
24 import org.simantics.Simantics;\r
25 import org.simantics.charts.internal.JsonUtils;\r
26 import org.simantics.charts.ontology.ChartResource;\r
27 import org.simantics.charts.query.AddChartItem;\r
28 import org.simantics.charts.query.ChartItemDescriptor;\r
29 import org.simantics.databoard.Bindings;\r
30 import org.simantics.databoard.type.BooleanType;\r
31 import org.simantics.databoard.type.Datatype;\r
32 import org.simantics.databoard.type.NumberType;\r
33 import org.simantics.databoard.util.ObjectUtils;\r
34 import org.simantics.db.ReadGraph;\r
35 import org.simantics.db.Resource;\r
36 import org.simantics.db.common.procedure.adapter.ProcedureAdapter;\r
37 import org.simantics.db.exception.DatabaseException;\r
38 import org.simantics.db.layer0.SelectionHints;\r
39 import org.simantics.db.layer0.adapter.DropActionFactory;\r
40 import org.simantics.db.layer0.request.PossibleModel;\r
41 import org.simantics.db.layer0.variable.RVI;\r
42 import org.simantics.db.layer0.variable.Variable;\r
43 import org.simantics.db.layer0.variable.VariableReference;\r
44 import org.simantics.modeling.ModelingResources;\r
45 import org.simantics.modeling.PropertyVariables;\r
46 import org.simantics.modeling.PropertyVariablesImpl;\r
47 import org.simantics.modeling.utils.VariableReferences;\r
48 import org.simantics.trend.configuration.TrendItem.Renderer;\r
49 import org.simantics.ui.selection.WorkbenchSelectionElement;\r
50 import org.simantics.utils.ui.ErrorLogger;\r
51 import org.simantics.utils.ui.ISelectionUtils;\r
52 \r
53 /**\r
54  * @author Tuukka Lehtonen\r
55  */\r
56 public class ChartDropActionFactory implements DropActionFactory {\r
57 \r
58     @Override\r
59     public Runnable create(ReadGraph g, Object target, Object source, int operation) throws DatabaseException {\r
60         //System.out.println("DROP: " + source + " -> " + target);\r
61 \r
62         final Resource chart = ISelectionUtils.getSinglePossibleKey(target, SelectionHints.KEY_MAIN, Resource.class);\r
63         if (chart == null) return null;\r
64         Resource targetModel = g.syncRequest(new PossibleModel(chart));\r
65         if (targetModel == null) return null;\r
66 \r
67         if(source instanceof RVI) {\r
68             List<VariableReference> refs = Collections.singletonList(new VariableReference((RVI)source,\r
69                     SubscriptionDropActionFactory.getDatatype(g, targetModel, (RVI) source), null));\r
70             return new AddVariableToChartAction(chart, null, refs).init(g);\r
71         }\r
72 \r
73         List<PropertyVariables> vars = ISelectionUtils.getPossibleKeys(source, SelectionHints.KEY_MAIN, PropertyVariables.class);\r
74         if (!vars.isEmpty()) {\r
75             // FIXME: this is a hack for indexed value support\r
76             vars = PropertyVariablesImpl.resolve(g, vars);\r
77             List<VariableReference> references = toPropertyReferences(g, targetModel, vars);\r
78             if (!references.isEmpty())\r
79                 return new AddVariableToChartAction(chart, null, references).init(g);\r
80         }\r
81 \r
82         List<Variable> vars2 = ISelectionUtils.getPossibleKeys(source, SelectionHints.KEY_MAIN, Variable.class);\r
83         if (!vars2.isEmpty()) {\r
84             List<VariableReference> references = toReferences(g, targetModel, vars2);\r
85             if (!references.isEmpty())\r
86                 return new AddVariableToChartAction(chart, null, references).init(g);\r
87         }\r
88 \r
89         if(source instanceof ISelection) {\r
90             List<WorkbenchSelectionElement> wses = ISelectionUtils.filterSelection((ISelection)source, WorkbenchSelectionElement.class);\r
91             if (!wses.isEmpty()) {\r
92                 List<Variable> wsevars = new ArrayList<>();\r
93                 ChartVariable av = new ChartVariable(g);\r
94                 for(WorkbenchSelectionElement wse : wses) {\r
95                     Variable v = wse.getContent(av);\r
96                     if(v != null)\r
97                         wsevars.add(v);\r
98                 }\r
99 \r
100                 List<VariableReference> references = toReferences(g, targetModel, wsevars);\r
101                 if (!wsevars.isEmpty()) {\r
102                     return new AddVariableToChartAction(chart, null, references).init(g);\r
103                 }\r
104             }\r
105         }\r
106         {\r
107             List<Resource> srcs = ISelectionUtils.getPossibleKeys(source, SelectionHints.KEY_MAIN, Resource.class);\r
108             ModelingResources MOD = ModelingResources.getInstance(g);\r
109             ChartResource CHART = ChartResource.getInstance(g);\r
110             List<ChartItemDescriptor> newItems = new ArrayList<ChartItemDescriptor>();\r
111             Set<Resource> movedPlots = new HashSet<Resource>();\r
112             for (Resource res : srcs) {\r
113                 if (g.isInstanceOf(res, MOD.Subscription_Item)) {\r
114                     Resource model = g.syncRequest(new PossibleModel(res));\r
115                     if (ObjectUtils.objectEquals(targetModel, model)) {\r
116                         ChartItemDescriptor desc = new ChartItemDescriptor();\r
117                         desc.subscriptionItem = res;\r
118 \r
119                         Datatype datatype = g.getPossibleRelatedValue(res, MOD.Subscription_Item_Datatype, Bindings.getBindingUnchecked(Datatype.class));\r
120                         desc.renderer = datatype instanceof BooleanType ? Renderer.Binary : Renderer.Analog;\r
121 \r
122                         newItems.add(desc);\r
123                     }\r
124                 } else if (g.isInstanceOf(res, CHART.Chart_Item)) {\r
125                     Resource model = g.syncRequest(new PossibleModel(res));\r
126                     if (ObjectUtils.objectEquals(targetModel, model))\r
127                         movedPlots.add(res);\r
128                 }\r
129             }\r
130             if (!newItems.isEmpty() || !movedPlots.isEmpty())\r
131                 return addPlots(chart, newItems, movedPlots);\r
132         }\r
133 \r
134         if (source instanceof String) {\r
135             // JSON ?\r
136             Optional<Variable> v = JsonUtils.tryParseJsonPropertyVariable(g, (String) source);\r
137             if (v.isPresent()) {\r
138                 List<VariableReference> references = toReferences(g, targetModel, Collections.singletonList(v.get()));\r
139                 if (!references.isEmpty())\r
140                     return new AddVariableToChartAction(chart, null, references).init(g);\r
141             }\r
142         }\r
143 \r
144         return null;\r
145     }\r
146 \r
147     private static List<VariableReference> toReferences(ReadGraph graph, Resource contextIndexRoot, List<Variable> variables) throws DatabaseException {\r
148         if (variables.isEmpty())\r
149             return Collections.emptyList();\r
150         return filterReferences( graph.syncRequest(VariableReferences.variablesToReferences(contextIndexRoot, variables)) );\r
151     }\r
152 \r
153     private static List<VariableReference> toPropertyReferences(ReadGraph graph, Resource contextIndexRoot, List<PropertyVariables> variables) throws DatabaseException {\r
154         if (variables.isEmpty())\r
155             return Collections.emptyList();\r
156         return filterReferences( graph.syncRequest(VariableReferences.toReferences(contextIndexRoot, variables)) );\r
157     }\r
158 \r
159     private static List<VariableReference> filterReferences(List<VariableReference> variables) throws DatabaseException {\r
160         return variables.stream()\r
161                 .filter(ref -> ref.datatype instanceof BooleanType || ref.datatype instanceof NumberType)\r
162                 .collect(Collectors.toList());\r
163     }\r
164 \r
165     public static Runnable addPlots(Resource chart, List<ChartItemDescriptor> references, Set<Resource> movedPlots) {\r
166         return () -> {\r
167             Simantics.getSession().asyncRequest(\r
168                     AddChartItem.addAndMoveChartItems(chart, references, movedPlots),\r
169                     new ProcedureAdapter<Collection<Resource>>() {\r
170                         @Override\r
171                         public void exception(Throwable e) {\r
172                             if (e != null)\r
173                                 ErrorLogger.defaultLogError(e);\r
174                         }\r
175                     });\r
176         };\r
177     }\r
178 \r
179 }