-/*******************************************************************************\r
- * Copyright (c) 2007, 2014 Association for Decentralized Information Management in\r
- * Industry THTH ry.\r
- * All rights reserved. This program and the accompanying materials\r
- * are made available under the terms of the Eclipse Public License v1.0\r
- * which accompanies this distribution, and is available at\r
- * http://www.eclipse.org/legal/epl-v10.html\r
- *\r
- * Contributors:\r
- * VTT Technical Research Centre of Finland - initial API and implementation\r
- *******************************************************************************/\r
-package org.simantics.charts.ui;\r
-\r
-import java.util.ArrayList;\r
-import java.util.Collection;\r
-import java.util.Collections;\r
-import java.util.HashSet;\r
-import java.util.List;\r
-import java.util.Optional;\r
-import java.util.Set;\r
-import java.util.stream.Collectors;\r
-\r
-import org.eclipse.jface.viewers.ISelection;\r
-import org.simantics.Simantics;\r
-import org.simantics.charts.internal.JsonUtils;\r
-import org.simantics.charts.ontology.ChartResource;\r
-import org.simantics.charts.query.AddChartItem;\r
-import org.simantics.charts.query.ChartItemDescriptor;\r
-import org.simantics.databoard.Bindings;\r
-import org.simantics.databoard.type.BooleanType;\r
-import org.simantics.databoard.type.Datatype;\r
-import org.simantics.databoard.type.NumberType;\r
-import org.simantics.databoard.util.ObjectUtils;\r
-import org.simantics.db.ReadGraph;\r
-import org.simantics.db.Resource;\r
-import org.simantics.db.common.procedure.adapter.ProcedureAdapter;\r
-import org.simantics.db.exception.DatabaseException;\r
-import org.simantics.db.layer0.SelectionHints;\r
-import org.simantics.db.layer0.adapter.DropActionFactory;\r
-import org.simantics.db.layer0.request.PossibleModel;\r
-import org.simantics.db.layer0.variable.RVI;\r
-import org.simantics.db.layer0.variable.Variable;\r
-import org.simantics.db.layer0.variable.VariableReference;\r
-import org.simantics.modeling.ModelingResources;\r
-import org.simantics.modeling.PropertyVariables;\r
-import org.simantics.modeling.PropertyVariablesImpl;\r
-import org.simantics.modeling.utils.VariableReferences;\r
-import org.simantics.trend.configuration.TrendItem.Renderer;\r
-import org.simantics.ui.selection.WorkbenchSelectionElement;\r
-import org.simantics.utils.ui.ErrorLogger;\r
-import org.simantics.utils.ui.ISelectionUtils;\r
-\r
-/**\r
- * @author Tuukka Lehtonen\r
- */\r
-public class ChartDropActionFactory implements DropActionFactory {\r
-\r
- @Override\r
- public Runnable create(ReadGraph g, Object target, Object source, int operation) throws DatabaseException {\r
- //System.out.println("DROP: " + source + " -> " + target);\r
-\r
- final Resource chart = ISelectionUtils.getSinglePossibleKey(target, SelectionHints.KEY_MAIN, Resource.class);\r
- if (chart == null) return null;\r
- Resource targetModel = g.syncRequest(new PossibleModel(chart));\r
- if (targetModel == null) return null;\r
-\r
- if(source instanceof RVI) {\r
- List<VariableReference> refs = Collections.singletonList(new VariableReference((RVI)source,\r
- SubscriptionDropActionFactory.getDatatype(g, targetModel, (RVI) source), null));\r
- return new AddVariableToChartAction(chart, null, refs).init(g);\r
- }\r
-\r
- List<PropertyVariables> vars = ISelectionUtils.getPossibleKeys(source, SelectionHints.KEY_MAIN, PropertyVariables.class);\r
- if (!vars.isEmpty()) {\r
- // FIXME: this is a hack for indexed value support\r
- vars = PropertyVariablesImpl.resolve(g, vars);\r
- List<VariableReference> references = toPropertyReferences(g, targetModel, vars);\r
- if (!references.isEmpty())\r
- return new AddVariableToChartAction(chart, null, references).init(g);\r
- }\r
-\r
- List<Variable> vars2 = ISelectionUtils.getPossibleKeys(source, SelectionHints.KEY_MAIN, Variable.class);\r
- if (!vars2.isEmpty()) {\r
- List<VariableReference> references = toReferences(g, targetModel, vars2);\r
- if (!references.isEmpty())\r
- return new AddVariableToChartAction(chart, null, references).init(g);\r
- }\r
-\r
- if(source instanceof ISelection) {\r
- List<WorkbenchSelectionElement> wses = ISelectionUtils.filterSelection((ISelection)source, WorkbenchSelectionElement.class);\r
- if (!wses.isEmpty()) {\r
- List<Variable> wsevars = new ArrayList<>();\r
- ChartVariable av = new ChartVariable(g);\r
- for(WorkbenchSelectionElement wse : wses) {\r
- Variable v = wse.getContent(av);\r
- if(v != null)\r
- wsevars.add(v);\r
- }\r
-\r
- List<VariableReference> references = toReferences(g, targetModel, wsevars);\r
- if (!wsevars.isEmpty()) {\r
- return new AddVariableToChartAction(chart, null, references).init(g);\r
- }\r
- }\r
- }\r
- {\r
- List<Resource> srcs = ISelectionUtils.getPossibleKeys(source, SelectionHints.KEY_MAIN, Resource.class);\r
- ModelingResources MOD = ModelingResources.getInstance(g);\r
- ChartResource CHART = ChartResource.getInstance(g);\r
- List<ChartItemDescriptor> newItems = new ArrayList<ChartItemDescriptor>();\r
- Set<Resource> movedPlots = new HashSet<Resource>();\r
- for (Resource res : srcs) {\r
- if (g.isInstanceOf(res, MOD.Subscription_Item)) {\r
- Resource model = g.syncRequest(new PossibleModel(res));\r
- if (ObjectUtils.objectEquals(targetModel, model)) {\r
- ChartItemDescriptor desc = new ChartItemDescriptor();\r
- desc.subscriptionItem = res;\r
-\r
- Datatype datatype = g.getPossibleRelatedValue(res, MOD.Subscription_Item_Datatype, Bindings.getBindingUnchecked(Datatype.class));\r
- desc.renderer = datatype instanceof BooleanType ? Renderer.Binary : Renderer.Analog;\r
-\r
- newItems.add(desc);\r
- }\r
- } else if (g.isInstanceOf(res, CHART.Chart_Item)) {\r
- Resource model = g.syncRequest(new PossibleModel(res));\r
- if (ObjectUtils.objectEquals(targetModel, model))\r
- movedPlots.add(res);\r
- }\r
- }\r
- if (!newItems.isEmpty() || !movedPlots.isEmpty())\r
- return addPlots(chart, newItems, movedPlots);\r
- }\r
-\r
- if (source instanceof String) {\r
- // JSON ?\r
- Optional<Variable> v = JsonUtils.tryParseJsonPropertyVariable(g, (String) source);\r
- if (v.isPresent()) {\r
- List<VariableReference> references = toReferences(g, targetModel, Collections.singletonList(v.get()));\r
- if (!references.isEmpty())\r
- return new AddVariableToChartAction(chart, null, references).init(g);\r
- }\r
- }\r
-\r
- return null;\r
- }\r
-\r
- private static List<VariableReference> toReferences(ReadGraph graph, Resource contextIndexRoot, List<Variable> variables) throws DatabaseException {\r
- if (variables.isEmpty())\r
- return Collections.emptyList();\r
- return filterReferences( graph.syncRequest(VariableReferences.variablesToReferences(contextIndexRoot, variables)) );\r
- }\r
-\r
- private static List<VariableReference> toPropertyReferences(ReadGraph graph, Resource contextIndexRoot, List<PropertyVariables> variables) throws DatabaseException {\r
- if (variables.isEmpty())\r
- return Collections.emptyList();\r
- return filterReferences( graph.syncRequest(VariableReferences.toReferences(contextIndexRoot, variables)) );\r
- }\r
-\r
- private static List<VariableReference> filterReferences(List<VariableReference> variables) throws DatabaseException {\r
- return variables.stream()\r
- .filter(ref -> ref.datatype instanceof BooleanType || ref.datatype instanceof NumberType)\r
- .collect(Collectors.toList());\r
- }\r
-\r
- public static Runnable addPlots(Resource chart, List<ChartItemDescriptor> references, Set<Resource> movedPlots) {\r
- return () -> {\r
- Simantics.getSession().asyncRequest(\r
- AddChartItem.addAndMoveChartItems(chart, references, movedPlots),\r
- new ProcedureAdapter<Collection<Resource>>() {\r
- @Override\r
- public void exception(Throwable e) {\r
- if (e != null)\r
- ErrorLogger.defaultLogError(e);\r
- }\r
- });\r
- };\r
- }\r
-\r
+/*******************************************************************************
+ * Copyright (c) 2007, 2017 Association for Decentralized Information Management in
+ * Industry THTH ry.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * VTT Technical Research Centre of Finland - initial API and implementation
+ * Semantum Oy - #7313
+ *******************************************************************************/
+package org.simantics.charts.ui;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+import java.util.stream.Collectors;
+
+import org.eclipse.core.runtime.IStatus;
+import org.eclipse.core.runtime.Status;
+import org.eclipse.jface.viewers.ISelection;
+import org.simantics.Simantics;
+import org.simantics.charts.Activator;
+import org.simantics.charts.internal.VariableUtils;
+import org.simantics.charts.ontology.ChartResource;
+import org.simantics.charts.query.AddChartItem;
+import org.simantics.charts.query.ChartItemDescriptor;
+import org.simantics.databoard.Bindings;
+import org.simantics.databoard.type.BooleanType;
+import org.simantics.databoard.type.Datatype;
+import org.simantics.databoard.type.NumberType;
+import org.simantics.databoard.util.ObjectUtils;
+import org.simantics.db.ReadGraph;
+import org.simantics.db.Resource;
+import org.simantics.db.common.procedure.adapter.ProcedureAdapter;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.layer0.SelectionHints;
+import org.simantics.db.layer0.adapter.DropActionFactory;
+import org.simantics.db.layer0.request.PossibleModel;
+import org.simantics.db.layer0.variable.RVI;
+import org.simantics.db.layer0.variable.Variable;
+import org.simantics.db.layer0.variable.VariableReference;
+import org.simantics.modeling.ModelingResources;
+import org.simantics.modeling.PropertyVariables;
+import org.simantics.modeling.PropertyVariablesImpl;
+import org.simantics.modeling.utils.VariableReferences;
+import org.simantics.trend.configuration.TrendItem.Renderer;
+import org.simantics.ui.selection.WorkbenchSelectionElement;
+import org.simantics.utils.ui.ErrorLogger;
+import org.simantics.utils.ui.ISelectionUtils;
+
+/**
+ * @author Tuukka Lehtonen
+ */
+public class ChartDropActionFactory implements DropActionFactory {
+
+ @Override
+ public Runnable create(ReadGraph g, Object target, Object source, int operation) throws DatabaseException {
+ //System.out.println("CHART DROP: " + source + " -> " + target);
+
+ final Resource chart = ISelectionUtils.getSinglePossibleKey(target, SelectionHints.KEY_MAIN, Resource.class);
+ if (chart == null) return null;
+ Resource targetModel = g.syncRequest(new PossibleModel(chart));
+ if (targetModel == null) return null;
+
+ if(source instanceof RVI) {
+ List<VariableReference> refs = Collections.singletonList(new VariableReference((RVI)source,
+ VariableUtils.getDatatype(g, targetModel, (RVI) source), null));
+ return new AddVariableToChartAction(chart, null, refs).init(g);
+ }
+
+ List<PropertyVariables> vars = ISelectionUtils.getPossibleKeys(source, SelectionHints.KEY_MAIN, PropertyVariables.class);
+ if (!vars.isEmpty()) {
+ // FIXME: this is a hack for indexed value support
+ vars = PropertyVariablesImpl.resolve(g, vars);
+ List<VariableReference> references = toPropertyReferences(g, targetModel, vars);
+ if (!references.isEmpty())
+ return new AddVariableToChartAction(chart, null, references).init(g);
+ }
+
+ List<Variable> vars2 = ISelectionUtils.getPossibleKeys(source, SelectionHints.KEY_MAIN, Variable.class);
+ if (!vars2.isEmpty()) {
+ List<VariableReference> references = toReferences(g, targetModel, vars2);
+ if (!references.isEmpty())
+ return new AddVariableToChartAction(chart, null, references).init(g);
+ }
+
+ if(source instanceof ISelection) {
+ List<WorkbenchSelectionElement> wses = ISelectionUtils.filterSelection((ISelection)source, WorkbenchSelectionElement.class);
+ if (!wses.isEmpty()) {
+ List<Variable> wsevars = new ArrayList<>();
+ ChartVariable av = new ChartVariable(g);
+ for(WorkbenchSelectionElement wse : wses) {
+ Variable v = wse.getContent(av);
+ if(v != null)
+ wsevars.add(v);
+ }
+
+ List<VariableReference> references = toReferences(g, targetModel, wsevars);
+ if (!wsevars.isEmpty()) {
+ return new AddVariableToChartAction(chart, null, references).init(g);
+ }
+ }
+ }
+ {
+ List<Resource> srcs = ISelectionUtils.getPossibleKeys(source, SelectionHints.KEY_MAIN, Resource.class);
+ ModelingResources MOD = ModelingResources.getInstance(g);
+ ChartResource CHART = ChartResource.getInstance(g);
+ List<ChartItemDescriptor> newItems = new ArrayList<ChartItemDescriptor>();
+ Set<Resource> movedPlots = new HashSet<Resource>();
+ for (Resource res : srcs) {
+ if (g.isInstanceOf(res, MOD.Subscription_Item)) {
+ Resource model = g.syncRequest(new PossibleModel(res));
+ if (ObjectUtils.objectEquals(targetModel, model)) {
+ ChartItemDescriptor desc = new ChartItemDescriptor();
+ desc.subscriptionItem = res;
+
+ Datatype datatype = g.getPossibleRelatedValue(res, MOD.Subscription_Item_Datatype, Bindings.getBindingUnchecked(Datatype.class));
+ desc.renderer = datatype instanceof BooleanType ? Renderer.Binary : Renderer.Analog;
+
+ newItems.add(desc);
+ }
+ } else if (g.isInstanceOf(res, CHART.Chart_Item)) {
+ Resource model = g.syncRequest(new PossibleModel(res));
+ if (ObjectUtils.objectEquals(targetModel, model))
+ movedPlots.add(res);
+ }
+ }
+ if (!newItems.isEmpty() || !movedPlots.isEmpty())
+ return addPlots(chart, newItems, movedPlots);
+ }
+
+ if (source instanceof String) {
+ return handleStringDrop(g, chart, targetModel, (String) source);
+ }
+
+ return null;
+ }
+
+ private Runnable handleStringDrop(ReadGraph graph, Resource chart, Resource targetModel, String source) {
+ try {
+ List<VariableReference> refs = VariableUtils.getVariableReferencesFromString(graph, targetModel, source);
+ return new AddVariableToChartAction(chart, null, refs).init(graph);
+ } catch (DatabaseException e) {
+ Activator.getDefault().getLog().log(new Status(IStatus.WARNING, Activator.PLUGIN_ID, getClass().getSimpleName() + ": Unrecognized String input: " + source));
+ return null;
+ }
+ }
+
+ private static List<VariableReference> toReferences(ReadGraph graph, Resource contextIndexRoot, List<Variable> variables) throws DatabaseException {
+ if (variables.isEmpty())
+ return Collections.emptyList();
+ return filterReferences( graph.syncRequest(VariableReferences.variablesToReferences(contextIndexRoot, variables)) );
+ }
+
+ private static List<VariableReference> toPropertyReferences(ReadGraph graph, Resource contextIndexRoot, List<PropertyVariables> variables) throws DatabaseException {
+ if (variables.isEmpty())
+ return Collections.emptyList();
+ return filterReferences( graph.syncRequest(VariableReferences.toReferences(contextIndexRoot, variables)) );
+ }
+
+ private static List<VariableReference> filterReferences(List<VariableReference> variables) throws DatabaseException {
+ return variables.stream()
+ .filter(ref -> ref.datatype instanceof BooleanType || ref.datatype instanceof NumberType)
+ .collect(Collectors.toList());
+ }
+
+ public static Runnable addPlots(Resource chart, List<ChartItemDescriptor> references, Set<Resource> movedPlots) {
+ return () -> {
+ Simantics.getSession().asyncRequest(
+ AddChartItem.addAndMoveChartItems(chart, references, movedPlots),
+ new ProcedureAdapter<Collection<Resource>>() {
+ @Override
+ public void exception(Throwable e) {
+ if (e != null)
+ ErrorLogger.defaultLogError(e);
+ }
+ });
+ };
+ }
+
}
\ No newline at end of file