1 /*******************************************************************************
2 * Copyright (c) 2007, 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.ArrayList;
16 import java.util.Collection;
17 import java.util.Collections;
18 import java.util.HashSet;
19 import java.util.List;
21 import java.util.stream.Collectors;
23 import org.eclipse.core.runtime.IStatus;
24 import org.eclipse.core.runtime.Status;
25 import org.eclipse.jface.viewers.ISelection;
26 import org.simantics.Simantics;
27 import org.simantics.charts.Activator;
28 import org.simantics.charts.internal.VariableUtils;
29 import org.simantics.charts.ontology.ChartResource;
30 import org.simantics.charts.query.AddChartItem;
31 import org.simantics.charts.query.ChartItemDescriptor;
32 import org.simantics.databoard.Bindings;
33 import org.simantics.databoard.type.BooleanType;
34 import org.simantics.databoard.type.Datatype;
35 import org.simantics.databoard.type.NumberType;
36 import org.simantics.databoard.util.ObjectUtils;
37 import org.simantics.db.ReadGraph;
38 import org.simantics.db.Resource;
39 import org.simantics.db.common.procedure.adapter.ProcedureAdapter;
40 import org.simantics.db.exception.DatabaseException;
41 import org.simantics.db.layer0.SelectionHints;
42 import org.simantics.db.layer0.adapter.DropActionFactory;
43 import org.simantics.db.layer0.request.PossibleModel;
44 import org.simantics.db.layer0.variable.RVI;
45 import org.simantics.db.layer0.variable.Variable;
46 import org.simantics.db.layer0.variable.VariableReference;
47 import org.simantics.modeling.ModelingResources;
48 import org.simantics.modeling.PropertyVariables;
49 import org.simantics.modeling.PropertyVariablesImpl;
50 import org.simantics.modeling.utils.VariableReferences;
51 import org.simantics.trend.configuration.TrendItem.Renderer;
52 import org.simantics.ui.selection.WorkbenchSelectionElement;
53 import org.simantics.utils.ui.ErrorLogger;
54 import org.simantics.utils.ui.ISelectionUtils;
57 * @author Tuukka Lehtonen
59 public class ChartDropActionFactory implements DropActionFactory {
62 public Runnable create(ReadGraph g, Object target, Object source, int operation) throws DatabaseException {
63 //System.out.println("CHART DROP: " + source + " -> " + target);
65 final Resource chart = ISelectionUtils.getSinglePossibleKey(target, SelectionHints.KEY_MAIN, Resource.class);
66 if (chart == null) return null;
67 Resource targetModel = g.syncRequest(new PossibleModel(chart));
68 if (targetModel == null) return null;
70 if(source instanceof RVI) {
71 List<VariableReference> refs = Collections.singletonList(new VariableReference((RVI)source,
72 VariableUtils.getDatatype(g, targetModel, (RVI) source), null));
73 return new AddVariableToChartAction(chart, null, refs).init(g);
76 List<PropertyVariables> vars = ISelectionUtils.getPossibleKeys(source, SelectionHints.KEY_MAIN, PropertyVariables.class);
77 if (!vars.isEmpty()) {
78 // FIXME: this is a hack for indexed value support
79 vars = PropertyVariablesImpl.resolve(g, vars);
80 List<VariableReference> references = toPropertyReferences(g, targetModel, vars);
81 if (!references.isEmpty())
82 return new AddVariableToChartAction(chart, null, references).init(g);
85 List<Variable> vars2 = ISelectionUtils.getPossibleKeys(source, SelectionHints.KEY_MAIN, Variable.class);
86 if (!vars2.isEmpty()) {
87 List<VariableReference> references = toReferences(g, targetModel, vars2);
88 if (!references.isEmpty())
89 return new AddVariableToChartAction(chart, null, references).init(g);
92 if(source instanceof ISelection) {
93 List<WorkbenchSelectionElement> wses = ISelectionUtils.filterSelection((ISelection)source, WorkbenchSelectionElement.class);
94 if (!wses.isEmpty()) {
95 List<Variable> wsevars = new ArrayList<>();
96 ChartVariable av = new ChartVariable(g);
97 for(WorkbenchSelectionElement wse : wses) {
98 Variable v = wse.getContent(av);
103 List<VariableReference> references = toReferences(g, targetModel, wsevars);
104 if (!wsevars.isEmpty()) {
105 return new AddVariableToChartAction(chart, null, references).init(g);
110 List<Resource> srcs = ISelectionUtils.getPossibleKeys(source, SelectionHints.KEY_MAIN, Resource.class);
111 ModelingResources MOD = ModelingResources.getInstance(g);
112 ChartResource CHART = ChartResource.getInstance(g);
113 List<ChartItemDescriptor> newItems = new ArrayList<ChartItemDescriptor>();
114 Set<Resource> movedPlots = new HashSet<Resource>();
115 for (Resource res : srcs) {
116 if (g.isInstanceOf(res, MOD.Subscription_Item)) {
117 Resource model = g.syncRequest(new PossibleModel(res));
118 if (ObjectUtils.objectEquals(targetModel, model)) {
119 ChartItemDescriptor desc = new ChartItemDescriptor();
120 desc.subscriptionItem = res;
122 Datatype datatype = g.getPossibleRelatedValue(res, MOD.Subscription_Item_Datatype, Bindings.getBindingUnchecked(Datatype.class));
123 desc.renderer = datatype instanceof BooleanType ? Renderer.Binary : Renderer.Analog;
127 } else if (g.isInstanceOf(res, CHART.Chart_Item)) {
128 Resource model = g.syncRequest(new PossibleModel(res));
129 if (ObjectUtils.objectEquals(targetModel, model))
133 if (!newItems.isEmpty() || !movedPlots.isEmpty())
134 return addPlots(chart, newItems, movedPlots);
137 if (source instanceof String) {
138 return handleStringDrop(g, chart, targetModel, (String) source);
144 private Runnable handleStringDrop(ReadGraph graph, Resource chart, Resource targetModel, String source) {
146 List<VariableReference> refs = VariableUtils.getVariableReferencesFromString(graph, targetModel, source);
147 return new AddVariableToChartAction(chart, null, refs).init(graph);
148 } catch (DatabaseException e) {
149 Activator.getDefault().getLog().log(new Status(IStatus.WARNING, Activator.PLUGIN_ID, getClass().getSimpleName() + ": Unrecognized String input: " + source));
154 private static List<VariableReference> toReferences(ReadGraph graph, Resource contextIndexRoot, List<Variable> variables) throws DatabaseException {
155 if (variables.isEmpty())
156 return Collections.emptyList();
157 return filterReferences( graph.syncRequest(VariableReferences.variablesToReferences(contextIndexRoot, variables)) );
160 private static List<VariableReference> toPropertyReferences(ReadGraph graph, Resource contextIndexRoot, List<PropertyVariables> variables) throws DatabaseException {
161 if (variables.isEmpty())
162 return Collections.emptyList();
163 return filterReferences( graph.syncRequest(VariableReferences.toReferences(contextIndexRoot, variables)) );
166 private static List<VariableReference> filterReferences(List<VariableReference> variables) throws DatabaseException {
167 return variables.stream()
168 .filter(ref -> ref.datatype instanceof BooleanType || ref.datatype instanceof NumberType)
169 .collect(Collectors.toList());
172 public static Runnable addPlots(Resource chart, List<ChartItemDescriptor> references, Set<Resource> movedPlots) {
174 Simantics.getSession().asyncRequest(
175 AddChartItem.addAndMoveChartItems(chart, references, movedPlots),
176 new ProcedureAdapter<Collection<Resource>>() {
178 public void exception(Throwable e) {
180 ErrorLogger.defaultLogError(e);