X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=blobdiff_plain;f=bundles%2Forg.simantics.debug.graphical%2Fsrc%2Forg%2Fsimantics%2Fdebug%2Fgraphical%2FDebuggerCanvas.java;h=310d8a0865e824cfc8b0f6e51dd26af1bf41c78b;hp=107125f46e35e9f6b20349f6d18528b8a0efcd33;hb=d4361a11a962437b51c72f51e1a913c089e4351e;hpb=58a208e96edcddb07af89796859416dc6fc59f14 diff --git a/bundles/org.simantics.debug.graphical/src/org/simantics/debug/graphical/DebuggerCanvas.java b/bundles/org.simantics.debug.graphical/src/org/simantics/debug/graphical/DebuggerCanvas.java index 107125f46..310d8a086 100644 --- a/bundles/org.simantics.debug.graphical/src/org/simantics/debug/graphical/DebuggerCanvas.java +++ b/bundles/org.simantics.debug.graphical/src/org/simantics/debug/graphical/DebuggerCanvas.java @@ -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 nodes = new ArrayList(); @@ -197,33 +207,55 @@ 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); - - dtde.getDropTargetContext().dropComplete( true ); + handleDrop(dtde, obj); } else { + DataFlavor textFlavor = DataFlavor.selectBestTextFlavor(transferable.getTransferDataFlavors()); + if(textFlavor != null) { + // Try to read the textual content of the drop event as a JSON object containing a field named resourceId + 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() { + public Resource perform(ReadGraph graph) throws DatabaseException { + SerialisationSupport ss = graph.getService(SerialisationSupport.class); + return ss.getResource(((NumericNode)resourceId).longValue()); + } + })); + handleDrop(dtde, obj); + 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); + + dtde.getDropTargetContext().dropComplete( true ); + } + public void keyPressed(KeyEvent e) { switch(e.getKeyCode()) { case KeyEvent.VK_1: @@ -272,6 +304,8 @@ public class DebuggerCanvas extends JPanel { if(resource != null) return resource; } + if(obj instanceof Resource) + return (Resource)obj; return null; }