]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.charts/src/org/simantics/charts/ui/ChartDropActionFactory.java
Fixed diagram profile monitor DnD regression.
[simantics/platform.git] / bundles / org.simantics.charts / src / org / simantics / charts / ui / ChartDropActionFactory.java
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)) );