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

Print this page




  23  * questions.
  24  */
  25 
  26 package javafx.embed.swing;
  27 
  28 import com.sun.javafx.geom.BaseBounds;
  29 import com.sun.javafx.geom.transform.BaseTransform;
  30 import com.sun.javafx.jmx.MXNodeAlgorithm;
  31 import com.sun.javafx.jmx.MXNodeAlgorithmContext;
  32 import com.sun.javafx.scene.DirtyBits;
  33 import com.sun.javafx.sg.prism.NGExternalNode;
  34 import com.sun.javafx.sg.prism.NGNode;
  35 import com.sun.javafx.stage.FocusUngrabEvent;
  36 import javafx.application.Platform;
  37 import javafx.beans.InvalidationListener;
  38 import javafx.beans.Observable;
  39 import javafx.beans.value.ChangeListener;
  40 import javafx.beans.value.ObservableValue;
  41 import javafx.event.EventHandler;
  42 import javafx.geometry.Point2D;
  43 import javafx.scene.Node;
  44 import javafx.scene.Scene;
  45 import javafx.scene.input.KeyCode;
  46 import javafx.scene.input.KeyEvent;
  47 import javafx.scene.input.MouseButton;
  48 import javafx.scene.input.MouseEvent;
  49 import javafx.stage.Window;
  50 import sun.awt.UngrabEvent;
  51 import sun.swing.JLightweightFrame;
  52 import sun.swing.LightweightContent;
  53 
  54 import javax.swing.*;
  55 import java.awt.*;
  56 import java.awt.event.WindowEvent;
  57 import java.awt.event.WindowFocusListener;
  58 import java.nio.IntBuffer;
  59 import java.security.AccessController;
  60 import java.security.PrivilegedAction;
  61 import java.util.ArrayList;
  62 import java.util.List;
  63 import java.util.concurrent.locks.ReentrantLock;
  64 


 624             Platform.runLater(new Runnable() {
 625                 @Override
 626                 public void run() {
 627                     SwingNode.this.maxWidth = width;
 628                     SwingNode.this.maxHeight = height;
 629                     SwingNode.this.impl_notifyLayoutBoundsChanged();
 630                 }
 631             });            
 632         }
 633         @Override
 634         public void minimumSizeChanged(final int width, final int height) {
 635             Platform.runLater(new Runnable() {
 636                 @Override
 637                 public void run() {
 638                     SwingNode.this.minWidth = width;
 639                     SwingNode.this.minHeight = height;
 640                     SwingNode.this.impl_notifyLayoutBoundsChanged();
 641                 }
 642             });            
 643         }




 644     }
 645 
 646     private void ungrabFocus(boolean postUngrabEvent) {
 647         if (grabbed &&
 648             getScene() != null &&
 649             getScene().getWindow() != null &&
 650             getScene().getWindow().impl_getPeer() != null)
 651         {
 652             skipBackwardUnrgabNotification = !postUngrabEvent;
 653             getScene().getWindow().impl_getPeer().ungrabFocus();
 654             skipBackwardUnrgabNotification = false;
 655             grabbed = false;
 656         }
 657     }
 658 
 659     private class PostEventAction implements PrivilegedAction<Void> {
 660         private AWTEvent event;
 661         public PostEventAction(AWTEvent event) {
 662             this.event = event;
 663         }
 664         @Override
 665         public Void run() {
 666             EventQueue eq = Toolkit.getDefaultToolkit().getSystemEventQueue();
 667             eq.postEvent(event);
 668             return null;
 669         }
 670     }
 671 
 672     private class SwingMouseEventHandler implements EventHandler<MouseEvent> {
 673         @Override
 674         public void handle(MouseEvent event) {
 675             if (event.getEventType() == MouseEvent.MOUSE_PRESSED &&





 676                 !SwingNode.this.isFocused() && SwingNode.this.isFocusTraversable())
 677             {
 678                 SwingNode.this.requestFocus();
 679             }
 680             JLightweightFrame frame = lwFrame;
 681             if (frame == null) {
 682                 return;
 683             }
 684             int swingID = SwingEvents.fxMouseEventTypeToMouseID(event);
 685             if (swingID < 0) {
 686                 return;
 687             }
 688             int swingModifiers = SwingEvents.fxMouseModsToMouseMods(event);
 689             // TODO: popupTrigger
 690             boolean swingPopupTrigger = event.getButton() == MouseButton.SECONDARY;
 691             int swingButton = SwingEvents.fxMouseButtonToMouseButton(event);
 692             long swingWhen = System.currentTimeMillis();
 693             java.awt.event.MouseEvent mouseEvent =
 694                     new java.awt.event.MouseEvent(
 695                         frame, swingID, swingWhen, swingModifiers,




  23  * questions.
  24  */
  25 
  26 package javafx.embed.swing;
  27 
  28 import com.sun.javafx.geom.BaseBounds;
  29 import com.sun.javafx.geom.transform.BaseTransform;
  30 import com.sun.javafx.jmx.MXNodeAlgorithm;
  31 import com.sun.javafx.jmx.MXNodeAlgorithmContext;
  32 import com.sun.javafx.scene.DirtyBits;
  33 import com.sun.javafx.sg.prism.NGExternalNode;
  34 import com.sun.javafx.sg.prism.NGNode;
  35 import com.sun.javafx.stage.FocusUngrabEvent;
  36 import javafx.application.Platform;
  37 import javafx.beans.InvalidationListener;
  38 import javafx.beans.Observable;
  39 import javafx.beans.value.ChangeListener;
  40 import javafx.beans.value.ObservableValue;
  41 import javafx.event.EventHandler;
  42 import javafx.geometry.Point2D;
  43 import javafx.scene.*;

  44 import javafx.scene.input.KeyCode;
  45 import javafx.scene.input.KeyEvent;
  46 import javafx.scene.input.MouseButton;
  47 import javafx.scene.input.MouseEvent;
  48 import javafx.stage.Window;
  49 import sun.awt.UngrabEvent;
  50 import sun.swing.JLightweightFrame;
  51 import sun.swing.LightweightContent;
  52 
  53 import javax.swing.*;
  54 import java.awt.*;
  55 import java.awt.event.WindowEvent;
  56 import java.awt.event.WindowFocusListener;
  57 import java.nio.IntBuffer;
  58 import java.security.AccessController;
  59 import java.security.PrivilegedAction;
  60 import java.util.ArrayList;
  61 import java.util.List;
  62 import java.util.concurrent.locks.ReentrantLock;
  63 


 623             Platform.runLater(new Runnable() {
 624                 @Override
 625                 public void run() {
 626                     SwingNode.this.maxWidth = width;
 627                     SwingNode.this.maxHeight = height;
 628                     SwingNode.this.impl_notifyLayoutBoundsChanged();
 629                 }
 630             });            
 631         }
 632         @Override
 633         public void minimumSizeChanged(final int width, final int height) {
 634             Platform.runLater(new Runnable() {
 635                 @Override
 636                 public void run() {
 637                     SwingNode.this.minWidth = width;
 638                     SwingNode.this.minHeight = height;
 639                     SwingNode.this.impl_notifyLayoutBoundsChanged();
 640                 }
 641             });            
 642         }
 643         @Override
 644         public void invokeOnContentsThread(Runnable runnable) {
 645             Platform.runLater(runnable);
 646         }
 647     }
 648 
 649     private void ungrabFocus(boolean postUngrabEvent) {
 650         if (grabbed &&
 651             getScene() != null &&
 652             getScene().getWindow() != null &&
 653             getScene().getWindow().impl_getPeer() != null)
 654         {
 655             skipBackwardUnrgabNotification = !postUngrabEvent;
 656             getScene().getWindow().impl_getPeer().ungrabFocus();
 657             skipBackwardUnrgabNotification = false;
 658             grabbed = false;
 659         }
 660     }
 661 
 662     private class PostEventAction implements PrivilegedAction<Void> {
 663         private AWTEvent event;
 664         public PostEventAction(AWTEvent event) {
 665             this.event = event;
 666         }
 667         @Override
 668         public Void run() {
 669             EventQueue eq = Toolkit.getDefaultToolkit().getSystemEventQueue();
 670             eq.postEvent(event);
 671             return null;
 672         }
 673     }
 674 
 675     private class SwingMouseEventHandler implements EventHandler<MouseEvent> {
 676         @Override
 677         public void handle(MouseEvent event) {
 678             // Disable the FX cursor updates when mouse enters SwingNode and enable bask when exits
 679             if (event.getEventType() == MouseEvent.MOUSE_ENTERED) {
 680                 getScene().getWindow().impl_getPeer().setUpdatesCursor(false);
 681             } else if (event.getEventType() == MouseEvent.MOUSE_EXITED) {
 682                 getScene().getWindow().impl_getPeer().setUpdatesCursor(true);
 683             } else if (event.getEventType() == MouseEvent.MOUSE_PRESSED &&
 684                 !SwingNode.this.isFocused() && SwingNode.this.isFocusTraversable())
 685             {
 686                 SwingNode.this.requestFocus();
 687             }
 688             JLightweightFrame frame = lwFrame;
 689             if (frame == null) {
 690                 return;
 691             }
 692             int swingID = SwingEvents.fxMouseEventTypeToMouseID(event);
 693             if (swingID < 0) {
 694                 return;
 695             }
 696             int swingModifiers = SwingEvents.fxMouseModsToMouseMods(event);
 697             // TODO: popupTrigger
 698             boolean swingPopupTrigger = event.getButton() == MouseButton.SECONDARY;
 699             int swingButton = SwingEvents.fxMouseButtonToMouseButton(event);
 700             long swingWhen = System.currentTimeMillis();
 701             java.awt.event.MouseEvent mouseEvent =
 702                     new java.awt.event.MouseEvent(
 703                         frame, swingID, swingWhen, swingModifiers,