From: Jussi Koskela Date: Thu, 6 Sep 2018 13:46:17 +0000 (+0300) Subject: Trigger all drag start events from single place X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=c92d827b26188f3c8cf6ae419576be5bb3f6b287;p=simantics%2Fplatform.git Trigger all drag start events from single place gitlab #112 Change-Id: Ifdde60194967fc80fbf0276c754af00e3c2a3ca2 (cherry picked from commit bdb7180f6425b460b8566a3223b319caf9dbc56f) --- diff --git a/bundles/org.simantics.diagram/src/org/simantics/diagram/elements/TextNode.java b/bundles/org.simantics.diagram/src/org/simantics/diagram/elements/TextNode.java index 4fe3b51d8..43653cbd0 100644 --- a/bundles/org.simantics.diagram/src/org/simantics/diagram/elements/TextNode.java +++ b/bundles/org.simantics.diagram/src/org/simantics/diagram/elements/TextNode.java @@ -1806,7 +1806,6 @@ public class TextNode extends G2DNode implements IDynamicSelectionPainterNode, L protected boolean mouseDragged(MouseDragBegin e) { if (isHovering() && (isControlDown(e) || isShiftDown(e)) - && e.context instanceof NodeEventHandler && (dataRVI != null || text != null)) { List trs = new ArrayList<>(2); diff --git a/bundles/org.simantics.g2d/src/org/simantics/g2d/chassis/AWTChassis.java b/bundles/org.simantics.g2d/src/org/simantics/g2d/chassis/AWTChassis.java index 2679ef620..06b3ca220 100644 --- a/bundles/org.simantics.g2d/src/org/simantics/g2d/chassis/AWTChassis.java +++ b/bundles/org.simantics.g2d/src/org/simantics/g2d/chassis/AWTChassis.java @@ -16,6 +16,7 @@ import java.awt.Container; import java.awt.Cursor; import java.awt.Graphics; import java.awt.Graphics2D; +import java.awt.GraphicsEnvironment; import java.awt.Rectangle; import java.awt.image.VolatileImage; import java.lang.reflect.Method; @@ -31,6 +32,7 @@ import org.simantics.g2d.canvas.IMouseCursorListener; import org.simantics.g2d.dnd.DragInteractor; import org.simantics.g2d.dnd.DropInteractor; import org.simantics.g2d.internal.DebugPolicy; +import org.simantics.g2d.participant.TransformUtil; import org.simantics.scenegraph.g2d.G2DRenderingHints; import org.simantics.scenegraph.g2d.events.Event; import org.simantics.scenegraph.g2d.events.IEventQueue; @@ -239,6 +241,12 @@ public class AWTChassis extends JComponent implements ICanvasChassis { holder.addMouseMotionListener(mouseAdapter); holder.addMouseWheelListener(mouseAdapter); holder.addFocusListener(focusAdapter); + + TransformUtil util = canvasContext.getAtMostOneItemOfClass(TransformUtil.class); + if (util != null) { + mouseAdapter.initDragGestureListener(holder, (control) -> util.controlToCanvas(control, null)); + } + } }; if (AWTThread.getThreadAccess().currentThreadAccess()) diff --git a/bundles/org.simantics.g2d/src/org/simantics/g2d/diagram/impl/ElementDiagram.java b/bundles/org.simantics.g2d/src/org/simantics/g2d/diagram/impl/ElementDiagram.java index 7bec43780..07ffe5436 100644 --- a/bundles/org.simantics.g2d/src/org/simantics/g2d/diagram/impl/ElementDiagram.java +++ b/bundles/org.simantics.g2d/src/org/simantics/g2d/diagram/impl/ElementDiagram.java @@ -670,20 +670,6 @@ public class ElementDiagram implements IDiagram { if (deltaDistance > 0) mi.addDistanceForButtons(deltaDistance); - // Send mouse drag events. - for (ButtonInfo bi : mi.buttonPressInfo.values()) { - if (!bi.down) - continue; - if (bi.deltaMotion <= profile.movementTolerance) - continue; - if (bi.drag) - continue; - bi.drag = true; - MouseDragBegin db = new MouseDragBegin(this, e.time, e.mouseId, e.buttons, e.stateMask, bi.button, - bi.canvasPosition, bi.controlPosition, e.controlPosition, e.screenPosition); - getContext().getEventQueue().queueFirst(db); - } - } else if (e instanceof MouseButtonPressedEvent) { Point2D canvasPosition = util.controlToCanvas(e.controlPosition, null); MouseButtonPressedEvent me = (MouseButtonPressedEvent) e; diff --git a/bundles/org.simantics.g2d/src/org/simantics/g2d/participant/MouseUtil.java b/bundles/org.simantics.g2d/src/org/simantics/g2d/participant/MouseUtil.java index 44ded4729..d5fbdc12b 100644 --- a/bundles/org.simantics.g2d/src/org/simantics/g2d/participant/MouseUtil.java +++ b/bundles/org.simantics.g2d/src/org/simantics/g2d/participant/MouseUtil.java @@ -111,21 +111,6 @@ public class MouseUtil extends AbstractCanvasParticipant { if (deltaDistance>0) mi.addDistanceForButtons(deltaDistance); - // Send mouse drag events. - for (ButtonInfo bi : mi.buttonPressInfo.values()) - { - if (!bi.down) continue; - if (bi.deltaMotion<=profile.movementTolerance) continue; - if (bi.drag) continue; - bi.drag = true; - MouseDragBegin db = new MouseDragBegin( - this, e.time, e.mouseId, e.buttons, e.stateMask, bi.button, - bi.canvasPosition, bi.controlPosition, - e.controlPosition, e.screenPosition - ); - getContext().getEventQueue().queueFirst(db); - } - } else if (e instanceof MouseButtonPressedEvent) { diff --git a/bundles/org.simantics.scenegraph/src/org/simantics/scenegraph/g2d/G2DSceneGraph.java b/bundles/org.simantics.scenegraph/src/org/simantics/scenegraph/g2d/G2DSceneGraph.java index 885b8c3a0..204181c37 100644 --- a/bundles/org.simantics.scenegraph/src/org/simantics/scenegraph/g2d/G2DSceneGraph.java +++ b/bundles/org.simantics.scenegraph/src/org/simantics/scenegraph/g2d/G2DSceneGraph.java @@ -158,7 +158,6 @@ public class G2DSceneGraph extends G2DParentNode implements ILookupService, INod RepaintManager.setCurrentManager(repaintManager); } this.rootPane = rootPane; - eventHandler.setRootPane(rootPane); } private RepaintManager findProperRepaintManager(RepaintManager old) { diff --git a/bundles/org.simantics.scenegraph/src/org/simantics/scenegraph/g2d/events/NodeEventHandler.java b/bundles/org.simantics.scenegraph/src/org/simantics/scenegraph/g2d/events/NodeEventHandler.java index 1c1ac8a37..8d3ba3a94 100644 --- a/bundles/org.simantics.scenegraph/src/org/simantics/scenegraph/g2d/events/NodeEventHandler.java +++ b/bundles/org.simantics.scenegraph/src/org/simantics/scenegraph/g2d/events/NodeEventHandler.java @@ -11,18 +11,6 @@ *******************************************************************************/ package org.simantics.scenegraph.g2d.events; -import java.awt.Component; -import java.awt.GraphicsEnvironment; -import java.awt.dnd.DnDConstants; -import java.awt.dnd.DragGestureEvent; -import java.awt.dnd.DragGestureListener; -import java.awt.dnd.DragSource; -import java.awt.dnd.DragSourceDragEvent; -import java.awt.dnd.DragSourceDropEvent; -import java.awt.dnd.DragSourceEvent; -import java.awt.dnd.DragSourceListener; -import java.awt.event.InputEvent; -import java.awt.geom.Point2D; import java.util.ArrayList; import java.util.Arrays; import java.util.Comparator; @@ -34,7 +22,6 @@ import org.simantics.scenegraph.g2d.G2DSceneGraph; import org.simantics.scenegraph.g2d.IG2DNode; import org.simantics.scenegraph.g2d.events.MouseEvent.MouseButtonPressedEvent; import org.simantics.scenegraph.g2d.events.MouseEvent.MouseDragBegin; -import org.simantics.scenegraph.g2d.events.adapter.AWTMouseEventAdapter; import org.simantics.scenegraph.g2d.events.command.CommandEvent; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -220,65 +207,6 @@ public class NodeEventHandler implements IEventHandler { return sort; } - public void setRootPane(Component rootPane) { - if (GraphicsEnvironment.isHeadless()) { - LOGGER.info("Disabling DragSource in headless environments"); - return; - } - final DragSource ds = new DragSource(); - final DragSourceListener dsl = new DragSourceListener() { - @Override - public void dropActionChanged(DragSourceDragEvent dsde) { - } - @Override - public void dragOver(DragSourceDragEvent dsde) { - } - @Override - public void dragExit(DragSourceEvent dse) { - } - @Override - public void dragEnter(DragSourceDragEvent dsde) { - } - @Override - public void dragDropEnd(DragSourceDropEvent dsde) { - } - }; - DragGestureListener dgl = new DragGestureListener() { - @Override - public void dragGestureRecognized(DragGestureEvent dge) { - InputEvent ie = dge.getTriggerEvent(); - if (ie instanceof java.awt.event.MouseEvent) { - java.awt.event.MouseEvent e = (java.awt.event.MouseEvent) ie; - Point2D controlPos = AWTMouseEventAdapter.getControlPosition(e); - MouseDragBegin event = new MouseDragBegin(NodeEventHandler.this, - e.getWhen(), 0, - AWTMouseEventAdapter.getButtonStatus(e), - AWTMouseEventAdapter.getStateMask(e), - AWTMouseEventAdapter.getMouseButton(e), - // TODO: fix canvas position if necessary - new Point2D.Double(), - controlPos, - controlPos, - AWTMouseEventAdapter.getScreenPosition(e)); - - // Send MouseDragBegin to the scenegraph and see - // if anyone sets event.transferable to start DnD. - handleMouseDragBeginEvent(event, EventTypes.MouseDragBegin); - if (event.transferable != null) { - ds.startDrag(dge, null, event.transferable, dsl); - if (DEBUG_EVENTS) - debug("dragGestureRecognized: startDrag " + event.transferable); - } - } - } - }; - ds.createDefaultDragGestureRecognizer( - rootPane, - DnDConstants.ACTION_COPY_OR_MOVE | DnDConstants.ACTION_LINK, - dgl); - ds.addDragSourceListener(dsl); - } - public boolean mousePressed(MouseButtonPressedEvent event) { G2DFocusManager.INSTANCE.clearFocus(); try { diff --git a/bundles/org.simantics.scenegraph/src/org/simantics/scenegraph/g2d/events/adapter/AWTMouseEventAdapter.java b/bundles/org.simantics.scenegraph/src/org/simantics/scenegraph/g2d/events/adapter/AWTMouseEventAdapter.java index bad131312..e01876c6d 100644 --- a/bundles/org.simantics.scenegraph/src/org/simantics/scenegraph/g2d/events/adapter/AWTMouseEventAdapter.java +++ b/bundles/org.simantics.scenegraph/src/org/simantics/scenegraph/g2d/events/adapter/AWTMouseEventAdapter.java @@ -15,6 +15,11 @@ */ package org.simantics.scenegraph.g2d.events.adapter; +import java.awt.Component; +import java.awt.dnd.DnDConstants; +import java.awt.dnd.DragGestureEvent; +import java.awt.dnd.DragGestureListener; +import java.awt.dnd.DragSource; import java.awt.event.InputEvent; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; @@ -22,6 +27,7 @@ import java.awt.event.MouseMotionListener; import java.awt.event.MouseWheelEvent; import java.awt.event.MouseWheelListener; import java.awt.geom.Point2D; +import java.util.function.UnaryOperator; import org.simantics.scenegraph.g2d.events.EventDebugPolicy; import org.simantics.scenegraph.g2d.events.IEventHandler; @@ -29,6 +35,7 @@ import org.simantics.scenegraph.g2d.events.IEventQueue; import org.simantics.scenegraph.g2d.events.MouseEvent.MouseButtonPressedEvent; import org.simantics.scenegraph.g2d.events.MouseEvent.MouseButtonReleasedEvent; import org.simantics.scenegraph.g2d.events.MouseEvent.MouseDoubleClickedEvent; +import org.simantics.scenegraph.g2d.events.MouseEvent.MouseDragBegin; import org.simantics.scenegraph.g2d.events.MouseEvent.MouseEnterEvent; import org.simantics.scenegraph.g2d.events.MouseEvent.MouseExitEvent; import org.simantics.scenegraph.g2d.events.MouseEvent.MouseMovedEvent; @@ -289,4 +296,38 @@ public class AWTMouseEventAdapter extends AbstractEventAdapter implements MouseL System.out.println("AWT mouse clicked: " + e); } + public void initDragGestureListener(Component rootPane, UnaryOperator controlToCanvas) { + final DragSource ds = new DragSource(); + DragGestureListener dgl = new DragGestureListener() { + @Override + public void dragGestureRecognized(DragGestureEvent dge) { + InputEvent ie = dge.getTriggerEvent(); + if (ie instanceof java.awt.event.MouseEvent) { + java.awt.event.MouseEvent e = (java.awt.event.MouseEvent) ie; + Point2D controlPos = getControlPosition(e); + Point2D canvasPos = controlToCanvas.apply(controlPos); + + MouseDragBegin event = new MouseDragBegin(this, + e.getWhen(), 0, + getButtonStatus(e), + getStateMask(e), + getMouseButton(e), + canvasPos, + controlPos, + controlPos, + getScreenPosition(e)); + + handleEvent(event); + + if (event.transferable != null) { + ds.startDrag(dge, null, event.transferable, null); + } + } + } + }; + ds.createDefaultDragGestureRecognizer( + rootPane, + DnDConstants.ACTION_COPY_OR_MOVE | DnDConstants.ACTION_LINK, + dgl); + } }