modules/swing/src/main/java/javafx/embed/swing/SwingFXUtils.java

Print this page




  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 java.awt.AlphaComposite;
  29 import java.awt.EventQueue;
  30 import java.awt.Graphics2D;
  31 import java.awt.SecondaryLoop;
  32 import java.awt.image.BufferedImage;
  33 import java.lang.reflect.InvocationTargetException;
  34 import java.lang.reflect.Method;
  35 import java.nio.IntBuffer;
  36 import java.security.AccessController;
  37 import java.security.PrivilegedAction;


  38 import java.util.concurrent.atomic.AtomicBoolean;
  39 import javafx.application.Platform;
  40 import javafx.scene.image.Image;
  41 import javafx.scene.image.PixelFormat;
  42 import javafx.scene.image.PixelReader;
  43 import javafx.scene.image.PixelWriter;
  44 import javafx.scene.image.WritableImage;
  45 import javafx.scene.image.WritablePixelFormat;
  46 import com.sun.javafx.application.PlatformImpl;
  47 import com.sun.javafx.tk.Toolkit;
  48 import sun.awt.AWTAccessor;
  49 import sun.awt.FwDispatcher;
  50 import sun.awt.image.IntegerComponentRaster;
  51 
  52 import javax.swing.*;
  53 
  54 /**
  55  * This class provides utility methods for converting data types between
  56  * Swing/AWT and JavaFX formats.
  57  * @since JavaFX 2.2


 205     static void runOnFxThread(Runnable runnable) {
 206         if (Platform.isFxApplicationThread()) {
 207             runnable.run();
 208         } else {
 209             Platform.runLater(runnable);
 210         }
 211     }
 212 
 213     /**
 214      * If called from the event dispatch thread
 215      * invokes a runnable directly blocking the calling code
 216      * Otherwise
 217      * uses SwingUtilities.invokeLater without blocking
 218      */
 219     static void runOnEDT(final Runnable r) {
 220         if (SwingUtilities.isEventDispatchThread()) {
 221             r.run();
 222         } else {
 223             SwingUtilities.invokeLater(r);
 224         }































 225     }
 226 
 227     private static class FwSecondaryLoop implements SecondaryLoop {
 228 
 229         private final AtomicBoolean isRunning = new AtomicBoolean(false);
 230 
 231         @Override public boolean enter() {
 232             if (isRunning.compareAndSet(false, true)) {
 233                 PlatformImpl.runAndWait(() -> {
 234                     Toolkit.getToolkit().enterNestedEventLoop(FwSecondaryLoop.this);
 235                 });
 236                 return true;
 237             }
 238             return false;
 239         }
 240 
 241         @Override public boolean exit() {
 242             if (isRunning.compareAndSet(true, false)) {
 243                 PlatformImpl.runAndWait(() -> {
 244                     Toolkit.getToolkit().exitNestedEventLoop(FwSecondaryLoop.this, null);




  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 java.awt.AlphaComposite;
  29 import java.awt.EventQueue;
  30 import java.awt.Graphics2D;
  31 import java.awt.SecondaryLoop;
  32 import java.awt.image.BufferedImage;
  33 import java.lang.reflect.InvocationTargetException;
  34 import java.lang.reflect.Method;
  35 import java.nio.IntBuffer;
  36 import java.security.AccessController;
  37 import java.security.PrivilegedAction;
  38 import java.util.Set;
  39 import java.util.HashSet;
  40 import java.util.concurrent.atomic.AtomicBoolean;
  41 import javafx.application.Platform;
  42 import javafx.scene.image.Image;
  43 import javafx.scene.image.PixelFormat;
  44 import javafx.scene.image.PixelReader;
  45 import javafx.scene.image.PixelWriter;
  46 import javafx.scene.image.WritableImage;
  47 import javafx.scene.image.WritablePixelFormat;
  48 import com.sun.javafx.application.PlatformImpl;
  49 import com.sun.javafx.tk.Toolkit;
  50 import sun.awt.AWTAccessor;
  51 import sun.awt.FwDispatcher;
  52 import sun.awt.image.IntegerComponentRaster;
  53 
  54 import javax.swing.*;
  55 
  56 /**
  57  * This class provides utility methods for converting data types between
  58  * Swing/AWT and JavaFX formats.
  59  * @since JavaFX 2.2


 207     static void runOnFxThread(Runnable runnable) {
 208         if (Platform.isFxApplicationThread()) {
 209             runnable.run();
 210         } else {
 211             Platform.runLater(runnable);
 212         }
 213     }
 214 
 215     /**
 216      * If called from the event dispatch thread
 217      * invokes a runnable directly blocking the calling code
 218      * Otherwise
 219      * uses SwingUtilities.invokeLater without blocking
 220      */
 221     static void runOnEDT(final Runnable r) {
 222         if (SwingUtilities.isEventDispatchThread()) {
 223             r.run();
 224         } else {
 225             SwingUtilities.invokeLater(r);
 226         }
 227     }
 228 
 229     private static final Set<Object> eventLoopKeys = new HashSet<>();
 230 
 231     /**
 232      * The runnable is responsible for leaving the nested event loop.
 233      */
 234     static void runOnEDTAndWait(Object nestedLoopKey, Runnable r) {
 235         Toolkit.getToolkit().checkFxUserThread();
 236 
 237         if (SwingUtilities.isEventDispatchThread()) {
 238             r.run();
 239         } else {
 240             eventLoopKeys.add(nestedLoopKey);
 241             SwingUtilities.invokeLater(r);
 242             Toolkit.getToolkit().enterNestedEventLoop(nestedLoopKey);
 243         }
 244     }
 245 
 246     static void leaveFXNestedLoop(Object nestedLoopKey) {
 247         if (!eventLoopKeys.contains(nestedLoopKey)) return;
 248 
 249         if (Platform.isFxApplicationThread()) {
 250             Toolkit.getToolkit().exitNestedEventLoop(nestedLoopKey, null);
 251         } else {
 252             Platform.runLater(() -> {
 253                 Toolkit.getToolkit().exitNestedEventLoop(nestedLoopKey, null);
 254             });
 255         }
 256 
 257         eventLoopKeys.remove(nestedLoopKey);
 258     }
 259 
 260     private static class FwSecondaryLoop implements SecondaryLoop {
 261 
 262         private final AtomicBoolean isRunning = new AtomicBoolean(false);
 263 
 264         @Override public boolean enter() {
 265             if (isRunning.compareAndSet(false, true)) {
 266                 PlatformImpl.runAndWait(() -> {
 267                     Toolkit.getToolkit().enterNestedEventLoop(FwSecondaryLoop.this);
 268                 });
 269                 return true;
 270             }
 271             return false;
 272         }
 273 
 274         @Override public boolean exit() {
 275             if (isRunning.compareAndSet(true, false)) {
 276                 PlatformImpl.runAndWait(() -> {
 277                     Toolkit.getToolkit().exitNestedEventLoop(FwSecondaryLoop.this, null);