Support DnD from diagram to graphical debugger 32/2332/1
authorHannu Niemistö <hannu.niemisto@semantum.fi>
Thu, 18 Oct 2018 10:15:31 +0000 (13:15 +0300)
committerHannu Niemistö <hannu.niemisto@semantum.fi>
Thu, 18 Oct 2018 11:39:35 +0000 (14:39 +0300)
Change-Id: I80cfb90e6f6a009c0fc4c6b84523c7bc7de05ff0

bundles/org.simantics.debug.graphical/META-INF/MANIFEST.MF
bundles/org.simantics.debug.graphical/src/org/simantics/debug/graphical/DebuggerCanvas.java

index b7790ca384f27d9da333f9d03f0f45e37f979f21..f29797cc14a55c252518875dd032f710f75cf0c7 100644 (file)
@@ -7,4 +7,7 @@ Bundle-RequiredExecutionEnvironment: JavaSE-1.8
 Require-Bundle: org.simantics.db;bundle-version="1.1.0",
  org.eclipse.core.runtime;bundle-version="3.6.0",
  org.eclipse.ui;bundle-version="3.6.0",
- org.simantics.ui;bundle-version="1.0.0"
+ org.simantics.ui;bundle-version="1.0.0",
+ com.fasterxml.jackson.core.jackson-core;bundle-version="2.8.8",
+ org.slf4j.api;bundle-version="1.7.25",
+ com.fasterxml.jackson.core.jackson-databind;bundle-version="2.8.8"
index 107125f46e35e9f6b20349f6d18528b8a0efcd33..6430d029beca6185d922dddd3e9a1bc41a4d3bb8 100644 (file)
@@ -5,8 +5,8 @@ import java.awt.GradientPaint;
 import java.awt.Graphics;
 import java.awt.Graphics2D;
 import java.awt.RenderingHints;
+import java.awt.datatransfer.DataFlavor;
 import java.awt.datatransfer.Transferable;
-import java.awt.datatransfer.UnsupportedFlavorException;
 import java.awt.dnd.DnDConstants;
 import java.awt.dnd.DropTarget;
 import java.awt.dnd.DropTargetAdapter;
@@ -21,7 +21,7 @@ import java.awt.event.MouseWheelListener;
 import java.awt.geom.AffineTransform;
 import java.awt.geom.Point2D;
 import java.awt.geom.Rectangle2D;
-import java.io.IOException;
+import java.io.Reader;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Random;
@@ -31,6 +31,7 @@ import javax.swing.SwingUtilities;
 
 import org.eclipse.core.runtime.IAdaptable;
 import org.eclipse.jface.viewers.IStructuredSelection;
+import org.eclipse.jface.viewers.StructuredSelection;
 import org.simantics.Simantics;
 import org.simantics.db.ChangeSet;
 import org.simantics.db.ChangeSetIdentifier;
@@ -41,7 +42,9 @@ import org.simantics.db.Statement;
 import org.simantics.db.common.request.ReadRequest;
 import org.simantics.db.common.utils.NameUtils;
 import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.request.Read;
 import org.simantics.db.service.ManagementSupport;
+import org.simantics.db.service.SerialisationSupport;
 import org.simantics.debug.graphical.layout.ExtensionLayoutAlgorithm;
 import org.simantics.debug.graphical.layout.LayoutGraph;
 import org.simantics.debug.graphical.model.Edge;
@@ -55,13 +58,20 @@ import org.simantics.ui.dnd.LocalObjectTransfer;
 import org.simantics.ui.dnd.LocalObjectTransferable;
 import org.simantics.ui.selection.AnyResource;
 import org.simantics.ui.selection.WorkbenchSelectionElement;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.node.NumericNode;
 
 import gnu.trove.list.array.TDoubleArrayList;
 import gnu.trove.map.hash.THashMap;
 import gnu.trove.map.hash.TObjectIntHashMap;
 
 public class DebuggerCanvas extends JPanel {
-
+    private static final Logger LOGGER = LoggerFactory.getLogger(DebuggerCanvas.class);
+    
     private static final long serialVersionUID = -718678297301786379L;
     
     ArrayList<Node> nodes = new ArrayList<Node>();
@@ -197,33 +207,56 @@ public class DebuggerCanvas extends JPanel {
             public void drop(DropTargetDropEvent dtde) {
                 try {
                     Transferable transferable = dtde.getTransferable();
-                    
                     if( transferable.isDataFlavorSupported( 
                             LocalObjectTransferable.FLAVOR ) ) {
                         dtde.acceptDrop( DnDConstants.ACTION_MOVE );
                         
-                        transferable.getTransferData(LocalObjectTransferable.FLAVOR );
+                        transferable.getTransferData(LocalObjectTransferable.FLAVOR);
                         Object obj = LocalObjectTransfer.getTransfer().getObject();
-                        double x = canvasPosX+dtde.getLocation().getX()/canvasZoom;
-                        double y = canvasPosY+dtde.getLocation().getY()/canvasZoom;
-                        handleDrop(x, y, obj);
+                        handleDrop(dtde, obj);
                         
                         dtde.getDropTargetContext().dropComplete( true );
                     }
                     else {
+                        DataFlavor textFlavor = DataFlavor.selectBestTextFlavor(transferable.getTransferDataFlavors());
+                        if(textFlavor != null) {
+                            try(Reader reader = textFlavor.getReaderForText(transferable)) {
+                                ObjectMapper mapper = new ObjectMapper();
+                                JsonNode node = mapper.readTree(reader);
+                                Object resourceId = node.get("resourceId");
+                                if(resourceId instanceof NumericNode) {
+                                    dtde.acceptDrop( DnDConstants.ACTION_MOVE );
+                                    
+                                    transferable.getTransferData(LocalObjectTransferable.FLAVOR);
+                                    Object obj = new StructuredSelection(Simantics.getSession().syncRequest(new Read<Resource>() {
+                                        public Resource perform(ReadGraph graph) throws DatabaseException {
+                                            SerialisationSupport ss = graph.getService(SerialisationSupport.class);
+                                            return ss.getResource(((NumericNode)resourceId).longValue());
+                                        }
+                                    }));
+                                    handleDrop(dtde, obj);
+                                    
+                                    dtde.getDropTargetContext().dropComplete( true );
+                                    return;
+                                }
+                            }
+                        }
                         dtde.rejectDrop();
                     }
-                } catch( IOException exception ) {
-                    exception.printStackTrace();
-                    dtde.rejectDrop();
-                } catch( UnsupportedFlavorException ufException ) {
-                    ufException.printStackTrace();
+                } catch( Exception exception ) {
+                    LOGGER.warn("Drop failed.", exception);
                     dtde.rejectDrop();
-                }
+                } 
             }     
         });
     }    
     
+    private void handleDrop(DropTargetDropEvent dtde, Object obj) {
+        double x = canvasPosX+dtde.getLocation().getX()/canvasZoom;
+        double y = canvasPosY+dtde.getLocation().getY()/canvasZoom;
+        handleDrop(x, y, obj);
+    }
+    
     public void keyPressed(KeyEvent e) {
         switch(e.getKeyCode()) {
         case KeyEvent.VK_1:
@@ -272,6 +305,8 @@ public class DebuggerCanvas extends JPanel {
             if(resource != null)
                 return resource;
         }
+        if(obj instanceof Resource)
+            return (Resource)obj;
         return null;
     }