From: Hannu Niemistö Date: Thu, 18 Oct 2018 10:15:31 +0000 (+0300) Subject: Support DnD from diagram to graphical debugger X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=b7097b294131967db72eb33038b36de8195d85c2;p=simantics%2Fplatform.git Support DnD from diagram to graphical debugger Change-Id: I80cfb90e6f6a009c0fc4c6b84523c7bc7de05ff0 --- diff --git a/bundles/org.simantics.debug.graphical/META-INF/MANIFEST.MF b/bundles/org.simantics.debug.graphical/META-INF/MANIFEST.MF index b7790ca38..f29797cc1 100644 --- a/bundles/org.simantics.debug.graphical/META-INF/MANIFEST.MF +++ b/bundles/org.simantics.debug.graphical/META-INF/MANIFEST.MF @@ -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" 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..6430d029b 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,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() { + 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; }