/*******************************************************************************
- * Copyright (c) 2007, 2016 Association for Decentralized Information Management in
+ * Copyright (c) 2007, 2017 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
*
* Contributors:
* VTT Technical Research Centre of Finland - initial API and implementation
- * Semantum Oy - JSON plain text input support
+ * Semantum Oy - JSON plain text input support, #7313
*******************************************************************************/
package org.simantics.charts.editor;
import org.eclipse.jface.viewers.ISelection;
import org.simantics.Simantics;
import org.simantics.charts.internal.JsonUtils;
+import org.simantics.charts.internal.VariableUtils;
import org.simantics.charts.query.AddChartItem;
import org.simantics.charts.query.ChartItemDescriptor;
import org.simantics.charts.ui.AddVariableToChartAction;
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.g2d.diagram.participant.AbstractDiagramParticipant;
import org.simantics.g2d.dnd.DragItem;
import org.simantics.g2d.dnd.IDnDContext;
VariableReferenceDragItem vrdi = new VariableReferenceDragItem(session.sync(new UnaryRead<RVI, VariableReference>((RVI)data) {
@Override
public VariableReference perform(ReadGraph graph) throws DatabaseException {
- return new VariableReference(parameter, Variables.getDatatype(graph, model, parameter), null);
+ return new VariableReference(parameter, VariableUtils.getDatatype(graph, model, parameter), null);
}
}));
items.add( vrdi );
--- /dev/null
+/*******************************************************************************
+ * Copyright (c) 2017 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
+ * Semantum Oy - #7313
+ *******************************************************************************/
+package org.simantics.charts.internal;
+
+import java.util.Collections;
+import java.util.List;
+import java.util.Optional;
+
+import org.simantics.databoard.type.Datatype;
+import org.simantics.db.ReadGraph;
+import org.simantics.db.Resource;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.layer0.request.PossibleActiveVariableFromVariable;
+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.modeling.utils.VariableReferences;
+
+/**
+ * @author Tuukka Lehtonen
+ * @since 1.28.1, 1.29.0
+ */
+public class VariableUtils {
+
+ public 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);
+ }
+
+ /**
+ * @param graph
+ * @param targetModel
+ * @param source
+ * @return <code>null</code> if the value is valid JSON but does not contain
+ * a variable reference
+ * @throws DatabaseException
+ * if the value fails to resolve as either JSON or an RVI string
+ */
+ public static List<VariableReference> getVariableReferencesFromString(ReadGraph graph, Resource targetModel, String source) throws DatabaseException {
+ try {
+ // JSON ?
+ Optional<Variable> v = JsonUtils.tryParseJsonPropertyVariable(graph, source);
+ if (v.isPresent())
+ return graph.syncRequest(VariableReferences.variablesToReferences(targetModel, Collections.singletonList(v.get())));
+ // JSON, but no variable info in it.
+ return null;
+ } catch (DatabaseException e) {
+ // RVI as String?
+ RVI rvi = RVI.fromResourceFormat(graph, source);
+ return Collections.singletonList(new VariableReference(rvi, getDatatype(graph, targetModel, rvi), null));
+ }
+ }
+
+}
/*******************************************************************************
- * Copyright (c) 2007, 2014 Association for Decentralized Information Management in
+ * Copyright (c) 2007, 2017 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
*
* Contributors:
* VTT Technical Research Centre of Finland - initial API and implementation
+ * Semantum Oy - #7313
*******************************************************************************/
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 java.util.stream.Collectors;
+import org.eclipse.core.runtime.IStatus;
+import org.eclipse.core.runtime.Status;
import org.eclipse.jface.viewers.ISelection;
import org.simantics.Simantics;
-import org.simantics.charts.internal.JsonUtils;
+import org.simantics.charts.Activator;
+import org.simantics.charts.internal.VariableUtils;
import org.simantics.charts.ontology.ChartResource;
import org.simantics.charts.query.AddChartItem;
import org.simantics.charts.query.ChartItemDescriptor;
@Override
public Runnable create(ReadGraph g, Object target, Object source, int operation) throws DatabaseException {
- //System.out.println("DROP: " + source + " -> " + target);
+ //System.out.println("CHART DROP: " + source + " -> " + target);
final Resource chart = ISelectionUtils.getSinglePossibleKey(target, SelectionHints.KEY_MAIN, Resource.class);
if (chart == null) return null;
if(source instanceof RVI) {
List<VariableReference> refs = Collections.singletonList(new VariableReference((RVI)source,
- SubscriptionDropActionFactory.getDatatype(g, targetModel, (RVI) source), null));
+ VariableUtils.getDatatype(g, targetModel, (RVI) source), null));
return new AddVariableToChartAction(chart, null, refs).init(g);
}
}
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 handleStringDrop(g, chart, targetModel, (String) source);
}
return null;
}
- private static List<VariableReference> toReferences(ReadGraph graph, Resource contextIndexRoot, List<Variable> variables) throws DatabaseException {
+ private Runnable handleStringDrop(ReadGraph graph, Resource chart, Resource targetModel, String source) {
+ try {
+ List<VariableReference> refs = VariableUtils.getVariableReferencesFromString(graph, targetModel, source);
+ return new AddVariableToChartAction(chart, null, refs).init(graph);
+ } catch (DatabaseException e) {
+ Activator.getDefault().getLog().log(new Status(IStatus.WARNING, Activator.PLUGIN_ID, getClass().getSimpleName() + ": Unrecognized String input: " + source));
+ 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)) );
/*******************************************************************************
- * Copyright (c) 2014 Association for Decentralized Information Management in
+ * Copyright (c) 2014, 2017 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
*
* Contributors:
* VTT Technical Research Centre of Finland - initial API and implementation
+ * Semantum Oy - #7313
*******************************************************************************/
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.eclipse.core.runtime.IStatus;
+import org.eclipse.core.runtime.Status;
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.charts.Activator;
+import org.simantics.charts.internal.VariableUtils;
import org.simantics.databoard.util.ObjectUtils;
import org.simantics.db.ReadGraph;
import org.simantics.db.Resource;
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;
@Override
public Runnable create(ReadGraph g, Object target, Object source, int operation) throws DatabaseException {
- //System.out.println("DROP: " + source + " -> " + target);
+ //System.out.println("SUBSCRIPTION DROP: " + source + " -> " + target);
final Resource subscription = ISelectionUtils.getSinglePossibleKey(target, SelectionHints.KEY_MAIN, Resource.class);
if (subscription == null)
if(source instanceof RVI) {
RVI rvi = (RVI)source;
- List<VariableReference> refs = Collections.singletonList(new VariableReference(rvi, getDatatype(g, targetModel, rvi), null));
+ List<VariableReference> refs = Collections.singletonList(new VariableReference(rvi, VariableUtils.getDatatype(g, targetModel, rvi), null));
return addSubscriptions(g, subscription, refs, Collections.<Resource>emptySet());
}
}
if (source instanceof String) {
- // JSON ?
- Optional<Variable> v = JsonUtils.tryParseJsonPropertyVariable(g, (String) source);
- if (v.isPresent()) {
- List<VariableReference> references = g.syncRequest(VariableReferences.variablesToReferences(targetModel, Collections.singletonList(v.get())));
- return addSubscriptions(g, subscription, references, Collections.emptySet());
- }
+ return handleStringDrop(g, subscription, targetModel, (String) source);
}
return null;
}
- private Runnable addSubscriptions(ReadGraph graph, Resource subscription, List<VariableReference> references,
+ private Runnable handleStringDrop(ReadGraph graph, Resource subscription, Resource targetModel, String source) throws DatabaseException {
+ try {
+ List<VariableReference> refs = VariableUtils.getVariableReferencesFromString(graph, targetModel, source);
+ return addSubscriptions(graph, subscription, refs, Collections.emptySet());
+ } catch (DatabaseException e) {
+ Activator.getDefault().getLog().log(new Status(IStatus.WARNING, Activator.PLUGIN_ID, getClass().getSimpleName() + ": Unrecognized String input: " + source));
+ return null;
+ }
+ }
+
+ private Runnable addSubscriptions(ReadGraph graph, Resource subscription, List<VariableReference> references,
Set<Resource> movedSubscriptionItems) throws DatabaseException {
AddVariableToChartAction action = new AddVariableToChartAction(null, subscription, references).init(graph);
return () -> {
};
}
- 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);
- }
-
}