changeset 11151:95c2792650ff

8211248: Create manual drag and drop tests for FX / Swing interop Reviewed-by: kcr
author psadhukhan
date Fri, 11 Jan 2019 11:21:23 +0530
parents 8843f890e66e
children 1d6bec87575f
files tests/manual/swing/DragDropFromSwingComponentInSwingNodeTest.java tests/manual/swing/DragDropOntoJavaFXControlInJFXPanelTest.java tests/manual/swing/DragDropOntoSwingComponentInSwingNodeTest.java
diffstat 3 files changed, 400 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/manual/swing/DragDropFromSwingComponentInSwingNodeTest.java	Fri Jan 11 11:21:23 2019 +0530
@@ -0,0 +1,141 @@
+/*
+ * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.  Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+import java.awt.Color;
+import java.awt.datatransfer.StringSelection;
+import java.awt.dnd.DnDConstants;
+import java.awt.dnd.DragGestureEvent;
+import java.awt.dnd.DragGestureListener;
+import java.awt.dnd.DragGestureRecognizer;
+import java.awt.dnd.DragSource;
+import java.awt.dnd.DropTarget;
+import java.awt.dnd.DropTargetAdapter;
+import java.awt.dnd.DropTargetDropEvent;
+import java.awt.dnd.DropTargetListener;
+
+import javafx.application.Application;
+import javafx.application.Platform;
+import javafx.beans.InvalidationListener;
+import javafx.beans.Observable;
+import javafx.embed.swing.SwingNode;
+import javafx.scene.Scene;
+import javafx.scene.control.Button;
+import javafx.scene.control.Label;
+import javafx.scene.layout.BorderPane;
+import javafx.scene.layout.HBox;
+import javafx.scene.layout.VBox;
+import javafx.stage.Stage;
+
+import javax.swing.JLabel;
+import javax.swing.JPanel;
+import javax.swing.SwingUtilities;
+
+public class DragDropFromSwingComponentInSwingNodeTest extends Application {
+
+    private DragSource dragSource;
+    private DropTarget dropTarget;
+    private DropTargetListener dropListener;
+    private DragGestureListener dgListener;
+    private DragGestureRecognizer dragRecognizer;
+
+    public static void main(String[] args) {
+        Application.launch(args);
+    }
+
+    @Override
+    public void start(Stage stage) {
+
+        final SwingNode swingNode = new SwingNode();
+
+        createSwingContent(swingNode);
+        BorderPane pane = new BorderPane();
+        pane.setCenter(swingNode);
+
+        stage.setTitle("Swing in JavaFX");
+        Button passButton = new Button("Pass");
+        Button failButton = new Button("Fail");
+        passButton.setOnAction(e -> this.quit());
+        failButton.setOnAction(e -> {
+            this.quit();
+            throw new AssertionError("Drag / drop from a Swing component in a SwingNode not working");
+        });
+
+        VBox rootNode = new VBox(6,
+                new Label("1. This is a test for drag / drop from a Swing component in a SwingNode."),
+                new Label("2. Drag JLabel \"Drag Me!\" text and drop into console."),
+                new Label("3. When the content is dropped into console, if it prints \"some string data\", click on Pass or else click on Fail"),
+                new Label(""),
+                new HBox(10, passButton, failButton), pane);
+
+        stage.setScene(new Scene(rootNode, 600, 250));
+        stage.onCloseRequestProperty().addListener(new InvalidationListener(){
+            @Override public void invalidated(Observable observable) {
+                System.exit(0);
+            }
+        });
+        stage.show();
+    }
+
+    private void quit() {
+        Platform.exit();
+    }
+
+    private void createSwingContent(final SwingNode swingNode) {
+        SwingUtilities.invokeLater(new Runnable() {
+
+            @Override
+            public void run() {
+
+                JPanel panel = new JPanel();
+                panel.setBackground(Color.RED);
+                panel.add(new JLabel("Drag me!"));
+
+                swingNode.setContent(panel);
+
+                dgListener = new DragGestureListener() {
+                    @Override public void dragGestureRecognized(DragGestureEvent dge) {
+                        System.out.println("drag recognized...");
+                        dge.startDrag(java.awt.Cursor.getPredefinedCursor(java.awt.Cursor.HAND_CURSOR), new StringSelection("some string data"));
+                    }
+                };
+
+                dropListener = new DropTargetAdapter() {
+                    @Override public void drop(DropTargetDropEvent dtde) {
+                        //this is never called =(
+                        System.out.println("drop... " + dtde.getTransferable());
+                    }
+                };
+
+                dragSource = new DragSource();
+                dragRecognizer = dragSource.createDefaultDragGestureRecognizer(panel, DnDConstants.ACTION_COPY_OR_MOVE, dgListener);
+                dropTarget = new DropTarget(panel, dropListener);
+                System.out.println(dragRecognizer);
+                System.out.println(dropTarget);
+
+            }
+        });
+    }
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/manual/swing/DragDropOntoJavaFXControlInJFXPanelTest.java	Fri Jan 11 11:21:23 2019 +0530
@@ -0,0 +1,142 @@
+/*
+ * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.  Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+import java.awt.FlowLayout;
+import java.awt.Point;
+import java.awt.datatransfer.StringSelection;
+import java.awt.datatransfer.Transferable;
+import java.awt.dnd.DnDConstants;
+import java.awt.dnd.DragSource;
+import java.awt.event.MouseAdapter;
+import java.awt.event.MouseEvent;
+
+import javafx.application.Platform;
+import javafx.embed.swing.JFXPanel;
+import javafx.scene.Scene;
+import javafx.scene.control.Button;
+import javafx.scene.control.Label;
+import javafx.scene.control.TextField;
+import javafx.scene.layout.HBox;
+import javafx.scene.layout.Priority;
+import javafx.scene.layout.VBox;
+
+import javax.swing.JComponent;
+import javax.swing.JFrame;
+import javax.swing.JLabel;
+import javax.swing.JRootPane;
+import javax.swing.JTextField;
+import javax.swing.SwingUtilities;
+import javax.swing.TransferHandler;
+import javax.swing.WindowConstants;
+
+public class DragDropOntoJavaFXControlInJFXPanelTest {
+
+    public static void main(final String... pArguments) {
+
+        SwingUtilities.invokeLater(new Runnable() {
+            @Override
+            public void run() {
+                JFrame frame = new JFrame("DnDTest");
+                frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
+
+                JRootPane rootPane = frame.getRootPane();
+                rootPane.setLayout(new FlowLayout());
+                JLabel label = new JLabel("Drag me");
+                label.setTransferHandler(new TransferHandler() {
+                    @Override
+                    protected Transferable createTransferable(final JComponent pComponent) {
+                        return new StringSelection("Drag text");
+                    }
+
+                    @Override
+                    public int getSourceActions(final JComponent pComponent) {
+                        return DnDConstants.ACTION_COPY;
+                    }
+                });
+                MouseAdapter dragGestureRecognizer = new MouseAdapter() {
+                    private Point mPoint;
+
+                    @Override
+                    public void mousePressed(MouseEvent pEvent) {
+                        mPoint = pEvent.getPoint();
+                    }
+
+                    @Override
+                    public void mouseReleased(MouseEvent pEvent) {
+                        mPoint = null;
+                    }
+
+                    @Override
+                    public void mouseDragged(MouseEvent pEvent) {
+                        if (mPoint == null) {
+                            mPoint = pEvent.getPoint();
+                        }
+                        double distance = pEvent.getPoint().distance(mPoint);
+                        if (distance > DragSource.getDragThreshold()) {
+                            JComponent component = (JComponent) pEvent.getComponent();
+                            TransferHandler transferHandler = component.getTransferHandler();
+                            transferHandler.exportAsDrag(component, pEvent, DnDConstants.ACTION_COPY);
+                        }
+                    }
+                };
+                label.addMouseListener(dragGestureRecognizer);
+                label.addMouseMotionListener(dragGestureRecognizer);
+                rootPane.add(label);
+
+                final JFXPanel panel = new JFXPanel();
+                Platform.runLater(new Runnable() {
+                    @Override
+                    public void run() {
+                        TextField textField = new TextField("Drop here");
+                        Button passButton = new Button("Pass");
+                        Button failButton = new Button("Fail");
+                        passButton.setOnAction(e -> frame.dispose());
+                        failButton.setOnAction(e -> {
+                            frame.dispose();
+                            throw new AssertionError("Drag / drop onto a JavaFX control in a JFXPanel not working");
+                        });
+                        HBox hBox1 = new HBox();
+                        HBox.setHgrow(textField, Priority.ALWAYS);
+                        hBox1.getChildren().add(textField);
+                        VBox rootNode = new VBox(6, hBox1,
+                            new Label("1. This is a test for drag / drop onto a JavaFX control in a JFXPanel."),
+                            new Label("2. Drag JLabel \"Drag Me\" text and drop into \"Drop here\" JavaFX textfield."),
+                            new Label("3. If \"Drag text\" text is added to existing text in JavaFX TextField control, click on Pass or else click on Fail"),
+                            new Label(""),
+                        new HBox(10, passButton, failButton));
+                        Scene scene = new Scene(rootNode);
+                        panel.setScene(scene);
+                    }
+                });
+
+                rootPane.add(panel);
+
+                frame.setSize(700, 400);
+                frame.setLocationRelativeTo(null);
+                frame.setVisible(true);
+            }
+        });
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/manual/swing/DragDropOntoSwingComponentInSwingNodeTest.java	Fri Jan 11 11:21:23 2019 +0530
@@ -0,0 +1,117 @@
+/*
+ * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.  Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+import java.awt.Color;
+import java.awt.Dimension;
+import java.awt.datatransfer.DataFlavor;
+import java.awt.dnd.DnDConstants;
+import java.awt.dnd.DropTarget;
+import java.awt.dnd.DropTargetAdapter;
+import java.awt.dnd.DropTargetDragEvent;
+import java.awt.dnd.DropTargetDropEvent;
+import javafx.application.Application;
+import javafx.application.Platform;
+import javafx.embed.swing.SwingNode;
+import javafx.scene.Scene;
+import javafx.scene.control.Button;
+import javafx.scene.control.Label;
+import javafx.scene.layout.HBox;
+import javafx.scene.layout.StackPane;
+import javafx.scene.layout.VBox;
+import javafx.stage.Stage;
+import javax.swing.JPanel;
+import javax.swing.SwingUtilities;
+
+public class DragDropOntoSwingComponentInSwingNodeTest extends Application
+{
+    public static void main(String[] args)
+    {
+        Application.launch(args);
+    }
+
+    @Override
+    public void start(Stage primaryStage) throws Exception
+    {
+    Button passButton = new Button("Pass");
+        Button failButton = new Button("Fail");
+        passButton.setOnAction(e -> this.quit());
+        failButton.setOnAction(e -> {
+            this.quit();
+            throw new AssertionError("Drag / drop onto a Swing component in a SwingNode not working");
+        });
+
+        SwingNode swingNode = new SwingNode();
+        StackPane pane = new StackPane(swingNode);
+        SwingUtilities.invokeLater( () -> {
+            swingNode.setContent(Content.createPanel());
+        });
+        VBox rootNode = new VBox(6,
+                new Label("1. This is a test for drag / drop onto a Swing component in a SwingNode."),
+                new Label("2. Select and drag some text/image from a browser or document and drop into red JPanel."),
+                new Label("3. When the content is dropped into JPanel, if it prints the mime dataflavor on console, click on Pass or else click on Fail"),
+                new Label(""),
+                new HBox(10, passButton, failButton), pane);
+
+
+        primaryStage.setScene(new Scene(rootNode));
+
+        primaryStage.setWidth(800);
+        primaryStage.setHeight(600);
+        primaryStage.show();
+    }
+
+    private void quit() {
+        Platform.exit();
+    }
+
+    static class Content {
+        public static JPanel createPanel()
+        {
+            JPanel panel = new JPanel();
+            panel.setPreferredSize(new Dimension(400, 400));
+            panel.setBackground(Color.RED);
+
+            panel.setDropTarget(new DropTarget(
+                      panel, DnDConstants.ACTION_COPY, new DropTargetAdapter()
+                {
+                    @Override
+                    public void dragEnter(DropTargetDragEvent dtde)
+                    {
+                        dtde.acceptDrag(dtde.getDropAction());
+                    }
+
+                    @Override
+                    public void drop(DropTargetDropEvent dtde)
+                    {
+                        for( DataFlavor dataFlavor : dtde.getCurrentDataFlavors() )
+                        {
+                            System.out.println(dataFlavor);
+                         }
+                    }
+                }));
+            return panel;
+        }
+    }
+}