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