From: Tuukka Lehtonen Date: Fri, 16 Jun 2017 09:34:43 +0000 (+0300) Subject: Fixed diagram profile monitor DnD regression. X-Git-Tag: v1.31.0~308 X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=commitdiff_plain;h=c97b685ff67f795fd8cd7b166ee740d234a577f4 Fixed diagram profile monitor DnD regression. CTRL+dragging diagram profile monitor values from diagram to Model Browser chart/subscription nodes now works again. The code now processes also dragged data that contains an RVI in String format. refs #7313 Change-Id: I0154414df73ea96c29a8e5aa44881781f5d62675 (cherry picked from commit b9d4701787a7aba852ec02aaef6149b8203344b3) --- diff --git a/bundles/org.simantics.charts/src/org/simantics/charts/editor/SubscriptionDropParticipant.java b/bundles/org.simantics.charts/src/org/simantics/charts/editor/SubscriptionDropParticipant.java index 861d220ea..9b08eacbc 100644 --- a/bundles/org.simantics.charts/src/org/simantics/charts/editor/SubscriptionDropParticipant.java +++ b/bundles/org.simantics.charts/src/org/simantics/charts/editor/SubscriptionDropParticipant.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2007, 2016 Association for Decentralized Information Management in + * 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 @@ -8,7 +8,7 @@ * * Contributors: * VTT Technical Research Centre of Finland - initial API and implementation - * Semantum Oy - JSON plain text input support + * Semantum Oy - JSON plain text input support, #7313 *******************************************************************************/ package org.simantics.charts.editor; @@ -32,6 +32,7 @@ import java.util.stream.Collectors; import org.eclipse.jface.viewers.ISelection; import org.simantics.Simantics; import org.simantics.charts.internal.JsonUtils; +import org.simantics.charts.internal.VariableUtils; import org.simantics.charts.query.AddChartItem; import org.simantics.charts.query.ChartItemDescriptor; import org.simantics.charts.ui.AddVariableToChartAction; @@ -49,7 +50,6 @@ 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.db.layer0.variable.Variables; import org.simantics.g2d.diagram.participant.AbstractDiagramParticipant; import org.simantics.g2d.dnd.DragItem; import org.simantics.g2d.dnd.IDnDContext; @@ -149,7 +149,7 @@ public class SubscriptionDropParticipant extends AbstractDiagramParticipant impl VariableReferenceDragItem vrdi = new VariableReferenceDragItem(session.sync(new UnaryRead((RVI)data) { @Override public VariableReference perform(ReadGraph graph) throws DatabaseException { - return new VariableReference(parameter, Variables.getDatatype(graph, model, parameter), null); + return new VariableReference(parameter, VariableUtils.getDatatype(graph, model, parameter), null); } })); items.add( vrdi ); diff --git a/bundles/org.simantics.charts/src/org/simantics/charts/internal/VariableUtils.java b/bundles/org.simantics.charts/src/org/simantics/charts/internal/VariableUtils.java new file mode 100644 index 000000000..8e321c9b4 --- /dev/null +++ b/bundles/org.simantics.charts/src/org/simantics/charts/internal/VariableUtils.java @@ -0,0 +1,67 @@ +/******************************************************************************* + * Copyright (c) 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.internal; + +import java.util.Collections; +import java.util.List; +import java.util.Optional; + +import org.simantics.databoard.type.Datatype; +import org.simantics.db.ReadGraph; +import org.simantics.db.Resource; +import org.simantics.db.exception.DatabaseException; +import org.simantics.db.layer0.request.PossibleActiveVariableFromVariable; +import org.simantics.db.layer0.variable.RVI; +import org.simantics.db.layer0.variable.Variable; +import org.simantics.db.layer0.variable.VariableReference; +import org.simantics.db.layer0.variable.Variables; +import org.simantics.modeling.utils.VariableReferences; + +/** + * @author Tuukka Lehtonen + * @since 1.28.1, 1.29.0 + */ +public class VariableUtils { + + public static Datatype getDatatype(ReadGraph graph, Resource resource, RVI rvi) throws DatabaseException { + Variable configuration = Variables.getConfigurationContext(graph, resource); + Variable active = graph.syncRequest(new PossibleActiveVariableFromVariable(configuration)); + Variable var = rvi.resolve(graph, active != null ? active : configuration); + return var.getDatatype(graph); + } + + /** + * @param graph + * @param targetModel + * @param source + * @return null if the value is valid JSON but does not contain + * a variable reference + * @throws DatabaseException + * if the value fails to resolve as either JSON or an RVI string + */ + public static List getVariableReferencesFromString(ReadGraph graph, Resource targetModel, String source) throws DatabaseException { + try { + // JSON ? + Optional v = JsonUtils.tryParseJsonPropertyVariable(graph, source); + if (v.isPresent()) + return graph.syncRequest(VariableReferences.variablesToReferences(targetModel, Collections.singletonList(v.get()))); + // JSON, but no variable info in it. + return null; + } catch (DatabaseException e) { + // RVI as String? + RVI rvi = RVI.fromResourceFormat(graph, source); + return Collections.singletonList(new VariableReference(rvi, getDatatype(graph, targetModel, rvi), null)); + } + } + +} diff --git a/bundles/org.simantics.charts/src/org/simantics/charts/ui/ChartDropActionFactory.java b/bundles/org.simantics.charts/src/org/simantics/charts/ui/ChartDropActionFactory.java index 6a3de89a9..7d08d9430 100644 --- a/bundles/org.simantics.charts/src/org/simantics/charts/ui/ChartDropActionFactory.java +++ b/bundles/org.simantics.charts/src/org/simantics/charts/ui/ChartDropActionFactory.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2007, 2014 Association for Decentralized Information Management in + * 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 @@ -8,6 +8,7 @@ * * Contributors: * VTT Technical Research Centre of Finland - initial API and implementation + * Semantum Oy - #7313 *******************************************************************************/ package org.simantics.charts.ui; @@ -16,13 +17,15 @@ import java.util.Collection; import java.util.Collections; import java.util.HashSet; import java.util.List; -import java.util.Optional; 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.internal.JsonUtils; +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; @@ -57,7 +60,7 @@ public class ChartDropActionFactory implements DropActionFactory { @Override public Runnable create(ReadGraph g, Object target, Object source, int operation) throws DatabaseException { - //System.out.println("DROP: " + source + " -> " + target); + //System.out.println("CHART DROP: " + source + " -> " + target); final Resource chart = ISelectionUtils.getSinglePossibleKey(target, SelectionHints.KEY_MAIN, Resource.class); if (chart == null) return null; @@ -66,7 +69,7 @@ public class ChartDropActionFactory implements DropActionFactory { if(source instanceof RVI) { List refs = Collections.singletonList(new VariableReference((RVI)source, - SubscriptionDropActionFactory.getDatatype(g, targetModel, (RVI) source), null)); + VariableUtils.getDatatype(g, targetModel, (RVI) source), null)); return new AddVariableToChartAction(chart, null, refs).init(g); } @@ -132,19 +135,23 @@ public class ChartDropActionFactory implements DropActionFactory { } if (source instanceof String) { - // JSON ? - Optional v = JsonUtils.tryParseJsonPropertyVariable(g, (String) source); - if (v.isPresent()) { - List references = toReferences(g, targetModel, Collections.singletonList(v.get())); - if (!references.isEmpty()) - return new AddVariableToChartAction(chart, null, references).init(g); - } + return handleStringDrop(g, chart, targetModel, (String) source); } return null; } - private static List toReferences(ReadGraph graph, Resource contextIndexRoot, List variables) throws DatabaseException { + private Runnable handleStringDrop(ReadGraph graph, Resource chart, Resource targetModel, String source) { + try { + List 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 toReferences(ReadGraph graph, Resource contextIndexRoot, List variables) throws DatabaseException { if (variables.isEmpty()) return Collections.emptyList(); return filterReferences( graph.syncRequest(VariableReferences.variablesToReferences(contextIndexRoot, variables)) ); diff --git a/bundles/org.simantics.charts/src/org/simantics/charts/ui/SubscriptionDropActionFactory.java b/bundles/org.simantics.charts/src/org/simantics/charts/ui/SubscriptionDropActionFactory.java index ba9415982..5fdaea697 100644 --- a/bundles/org.simantics.charts/src/org/simantics/charts/ui/SubscriptionDropActionFactory.java +++ b/bundles/org.simantics.charts/src/org/simantics/charts/ui/SubscriptionDropActionFactory.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2014 Association for Decentralized Information Management in + * Copyright (c) 2014, 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 @@ -8,19 +8,21 @@ * * Contributors: * VTT Technical Research Centre of Finland - initial API and implementation + * Semantum Oy - #7313 *******************************************************************************/ package org.simantics.charts.ui; import java.util.Collections; import java.util.HashSet; import java.util.List; -import java.util.Optional; import java.util.Set; +import org.eclipse.core.runtime.IStatus; +import org.eclipse.core.runtime.Status; import org.simantics.Simantics; import org.simantics.browsing.ui.common.ErrorLogger; -import org.simantics.charts.internal.JsonUtils; -import org.simantics.databoard.type.Datatype; +import org.simantics.charts.Activator; +import org.simantics.charts.internal.VariableUtils; import org.simantics.databoard.util.ObjectUtils; import org.simantics.db.ReadGraph; import org.simantics.db.Resource; @@ -29,12 +31,9 @@ import org.simantics.db.common.request.WriteRequest; 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.PossibleActiveVariableFromVariable; 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.db.layer0.variable.Variables; import org.simantics.layer0.Layer0; import org.simantics.modeling.ModelingResources; import org.simantics.modeling.PropertyVariables; @@ -49,7 +48,7 @@ public class SubscriptionDropActionFactory implements DropActionFactory { @Override public Runnable create(ReadGraph g, Object target, Object source, int operation) throws DatabaseException { - //System.out.println("DROP: " + source + " -> " + target); + //System.out.println("SUBSCRIPTION DROP: " + source + " -> " + target); final Resource subscription = ISelectionUtils.getSinglePossibleKey(target, SelectionHints.KEY_MAIN, Resource.class); if (subscription == null) @@ -58,7 +57,7 @@ public class SubscriptionDropActionFactory implements DropActionFactory { if(source instanceof RVI) { RVI rvi = (RVI)source; - List refs = Collections.singletonList(new VariableReference(rvi, getDatatype(g, targetModel, rvi), null)); + List refs = Collections.singletonList(new VariableReference(rvi, VariableUtils.getDatatype(g, targetModel, rvi), null)); return addSubscriptions(g, subscription, refs, Collections.emptySet()); } @@ -85,18 +84,23 @@ public class SubscriptionDropActionFactory implements DropActionFactory { } if (source instanceof String) { - // JSON ? - Optional v = JsonUtils.tryParseJsonPropertyVariable(g, (String) source); - if (v.isPresent()) { - List references = g.syncRequest(VariableReferences.variablesToReferences(targetModel, Collections.singletonList(v.get()))); - return addSubscriptions(g, subscription, references, Collections.emptySet()); - } + return handleStringDrop(g, subscription, targetModel, (String) source); } return null; } - private Runnable addSubscriptions(ReadGraph graph, Resource subscription, List references, + private Runnable handleStringDrop(ReadGraph graph, Resource subscription, Resource targetModel, String source) throws DatabaseException { + try { + List refs = VariableUtils.getVariableReferencesFromString(graph, targetModel, source); + return addSubscriptions(graph, subscription, refs, Collections.emptySet()); + } catch (DatabaseException e) { + Activator.getDefault().getLog().log(new Status(IStatus.WARNING, Activator.PLUGIN_ID, getClass().getSimpleName() + ": Unrecognized String input: " + source)); + return null; + } + } + + private Runnable addSubscriptions(ReadGraph graph, Resource subscription, List references, Set movedSubscriptionItems) throws DatabaseException { AddVariableToChartAction action = new AddVariableToChartAction(null, subscription, references).init(graph); return () -> { @@ -119,11 +123,4 @@ public class SubscriptionDropActionFactory implements DropActionFactory { }; } - static Datatype getDatatype(ReadGraph graph, Resource resource, RVI rvi) throws DatabaseException { - Variable configuration = Variables.getConfigurationContext(graph, resource); - Variable active = graph.syncRequest(new PossibleActiveVariableFromVariable(configuration)); - Variable var = rvi.resolve(graph, active != null ? active : configuration); - return var.getDatatype(graph); - } - }