1 /* 2 * Copyright (c) 2014, 2015, 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 test.sandbox.app; 27 28 import java.awt.BorderLayout; 29 import java.awt.Dimension; 30 import java.awt.FlowLayout; 31 import java.util.concurrent.atomic.AtomicBoolean; 32 import java.util.concurrent.atomic.AtomicReference; 33 import javafx.application.Platform; 34 import javafx.embed.swing.JFXPanel; 35 import javafx.scene.Scene; 36 import javax.swing.JButton; 37 import javax.swing.JFrame; 38 import javax.swing.JPanel; 39 import javax.swing.SwingUtilities; 40 import javax.swing.Timer; 41 42 import static test.sandbox.Constants.*; 43 44 /** 45 * JFXPanel application to test running with a security manager installed. 46 * The main program in this class will run the test such that the application 47 * will be shutdown by an explicit call to System.exit(). 48 */ 49 public class JFXPanelApp { 50 51 private final AtomicBoolean createSceneDone = new AtomicBoolean(false); 52 private final AtomicReference<Throwable> err = new AtomicReference<>(null); 53 54 private void initApp(final boolean implicitExit) throws Exception { 55 final JFrame frame = new JFrame("JFXPanel Test"); 56 frame.setLayout(new BorderLayout()); 57 58 JPanel swingPanel = new JPanel(); 59 swingPanel.setLayout(new FlowLayout()); 60 frame.getContentPane().add(swingPanel, BorderLayout.NORTH); 61 62 JButton swingButton = new JButton("Swing Button"); 63 swingButton.addActionListener(e -> System.err.println("Hi")); 64 swingPanel.add(swingButton); 65 66 // Create javafx panel 67 final JFXPanel jfxPanel = new JFXPanel(); 68 jfxPanel.setPreferredSize(new Dimension(400,300)); 69 frame.getContentPane().add(jfxPanel, BorderLayout.CENTER); 70 71 // create JavaFX scene 72 createScene(jfxPanel); 73 if (!implicitExit) { 74 Platform.setImplicitExit(false); 75 } 76 77 // show frame 78 frame.pack(); 79 frame.setVisible(true); 80 81 // Hide the frame after the specified amount of time 82 Timer timer = new Timer(SHOWTIME, e -> { 83 // Verify that the FX scene was created successfully 84 if (!createSceneDone.get()) { 85 System.exit(ERROR_TIMEOUT); 86 } 87 Throwable t = err.get(); 88 if (t != null) { 89 if (t instanceof SecurityException) { 90 System.exit(ERROR_SECURITY_EXCEPTION); 91 } else if (t instanceof ExceptionInInitializerError) { 92 Throwable cause = t.getCause(); 93 if (cause instanceof SecurityException) { 94 System.exit(ERROR_SECURITY_EXCEPTION); 95 } 96 } 97 System.exit(ERROR_UNEXPECTED_EXCEPTION); 98 } 99 100 if (implicitExit) { 101 frame.setVisible(false); 102 frame.dispose(); 103 } else { 104 System.exit(ERROR_NONE); 105 } 106 }); 107 timer.setRepeats(false); 108 timer.start(); 109 } 110 111 private void createScene(final JFXPanel jfxPanel) throws Exception { 112 Platform.runLater(() -> { 113 try { 114 final Scene scene = Util.createScene(); 115 jfxPanel.setScene(scene); 116 } catch (Error | Exception t) { 117 t.printStackTrace(); 118 err.set(t); 119 } finally { 120 createSceneDone.set(true); 121 } 122 }); 123 } 124 125 public JFXPanelApp(boolean implicitExit) { 126 try { 127 try { 128 // Ensure that we are running with a restrictive 129 // security manager 130 System.getProperty("sun.something"); 131 System.err.println("*** Did not get expected security exception"); 132 System.exit(ERROR_NO_SECURITY_EXCEPTION); 133 } catch (SecurityException ex) { 134 // This is expected 135 } 136 initApp(implicitExit); 137 } catch (SecurityException ex) { 138 ex.printStackTrace(System.err); 139 System.exit(ERROR_SECURITY_EXCEPTION); 140 } catch (ExceptionInInitializerError ex) { 141 Throwable cause = ex.getCause(); 142 if (cause instanceof SecurityException) { 143 System.exit(ERROR_SECURITY_EXCEPTION); 144 } 145 System.exit(ERROR_UNEXPECTED_EXCEPTION); 146 } catch (Error | Exception ex) { 147 ex.printStackTrace(System.err); 148 System.exit(ERROR_UNEXPECTED_EXCEPTION); 149 } 150 } 151 152 public static void runTest(final boolean implicitExit) { 153 Util.setupTimeoutThread(); 154 SwingUtilities.invokeLater(() -> new JFXPanelApp(implicitExit)); 155 } 156 157 public static void main(String[] args) { 158 runTest(false); 159 } 160 161 }