Commit 876ede6b authored by Tuukka Lehtonen's avatar Tuukka Lehtonen
Browse files

Sync git svn branch with SVN repository r33406.

parent b9450ae7
......@@ -47,6 +47,7 @@ Require-Bundle: org.eclipse.ui,
org.eclipse.e4.core.commands,
org.eclipse.e4.ui.bindings;bundle-version="0.11.0",
org.eclipse.e4.core.di.annotations,
org.eclipse.e4.core.services
org.eclipse.e4.core.services,
com.fasterxml.jackson.core.jackson-core;bundle-version="2.5.0"
Bundle-RequiredExecutionEnvironment: JavaSE-1.8
Bundle-ActivationPolicy: lazy
/*******************************************************************************
* Copyright (c) 2007, 2011 Association for Decentralized Information Management in
* Copyright (c) 2007, 2016 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
......@@ -8,9 +8,11 @@
*
* Contributors:
* VTT Technical Research Centre of Finland - initial API and implementation
* Semantum Oy - JSON plain text input support
*******************************************************************************/
package org.simantics.charts.editor;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.dnd.DnDConstants;
......@@ -18,13 +20,18 @@ import java.awt.dnd.DropTargetDragEvent;
import java.awt.dnd.DropTargetDropEvent;
import java.awt.dnd.DropTargetEvent;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
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.query.AddChartItem;
import org.simantics.charts.query.ChartItemDescriptor;
import org.simantics.charts.ui.AddVariableToChartAction;
......@@ -33,7 +40,9 @@ import org.simantics.charts.ui.ChartVariable;
import org.simantics.databoard.util.ObjectUtils;
import org.simantics.db.ReadGraph;
import org.simantics.db.Resource;
import org.simantics.db.Session;
import org.simantics.db.common.request.UnaryRead;
import org.simantics.db.common.request.UniqueRead;
import org.simantics.db.exception.DatabaseException;
import org.simantics.db.layer0.SelectionHints;
import org.simantics.db.layer0.request.PossibleModel;
......@@ -41,7 +50,6 @@ 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.db.request.Read;
import org.simantics.g2d.diagram.participant.AbstractDiagramParticipant;
import org.simantics.g2d.dnd.DragItem;
import org.simantics.g2d.dnd.IDnDContext;
......@@ -54,6 +62,7 @@ import org.simantics.modeling.utils.VariableReferences;
import org.simantics.ui.dnd.LocalObjectTransfer;
import org.simantics.ui.dnd.LocalObjectTransferable;
import org.simantics.ui.selection.WorkbenchSelectionElement;
import org.simantics.utils.FileUtils;
import org.simantics.utils.ui.ErrorLogger;
import org.simantics.utils.ui.ISelectionUtils;
import org.simantics.utils.ui.dialogs.ShowMessage;
......@@ -88,12 +97,6 @@ public class SubscriptionDropParticipant extends AbstractDiagramParticipant impl
@Override
public void dragEnter(DropTargetDragEvent dtde, IDnDContext dp) {
// The transferable doesn't know LOT
if (!dtde.isDataFlavorSupported(LocalObjectTransferable.FLAVOR)) {
dtde.rejectDrag();
return;
}
// The source cannot link, too bad
if ((dtde.getSourceActions() & DnDConstants.ACTION_LINK) == 0) {
dtde.rejectDrag();
......@@ -101,26 +104,34 @@ public class SubscriptionDropParticipant extends AbstractDiagramParticipant impl
}
// Ensure the content is usable
if (dtde.isDataFlavorSupported(LocalObjectTransferable.FLAVOR)) {
dragEnterLocalObject(dtde, dp);
} else if (dtde.isDataFlavorSupported(DataFlavor.getTextPlainUnicodeFlavor())) {
dragEnterPlainText(dtde, dp);
} else {
dtde.rejectDrag();
}
}
private void dragEnterLocalObject(DropTargetDragEvent dtde, IDnDContext dp) {
try {
Transferable t = dtde.getTransferable();
Object data = t.getTransferData(LocalObjectTransferable.FLAVOR);
Object data = dtde.getTransferable().getTransferData(LocalObjectTransferable.FLAVOR);
data = LocalObjectTransfer.getTransfer().getObject();
List<IDragItem> items = new ArrayList<IDragItem>();
List<IDragItem> items = new ArrayList<>();
Session session = Simantics.getSession();
final List<Resource> resources = ISelectionUtils.getPossibleKeys(data, SelectionHints.KEY_MAIN, Resource.class);
if (!resources.isEmpty()) {
// Support SubscriptionItem drags
items.addAll( Simantics.getSession().syncRequest(new Read<List<IDragItem>>() {
items.addAll( session.syncRequest(new UniqueRead<List<IDragItem>>() {
@Override
public List<IDragItem> perform(ReadGraph graph) throws DatabaseException {
List<IDragItem> result = new ArrayList<IDragItem>();
//Layer0 L0 = Layer0.getInstance(graph);
List<IDragItem> result = new ArrayList<>();
ModelingResources MOD = ModelingResources.getInstance(graph);
Resource targetModel = graph.syncRequest(new PossibleModel(container));
if (targetModel != null) {
for (Resource r : resources) {
//System.out.println( graph.getPossibleRelatedValue(r, L0.HasName) );
if (graph.isInstanceOf(r, MOD.Subscription_Item)) {
Resource model = graph.syncRequest(new PossibleModel(r));
if (ObjectUtils.objectEquals(targetModel, model))
......@@ -135,7 +146,7 @@ public class SubscriptionDropParticipant extends AbstractDiagramParticipant impl
if(data instanceof RVI) {
VariableReferenceDragItem vrdi = new VariableReferenceDragItem(Simantics.getSession().sync(new UnaryRead<RVI, VariableReference>((RVI)data) {
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);
......@@ -150,7 +161,7 @@ public class SubscriptionDropParticipant extends AbstractDiagramParticipant impl
// 1st try Variable
final List<Variable> vars2 = ISelectionUtils.getPossibleKeys(data, SelectionHints.KEY_MAIN, Variable.class);
if (!vars2.isEmpty()) {
varItems = Simantics.getSession().syncRequest( new Read<List<IDragItem>>() {
varItems = session.syncRequest( new UniqueRead<List<IDragItem>>() {
@Override
public List<IDragItem> perform(ReadGraph graph) throws DatabaseException {
return toDragItems( graph.syncRequest(VariableReferences.variablesToReferences(model, vars2)) );
......@@ -161,7 +172,7 @@ public class SubscriptionDropParticipant extends AbstractDiagramParticipant impl
// Try legacy PropertyVariables
final List<PropertyVariables> vars = ISelectionUtils.getPossibleKeys(data, SelectionHints.KEY_MAIN, PropertyVariables.class);
if (!vars.isEmpty()) {
varItems = Simantics.getSession().syncRequest( new Read<List<IDragItem>>() {
varItems = session.syncRequest( new UniqueRead<List<IDragItem>>() {
@Override
public List<IDragItem> perform(ReadGraph graph) throws DatabaseException {
List<PropertyVariables> vars2 = PropertyVariablesImpl.resolve(graph, vars);
......@@ -177,10 +188,10 @@ public class SubscriptionDropParticipant extends AbstractDiagramParticipant impl
if (data instanceof ISelection) {
final List<WorkbenchSelectionElement> wses = ISelectionUtils.filterSelection((ISelection)data, WorkbenchSelectionElement.class);
if (!wses.isEmpty()) {
items.addAll( Simantics.getSession().syncRequest( new Read<List<IDragItem>>() {
items.addAll( session.syncRequest( new UniqueRead<List<IDragItem>>() {
@Override
public List<IDragItem> perform(ReadGraph graph) throws DatabaseException {
List<Variable> wsevars = new ArrayList<Variable>();
List<Variable> wsevars = new ArrayList<>();
ChartVariable av = new ChartVariable(graph);
for(WorkbenchSelectionElement wse : wses) {
Variable v = wse.getContent(av);
......@@ -214,6 +225,40 @@ public class SubscriptionDropParticipant extends AbstractDiagramParticipant impl
}
}
private void dragEnterPlainText(DropTargetDragEvent dtde, IDnDContext dp) {
try {
DataFlavor flavor = DataFlavor.getTextPlainUnicodeFlavor();
String flavorCharset = flavor.getParameter("charset");
Transferable t = dtde.getTransferable();
InputStream in = (InputStream) t.getTransferData(flavor);
String data = FileUtils.getContents(in, Charset.forName(flavorCharset));
List<IDragItem> items = new ArrayList<>();
Session session = Simantics.getSession();
Optional<Variable> v = JsonUtils.tryParseJsonPropertyVariable(session, data);
if (v.isPresent()) {
items.addAll( toDragItems( session.syncRequest(VariableReferences.variablesToReferences(model, Collections.singletonList(v.get()))) ) );
}
if (items.isEmpty()) {
dtde.rejectDrag();
} else {
// Accept, make sure it is Link
for (IDragItem i : items)
dp.add(i);
dtde.acceptDrag( DnDConstants.ACTION_LINK );
}
} catch (UnsupportedFlavorException e) {
throw new RuntimeException(e);
} catch (IOException e) {
ErrorLogger.defaultLogError(e);
dtde.rejectDrag();
} catch (DatabaseException e) {
ErrorLogger.defaultLogError(e);
dtde.rejectDrag();
}
}
@Override
public void dragExit(DropTargetEvent dte, IDnDContext dp) {
for (IDragItem i : dp.getItemsByClass(SubscriptionItemDragItem.class))
......@@ -228,30 +273,23 @@ public class SubscriptionDropParticipant extends AbstractDiagramParticipant impl
@Override
public void drop(DropTargetDropEvent dtde, IDnDContext dp) {
// Subscription Item
// Subscription Item
Collection<SubscriptionItemDragItem> subs = dp.getItemsByClass(SubscriptionItemDragItem.class);
if (!subs.isEmpty()) {
List<ChartItemDescriptor> cicr = new ArrayList<ChartItemDescriptor>();
for (SubscriptionItemDragItem sidi : subs) {
cicr.add(sidi.getObject());
}
ChartDropActionFactory.addPlots(container, cicr, Collections.<Resource>emptySet()).run();
ChartDropActionFactory.addPlots(container,
subs.stream().map(DragItem::getObject).collect(Collectors.toList()),
Collections.<Resource>emptySet()).run();
dtde.dropComplete(true);
return;
}
// Variable Reference
Collection<VariableReferenceDragItem> vrdis = dp.getItemsByClass(VariableReferenceDragItem.class);
if (!vrdis.isEmpty()) {
List<VariableReference> refs = new ArrayList<VariableReference>();
for (VariableReferenceDragItem vrdi : vrdis) {
refs.add( vrdi.getObject() );
}
Resource chart = container;
try {
AddVariableToChartAction a = new AddVariableToChartAction( chart, null, refs );
a.init();
a.run();
new AddVariableToChartAction( container, null,
vrdis.stream().map(DragItem::getObject).collect(Collectors.toList()) )
.init().run();
dtde.dropComplete(true);
} catch (DatabaseException e) {
ShowMessage.showError(e.getClass().getName(), e.getMessage());
......@@ -259,7 +297,7 @@ public class SubscriptionDropParticipant extends AbstractDiagramParticipant impl
}
return;
}
dtde.rejectDrop();
}
......@@ -274,14 +312,9 @@ public class SubscriptionDropParticipant extends AbstractDiagramParticipant impl
}
private static List<IDragItem> toDragItems(Collection<VariableReference> references) {
List<IDragItem> result = new ArrayList<IDragItem>(references.size());
for (VariableReference vr : references) {
VariableReferenceDragItem di = new VariableReferenceDragItem( vr );
result.add( di );
}
return result;
return references.stream().map(VariableReferenceDragItem::new).collect(Collectors.toList());
}
@Override
public double getPriority() {
return 10.0;
......
package org.simantics.charts.internal;
import java.io.IOException;
import java.util.Optional;
import org.simantics.db.ReadGraph;
import org.simantics.db.RequestProcessor;
import org.simantics.db.common.request.UniqueRead;
import org.simantics.db.exception.DatabaseException;
import org.simantics.db.layer0.variable.Variable;
import org.simantics.db.layer0.variable.Variables;
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;
/**
* @author Tuukka Lehtonen
*
*/
public class JsonUtils {
public static Optional<Variable> tryParseJsonPropertyVariable(RequestProcessor processor, String json) throws DatabaseException {
return processor.syncRequest(new UniqueRead<Optional<Variable>>() {
@Override
public Optional<Variable> perform(ReadGraph graph) throws DatabaseException {
return tryParseJsonPropertyVariable(graph, json);
}
});
}
public static Optional<Variable> tryParseJsonPropertyVariable(ReadGraph graph, String json) throws DatabaseException {
try (JsonParser jp = new JsonFactory().createParser(json)) {
return JsonUtils.readPossibleVariable(graph, jp);
} catch (IOException e) {
throw new DatabaseException(e);
}
}
public static Optional<Variable> readPossibleVariable(ReadGraph graph, JsonParser jp) throws IOException, DatabaseException {
Optional<String> uri = readPossibleVariableUri(jp);
return uri.isPresent() ? Optional.ofNullable(Variables.getPossibleVariable(graph, uri.get())) : Optional.empty();
}
public static Optional<String> readPossibleVariableUri(JsonParser jp) throws IOException {
// Sanity check: verify that we got "Json Object":
if (jp.nextToken() != JsonToken.START_OBJECT)
throw new IOException("Expected data to start with an Object");
String uri = null;
String type = null;
String defaultPropertyUri = null;
while (jp.nextToken() != JsonToken.END_OBJECT) {
String fieldName = jp.getCurrentName();
jp.nextToken();
if (fieldName.equals("uri")) {
uri = jp.getValueAsString();
} else if (fieldName.equals("type")) {
type = jp.getValueAsString();
} else if (fieldName.equals("defaultPropertyUri")) {
defaultPropertyUri = jp.getValueAsString();
}
}
return Optional.ofNullable("Variable".equals(type) ?
defaultPropertyUri != null ? defaultPropertyUri : uri
: null);
}
}
......@@ -65,7 +65,7 @@ public class AddVariableToChartAction implements Runnable {
*
* @throws DatabaseException
*/
public void init() throws DatabaseException
public AddVariableToChartAction init() throws DatabaseException
{
Simantics.getSession().sync( new ReadRequest() {
@Override
......@@ -73,6 +73,7 @@ public class AddVariableToChartAction implements Runnable {
init(g);
}
} );
return this;
}
/**
......@@ -80,7 +81,7 @@ public class AddVariableToChartAction implements Runnable {
*
* @throws DatabaseException
*/
public void init( ReadGraph g ) throws DatabaseException
public AddVariableToChartAction init( ReadGraph g ) throws DatabaseException
{
Layer0 L0 = Layer0.getInstance(g);
if(chart != null) {
......@@ -103,6 +104,8 @@ public class AddVariableToChartAction implements Runnable {
variableReferences.add( var.getVariableId().toPossibleString(g, configuration) );
}
}
return this;
}
/**
......
......@@ -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<VariableReference> 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<VariableReference> refs = Collections.singletonList(new VariableReference((RVI)source,
SubscriptionDropActionFactory.getDatatype(g, targetModel, (RVI) source), null));
return new AddVariableToChartAction(chart, null, refs).init(g);
}
List<PropertyVariables> 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<VariableReference> references2 = g.syncRequest(VariableReferences.toReferences(targetModel, vars));
List<VariableReference> references = new ArrayList<VariableReference>();
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<VariableReference> references = toPropertyReferences(g, targetModel, vars);
if (!references.isEmpty())
return new AddVariableToChartAction(chart, null, references).init(g);
}
final List<Variable> vars2 = ISelectionUtils.getPossibleKeys(source, SelectionHints.KEY_MAIN, Variable.class);
List<Variable> vars2 = ISelectionUtils.getPossibleKeys(source, SelectionHints.KEY_MAIN, Variable.class);
if (!vars2.isEmpty()) {
// FIXME: this is a hack for indexed value support
List<VariableReference> references2 = g.syncRequest(VariableReferences.variablesToReferences(targetModel, vars2));
List<VariableReference> references = new ArrayList<VariableReference>();
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<VariableReference> references = toReferences(g, targetModel, vars2);
if (!references.isEmpty())
return new AddVariableToChartAction(chart, null, references).init(g);
}
if(source instanceof ISelection) {
List<WorkbenchSelectionElement> wses = ISelectionUtils.filterSelection((ISelection)source, WorkbenchSelectionElement.class);
if (!wses.isEmpty()) {
List<Variable> wsevars = new ArrayList<Variable>();
List<Variable> 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<VariableReference> references = toReferences(g, targetModel, wsevars);
if (!wsevars.isEmpty()) {
// FIXME: this is a hack for indexed value support
List<VariableReference> references2 = g.syncRequest(VariableReferences.variablesToReferences(targetModel, wsevars));
List<VariableReference> references = new ArrayList<VariableReference>();
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<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 null;
}
public static Runnable addPlots(final Resource chart, final List<ChartItemDescriptor> references, final Set<Resource> movedPlots) {
return new Runnable() {
@Override
public void run() {
Simantics.getSession().asyncRequest(
AddChartItem.addAndMoveChartItems(chart, references, movedPlots),
new ProcedureAdapter<Collection<Resource>>() {
@Override
public void exception(Throwable e) {
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)) );
}
private static List<VariableReference> toPropertyReferences(ReadGraph graph, Resource contextIndexRoot, List<PropertyVariables> variables) throws DatabaseException {
if (variables.isEmpty())
return Collections.emptyList();
return filterReferences( graph.syncRequest(VariableReferences.toReferences(contextIndexRoot, variables)) );
}
private static List<VariableReference> filterReferences(List<VariableReference> 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<ChartItemDescriptor> references, Set<Resource> movedPlots) {
return () -> {
Simantics.getSession().asyncRequest(
AddChartItem.addAndMoveChartItems(chart, references, movedPlots),
new ProcedureAdapter<Collection<Resource>>() {
@Override
public void exception(Throwable e) {
if (e != null)
ErrorLogger.defaultLogError(e);
}
});
}
}
});
};
}
......
......@@ -14,10 +14,12 @@ 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;
......@@ -38,8 +40,6 @@ import org.simantics.modeling.ModelingResources;
import org.simantics.modeling.PropertyVariables;
<