import com.sun.glass.ui.Robot; import java.awt.event.HierarchyBoundsListener; import java.awt.event.HierarchyEvent; import java.awt.event.MouseEvent; import java.util.concurrent.Semaphore; import javafx.application.Application; import javafx.embed.swing.SwingNode; import javafx.event.EventHandler; import javafx.scene.Scene; import javafx.scene.layout.StackPane; import javafx.stage.Stage; import javafx.stage.WindowEvent; import javax.swing.JButton; import javax.swing.JCheckBox; import javax.swing.JComponent; import javax.swing.JLabel; import javax.swing.JTextField; import javax.swing.SwingUtilities; /** * * @author aeremeev */ public class SwingNodeHierarchyBoundsListenerTest extends Application { private Semaphore semaphore = new Semaphore(1); private JComponent[] components = new JComponent[]{ new JButton(), new JTextField(), new JCheckBox(), new JLabel() }; private Robot robot; private boolean ancestorMoved; private boolean ancestorResized; private Robot createRobot() { return com.sun.glass.ui.Application.GetApplication().createRobot(); } @Override public void start(final Stage stage) throws Exception { StackPane pane = new StackPane(); Scene scene = new Scene(pane); final SwingNode swingNode = new SwingNode(); pane.getChildren().add(swingNode); stage.setScene(scene); robot = createRobot(); stage.setOnShown(new EventHandler() { @Override public void handle(WindowEvent t) { new Thread(new Runnable() { @Override public void run() { int failCount = 0; try { System.out.println("Started"); for (JComponent c : components) { System.out.println("Testing component : " + c); invokeAndWait(new Runnable() { @Override public void run() { robot.mouseMove(0, 0); } }); invokeAndWait(new Runnable() { @Override public void run() { stage.setX(50); stage.setY(50); stage.setHeight(200); stage.setWidth(200); } }); final int x = (int) stage.getX(); final int y = (int) stage.getY(); final int width = (int) stage.getWidth(); final int height = (int) stage.getHeight(); init(swingNode, c); semaphore.acquire(); delay(1000); reset(); invokeAndWait(new Runnable() { @Override public void run() { robot.mouseMove(x + width - 1, y + height - 1); robot.mousePress(MouseEvent.BUTTON1); } }); delay(50); for (int i = 0; i < 20; ++i) { final int index = i; invokeAndWait(new Runnable() { @Override public void run() { robot.mouseMove(x + width + 5 * index, y + height + 5 * index); } }); delay(50); } if (!ancestorResized) { synchronized (c) { c.wait(2500); } } if (!ancestorResized) { System.err.println("Swing component did not get ancestor resized event : " + c); ++failCount; } if (ancestorMoved) { System.err.println("Swing component got ancestor moved event : " + c); ++failCount; } invokeAndWait(new Runnable() { @Override public void run() { robot.mouseRelease(MouseEvent.BUTTON1); } }); invokeAndWait(new Runnable() { @Override public void run() { robot.mouseMove(x + width / 2, y + 15); robot.mousePress(MouseEvent.BUTTON1); } }); for (int i = 0; i < 20; ++i) { final int index = i; invokeAndWait(new Runnable() { @Override public void run() { robot.mouseMove(x + width / 2 + 5 * index, y + 15); } }); delay(50); } invokeAndWait(new Runnable() { @Override public void run() { robot.mouseRelease(MouseEvent.BUTTON1); } }); if (!ancestorMoved) { synchronized (c) { c.wait(2500); } } if (!ancestorMoved) { System.err.println("Swing component did not get ancestor moved event : " + c); ++failCount; } if (ancestorResized) { System.err.println("Swing component got ancestor resized event : " + c); ++failCount; } semaphore.release(); } } catch (Exception e) { e.printStackTrace(); System.exit(1); } if (failCount > 0) { System.exit(1); } else { System.exit(0); } } }).start(); } }); stage.show(); } private void invokeAndWait(Runnable run) { com.sun.glass.ui.Application.invokeAndWait(run); } private void reset() { ancestorResized = false; ancestorMoved = false; } private void delay(int time) throws InterruptedException { Thread.sleep(time); } private void init(final SwingNode swing, final JComponent component) { semaphore.acquireUninterruptibly(); SwingUtilities.invokeLater(new Runnable() { @Override public void run() { component.addHierarchyBoundsListener(new HierarchyBoundsListener() { @Override public void ancestorMoved(HierarchyEvent e) { synchronized (component) { ancestorMoved = true; component.notifyAll(); } } @Override public void ancestorResized(HierarchyEvent e) { synchronized (component) { ancestorResized = true; component.notifyAll(); } } }); swing.setContent(component); semaphore.release(); } }); } public static void main(String[] args) { launch(args); } }