]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.charts/src/org/simantics/charts/ui/ChartDropActionFactory.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.charts / src / org / simantics / charts / ui / ChartDropActionFactory.java
index 648336b6770af0023b2818c9ce9e3ae3e8b0c22d..6a3de89a90700a47ea86a298c911ddac66588396 100644 (file)
-/*******************************************************************************\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.HashSet;\r
-import java.util.List;\r
-import java.util.Set;\r
-\r
-import org.eclipse.jface.viewers.ISelection;\r
-import org.simantics.Simantics;\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.datastructures.collections.CollectionUtils;\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
-\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
-        }\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
-        }\r
-        final 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
-        }\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
-                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
-\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
-                }\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
-        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
-                                ErrorLogger.defaultLogError(e);\r
-                            }\r
-                        });\r
-            }\r
-        };\r
-    }\r
-\r
+/*******************************************************************************
+ * Copyright (c) 2007, 2014 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
+ *******************************************************************************/
+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.Optional;
+import java.util.Set;
+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.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("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,
+                    SubscriptionDropActionFactory.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) {
+            // 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 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