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=6a3de89a90700a47ea86a298c911ddac66588396;hp=db93f33e42581d165b215b6663900d9aa8f0b190;hb=0ae2b770234dfc3cbb18bd38f324125cf0faca07;hpb=777f66ad0fc587dd8d313705442dd2338c65ad30 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 db93f33e4..6a3de89a9 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 @@ -1,179 +1,179 @@ -/******************************************************************************* - * 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 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 references = toPropertyReferences(g, targetModel, vars); - if (!references.isEmpty()) - return new AddVariableToChartAction(chart, null, references).init(g); - } - - List vars2 = ISelectionUtils.getPossibleKeys(source, SelectionHints.KEY_MAIN, Variable.class); - if (!vars2.isEmpty()) { - 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<>(); - ChartVariable av = new ChartVariable(g); - for(WorkbenchSelectionElement wse : wses) { - Variable v = wse.getContent(av); - if(v != null) - wsevars.add(v); - } - - List references = toReferences(g, targetModel, wsevars); - if (!wsevars.isEmpty()) { - return new AddVariableToChartAction(chart, null, references).init(g); - } - } - } - { - List srcs = ISelectionUtils.getPossibleKeys(source, SelectionHints.KEY_MAIN, Resource.class); - ModelingResources MOD = ModelingResources.getInstance(g); - ChartResource CHART = ChartResource.getInstance(g); - List newItems = new ArrayList(); - Set movedPlots = new HashSet(); - 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 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; - } - - 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); - } - }); - }; - } - +/******************************************************************************* + * 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 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 references = toPropertyReferences(g, targetModel, vars); + if (!references.isEmpty()) + return new AddVariableToChartAction(chart, null, references).init(g); + } + + List vars2 = ISelectionUtils.getPossibleKeys(source, SelectionHints.KEY_MAIN, Variable.class); + if (!vars2.isEmpty()) { + 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<>(); + ChartVariable av = new ChartVariable(g); + for(WorkbenchSelectionElement wse : wses) { + Variable v = wse.getContent(av); + if(v != null) + wsevars.add(v); + } + + List references = toReferences(g, targetModel, wsevars); + if (!wsevars.isEmpty()) { + return new AddVariableToChartAction(chart, null, references).init(g); + } + } + } + { + List srcs = ISelectionUtils.getPossibleKeys(source, SelectionHints.KEY_MAIN, Resource.class); + ModelingResources MOD = ModelingResources.getInstance(g); + ChartResource CHART = ChartResource.getInstance(g); + List newItems = new ArrayList(); + Set movedPlots = new HashSet(); + 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 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; + } + + 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); + } + }); + }; + } + } \ No newline at end of file