]> gerrit.simantics Code Review - simantics/platform.git/commitdiff
Added support for JSON PlainTextTransferable to charts and subscriptions 86/186/2
authorTuukka Lehtonen <tuukka.lehtonen@semantum.fi>
Wed, 30 Nov 2016 13:48:51 +0000 (15:48 +0200)
committerTuukka Lehtonen <tuukka.lehtonen@semantum.fi>
Wed, 30 Nov 2016 13:57:21 +0000 (16:57 +0300)
With these changes dragging JSON text using
org.simantics.ui.dnd.PlainTextTransfer on top of model browser charts
and subscriptions and also on a chart editor is now supported.

The dragged JSON data must be in the following format:
<pre>
{
    "type" : "variable",
    "uri" : "variable space URI to dragged entity",
    "defaultPropertyUri" : "variable space URI to default referenced
property variable of the dragged entity"
}
</pre>

Only "type" and "defaultPropertyUri" keys are required.

refs #6844

Change-Id: I3684a879a6c80bbff812166d14aa28eac5244663

bundles/org.simantics.charts/META-INF/MANIFEST.MF
bundles/org.simantics.charts/src/org/simantics/charts/editor/SubscriptionDropParticipant.java
bundles/org.simantics.charts/src/org/simantics/charts/internal/JsonUtils.java [new file with mode: 0644]
bundles/org.simantics.charts/src/org/simantics/charts/ui/AddVariableToChartAction.java
bundles/org.simantics.charts/src/org/simantics/charts/ui/ChartDropActionFactory.java
bundles/org.simantics.charts/src/org/simantics/charts/ui/SubscriptionDropActionFactory.java
bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/diagramEditor/DiagramViewerSelectionProvider.java
features/org.simantics.charts.feature/feature.xml

index a0c30d5897775cbd0da4d80a4e6965e16c306351..3d289e419098332f1f6126f5d5835bb41454ddbf 100644 (file)
@@ -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.8.2"
 Bundle-RequiredExecutionEnvironment: JavaSE-1.8
 Bundle-ActivationPolicy: lazy
index c410414d2276a3b2930a00d4938b5b2f6563a837..c41070934cbebe63344c49fbe8e640313bfdc6c5 100644 (file)
@@ -1,5 +1,5 @@
 /*******************************************************************************\r
- * Copyright (c) 2007, 2011 Association for Decentralized Information Management in\r
+ * Copyright (c) 2007, 2016 Association for Decentralized Information Management in\r
  * Industry THTH ry.\r
  * All rights reserved. This program and the accompanying materials\r
  * are made available under the terms of the Eclipse Public License v1.0\r
@@ -8,9 +8,11 @@
  *\r
  * Contributors:\r
  *     VTT Technical Research Centre of Finland - initial API and implementation\r
+ *     Semantum Oy - JSON plain text input support\r
  *******************************************************************************/\r
 package org.simantics.charts.editor;\r
 \r
+import java.awt.datatransfer.DataFlavor;\r
 import java.awt.datatransfer.Transferable;\r
 import java.awt.datatransfer.UnsupportedFlavorException;\r
 import java.awt.dnd.DnDConstants;\r
@@ -18,13 +20,18 @@ import java.awt.dnd.DropTargetDragEvent;
 import java.awt.dnd.DropTargetDropEvent;\r
 import java.awt.dnd.DropTargetEvent;\r
 import java.io.IOException;\r
+import java.io.InputStream;\r
+import java.nio.charset.Charset;\r
 import java.util.ArrayList;\r
 import java.util.Collection;\r
 import java.util.Collections;\r
 import java.util.List;\r
+import java.util.Optional;\r
+import java.util.stream.Collectors;\r
 \r
 import org.eclipse.jface.viewers.ISelection;\r
 import org.simantics.Simantics;\r
+import org.simantics.charts.internal.JsonUtils;\r
 import org.simantics.charts.query.AddChartItem;\r
 import org.simantics.charts.query.ChartItemDescriptor;\r
 import org.simantics.charts.ui.AddVariableToChartAction;\r
@@ -33,7 +40,9 @@ import org.simantics.charts.ui.ChartVariable;
 import org.simantics.databoard.util.ObjectUtils;\r
 import org.simantics.db.ReadGraph;\r
 import org.simantics.db.Resource;\r
+import org.simantics.db.Session;\r
 import org.simantics.db.common.request.UnaryRead;\r
+import org.simantics.db.common.request.UniqueRead;\r
 import org.simantics.db.exception.DatabaseException;\r
 import org.simantics.db.layer0.SelectionHints;\r
 import org.simantics.db.layer0.request.PossibleModel;\r
@@ -41,7 +50,6 @@ import org.simantics.db.layer0.variable.RVI;
 import org.simantics.db.layer0.variable.Variable;\r
 import org.simantics.db.layer0.variable.VariableReference;\r
 import org.simantics.db.layer0.variable.Variables;\r
-import org.simantics.db.request.Read;\r
 import org.simantics.g2d.diagram.participant.AbstractDiagramParticipant;\r
 import org.simantics.g2d.dnd.DragItem;\r
 import org.simantics.g2d.dnd.IDnDContext;\r
