forked from openjdk/jfx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
JFXPanel.java
1072 lines (969 loc) · 38.4 KB
/
JFXPanel.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2010, 2019, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package javafx.embed.swing;
import java.awt.AlphaComposite;
import java.awt.Component;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.KeyboardFocusManager;
import java.awt.Point;
import java.awt.Window;
import java.awt.Insets;
import java.awt.EventQueue;
import java.awt.SecondaryLoop;
import java.awt.GraphicsEnvironment;
import java.awt.event.AWTEventListener;
import java.awt.event.ComponentEvent;
import java.awt.event.FocusEvent;
import java.awt.event.HierarchyEvent;
import java.awt.event.InputEvent;
import java.awt.event.InputMethodEvent;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseWheelEvent;
import java.awt.event.InvocationEvent;
import java.awt.im.InputMethodRequests;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferInt;
import java.nio.IntBuffer;
import java.util.concurrent.atomic.AtomicInteger;
import java.security.AccessController;
import java.security.PrivilegedAction;
import javax.swing.JComponent;
import javax.swing.SwingUtilities;
import javafx.application.Platform;
import javafx.scene.Scene;
import com.sun.javafx.application.PlatformImpl;
import com.sun.javafx.cursor.CursorFrame;
import com.sun.javafx.stage.EmbeddedWindow;
import com.sun.javafx.tk.Toolkit;
import com.sun.javafx.PlatformUtil;
import com.sun.javafx.logging.PlatformLogger;
import com.sun.javafx.embed.AbstractEvents;
import com.sun.javafx.embed.EmbeddedSceneInterface;
import com.sun.javafx.embed.EmbeddedStageInterface;
import com.sun.javafx.embed.HostInterface;
import com.sun.javafx.embed.swing.SwingDnD;
import com.sun.javafx.embed.swing.SwingEvents;
import com.sun.javafx.embed.swing.SwingCursors;
import com.sun.javafx.embed.swing.SwingNodeHelper;
import com.sun.javafx.embed.swing.newimpl.JFXPanelInteropN;
/**
* {@code JFXPanel} is a component to embed JavaFX content into
* Swing applications. The content to be displayed is specified
* with the {@link #setScene} method that accepts an instance of
* JavaFX {@code Scene}. After the scene is assigned, it gets
* repainted automatically. All the input and focus events are
* forwarded to the scene transparently to the developer.
* <p>
* There are some restrictions related to {@code JFXPanel}. As a
* Swing component, it should only be accessed from the event
* dispatch thread, except the {@link #setScene} method, which can
* be called either on the event dispatch thread or on the JavaFX
* application thread.
* <p>
* Here is a typical pattern how {@code JFXPanel} can used:
* <pre>
* public class Test {
*
* private static void initAndShowGUI() {
* // This method is invoked on Swing thread
* JFrame frame = new JFrame("FX");
* final JFXPanel fxPanel = new JFXPanel();
* frame.add(fxPanel);
* frame.setVisible(true);
*
* Platform.runLater(new Runnable() {
* @Override
* public void run() {
* initFX(fxPanel);
* }
* });
* }
*
* private static void initFX(JFXPanel fxPanel) {
* // This method is invoked on JavaFX thread
* Scene scene = createScene();
* fxPanel.setScene(scene);
* }
*
* public static void main(String[] args) {
* SwingUtilities.invokeLater(new Runnable() {
* @Override
* public void run() {
* initAndShowGUI();
* }
* });
* }
* }
* </pre>
*
* @since JavaFX 2.0
*/
public class JFXPanel extends JComponent {
private final static PlatformLogger log = PlatformLogger.getLogger(JFXPanel.class.getName());
private static AtomicInteger instanceCount = new AtomicInteger(0);
private static PlatformImpl.FinishListener finishListener;
private transient HostContainer hostContainer;
private transient volatile EmbeddedWindow stage;
private transient volatile Scene scene;
// Accessed on EDT only
private transient SwingDnD dnd;
private transient EmbeddedStageInterface stagePeer;
private transient EmbeddedSceneInterface scenePeer;
// The logical size of the FX content
private int pWidth;
private int pHeight;
// The scale factor, used to translate b/w the logical (the FX content dimension)
// and physical (the back buffer's dimension) coordinate spaces
private double scaleFactorX = 1.0;
private double scaleFactorY = 1.0;
// Preferred size set from FX
private volatile int pPreferredWidth = -1;
private volatile int pPreferredHeight = -1;
// Cached copy of this component's location on screen to avoid
// calling getLocationOnScreen() under the tree lock on FX thread
private volatile int screenX = 0;
private volatile int screenY = 0;
// Accessed on EDT only
private BufferedImage pixelsIm;
private volatile float opacity = 1.0f;
// Indicates how many times setFxEnabled(false) has been called.
// A value of 0 means the component is enabled.
private AtomicInteger disableCount = new AtomicInteger(0);
private boolean isCapturingMouse = false;
private static boolean fxInitialized;
private JFXPanelInteropN jfxPanelIOP;
private synchronized void registerFinishListener() {
if (instanceCount.getAndIncrement() > 0) {
// Already registered
return;
}
// Need to install a finish listener to catch calls to Platform.exit
finishListener = new PlatformImpl.FinishListener() {
@Override public void idle(boolean implicitExit) {
}
@Override public void exitCalled() {
}
};
PlatformImpl.addListener(finishListener);
}
private synchronized void deregisterFinishListener() {
if (instanceCount.decrementAndGet() > 0) {
// Other JFXPanels still alive
return;
}
PlatformImpl.removeListener(finishListener);
finishListener = null;
}
// Initialize FX runtime when the JFXPanel instance is constructed
private synchronized static void initFx() {
// Note that calling PlatformImpl.startup more than once is OK
if (fxInitialized) {
return;
}
EventQueue eventQueue = AccessController.doPrivileged(
(PrivilegedAction<EventQueue>) java.awt.Toolkit
.getDefaultToolkit()::getSystemEventQueue);
if (eventQueue.isDispatchThread()) {
// We won't block EDT by FX initialization
SecondaryLoop secondaryLoop = eventQueue.createSecondaryLoop();
final Throwable[] th = {null};
new Thread(() -> {
try {
PlatformImpl.startup(() -> {});
} catch (Throwable t) {
th[0] = t;
} finally {
secondaryLoop.exit();
}
}).start();
secondaryLoop.enter();
if (th[0] != null) {
if (th[0] instanceof RuntimeException) {
throw (RuntimeException) th[0];
} else if (th[0] instanceof Error) {
throw (Error) th[0];
}
throw new RuntimeException("FX initialization failed", th[0]);
}
} else {
PlatformImpl.startup(() -> {});
}
fxInitialized = true;
}
/**
* Creates a new {@code JFXPanel} object.
* <p>
* <b>Implementation note</b>: when the first {@code JFXPanel} object
* is created, it implicitly initializes the JavaFX runtime. This is the
* preferred way to initialize JavaFX in Swing.
*/
public JFXPanel() {
super();
jfxPanelIOP = new JFXPanelInteropN();
initFx();
hostContainer = new HostContainer();
enableEvents(InputEvent.COMPONENT_EVENT_MASK |
InputEvent.FOCUS_EVENT_MASK |
InputEvent.HIERARCHY_BOUNDS_EVENT_MASK |
InputEvent.HIERARCHY_EVENT_MASK |
InputEvent.MOUSE_EVENT_MASK |
InputEvent.MOUSE_MOTION_EVENT_MASK |
InputEvent.MOUSE_WHEEL_EVENT_MASK |
InputEvent.KEY_EVENT_MASK |
InputEvent.INPUT_METHOD_EVENT_MASK);
setFocusable(true);
setFocusTraversalKeysEnabled(false);
}
/**
* Returns the JavaFX scene attached to this {@code JFXPanel}.
*
* @return the {@code Scene} attached to this {@code JFXPanel}
*/
public Scene getScene() {
return scene;
}
/**
* Attaches a {@code Scene} object to display in this {@code
* JFXPanel}. This method can be called either on the event
* dispatch thread or the JavaFX application thread.
*
* @param newScene a scene to display in this {@code JFXpanel}
*
* @see java.awt.EventQueue#isDispatchThread()
* @see javafx.application.Platform#isFxApplicationThread()
*/
public void setScene(final Scene newScene) {
if (Toolkit.getToolkit().isFxUserThread()) {
setSceneImpl(newScene);
} else {
EventQueue eventQueue = AccessController.doPrivileged(
(PrivilegedAction<EventQueue>) java.awt.Toolkit
.getDefaultToolkit()::getSystemEventQueue);
SecondaryLoop secondaryLoop = eventQueue.createSecondaryLoop();
if (secondaryLoop.enter()) {
Platform.runLater(() -> {
setSceneImpl(newScene);
});
secondaryLoop.exit();
}
}
}
/*
* Called on JavaFX app thread.
*/
private void setSceneImpl(Scene newScene) {
if ((stage != null) && (newScene == null)) {
stage.hide();
stage = null;
}
scene = newScene;
if ((stage == null) && (newScene != null)) {
stage = new EmbeddedWindow(hostContainer);
}
if (stage != null) {
stage.setScene(newScene);
if (!stage.isShowing()) {
stage.show();
}
}
}
/**
* {@code JFXPanel}'s opacity is controlled by the JavaFX content
* which is displayed in this component, so this method overrides
* {@link javax.swing.JComponent#setOpaque(boolean)} to only accept a
* {@code false} value. If this method is called with a {@code true}
* value, no action is performed.
*
* @param opaque must be {@code false}
*/
@Override
public final void setOpaque(boolean opaque) {
// Don't let user control opacity
if (!opaque) {
super.setOpaque(opaque);
}
}
/**
* {@code JFXPanel}'s opacity is controlled by the JavaFX content
* which is displayed in this component, so this method overrides
* {@link javax.swing.JComponent#isOpaque()} to always return a
* {@code false} value.
*
* @return a {@code false} value
*/
@Override
public final boolean isOpaque() {
return false;
}
private void sendMouseEventToFX(MouseEvent e) {
if (scenePeer == null || !isFxEnabled()) {
return;
}
// FX only supports 5 buttons so don't send the event for other buttons
switch (e.getID()) {
case MouseEvent.MOUSE_DRAGGED:
case MouseEvent.MOUSE_PRESSED:
case MouseEvent.MOUSE_RELEASED:
if (e.getButton() > 5) return;
break;
}
int extModifiers = e.getModifiersEx();
// Fix for RT-15457: we should report no mouse button upon mouse release, so
// *BtnDown values are calculated based on extMofifiers, not e.getButton()
boolean primaryBtnDown = (extModifiers & MouseEvent.BUTTON1_DOWN_MASK) != 0;
boolean middleBtnDown = (extModifiers & MouseEvent.BUTTON2_DOWN_MASK) != 0;
boolean secondaryBtnDown = (extModifiers & MouseEvent.BUTTON3_DOWN_MASK) != 0;
boolean backBtnDown = (extModifiers & MouseEvent.getMaskForButton(4)) != 0;
boolean forwardBtnDown = (extModifiers & MouseEvent.getMaskForButton(5)) != 0;
// Fix for RT-16558: if a PRESSED event is consumed, e.g. by a Swing Popup,
// subsequent DRAGGED and RELEASED events should not be sent to FX as well
if (e.getID() == MouseEvent.MOUSE_DRAGGED) {
if (!isCapturingMouse) {
return;
}
} else if (e.getID() == MouseEvent.MOUSE_PRESSED) {
isCapturingMouse = true;
} else if (e.getID() == MouseEvent.MOUSE_RELEASED) {
if (!isCapturingMouse) {
return;
}
isCapturingMouse = primaryBtnDown || middleBtnDown || secondaryBtnDown || backBtnDown || forwardBtnDown;
} else if (e.getID() == MouseEvent.MOUSE_CLICKED) {
// Don't send click events to FX, as they are generated in Scene
return;
}
// A workaround until JDK-8065131 is fixed.
boolean popupTrigger = false;
if (e.getID() == MouseEvent.MOUSE_PRESSED || e.getID() == MouseEvent.MOUSE_RELEASED) {
popupTrigger = e.isPopupTrigger();
}
if(e.getID() == MouseEvent.MOUSE_WHEEL) {
scenePeer.scrollEvent(AbstractEvents.MOUSEEVENT_VERTICAL_WHEEL,
0, -SwingEvents.getWheelRotation(e),
0, 0, // total scroll
40, 40, // multiplier
e.getX(), e.getY(),
e.getXOnScreen(), e.getYOnScreen(),
(extModifiers & MouseEvent.SHIFT_DOWN_MASK) != 0,
(extModifiers & MouseEvent.CTRL_DOWN_MASK) != 0,
(extModifiers & MouseEvent.ALT_DOWN_MASK) != 0,
(extModifiers & MouseEvent.META_DOWN_MASK) != 0, false);
} else {
scenePeer.mouseEvent(
SwingEvents.mouseIDToEmbedMouseType(e.getID()),
SwingEvents.mouseButtonToEmbedMouseButton(e.getButton(), extModifiers),
primaryBtnDown, middleBtnDown, secondaryBtnDown,
backBtnDown, forwardBtnDown,
e.getX(), e.getY(), e.getXOnScreen(), e.getYOnScreen(),
(extModifiers & MouseEvent.SHIFT_DOWN_MASK) != 0,
(extModifiers & MouseEvent.CTRL_DOWN_MASK) != 0,
(extModifiers & MouseEvent.ALT_DOWN_MASK) != 0,
(extModifiers & MouseEvent.META_DOWN_MASK) != 0,
popupTrigger);
}
if (e.isPopupTrigger()) {
scenePeer.menuEvent(e.getX(), e.getY(), e.getXOnScreen(), e.getYOnScreen(), false);
}
}
/**
* Overrides the {@link java.awt.Component#processMouseEvent(MouseEvent)}
* method to dispatch the mouse event to the JavaFX scene attached to this
* {@code JFXPanel}.
*
* @param e the mouse event to dispatch to the JavaFX scene
*/
@Override
protected void processMouseEvent(MouseEvent e) {
if ((e.getID() == MouseEvent.MOUSE_PRESSED) &&
(e.getButton() == MouseEvent.BUTTON1)) {
if (isFocusable() && !hasFocus()) {
requestFocus();
// The extra simulated mouse pressed event is removed by making the JavaFX scene focused.
// It is safe, because in JavaFX only the method "setFocused(true)" is called,
// which doesn't have any side-effects when called multiple times.
int focusCause = AbstractEvents.FOCUSEVENT_ACTIVATED;
stagePeer.setFocused(true, focusCause);
}
}
sendMouseEventToFX(e);
super.processMouseEvent(e);
}
/**
* Overrides the {@link java.awt.Component#processMouseMotionEvent(MouseEvent)}
* method to dispatch the mouse motion event to the JavaFX scene attached to
* this {@code JFXPanel}.
*
* @param e the mouse motion event to dispatch to the JavaFX scene
*/
@Override
protected void processMouseMotionEvent(MouseEvent e) {
sendMouseEventToFX(e);
super.processMouseMotionEvent(e);
}
/**
* Overrides the
* {@link java.awt.Component#processMouseWheelEvent(MouseWheelEvent)}
* method to dispatch the mouse wheel event to the JavaFX scene attached
* to this {@code JFXPanel}.
*
* @param e the mouse wheel event to dispatch to the JavaFX scene
*/
@Override
protected void processMouseWheelEvent(MouseWheelEvent e) {
sendMouseEventToFX(e);
super.processMouseWheelEvent(e);
}
private void sendKeyEventToFX(final KeyEvent e) {
if (scenePeer == null || !isFxEnabled()) {
return;
}
char[] chars = (e.getKeyChar() == KeyEvent.CHAR_UNDEFINED)
? new char[] {}
: new char[] { SwingEvents.keyCharToEmbedKeyChar(e.getKeyChar()) };
scenePeer.keyEvent(
SwingEvents.keyIDToEmbedKeyType(e.getID()),
e.getKeyCode(), chars,
SwingEvents.keyModifiersToEmbedKeyModifiers(e.getModifiersEx()));
}
/**
* Overrides the {@link java.awt.Component#processKeyEvent(KeyEvent)}
* method to dispatch the key event to the JavaFX scene attached to this
* {@code JFXPanel}.
*
* @param e the key event to dispatch to the JavaFX scene
*/
@Override
protected void processKeyEvent(KeyEvent e) {
sendKeyEventToFX(e);
super.processKeyEvent(e);
}
private void sendResizeEventToFX() {
if (stagePeer != null) {
stagePeer.setSize(pWidth, pHeight);
}
if (scenePeer != null) {
scenePeer.setSize(pWidth, pHeight);
}
}
/**
* Overrides the
* {@link java.awt.Component#processComponentEvent(ComponentEvent)}
* method to dispatch {@link java.awt.event.ComponentEvent#COMPONENT_RESIZED}
* events to the JavaFX scene attached to this {@code JFXPanel}. The JavaFX
* scene object is then resized to match the {@code JFXPanel} size.
*
* @param e the component event to dispatch to the JavaFX scene
*/
@Override
protected void processComponentEvent(ComponentEvent e) {
switch (e.getID()) {
case ComponentEvent.COMPONENT_RESIZED: {
updateComponentSize();
break;
}
case ComponentEvent.COMPONENT_MOVED: {
if (updateScreenLocation()) {
sendMoveEventToFX();
}
break;
}
default: {
break;
}
}
super.processComponentEvent(e);
}
// called on EDT only
private void updateComponentSize() {
int oldWidth = pWidth;
int oldHeight = pHeight;
// It's quite possible to get negative values here, this is not
// what JavaFX embedded scenes/stages are ready to
pWidth = Math.max(0, getWidth());
pHeight = Math.max(0, getHeight());
if (getBorder() != null) {
Insets i = getBorder().getBorderInsets(this);
pWidth -= (i.left + i.right);
pHeight -= (i.top + i.bottom);
}
double newScaleFactorX = scaleFactorX;
double newScaleFactorY = scaleFactorY;
Graphics g = getGraphics();
newScaleFactorX = GraphicsEnvironment.getLocalGraphicsEnvironment().
getDefaultScreenDevice().getDefaultConfiguration().
getDefaultTransform().getScaleX();
newScaleFactorY = GraphicsEnvironment.getLocalGraphicsEnvironment().
getDefaultScreenDevice().getDefaultConfiguration().
getDefaultTransform().getScaleY();
if (oldWidth != pWidth || oldHeight != pHeight ||
newScaleFactorX != scaleFactorX || newScaleFactorY != scaleFactorY)
{
createResizePixelBuffer(newScaleFactorX, newScaleFactorY);
if (scenePeer != null) {
scenePeer.setPixelScaleFactors((float) newScaleFactorX,
(float) newScaleFactorY);
}
scaleFactorX = newScaleFactorX;
scaleFactorY = newScaleFactorY;
sendResizeEventToFX();
}
}
// This methods should only be called on EDT
private boolean updateScreenLocation() {
synchronized (getTreeLock()) {
if (isShowing()) {
Point p = getLocationOnScreen();
screenX = p.x;
screenY = p.y;
return true;
}
}
return false;
}
private void sendMoveEventToFX() {
if (stagePeer == null) {
return;
}
stagePeer.setLocation(screenX, screenY);
}
/**
* Overrides the
* {@link java.awt.Component#processHierarchyBoundsEvent(HierarchyEvent)}
* method to process {@link java.awt.event.HierarchyEvent#ANCESTOR_MOVED}
* events and update the JavaFX scene location to match the {@code
* JFXPanel} location on the screen.
*
* @param e the hierarchy bounds event to process
*/
@Override
protected void processHierarchyBoundsEvent(HierarchyEvent e) {
if (e.getID() == HierarchyEvent.ANCESTOR_MOVED) {
if (updateScreenLocation()) {
sendMoveEventToFX();
}
}
super.processHierarchyBoundsEvent(e);
}
@Override
protected void processHierarchyEvent(HierarchyEvent e) {
if ((e.getChangeFlags() & HierarchyEvent.SHOWING_CHANGED) != 0) {
if (updateScreenLocation()) {
sendMoveEventToFX();
}
}
super.processHierarchyEvent(e);
}
private void sendFocusEventToFX(final FocusEvent e) {
if ((stage == null) || (stagePeer == null) || !isFxEnabled()) {
return;
}
boolean focused = (e.getID() == FocusEvent.FOCUS_GAINED);
int focusCause = (focused ? AbstractEvents.FOCUSEVENT_ACTIVATED :
AbstractEvents.FOCUSEVENT_DEACTIVATED);
if (focused) {
if (e.getCause() == FocusEvent.Cause.TRAVERSAL_FORWARD) {
focusCause = AbstractEvents.FOCUSEVENT_TRAVERSED_FORWARD;
} else if (e.getCause() == FocusEvent.Cause.TRAVERSAL_BACKWARD) {
focusCause = AbstractEvents.FOCUSEVENT_TRAVERSED_BACKWARD;
}
}
stagePeer.setFocused(focused, focusCause);
}
/**
* Overrides the
* {@link java.awt.Component#processFocusEvent(FocusEvent)}
* method to dispatch focus events to the JavaFX scene attached to this
* {@code JFXPanel}.
*
* @param e the focus event to dispatch to the JavaFX scene
*/
@Override
protected void processFocusEvent(FocusEvent e) {
sendFocusEventToFX(e);
super.processFocusEvent(e);
}
// called on EDT only
private void createResizePixelBuffer(double newScaleFactorX, double newScaleFactorY) {
if (scenePeer == null || pWidth <= 0 || pHeight <= 0) {
pixelsIm = null;
} else {
BufferedImage oldIm = pixelsIm;
int newPixelW = (int) Math.round(pWidth * newScaleFactorX);
int newPixelH = (int) Math.round(pHeight * newScaleFactorY);
pixelsIm = new BufferedImage(newPixelW, newPixelH,
SwingFXUtils.getBestBufferedImageType(
scenePeer.getPixelFormat(), null, false));
if (oldIm != null) {
double ratioX = newScaleFactorX / scaleFactorX;
double ratioY = newScaleFactorY / scaleFactorY;
// Transform old size to the new coordinate space.
int oldW = (int)Math.round(oldIm.getWidth() * ratioX);
int oldH = (int)Math.round(oldIm.getHeight() * ratioY);
Graphics g = pixelsIm.getGraphics();
try {
g.drawImage(oldIm, 0, 0, oldW, oldH, null);
} finally {
g.dispose();
}
}
}
}
@Override
protected void processInputMethodEvent(InputMethodEvent e) {
if (e.getID() == InputMethodEvent.INPUT_METHOD_TEXT_CHANGED) {
sendInputMethodEventToFX(e);
}
super.processInputMethodEvent(e);
}
private void sendInputMethodEventToFX(InputMethodEvent e) {
String t = InputMethodSupport.getTextForEvent(e);
int insertionIndex = 0;
if (e.getCaret() != null) {
insertionIndex = e.getCaret().getInsertionIndex();
}
scenePeer.inputMethodEvent(
javafx.scene.input.InputMethodEvent.INPUT_METHOD_TEXT_CHANGED,
InputMethodSupport.inputMethodEventComposed(t, e.getCommittedCharacterCount()),
t.substring(0, e.getCommittedCharacterCount()),
insertionIndex);
}
/**
* Overrides the {@link javax.swing.JComponent#paintComponent(Graphics)}
* method to paint the content of the JavaFX scene attached to this
* {@code JFXpanel}.
*
* @param g the Graphics context in which to paint
*
* @see #isOpaque()
*/
@Override
protected void paintComponent(Graphics g) {
if (scenePeer == null) {
return;
}
if (pixelsIm == null) {
createResizePixelBuffer(scaleFactorX, scaleFactorY);
if (pixelsIm == null) {
return;
}
}
DataBufferInt dataBuf = (DataBufferInt)pixelsIm.getRaster().getDataBuffer();
int[] pixelsData = dataBuf.getData();
IntBuffer buf = IntBuffer.wrap(pixelsData);
if (!scenePeer.getPixels(buf, pWidth, pHeight)) {
// In this case we just render what we have so far in the buffer.
}
Graphics gg = null;
try {
gg = g.create();
if ((opacity < 1.0f) && (gg instanceof Graphics2D)) {
Graphics2D g2d = (Graphics2D)gg;
AlphaComposite c = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, opacity);
g2d.setComposite(c);
}
if (getBorder() != null) {
Insets i = getBorder().getBorderInsets(this);
gg.translate(i.left, i.top);
}
gg.drawImage(pixelsIm, 0, 0, pWidth, pHeight, null);
double newScaleFactorX = scaleFactorX;
double newScaleFactorY = scaleFactorY;
newScaleFactorX = GraphicsEnvironment.getLocalGraphicsEnvironment().
getDefaultScreenDevice().getDefaultConfiguration().
getDefaultTransform().getScaleX();
newScaleFactorY = GraphicsEnvironment.getLocalGraphicsEnvironment().
getDefaultScreenDevice().getDefaultConfiguration().
getDefaultTransform().getScaleY();
if (scaleFactorX != newScaleFactorX || scaleFactorY != newScaleFactorY) {
createResizePixelBuffer(newScaleFactorX, newScaleFactorY);
// The scene will request repaint.
scenePeer.setPixelScaleFactors((float) newScaleFactorX,
(float) newScaleFactorY);
scaleFactorX = newScaleFactorX;
scaleFactorY = newScaleFactorY;
}
} catch (Throwable th) {
th.printStackTrace();
} finally {
if (gg != null) {
gg.dispose();
}
}
}
/**
* Returns the preferred size of this {@code JFXPanel}, either
* previously set with {@link #setPreferredSize(Dimension)} or
* based on the content of the JavaFX scene attached to this {@code
* JFXPanel}.
*
* @return prefSize this {@code JFXPanel} preferred size
*/
@Override
public Dimension getPreferredSize() {
if (isPreferredSizeSet() || scenePeer == null) {
return super.getPreferredSize();
}
return new Dimension(pPreferredWidth, pPreferredHeight);
}
private boolean isFxEnabled() {
return this.disableCount.get() == 0;
}
private void setFxEnabled(boolean enabled) {
if (!enabled) {
if (disableCount.incrementAndGet() == 1) {
if (dnd != null) {
dnd.removeNotify();
}
}
} else {
if (disableCount.get() == 0) {
//should report a warning about an extra enable call ?
return;
}
if (disableCount.decrementAndGet() == 0) {
if (dnd != null) {
dnd.addNotify();
}
}
}
}
private transient AWTEventListener ungrabListener = event -> {
if (jfxPanelIOP.isUngrabEvent(event)) {
SwingNodeHelper.runOnFxThread(() -> {
if (JFXPanel.this.stagePeer != null &&
getScene() != null &&
getScene().getFocusOwner() != null &&
getScene().getFocusOwner().isFocused()) {
JFXPanel.this.stagePeer.focusUngrab();
}
});
}
if (event instanceof MouseEvent) {
// Synthesize FOCUS_UNGRAB if user clicks the AWT top-level window
// that contains the JFXPanel.
if (event.getID() == MouseEvent.MOUSE_PRESSED && event.getSource() instanceof Component) {
final Window jfxPanelWindow = SwingUtilities.getWindowAncestor(JFXPanel.this);
final Component source = (Component)event.getSource();
final Window eventWindow = source instanceof Window ? (Window)source : SwingUtilities.getWindowAncestor(source);
if (jfxPanelWindow == eventWindow) {
SwingNodeHelper.runOnFxThread(() -> {
if (JFXPanel.this.stagePeer != null) {
// No need to check if grab is active or not.
// NoAutoHide popups don't request the grab and
// ignore the Ungrab event anyway.
// AutoHide popups actually should be hidden when
// user clicks some non-FX content, even if for
// some reason they didn't install the grab when
// they were shown.
JFXPanel.this.stagePeer.focusUngrab();
}
});
}
}
}
};
/**
* Notifies this component that it now has a parent component. When this
* method is invoked, the chain of parent components is set up with
* KeyboardAction event listeners.
*/
@Override
public void addNotify() {
super.addNotify();
registerFinishListener();
AccessController.doPrivileged((PrivilegedAction<Void>) () -> {
JFXPanel.this.getToolkit().addAWTEventListener(ungrabListener,
jfxPanelIOP.getMask());
return null;
});
updateComponentSize(); // see RT-23603
SwingNodeHelper.runOnFxThread(() -> {
if ((stage != null) && !stage.isShowing()) {
stage.show();
sendMoveEventToFX();
}
});
}
@Override
public InputMethodRequests getInputMethodRequests() {
EmbeddedSceneInterface scene = scenePeer;
if (scene == null) {
return null;
}
return new InputMethodSupport.InputMethodRequestsAdapter(scene.getInputMethodRequests());
}
/**
* Notifies this component that it no longer has a parent component.
* When this method is invoked, any KeyboardActions set up in the the
* chain of parent components are removed.
*/
@Override public void removeNotify() {
SwingNodeHelper.runOnFxThread(() -> {
if ((stage != null) && stage.isShowing()) {
stage.hide();
}
});
pixelsIm = null;
pWidth = 0;
pHeight = 0;
super.removeNotify();
AccessController.doPrivileged((PrivilegedAction<Void>) () -> {
JFXPanel.this.getToolkit().removeAWTEventListener(ungrabListener);
return null;
});
/* see CR 4867453 */
getInputContext().removeNotify(this);
deregisterFinishListener();
}
private void invokeOnClientEDT(Runnable r) {
jfxPanelIOP.postEvent(this, new InvocationEvent(this, r));
}
private class HostContainer implements HostInterface {
@Override
public void setEmbeddedStage(EmbeddedStageInterface embeddedStage) {
stagePeer = embeddedStage;
if (stagePeer == null) {
return;
}
if (pWidth > 0 && pHeight > 0) {
stagePeer.setSize(pWidth, pHeight);
}
invokeOnClientEDT(() -> {
if (stagePeer != null && JFXPanel.this.isFocusOwner()) {
stagePeer.setFocused(true, AbstractEvents.FOCUSEVENT_ACTIVATED);
}
});
sendMoveEventToFX();
}
@Override
public void setEmbeddedScene(EmbeddedSceneInterface embeddedScene) {
if (scenePeer == embeddedScene) {
return;
}
scenePeer = embeddedScene;
if (scenePeer == null) {
invokeOnClientEDT(() -> {
if (dnd != null) {
dnd.removeNotify();
dnd = null;
}
});
return;
}
if (pWidth > 0 && pHeight > 0) {
scenePeer.setSize(pWidth, pHeight);
}
scenePeer.setPixelScaleFactors((float) scaleFactorX, (float) scaleFactorY);
invokeOnClientEDT(() -> {
dnd = new SwingDnD(JFXPanel.this, scenePeer);
dnd.addNotify();
if (scenePeer != null) {
scenePeer.setDragStartListener(dnd.getDragStartListener());
}
});
}
@Override
public boolean requestFocus() {
return requestFocusInWindow();
}
@Override
public boolean traverseFocusOut(boolean forward) {
KeyboardFocusManager kfm = KeyboardFocusManager.getCurrentKeyboardFocusManager();
if (forward) {
kfm.focusNextComponent(JFXPanel.this);
} else {
kfm.focusPreviousComponent(JFXPanel.this);
}
return true;
}
@Override
public void setPreferredSize(final int width, final int height) {
invokeOnClientEDT(() -> {
JFXPanel.this.pPreferredWidth = width;