1 /*
   2  * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package javafx.embed.swing;
  27 
  28 import com.sun.javafx.tk.TKPulseListener;
  29 import java.awt.AWTException;
  30 import java.awt.Color;
  31 import java.awt.Robot;
  32 import javafx.animation.AnimationTimer;
  33 import javafx.scene.Scene;
  34 
  35 import javax.swing.SwingUtilities;
  36 
  37 import javafx.application.Application;
  38 import javafx.application.Platform;
  39 import javafx.scene.layout.Background;
  40 import javafx.scene.layout.BackgroundFill;
  41 import javafx.scene.layout.Pane;
  42 import javafx.scene.layout.StackPane;
  43 import javafx.stage.Stage;
  44 import javax.swing.JPanel;
  45 
  46 /**
  47  * RT-30650: SwingNode is not Resizable
  48  * 
  49  * The scenario: SwingNode contains JPanel. It should be initialized with the same layout bounds.
  50  * SwingNode is added to StackPane. The JPanel's max size is unbounded, so SwingNode is expected
  51  * to fill the whole space.
  52  */
  53 public class RT30650GUI extends Application {
  54     SwingNode swingNode;
  55     
  56     private final static int SIZE = 400;
  57     
  58     private static int pulseCount = 5;
  59     private static boolean passed;
  60         
  61     public static boolean test() {
  62         launch(new String[]{});
  63         return passed;
  64     }
  65 
  66     @Override
  67     public void start(final Stage stage) {
  68         
  69         // start constant pulse activity
  70         new AnimationTimer() {
  71             @Override public void handle(long l) {}
  72         }.start();
  73 
  74         swingNode = new SwingNode();
  75 
  76         Pane pane = new StackPane();
  77         pane.setBackground(new Background(new BackgroundFill(javafx.scene.paint.Color.GREEN, null, null)));
  78         pane.getChildren().add(swingNode);
  79         Scene scene = new Scene(pane, SIZE, SIZE);
  80 
  81         stage.setScene(scene);
  82         stage.setTitle("RT-30650");
  83         stage.show();
  84         
  85         SwingUtilities.invokeLater(new Runnable() {
  86             @Override
  87             public void run() {
  88                 JPanel panel = new JPanel();
  89                 panel.setBackground(Color.RED);
  90                 swingNode.setContent(panel);
  91                 
  92                 com.sun.javafx.tk.Toolkit.getToolkit().addSceneTkPulseListener(new TKPulseListener() {
  93                     @Override
  94                     public void pulse() {
  95                         if (--pulseCount == 0) {
  96                             SwingUtilities.invokeLater(new Runnable() {
  97                                 @Override
  98                                 public void run() {
  99                                     passed = testColor(stage);
 100                                     Platform.runLater(new Runnable() {
 101                                         @Override
 102                                         public void run() {
 103                                             stage.close();
 104                                         }
 105                                     });
 106                                 }
 107                             });
 108                         }
 109                     }
 110                 });
 111             }
 112         });        
 113     }
 114     
 115     public boolean testColor(Stage stage) {
 116         Robot r = null;
 117         try {
 118             r = new Robot();
 119         } catch (AWTException ex) {
 120             System.err.println("unexpected error: couldn't create java.awt.Robot: " + ex);
 121             return false;
 122         }
 123         
 124         int x = (int)stage.getX();
 125         int y = (int)stage.getY();
 126         
 127         Color color = r.getPixelColor(x + SIZE/2, y + SIZE/2);
 128         System.out.println("detected color: " + color);
 129 
 130         // On MacOSX the robot returns the color affected by the color profile.
 131         // And so the resulting color may differ from the requested one.
 132         // Here we have to check that the color is close to red rather than to green.
 133         return color.getRed() > 200 && color.getGreen() < 100;
 134     }
 135 }