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%2FSubscriptionDropActionFactory.java;fp=bundles%2Forg.simantics.charts%2Fsrc%2Forg%2Fsimantics%2Fcharts%2Fui%2FSubscriptionDropActionFactory.java;h=ba94159821a4e1afc4b095b1e8c7960d897ab43d;hp=ee3975d9515975a7c4f514830d162260221a14ab;hb=0ae2b770234dfc3cbb18bd38f324125cf0faca07;hpb=24e2b34260f219f0d1644ca7a138894980e25b14 diff --git a/bundles/org.simantics.charts/src/org/simantics/charts/ui/SubscriptionDropActionFactory.java b/bundles/org.simantics.charts/src/org/simantics/charts/ui/SubscriptionDropActionFactory.java index ee3975d95..ba9415982 100644 --- a/bundles/org.simantics.charts/src/org/simantics/charts/ui/SubscriptionDropActionFactory.java +++ b/bundles/org.simantics.charts/src/org/simantics/charts/ui/SubscriptionDropActionFactory.java @@ -1,129 +1,129 @@ -/******************************************************************************* - * Copyright (c) 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.Collections; -import java.util.HashSet; -import java.util.List; -import java.util.Optional; -import java.util.Set; - -import org.simantics.Simantics; -import org.simantics.browsing.ui.common.ErrorLogger; -import org.simantics.charts.internal.JsonUtils; -import org.simantics.databoard.type.Datatype; -import org.simantics.databoard.util.ObjectUtils; -import org.simantics.db.ReadGraph; -import org.simantics.db.Resource; -import org.simantics.db.WriteGraph; -import org.simantics.db.common.request.WriteRequest; -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.PossibleActiveVariableFromVariable; -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.db.layer0.variable.Variables; -import org.simantics.layer0.Layer0; -import org.simantics.modeling.ModelingResources; -import org.simantics.modeling.PropertyVariables; -import org.simantics.modeling.PropertyVariablesImpl; -import org.simantics.modeling.utils.VariableReferences; -import org.simantics.utils.ui.ISelectionUtils; - -/** - * @author Tuukka Lehtonen - */ -public class SubscriptionDropActionFactory implements DropActionFactory { - - @Override - public Runnable create(ReadGraph g, Object target, Object source, int operation) throws DatabaseException { - //System.out.println("DROP: " + source + " -> " + target); - - final Resource subscription = ISelectionUtils.getSinglePossibleKey(target, SelectionHints.KEY_MAIN, Resource.class); - if (subscription == null) - return null; - Resource targetModel = g.syncRequest(new PossibleModel(subscription)); - - if(source instanceof RVI) { - RVI rvi = (RVI)source; - List refs = Collections.singletonList(new VariableReference(rvi, getDatatype(g, targetModel, rvi), null)); - return addSubscriptions(g, subscription, refs, Collections.emptySet()); - } - - 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 = g.syncRequest(VariableReferences.toReferences(targetModel, vars)); - if (!references.isEmpty()) - return addSubscriptions(g, subscription, references, Collections.emptySet()); - } else { - List srcs = ISelectionUtils.getPossibleKeys(source, SelectionHints.KEY_MAIN, Resource.class); - ModelingResources MOD = ModelingResources.getInstance(g); - Set movedItems = new HashSet<>(); - for (Resource src : srcs) { - if (g.isInstanceOf(src, MOD.Subscription_Item)) { - Resource model = g.syncRequest(new PossibleModel(src)); - if (ObjectUtils.objectEquals(targetModel, model)) - movedItems.add(src); - } - } - if (!movedItems.isEmpty()) - return addSubscriptions(g, subscription, Collections.emptyList(), movedItems); - } - - if (source instanceof String) { - // JSON ? - Optional v = JsonUtils.tryParseJsonPropertyVariable(g, (String) source); - if (v.isPresent()) { - List references = g.syncRequest(VariableReferences.variablesToReferences(targetModel, Collections.singletonList(v.get()))); - return addSubscriptions(g, subscription, references, Collections.emptySet()); - } - } - - return null; - } - - private Runnable addSubscriptions(ReadGraph graph, Resource subscription, List references, - Set movedSubscriptionItems) throws DatabaseException { - AddVariableToChartAction action = new AddVariableToChartAction(null, subscription, references).init(graph); - return () -> { - action.run(); - if(!movedSubscriptionItems.isEmpty()) { - Simantics.getSession().asyncRequest(new WriteRequest() { - @Override - public void perform(WriteGraph graph) throws DatabaseException { - Layer0 L0 = Layer0.getInstance(graph); - for (Resource item : movedSubscriptionItems) { - graph.deny(item, L0.PartOf); - graph.claim(subscription, L0.ConsistsOf, item); - } - } - }, e -> { - if (e != null) - ErrorLogger.defaultLogError(e); - }); - } - }; - } - - static Datatype getDatatype(ReadGraph graph, Resource resource, RVI rvi) throws DatabaseException { - Variable configuration = Variables.getConfigurationContext(graph, resource); - Variable active = graph.syncRequest(new PossibleActiveVariableFromVariable(configuration)); - Variable var = rvi.resolve(graph, active != null ? active : configuration); - return var.getDatatype(graph); - } - -} +/******************************************************************************* + * Copyright (c) 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.Collections; +import java.util.HashSet; +import java.util.List; +import java.util.Optional; +import java.util.Set; + +import org.simantics.Simantics; +import org.simantics.browsing.ui.common.ErrorLogger; +import org.simantics.charts.internal.JsonUtils; +import org.simantics.databoard.type.Datatype; +import org.simantics.databoard.util.ObjectUtils; +import org.simantics.db.ReadGraph; +import org.simantics.db.Resource; +import org.simantics.db.WriteGraph; +import org.simantics.db.common.request.WriteRequest; +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.PossibleActiveVariableFromVariable; +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.db.layer0.variable.Variables; +import org.simantics.layer0.Layer0; +import org.simantics.modeling.ModelingResources; +import org.simantics.modeling.PropertyVariables; +import org.simantics.modeling.PropertyVariablesImpl; +import org.simantics.modeling.utils.VariableReferences; +import org.simantics.utils.ui.ISelectionUtils; + +/** + * @author Tuukka Lehtonen + */ +public class SubscriptionDropActionFactory implements DropActionFactory { + + @Override + public Runnable create(ReadGraph g, Object target, Object source, int operation) throws DatabaseException { + //System.out.println("DROP: " + source + " -> " + target); + + final Resource subscription = ISelectionUtils.getSinglePossibleKey(target, SelectionHints.KEY_MAIN, Resource.class); + if (subscription == null) + return null; + Resource targetModel = g.syncRequest(new PossibleModel(subscription)); + + if(source instanceof RVI) { + RVI rvi = (RVI)source; + List refs = Collections.singletonList(new VariableReference(rvi, getDatatype(g, targetModel, rvi), null)); + return addSubscriptions(g, subscription, refs, Collections.emptySet()); + } + + 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 = g.syncRequest(VariableReferences.toReferences(targetModel, vars)); + if (!references.isEmpty()) + return addSubscriptions(g, subscription, references, Collections.emptySet()); + } else { + List srcs = ISelectionUtils.getPossibleKeys(source, SelectionHints.KEY_MAIN, Resource.class); + ModelingResources MOD = ModelingResources.getInstance(g); + Set movedItems = new HashSet<>(); + for (Resource src : srcs) { + if (g.isInstanceOf(src, MOD.Subscription_Item)) { + Resource model = g.syncRequest(new PossibleModel(src)); + if (ObjectUtils.objectEquals(targetModel, model)) + movedItems.add(src); + } + } + if (!movedItems.isEmpty()) + return addSubscriptions(g, subscription, Collections.emptyList(), movedItems); + } + + if (source instanceof String) { + // JSON ? + Optional v = JsonUtils.tryParseJsonPropertyVariable(g, (String) source); + if (v.isPresent()) { + List references = g.syncRequest(VariableReferences.variablesToReferences(targetModel, Collections.singletonList(v.get()))); + return addSubscriptions(g, subscription, references, Collections.emptySet()); + } + } + + return null; + } + + private Runnable addSubscriptions(ReadGraph graph, Resource subscription, List references, + Set movedSubscriptionItems) throws DatabaseException { + AddVariableToChartAction action = new AddVariableToChartAction(null, subscription, references).init(graph); + return () -> { + action.run(); + if(!movedSubscriptionItems.isEmpty()) { + Simantics.getSession().asyncRequest(new WriteRequest() { + @Override + public void perform(WriteGraph graph) throws DatabaseException { + Layer0 L0 = Layer0.getInstance(graph); + for (Resource item : movedSubscriptionItems) { + graph.deny(item, L0.PartOf); + graph.claim(subscription, L0.ConsistsOf, item); + } + } + }, e -> { + if (e != null) + ErrorLogger.defaultLogError(e); + }); + } + }; + } + + static Datatype getDatatype(ReadGraph graph, Resource resource, RVI rvi) throws DatabaseException { + Variable configuration = Variables.getConfigurationContext(graph, resource); + Variable active = graph.syncRequest(new PossibleActiveVariableFromVariable(configuration)); + Variable var = rvi.resolve(graph, active != null ? active : configuration); + return var.getDatatype(graph); + } + +}