/******************************************************************************* * 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.HashSet; import java.util.List; import java.util.Set; import org.eclipse.jface.viewers.ISelection; import org.simantics.Simantics; 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.datastructures.collections.CollectionUtils; 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) { 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 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; } final 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; } 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); } } 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; } } } { 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); } 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) { ErrorLogger.defaultLogError(e); } }); } }; } }