Sync git svn branch with SVN repository r33406.
[simantics/platform.git] / bundles / org.simantics.charts / src / org / simantics / charts / ui / ChartDropActionFactory.java
index 648336b6770af0023b2818c9ce9e3ae3e8b0c22d..db93f33e42581d165b215b6663900d9aa8f0b190 100644 (file)
@@ -13,12 +13,16 @@ package org.simantics.charts.ui;
 \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
@@ -43,7 +47,6 @@ import org.simantics.modeling.PropertyVariablesImpl;
 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.datastructures.collections.CollectionUtils;\r
 import org.simantics.utils.ui.ErrorLogger;\r
 import org.simantics.utils.ui.ISelectionUtils;\r
 \r
@@ -62,71 +65,41 @@ public class ChartDropActionFactory implements DropActionFactory {
         if (targetModel == null) return null;\r
 \r
         if(source instanceof RVI) {\r
-\r
-               RVI rvi = (RVI)source;\r
-               List<VariableReference> refs = CollectionUtils.toList(new VariableReference(rvi, SubscriptionDropActionFactory.getDatatype(g, targetModel, rvi), null));\r
-               \r
-               AddVariableToChartAction a = new AddVariableToChartAction(chart, null, refs);\r
-               a.init(g);\r
-               return a;\r
-               \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
+\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> references2 = g.syncRequest(VariableReferences.toReferences(targetModel, vars));\r
-            List<VariableReference> references = new ArrayList<VariableReference>();\r
-            for (VariableReference ref : references2) {\r
-               if (ref.datatype instanceof BooleanType || ref.datatype instanceof NumberType) {\r
-                       references.add(ref);\r
-               }\r
-            }\r
-            \r
-               AddVariableToChartAction a = new AddVariableToChartAction(chart, null, references);\r
-               a.init(g);\r
-               return a;\r
+            List<VariableReference> references = toPropertyReferences(g, targetModel, vars);\r
+            if (!references.isEmpty())\r
+                return new AddVariableToChartAction(chart, null, references).init(g);\r
         }\r
-        final List<Variable> vars2 = ISelectionUtils.getPossibleKeys(source, SelectionHints.KEY_MAIN, Variable.class);\r
+\r
+        List<Variable> vars2 = ISelectionUtils.getPossibleKeys(source, SelectionHints.KEY_MAIN, Variable.class);\r
         if (!vars2.isEmpty()) {\r
-            // FIXME: this is a hack for indexed value support\r
-            List<VariableReference> references2 = g.syncRequest(VariableReferences.variablesToReferences(targetModel, vars2));\r
-            List<VariableReference> references = new ArrayList<VariableReference>();\r
-            for (VariableReference ref : references2) {\r
-                if (ref.datatype instanceof BooleanType || ref.datatype instanceof NumberType) {\r
-                    references.add(ref);\r
-                }\r
-            }\r
-            AddVariableToChartAction a = new AddVariableToChartAction(chart, null, references);\r
-            a.init(g);\r
-            return a;\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<Variable>();\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
+                    if(v != null)\r
                         wsevars.add(v);\r
-                    }\r
                 }\r
 \r
+                List<VariableReference> references = toReferences(g, targetModel, wsevars);\r
                 if (!wsevars.isEmpty()) {\r
-                    // FIXME: this is a hack for indexed value support\r
-                    List<VariableReference> references2 = g.syncRequest(VariableReferences.variablesToReferences(targetModel, wsevars));\r
-                    List<VariableReference> references = new ArrayList<VariableReference>();\r
-                    for (VariableReference ref : references2) {\r
-                        if (ref.datatype instanceof BooleanType || ref.datatype instanceof NumberType) {\r
-                            references.add(ref);\r
-                        }\r
-                    }\r
-                    AddVariableToChartAction a = new AddVariableToChartAction(chart, null, references);\r
-                    a.init(g);\r
-                    return a;\r
+                    return new AddVariableToChartAction(chart, null, references).init(g);\r
                 }\r
             }\r
         }\r
@@ -158,22 +131,48 @@ public class ChartDropActionFactory implements DropActionFactory {
                 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
-    public static Runnable addPlots(final Resource chart, final List<ChartItemDescriptor> references, final Set<Resource> movedPlots) {\r
-        return new Runnable() {\r
-            @Override\r
-            public void run() {\r
-                Simantics.getSession().asyncRequest(\r
-                        AddChartItem.addAndMoveChartItems(chart, references, movedPlots),\r
-                        new ProcedureAdapter<Collection<Resource>>() {\r
-                            @Override\r
-                            public void exception(Throwable e) {\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
         };\r
     }\r
 \r