X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=blobdiff_plain;f=bundles%2Forg.simantics.charts%2Fsrc%2Forg%2Fsimantics%2Fcharts%2Fui%2FChartDropActionFactory.java;h=db93f33e42581d165b215b6663900d9aa8f0b190;hp=648336b6770af0023b2818c9ce9e3ae3e8b0c22d;hb=876ede6b867e2e7966ffb46cc69dc820969c4394;hpb=b9450ae725ed484f18cb4a3a44eddc6da01ec9d7;ds=sidebyside 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 648336b67..db93f33e4 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 @@ -13,12 +13,16 @@ 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; @@ -43,7 +47,6 @@ 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.datastructures.collections.CollectionUtils; import org.simantics.utils.ui.ErrorLogger; import org.simantics.utils.ui.ISelectionUtils; @@ -62,71 +65,41 @@ public class ChartDropActionFactory implements DropActionFactory { if (targetModel == null) return null; if(source instanceof RVI) { - - RVI rvi = (RVI)source; - List refs = CollectionUtils.toList(new VariableReference(rvi, SubscriptionDropActionFactory.getDatatype(g, targetModel, rvi), null)); - - AddVariableToChartAction a = new AddVariableToChartAction(chart, null, refs); - a.init(g); - return a; - + List refs = Collections.singletonList(new VariableReference((RVI)source, + SubscriptionDropActionFactory.getDatatype(g, targetModel, (RVI) source), null)); + return new AddVariableToChartAction(chart, null, refs).init(g); } - + List 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 references2 = g.syncRequest(VariableReferences.toReferences(targetModel, vars)); - List references = new ArrayList(); - for (VariableReference ref : references2) { - if (ref.datatype instanceof BooleanType || ref.datatype instanceof NumberType) { - references.add(ref); - } - } - - AddVariableToChartAction a = new AddVariableToChartAction(chart, null, references); - a.init(g); - return a; + List references = toPropertyReferences(g, targetModel, vars); + if (!references.isEmpty()) + return new AddVariableToChartAction(chart, null, references).init(g); } - final List vars2 = ISelectionUtils.getPossibleKeys(source, SelectionHints.KEY_MAIN, Variable.class); + + List vars2 = ISelectionUtils.getPossibleKeys(source, SelectionHints.KEY_MAIN, Variable.class); if (!vars2.isEmpty()) { - // FIXME: this is a hack for indexed value support - List references2 = g.syncRequest(VariableReferences.variablesToReferences(targetModel, vars2)); - List references = new ArrayList(); - for (VariableReference ref : references2) { - if (ref.datatype instanceof BooleanType || ref.datatype instanceof NumberType) { - references.add(ref); - } - } - AddVariableToChartAction a = new AddVariableToChartAction(chart, null, references); - a.init(g); - return a; + List references = toReferences(g, targetModel, vars2); + if (!references.isEmpty()) + return new AddVariableToChartAction(chart, null, references).init(g); } if(source instanceof ISelection) { List wses = ISelectionUtils.filterSelection((ISelection)source, WorkbenchSelectionElement.class); if (!wses.isEmpty()) { - List wsevars = new ArrayList(); + List wsevars = new ArrayList<>(); ChartVariable av = new ChartVariable(g); for(WorkbenchSelectionElement wse : wses) { Variable v = wse.getContent(av); - if(v != null) { + if(v != null) wsevars.add(v); - } } + List references = toReferences(g, targetModel, wsevars); if (!wsevars.isEmpty()) { - // FIXME: this is a hack for indexed value support - List references2 = g.syncRequest(VariableReferences.variablesToReferences(targetModel, wsevars)); - List references = new ArrayList(); - for (VariableReference ref : references2) { - if (ref.datatype instanceof BooleanType || ref.datatype instanceof NumberType) { - references.add(ref); - } - } - AddVariableToChartAction a = new AddVariableToChartAction(chart, null, references); - a.init(g); - return a; + return new AddVariableToChartAction(chart, null, references).init(g); } } } @@ -158,22 +131,48 @@ public class ChartDropActionFactory implements DropActionFactory { return addPlots(chart, newItems, movedPlots); } + 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 null; } - public static Runnable addPlots(final Resource chart, final List references, final Set movedPlots) { - return new Runnable() { - @Override - public void run() { - Simantics.getSession().asyncRequest( - AddChartItem.addAndMoveChartItems(chart, references, movedPlots), - new ProcedureAdapter>() { - @Override - public void exception(Throwable e) { + 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)) ); + } + + private static List toPropertyReferences(ReadGraph graph, Resource contextIndexRoot, List variables) throws DatabaseException { + if (variables.isEmpty()) + return Collections.emptyList(); + return filterReferences( graph.syncRequest(VariableReferences.toReferences(contextIndexRoot, variables)) ); + } + + private static List filterReferences(List 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 references, Set movedPlots) { + return () -> { + Simantics.getSession().asyncRequest( + AddChartItem.addAndMoveChartItems(chart, references, movedPlots), + new ProcedureAdapter>() { + @Override + public void exception(Throwable e) { + if (e != null) ErrorLogger.defaultLogError(e); - } - }); - } + } + }); }; }