]> gerrit.simantics Code Review - simantics/platform.git/commitdiff
Fixed diagram profile monitor DnD regression. 35/635/1
authorTuukka Lehtonen <tuukka.lehtonen@semantum.fi>
Fri, 16 Jun 2017 09:34:43 +0000 (12:34 +0300)
committerTuukka Lehtonen <tuukka.lehtonen@semantum.fi>
Fri, 16 Jun 2017 10:04:08 +0000 (13:04 +0300)
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)

bundles/org.simantics.charts/src/org/simantics/charts/editor/SubscriptionDropParticipant.java
bundles/org.simantics.charts/src/org/simantics/charts/internal/VariableUtils.java [new file with mode: 0644]
bundles/org.simantics.charts/src/org/simantics/charts/ui/ChartDropActionFactory.java
bundles/org.simantics.charts/src/org/simantics/charts/ui/SubscriptionDropActionFactory.java

index 861d220ea701638d80d606d7f1a8912b94e52601..9b08eacbc26d297c1ecf6ac90c7b6fb5a00b90be 100644 (file)
@@ -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, VariableReference>((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 (file)
index 0000000..8e321c9
--- /dev/null
@@ -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 <code>null</code> 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<VariableReference> getVariableReferencesFromString(ReadGraph graph, Resource targetModel, String source) throws DatabaseException {
+        try {
+            // JSON ?
+            Optional<Variable> 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));
+        }
+    }
+
+}
index 6a3de89a90700a47ea86a298c911ddac66588396..7d08d943035d773060678e17d34ce859f107ebf8 100644 (file)
@@ -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<VariableReference> 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<Variable> v = JsonUtils.tryParseJsonPropertyVariable(g, (String) source);
-            if (v.isPresent()) {
-                List<VariableReference> 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<VariableReference> toReferences(ReadGraph graph, Resource contextIndexRoot, List<Variable> variables) throws DatabaseException {
+    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)) );
index ba94159821a4e1afc4b095b1e8c7960d897ab43d..5fdaea69710509293cbd6daa9a4bf77a8ca4b972 100644 (file)
@@ -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<VariableReference> refs = Collections.singletonList(new VariableReference(rvi, getDatatype(g, targetModel, rvi), null));
+            List<VariableReference> refs = Collections.singletonList(new VariableReference(rvi, VariableUtils.getDatatype(g, targetModel, rvi), null));
             return addSubscriptions(g, subscription, refs, Collections.<Resource>emptySet());
         }
 
@@ -85,18 +84,23 @@ public class SubscriptionDropActionFactory implements DropActionFactory {
         }
 
         if (source instanceof String) {
-            // JSON ?
-            Optional<Variable> v = JsonUtils.tryParseJsonPropertyVariable(g, (String) source);
-            if (v.isPresent()) {
-                List<VariableReference> 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<VariableReference> references,
+    private Runnable handleStringDrop(ReadGraph graph, Resource subscription, Resource targetModel, String source) throws DatabaseException {
+        try {
+            List<VariableReference> 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<VariableReference> references,
             Set<Resource> 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);
-    }
-
 }