@@ -54,6 +62,7 @@ import org.simantics.modeling.utils.VariableReferences;
 import org.simantics.ui.dnd.LocalObjectTransfer;\r
 import org.simantics.ui.dnd.LocalObjectTransferable;\r
 import org.simantics.ui.selection.WorkbenchSelectionElement;\r
+import org.simantics.utils.FileUtils;\r
 import org.simantics.utils.ui.ErrorLogger;\r
 import org.simantics.utils.ui.ISelectionUtils;\r
 import org.simantics.utils.ui.dialogs.ShowMessage;\r
@@ -88,12 +97,6 @@ public class SubscriptionDropParticipant extends AbstractDiagramParticipant impl
 \r
     @Override\r
     public void dragEnter(DropTargetDragEvent dtde, IDnDContext dp) {\r
-        // The transferable doesn't know LOT\r
-        if (!dtde.isDataFlavorSupported(LocalObjectTransferable.FLAVOR)) {\r
-            dtde.rejectDrag();\r
-            return;\r
-        }\r
-\r
         // The source cannot link, too bad\r
         if ((dtde.getSourceActions() & DnDConstants.ACTION_LINK) == 0) {\r
             dtde.rejectDrag();\r
@@ -101,26 +104,34 @@ public class SubscriptionDropParticipant extends AbstractDiagramParticipant impl
         }\r
 \r
         // Ensure the content is usable\r
+        if (dtde.isDataFlavorSupported(LocalObjectTransferable.FLAVOR)) {\r
+            dragEnterLocalObject(dtde, dp);\r
+        } else if (dtde.isDataFlavorSupported(DataFlavor.getTextPlainUnicodeFlavor())) {\r
+            dragEnterPlainText(dtde, dp);\r
+        } else {\r
+            dtde.rejectDrag();\r
+        }\r
+    }\r
+\r
+    private void dragEnterLocalObject(DropTargetDragEvent dtde, IDnDContext dp) {\r
         try {\r
-            Transferable t = dtde.getTransferable();\r
-            Object data = t.getTransferData(LocalObjectTransferable.FLAVOR);\r
+            Object data = dtde.getTransferable().getTransferData(LocalObjectTransferable.FLAVOR);\r
             data = LocalObjectTransfer.getTransfer().getObject();\r
 \r
-            List<IDragItem> items = new ArrayList<IDragItem>();\r
+            List<IDragItem> items = new ArrayList<>();\r
+            Session session = Simantics.getSession();\r
 \r
             final List<Resource> resources = ISelectionUtils.getPossibleKeys(data, SelectionHints.KEY_MAIN, Resource.class);\r
             if (!resources.isEmpty()) {\r
                 // Support SubscriptionItem drags\r
-                items.addAll( Simantics.getSession().syncRequest(new Read<List<IDragItem>>() {\r
+                items.addAll( session.syncRequest(new UniqueRead<List<IDragItem>>() {\r
                     @Override\r
                     public List<IDragItem> perform(ReadGraph graph) throws DatabaseException {\r
-                        List<IDragItem> result = new ArrayList<IDragItem>();\r
-                        //Layer0 L0 = Layer0.getInstance(graph);\r
+                        List<IDragItem> result = new ArrayList<>();\r
                         ModelingResources MOD = ModelingResources.getInstance(graph);\r
                         Resource targetModel = graph.syncRequest(new PossibleModel(container));\r
                         if (targetModel != null) {\r
                             for (Resource r : resources) {\r
-                               //System.out.println( graph.getPossibleRelatedValue(r, L0.HasName) );\r
                                 if (graph.isInstanceOf(r, MOD.Subscription_Item)) {\r
                                     Resource model = graph.syncRequest(new PossibleModel(r));\r
                                     if (ObjectUtils.objectEquals(targetModel, model))\r
@@ -135,7 +146,7 @@ public class SubscriptionDropParticipant extends AbstractDiagramParticipant impl
 \r
             if(data instanceof RVI) {\r
 \r
-                VariableReferenceDragItem vrdi = new VariableReferenceDragItem(Simantics.getSession().sync(new UnaryRead<RVI, VariableReference>((RVI)data) {\r
+                VariableReferenceDragItem vrdi = new VariableReferenceDragItem(session.sync(new UnaryRead<RVI, VariableReference>((RVI)data) {\r
                     @Override\r
                     public VariableReference perform(ReadGraph graph) throws DatabaseException {\r
                         return new VariableReference(parameter, Variables.getDatatype(graph, model, parameter), null);\r
@@ -150,7 +161,7 @@ public class SubscriptionDropParticipant extends AbstractDiagramParticipant impl
                 // 1st try Variable\r
                 final List<Variable> vars2 = ISelectionUtils.getPossibleKeys(data, SelectionHints.KEY_MAIN, Variable.class);\r
                 if (!vars2.isEmpty()) {\r
-                    varItems = Simantics.getSession().syncRequest( new Read<List<IDragItem>>() {\r
+                    varItems = session.syncRequest( new UniqueRead<List<IDragItem>>() {\r
                         @Override\r
                         public List<IDragItem> perform(ReadGraph graph) throws DatabaseException {\r
                             return toDragItems( graph.syncRequest(VariableReferences.variablesToReferences(model, vars2)) );\r
@@ -161,7 +172,7 @@ public class SubscriptionDropParticipant extends AbstractDiagramParticipant impl
                     // Try legacy PropertyVariables\r
                     final List<PropertyVariables> vars = ISelectionUtils.getPossibleKeys(data, SelectionHints.KEY_MAIN, PropertyVariables.class);\r
                     if (!vars.isEmpty()) {\r
-                        varItems = Simantics.getSession().syncRequest( new Read<List<IDragItem>>() {\r
+                        varItems = session.syncRequest( new UniqueRead<List<IDragItem>>() {\r
                             @Override\r
                             public List<IDragItem> perform(ReadGraph graph) throws DatabaseException {\r
                                 List<PropertyVariables> vars2 = PropertyVariablesImpl.resolve(graph, vars);\r
@@ -177,10 +188,10 @@ public class SubscriptionDropParticipant extends AbstractDiagramParticipant impl
                 if (data instanceof ISelection) {\r
                     final List<WorkbenchSelectionElement> wses = ISelectionUtils.filterSelection((ISelection)data, WorkbenchSelectionElement.class);\r
                     if (!wses.isEmpty()) {\r
-                        items.addAll( Simantics.getSession().syncRequest( new Read<List<IDragItem>>() {\r
+                        items.addAll( session.syncRequest( new UniqueRead<List<IDragItem>>() {\r
                             @Override\r
                             public List<IDragItem> perform(ReadGraph graph) throws DatabaseException {\r
-                                List<Variable> wsevars = new ArrayList<Variable>();\r
+                                List<Variable> wsevars = new ArrayList<>();\r
                                 ChartVariable av = new ChartVariable(graph);\r
                                 for(WorkbenchSelectionElement wse : wses) {\r
                                     Variable v = wse.getContent(av);\r
@@ -214,6 +225,40 @@ public class SubscriptionDropParticipant extends AbstractDiagramParticipant impl
         }\r
     }\r
 \r
+    private void dragEnterPlainText(DropTargetDragEvent dtde, IDnDContext dp) {\r
+        try {\r
+            DataFlavor flavor = DataFlavor.getTextPlainUnicodeFlavor();\r
+            String flavorCharset = flavor.getParameter("charset");\r
+            Transferable t = dtde.getTransferable();\r
+            InputStream in = (InputStream) t.getTransferData(flavor);\r
+            String data = FileUtils.getContents(in, Charset.forName(flavorCharset));\r
+\r
+            List<IDragItem> items = new ArrayList<>();\r
+            Session session = Simantics.getSession();\r
+            Optional<Variable> v = JsonUtils.tryParseJsonPropertyVariable(session, data);\r
+            if (v.isPresent()) {\r
+                items.addAll( toDragItems( session.syncRequest(VariableReferences.variablesToReferences(model, Collections.singletonList(v.get()))) ) );\r
+            }\r
+\r
+            if (items.isEmpty()) {\r
+                dtde.rejectDrag();\r
+            } else {\r
+                // Accept, make sure it is Link\r
+                for (IDragItem i : items)\r
+                    dp.add(i);\r
+                dtde.acceptDrag( DnDConstants.ACTION_LINK );\r
+            }\r
+        } catch (UnsupportedFlavorException e) {\r
+            throw new RuntimeException(e);\r
+        } catch (IOException e) {\r
+            ErrorLogger.defaultLogError(e);\r
+            dtde.rejectDrag();\r
+        } catch (DatabaseException e) {\r
+            ErrorLogger.defaultLogError(e);\r
+            dtde.rejectDrag();\r
+        }\r
+    }\r
+\r
     @Override\r
     public void dragExit(DropTargetEvent dte, IDnDContext dp) {\r
         for (IDragItem i : dp.getItemsByClass(SubscriptionItemDragItem.class))\r
@@ -228,30 +273,23 @@ public class SubscriptionDropParticipant extends AbstractDiagramParticipant impl
 \r
     @Override\r
     public void drop(DropTargetDropEvent dtde, IDnDContext dp) {\r
-       // Subscription Item\r
+        // Subscription Item\r
         Collection<SubscriptionItemDragItem> subs = dp.getItemsByClass(SubscriptionItemDragItem.class);\r
         if (!subs.isEmpty()) {\r
-            List<ChartItemDescriptor> cicr = new ArrayList<ChartItemDescriptor>();\r
-            for (SubscriptionItemDragItem sidi : subs) {\r
-                cicr.add(sidi.getObject());\r
-            }\r
-            ChartDropActionFactory.addPlots(container, cicr, Collections.<Resource>emptySet()).run();\r
+            ChartDropActionFactory.addPlots(container,\r
+                    subs.stream().map(DragItem::getObject).collect(Collectors.toList()),\r
+                    Collections.<Resource>emptySet()).run();\r
             dtde.dropComplete(true);\r
             return;\r
         }\r
-        \r
+\r
         // Variable Reference\r
         Collection<VariableReferenceDragItem> vrdis = dp.getItemsByClass(VariableReferenceDragItem.class);\r
         if (!vrdis.isEmpty()) {\r
-            List<VariableReference> refs = new ArrayList<VariableReference>();\r
-            for (VariableReferenceDragItem vrdi : vrdis) {\r
-                refs.add( vrdi.getObject() );\r
-            }\r
-            Resource chart = container;\r
             try {\r
-                AddVariableToChartAction a = new AddVariableToChartAction( chart, null, refs );\r
-                               a.init();\r
-                   a.run();\r
+                new AddVariableToChartAction( container, null,\r
+                        vrdis.stream().map(DragItem::getObject).collect(Collectors.toList()) )\r
+                .init().run();\r
                    dtde.dropComplete(true);\r
                        } catch (DatabaseException e) {\r
                                ShowMessage.showError(e.getClass().getName(), e.getMessage());\r
@@ -259,7 +297,7 @@ public class SubscriptionDropParticipant extends AbstractDiagramParticipant impl
                        }\r
             return;\r
         }\r
-        \r
+\r
         dtde.rejectDrop();\r
     }\r
 \r
@@ -274,14 +312,9 @@ public class SubscriptionDropParticipant extends AbstractDiagramParticipant impl
     }\r
 \r
     private static List<IDragItem> toDragItems(Collection<VariableReference> references) {\r
-        List<IDragItem> result = new ArrayList<IDragItem>(references.size());\r
-        for (VariableReference vr : references) {\r
-            VariableReferenceDragItem di = new VariableReferenceDragItem( vr );\r
-            result.add( di );\r
-        }\r
-        return result;\r
+        return references.stream().map(VariableReferenceDragItem::new).collect(Collectors.toList());\r
     }\r
-    \r
+\r
     @Override\r
     public double getPriority() {\r
        return 10.0;\r
diff --git a/bundles/org.simantics.charts/src/org/simantics/charts/internal/JsonUtils.java b/bundles/org.simantics.charts/src/org/simantics/charts/internal/JsonUtils.java
new file mode 100644 (file)
index 0000000..31861ea
--- /dev/null
@@ -0,0 +1,71 @@
+package org.simantics.charts.internal;\r
+\r
+import java.io.IOException;\r
+import java.util.Optional;\r
+\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.RequestProcessor;\r
+import org.simantics.db.common.request.UniqueRead;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.layer0.variable.Variable;\r
+import org.simantics.db.layer0.variable.Variables;\r
+\r
+import com.fasterxml.jackson.core.JsonFactory;\r
+import com.fasterxml.jackson.core.JsonParser;\r
+import com.fasterxml.jackson.core.JsonToken;\r
+\r
+/**\r
+ * @author Tuukka Lehtonen\r
+ *\r
+ */\r
+public class JsonUtils {\r
+\r
+    public static Optional<Variable> tryParseJsonPropertyVariable(RequestProcessor processor, String json) throws DatabaseException {\r
+        return processor.syncRequest(new UniqueRead<Optional<Variable>>() {\r
+            @Override\r
+            public Optional<Variable> perform(ReadGraph graph) throws DatabaseException {\r
+                return tryParseJsonPropertyVariable(graph, json);\r
+            }\r
+        });\r
+    }\r
+\r
+    public static Optional<Variable> tryParseJsonPropertyVariable(ReadGraph graph, String json) throws DatabaseException {\r
+        try (JsonParser jp = new JsonFactory().createParser(json)) {\r
+            return JsonUtils.readPossibleVariable(graph, jp);\r
+        } catch (IOException e) {\r
+            throw new DatabaseException(e);\r
+        }\r
+    }\r
+\r
+    public static Optional<Variable> readPossibleVariable(ReadGraph graph, JsonParser jp) throws IOException, DatabaseException {\r
+        Optional<String> uri = readPossibleVariableUri(jp);\r
+        return uri.isPresent() ? Optional.ofNullable(Variables.getPossibleVariable(graph, uri.get())) : Optional.empty();\r
+    }\r
+\r
+    public static Optional<String> readPossibleVariableUri(JsonParser jp) throws IOException {\r
+        // Sanity check: verify that we got "Json Object":\r
+        if (jp.nextToken() != JsonToken.START_OBJECT)\r
+            throw new IOException("Expected data to start with an Object");\r
+\r
+        String uri = null;\r
+        String type = null;\r
+        String defaultPropertyUri = null;\r
+\r
+        while (jp.nextToken() != JsonToken.END_OBJECT) {\r
+            String fieldName = jp.getCurrentName();\r
+            jp.nextToken();\r
+            if (fieldName.equals("uri")) {\r
+                uri = jp.getValueAsString();\r
+            } else if (fieldName.equals("type")) {\r
+                type = jp.getValueAsString();\r
+            } else if (fieldName.equals("defaultPropertyUri")) {\r
+                defaultPropertyUri = jp.getValueAsString();\r
+            }\r
+        }\r
+\r
+        return Optional.ofNullable("Variable".equals(type) ?\r
+                defaultPropertyUri != null ? defaultPropertyUri : uri\r
+                        : null);\r
+    }\r
+\r
+}\r
index 994624a8b1169d82adc3b0589f2968539a578905..4caa80a66037275d2cb8a364075c185496b41901 100644 (file)
@@ -65,7 +65,7 @@ public class AddVariableToChartAction implements Runnable {
         * \r
         * @throws DatabaseException\r
         */\r
-       public void init() throws DatabaseException \r
+       public AddVariableToChartAction init() throws DatabaseException \r
        {\r
        Simantics.getSession().sync( new ReadRequest() {\r
                        @Override\r
@@ -73,6 +73,7 @@ public class AddVariableToChartAction implements Runnable {
                                init(g);\r
                        }\r
                } );\r
+       return this;\r
        }\r
 \r
        /**\r
@@ -80,7 +81,7 @@ public class AddVariableToChartAction implements Runnable {
         * \r
         * @throws DatabaseException\r
         */\r
-       public void init( ReadGraph g ) throws DatabaseException \r
+       public AddVariableToChartAction init( ReadGraph g ) throws DatabaseException \r
        {\r
        Layer0 L0 = Layer0.getInstance(g);\r
        if(chart != null) {\r
@@ -103,6 +104,8 @@ public class AddVariableToChartAction implements Runnable {
                                variableReferences.add( var.getVariableId().toPossibleString(g, configuration) );\r
                        }\r
                }\r
+\r
+               return this;\r
        }\r
        \r
        /**\r
index 648336b6770af0023b2818c9ce9e3ae3e8b0c22d..db93f33e42581d165b215b6663900d9aa8f0b190 100644 (file)
@@ -13,12 +13,16 @@ package org.simantics.charts.ui;
 \r
 import java.util.ArrayList;\r
 import java.util.Collection;\r
+import java.util.Collections;\r
 import java.util.HashSet;\r
 import java.util.List;\r
+import java.util.Optional;\r
 import java.util.Set;\r
+import java.util.stream.Collectors;\r
 \r
 import org.eclipse.jface.viewers.ISelection;\r
 import org.simantics.Simantics;\r
+import org.simantics.charts.internal.JsonUtils;\r
 import org.simantics.charts.ontology.ChartResource;\r
 import org.simantics.charts.query.AddChartItem;\r
 import org.simantics.charts.query.ChartItemDescriptor;\r
@@ -43,7 +47,6 @@ import org.simantics.modeling.PropertyVariablesImpl;
 import org.simantics.modeling.utils.VariableReferences;\r
 import org.simantics.trend.configuration.TrendItem.Renderer;\r
 import org.simantics.ui.selection.WorkbenchSelectionElement;\r
-import org.simantics.utils.datastructures.collections.CollectionUtils;\r
 import org.simantics.utils.ui.ErrorLogger;\r
 import org.simantics.utils.ui.ISelectionUtils;\r
 \r
@@ -62,71 +65,41 @@ public class ChartDropActionFactory implements DropActionFactory {
         if (targetModel == null) return null;\r
 \r
         if(source instanceof RVI) {\r
-\r
-               RVI rvi = (RVI)source;\r
-               List<VariableReference> refs = CollectionUtils.toList(new VariableReference(rvi, SubscriptionDropActionFactory.getDatatype(g, targetModel, rvi), null));\r
-               \r
-               AddVariableToChartAction a = new AddVariableToChartAction(chart, null, refs);\r
-               a.init(g);\r
-               return a;\r
-               \r
+            List<VariableReference> refs = Collections.singletonList(new VariableReference((RVI)source,\r
+                    SubscriptionDropActionFactory.getDatatype(g, targetModel, (RVI) source), null));\r
+            return new AddVariableToChartAction(chart, null, refs).init(g);\r
         }\r
-        \r
+\r
         List<PropertyVariables> vars = ISelectionUtils.getPossibleKeys(source, SelectionHints.KEY_MAIN, PropertyVariables.class);\r
         if (!vars.isEmpty()) {\r
             // FIXME: this is a hack for indexed value support\r
             vars = PropertyVariablesImpl.resolve(g, vars);\r
-            List<VariableReference> references2 = g.syncRequest(VariableReferences.toReferences(targetModel, vars));\r
-            List<VariableReference> references = new ArrayList<VariableReference>();\r
-            for (VariableReference ref : references2) {\r
-               if (ref.datatype instanceof BooleanType || ref.datatype instanceof NumberType) {\r
-                       references.add(ref);\r
-               }\r
-            }\r
-            \r
-               AddVariableToChartAction a = new AddVariableToChartAction(chart, null, references);\r
-               a.init(g);\r
-               return a;\r
+            List<VariableReference> references = toPropertyReferences(g, targetModel, vars);\r
+            if (!references.isEmpty())\r
+                return new AddVariableToChartAction(chart, null, references).init(g);\r
         }\r
-        final List<Variable> vars2 = ISelectionUtils.getPossibleKeys(source, SelectionHints.KEY_MAIN, Variable.class);\r
+\r
+        List<Variable> vars2 = ISelectionUtils.getPossibleKeys(source, SelectionHints.KEY_MAIN, Variable.class);\r
         if (!vars2.isEmpty()) {\r
-            // FIXME: this is a hack for indexed value support\r
-            List<VariableReference> references2 = g.syncRequest(VariableReferences.variablesToReferences(targetModel, vars2));\r
-            List<VariableReference> references = new ArrayList<VariableReference>();\r
-            for (VariableReference ref : references2) {\r
-                if (ref.datatype instanceof BooleanType || ref.datatype instanceof NumberType) {\r
-                    references.add(ref);\r
-                }\r
-            }\r
-            AddVariableToChartAction a = new AddVariableToChartAction(chart, null, references);\r
-            a.init(g);\r
-            return a;\r
+            List<VariableReference> references = toReferences(g, targetModel, vars2);\r
+            if (!references.isEmpty())\r
+                return new AddVariableToChartAction(chart, null, references).init(g);\r
         }\r
 \r
         if(source instanceof ISelection) {\r
             List<WorkbenchSelectionElement> wses = ISelectionUtils.filterSelection((ISelection)source, WorkbenchSelectionElement.class);\r
             if (!wses.isEmpty()) {\r
-                List<Variable> wsevars = new ArrayList<Variable>();\r
+                List<Variable> wsevars = new ArrayList<>();\r
                 ChartVariable av = new ChartVariable(g);\r
                 for(WorkbenchSelectionElement wse : wses) {\r
                     Variable v = wse.getContent(av);\r
-                    if(v != null) {\r
+                    if(v != null)\r
                         wsevars.add(v);\r
-                    }\r
                 }\r
 \r
+                List<VariableReference> references = toReferences(g, targetModel, wsevars);\r
                 if (!wsevars.isEmpty()) {\r
-                    // FIXME: this is a hack for indexed value support\r
-                    List<VariableReference> references2 = g.syncRequest(VariableReferences.variablesToReferences(targetModel, wsevars));\r
-                    List<VariableReference> references = new ArrayList<VariableReference>();\r
-                    for (VariableReference ref : references2) {\r
-                        if (ref.datatype instanceof BooleanType || ref.datatype instanceof NumberType) {\r
-                            references.add(ref);\r
-                        }\r
-                    }\r
-                    AddVariableToChartAction a = new AddVariableToChartAction(chart, null, references);\r
-                    a.init(g);\r
-                    return a;\r
+                    return new AddVariableToChartAction(chart, null, references).init(g);\r
                 }\r
             }\r
         }\r
@@ -158,22 +131,48 @@ public class ChartDropActionFactory implements DropActionFactory {
                 return addPlots(chart, newItems, movedPlots);\r
         }\r
 \r
+        if (source instanceof String) {\r
+            // JSON ?\r
+            Optional<Variable> v = JsonUtils.tryParseJsonPropertyVariable(g, (String) source);\r
+            if (v.isPresent()) {\r
+                List<VariableReference> references = toReferences(g, targetModel, Collections.singletonList(v.get()));\r
+                if (!references.isEmpty())\r
+                    return new AddVariableToChartAction(chart, null, references).init(g);\r
+            }\r
+        }\r
+\r
         return null;\r
     }\r
 \r
-    public static Runnable addPlots(final Resource chart, final List<ChartItemDescriptor> references, final Set<Resource> movedPlots) {\r
-        return new Runnable() {\r
-            @Override\r
-            public void run() {\r
-                Simantics.getSession().asyncRequest(\r
-                        AddChartItem.addAndMoveChartItems(chart, references, movedPlots),\r
-                        new ProcedureAdapter<Collection<Resource>>() {\r
-                            @Override\r
-                            public void exception(Throwable e) {\r
+    private static List<VariableReference> toReferences(ReadGraph graph, Resource contextIndexRoot, List<Variable> variables) throws DatabaseException {\r
+        if (variables.isEmpty())\r
+            return Collections.emptyList();\r
+        return filterReferences( graph.syncRequest(VariableReferences.variablesToReferences(contextIndexRoot, variables)) );\r
+    }\r
+\r
+    private static List<VariableReference> toPropertyReferences(ReadGraph graph, Resource contextIndexRoot, List<PropertyVariables> variables) throws DatabaseException {\r
+        if (variables.isEmpty())\r
+            return Collections.emptyList();\r
+        return filterReferences( graph.syncRequest(VariableReferences.toReferences(contextIndexRoot, variables)) );\r
+    }\r
+\r
+    private static List<VariableReference> filterReferences(List<VariableReference> variables) throws DatabaseException {\r
+        return variables.stream()\r
+                .filter(ref -> ref.datatype instanceof BooleanType || ref.datatype instanceof NumberType)\r
+                .collect(Collectors.toList());\r
+    }\r
+\r
+    public static Runnable addPlots(Resource chart, List<ChartItemDescriptor> references, Set<Resource> movedPlots) {\r
+        return () -> {\r
+            Simantics.getSession().asyncRequest(\r
+                    AddChartItem.addAndMoveChartItems(chart, references, movedPlots),\r
+                    new ProcedureAdapter<Collection<Resource>>() {\r
+                        @Override\r
+                        public void exception(Throwable e) {\r
+                            if (e != null)\r
                                 ErrorLogger.defaultLogError(e);\r
-                            }\r
-                        });\r
-            }\r
+                        }\r
+                    });\r
         };\r
     }\r
 \r
index fe865cb8d4cfa462207ad23505031080db967903..ee3975d9515975a7c4f514830d162260221a14ab 100644 (file)
@@ -14,10 +14,12 @@ package org.simantics.charts.ui;
 import java.util.Collections;\r
 import java.util.HashSet;\r
 import java.util.List;\r
+import java.util.Optional;\r
 import java.util.Set;\r
 \r
 import org.simantics.Simantics;\r
 import org.simantics.browsing.ui.common.ErrorLogger;\r
+import org.simantics.charts.internal.JsonUtils;\r
 import org.simantics.databoard.type.Datatype;\r
 import org.simantics.databoard.util.ObjectUtils;\r
 import org.simantics.db.ReadGraph;\r
@@ -38,8 +40,6 @@ import org.simantics.modeling.ModelingResources;
 import org.simantics.modeling.PropertyVariables;\r
 import org.simantics.modeling.PropertyVariablesImpl;\r
 import org.simantics.modeling.utils.VariableReferences;\r
-import org.simantics.utils.datastructures.Callback;\r
-import org.simantics.utils.datastructures.collections.CollectionUtils;\r
 import org.simantics.utils.ui.ISelectionUtils;\r
 \r
 /**\r
@@ -57,68 +57,64 @@ public class SubscriptionDropActionFactory implements DropActionFactory {
         Resource targetModel = g.syncRequest(new PossibleModel(subscription));\r
 \r
         if(source instanceof RVI) {\r
+            RVI rvi = (RVI)source;\r
+            List<VariableReference> refs = Collections.singletonList(new VariableReference(rvi, getDatatype(g, targetModel, rvi), null));\r
+            return addSubscriptions(g, subscription, refs, Collections.<Resource>emptySet());\r
+        }\r
 \r
-               RVI rvi = (RVI)source;\r
-               List<VariableReference> refs = CollectionUtils.toList(new VariableReference(rvi, getDatatype(g, targetModel, rvi), null));\r
-               if (!refs.isEmpty())\r
-                       return addSubscriptions(g, subscription, refs, Collections.<Resource>emptySet());\r
-               \r
+        List<PropertyVariables> vars = ISelectionUtils.getPossibleKeys(source, SelectionHints.KEY_MAIN, PropertyVariables.class);\r
+        if (!vars.isEmpty()) {\r
+            // FIXME: this is a hack for indexed value support\r
+            vars = PropertyVariablesImpl.resolve(g, vars);\r
+            List<VariableReference> references = g.syncRequest(VariableReferences.toReferences(targetModel, vars));\r
+            if (!references.isEmpty())\r
+                return addSubscriptions(g, subscription, references, Collections.<Resource>emptySet());\r
         } else {\r
-        \r
-               List<PropertyVariables> vars = ISelectionUtils.getPossibleKeys(source, SelectionHints.KEY_MAIN, PropertyVariables.class);\r
-               if (!vars.isEmpty()) {\r
-                       // FIXME: this is a hack for indexed value support\r
-                       vars = PropertyVariablesImpl.resolve(g, vars);\r
+            List<Resource> srcs = ISelectionUtils.getPossibleKeys(source, SelectionHints.KEY_MAIN, Resource.class);\r
+            ModelingResources MOD = ModelingResources.getInstance(g);\r
+            Set<Resource> movedItems = new HashSet<>();\r
+            for (Resource src : srcs) {\r
+                if (g.isInstanceOf(src, MOD.Subscription_Item)) {\r
+                    Resource model = g.syncRequest(new PossibleModel(src));\r
+                    if (ObjectUtils.objectEquals(targetModel, model))\r
+                        movedItems.add(src);\r
+                }\r
+            }\r
+            if (!movedItems.isEmpty())\r
+                return addSubscriptions(g, subscription, Collections.emptyList(), movedItems);\r
+        }\r
 \r
-                       List<VariableReference> references = g.syncRequest(VariableReferences.toReferences(targetModel, vars));\r
-                       if (!references.isEmpty())\r
-                               return addSubscriptions(g, subscription, references, Collections.<Resource>emptySet());\r
-               } else {\r
-                       List<Resource> srcs = ISelectionUtils.getPossibleKeys(source, SelectionHints.KEY_MAIN, Resource.class);\r
-                       ModelingResources MOD = ModelingResources.getInstance(g);\r
-                       Set<Resource> movedItems = new HashSet<Resource>();\r
-                       for (Resource src : srcs) {\r
-                               if (g.isInstanceOf(src, MOD.Subscription_Item)) {\r
-                                       Resource model = g.syncRequest(new PossibleModel(src));\r
-                                       if (ObjectUtils.objectEquals(targetModel, model))\r
-                                               movedItems.add(src);\r
-                               }\r
-                       }\r
-                       if (!movedItems.isEmpty())\r
-                               return addSubscriptions(g, subscription, Collections.<VariableReference>emptyList(), movedItems);\r
-               }\r
-        \r
+        if (source instanceof String) {\r
+            // JSON ?\r
+            Optional<Variable> v = JsonUtils.tryParseJsonPropertyVariable(g, (String) source);\r
+            if (v.isPresent()) {\r
+                List<VariableReference> references = g.syncRequest(VariableReferences.variablesToReferences(targetModel, Collections.singletonList(v.get())));\r
+                return addSubscriptions(g, subscription, references, Collections.emptySet());\r
+            }\r
         }\r
 \r
         return null;\r
     }\r
 \r
-    private Runnable addSubscriptions(ReadGraph graph, final Resource subscription, final List<VariableReference> references,\r
-            final Set<Resource> movedSubscriptionItems) throws DatabaseException {\r
-        final AddVariableToChartAction action = new AddVariableToChartAction(null, subscription, references);\r
-        action.init(graph);\r
-        return new Runnable() {\r
-            @Override\r
-            public void run() {\r
-                action.run();\r
-                if(!movedSubscriptionItems.isEmpty()) {\r
-                    Simantics.getSession().asyncRequest(new WriteRequest() {\r
-                        @Override\r
-                        public void perform(WriteGraph graph) throws DatabaseException {\r
-                            Layer0 L0 = Layer0.getInstance(graph);\r
-                            for (Resource item : movedSubscriptionItems) {\r
-                                graph.deny(item, L0.PartOf);\r
-                                graph.claim(subscription, L0.ConsistsOf, item);\r
-                            }\r
-                        }\r
-                    }, new Callback<DatabaseException>() {\r
-                        @Override\r
-                        public void run(DatabaseException e) {\r
-                            if (e != null)\r
-                                ErrorLogger.defaultLogError(e);\r
+    private Runnable addSubscriptions(ReadGraph graph, Resource subscription, List<VariableReference> references,\r
+            Set<Resource> movedSubscriptionItems) throws DatabaseException {\r
+        AddVariableToChartAction action = new AddVariableToChartAction(null, subscription, references).init(graph);\r
+        return () -> {\r
+            action.run();\r
+            if(!movedSubscriptionItems.isEmpty()) {\r
+                Simantics.getSession().asyncRequest(new WriteRequest() {\r
+                    @Override\r
+                    public void perform(WriteGraph graph) throws DatabaseException {\r
+                        Layer0 L0 = Layer0.getInstance(graph);\r
+                        for (Resource item : movedSubscriptionItems) {\r
+                            graph.deny(item, L0.PartOf);\r
+                            graph.claim(subscription, L0.ConsistsOf, item);\r
                         }\r
-                    });\r
-                }\r
+                    }\r
+                }, e -> {\r
+                    if (e != null)\r
+                        ErrorLogger.defaultLogError(e);\r
+                });\r
             }\r
         };\r
     }\r
index 42c489e24d4b866d310507cf7d1d00d4cc1508cf..27e58b5d1b832a042504a9cf945ae15f13150510 100644 (file)
@@ -65,6 +65,7 @@ public class DiagramViewerSelectionProvider extends WorkbenchSelectionProvider {
 \r
                             DiagramResource DIA = DiagramResource.getInstance(graph);\r
                             ModelingResources MOD = ModelingResources.getInstance(graph);\r
+                            Layer0 L0 = Layer0.getInstance(graph);\r
 \r
                             String uri = graph.getPossibleRelatedValue(resource, DIA.RuntimeDiagram_HasVariable);\r
                             if (uri == null)\r
@@ -76,6 +77,11 @@ public class DiagramViewerSelectionProvider extends WorkbenchSelectionProvider {
 \r
                             Resource config = graph.getPossibleObject(resource2, MOD.ElementToComponent);\r
                             if (config == null) {\r
+                                if (graph.isInstanceOf(resource2, DIA.Connection)) {\r
+                                    Variable v = FlagUtil.getPossibleConnectionSignal(graph, var, resource2, L0.Entity);\r
+                                    if (v != null)\r
+                                        return v;\r
+                                }\r
                                 // Apros #9646: if resource2 is the diagram\r
                                 // itself, return the diagram composite variable\r
                                 // since it is generally more useful than the\r
@@ -87,7 +93,6 @@ public class DiagramViewerSelectionProvider extends WorkbenchSelectionProvider {
                                 }\r
                                 \r
                                 if(graph.isInstanceOf(resource2, DIA.Flag)) {\r
-                                       Layer0 L0 = Layer0.getInstance(graph);\r
                                        Variable signal = FlagUtil.getPossibleFlagSignal(graph, var, resource2, L0.Entity);\r
                                        if(signal != null)\r
                                                return signal;\r
index 32d412a74fb649d291c06cf0f5287cb624db1280..4e8dd9c07bf1182c43528cbbad5e37ea1f574e9b 100644 (file)
          version="0.0.0"\r
          unpack="false"/>\r
 \r
+   <plugin\r
+         id="com.fasterxml.jackson.core.jackson-core"\r
+         download-size="0"\r
+         install-size="0"\r
+         version="0.0.0"\r
+         unpack="false"/>\r
+\r
 </feature>\r