Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.desktop/macosx/classes/sun/lwawt/LWWindowPeer.java
41153 views
1
/*
2
* Copyright (c) 2011, 2020, 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 sun.lwawt;
27
28
import java.awt.AlphaComposite;
29
import java.awt.Color;
30
import java.awt.Component;
31
import java.awt.Dialog;
32
import java.awt.Dimension;
33
import java.awt.Font;
34
import java.awt.FontMetrics;
35
import java.awt.Frame;
36
import java.awt.Graphics;
37
import java.awt.Graphics2D;
38
import java.awt.GraphicsConfiguration;
39
import java.awt.GraphicsDevice;
40
import java.awt.GraphicsEnvironment;
41
import java.awt.Insets;
42
import java.awt.KeyboardFocusManager;
43
import java.awt.MenuBar;
44
import java.awt.Point;
45
import java.awt.Rectangle;
46
import java.awt.Shape;
47
import java.awt.SystemColor;
48
import java.awt.Toolkit;
49
import java.awt.Window;
50
import java.awt.event.FocusEvent;
51
import java.awt.event.KeyEvent;
52
import java.awt.event.MouseEvent;
53
import java.awt.event.MouseWheelEvent;
54
import java.awt.event.WindowEvent;
55
import java.awt.peer.ComponentPeer;
56
import java.awt.peer.DialogPeer;
57
import java.awt.peer.FramePeer;
58
import java.awt.peer.KeyboardFocusManagerPeer;
59
import java.awt.peer.WindowPeer;
60
import java.util.List;
61
62
import javax.swing.JComponent;
63
64
import sun.awt.AWTAccessor;
65
import sun.awt.AWTAccessor.ComponentAccessor;
66
import sun.awt.AppContext;
67
import sun.awt.CGraphicsDevice;
68
import sun.awt.DisplayChangedListener;
69
import sun.awt.ExtendedKeyCodes;
70
import sun.awt.FullScreenCapable;
71
import sun.awt.SunToolkit;
72
import sun.awt.TimedWindowEvent;
73
import sun.awt.UngrabEvent;
74
import sun.java2d.NullSurfaceData;
75
import sun.java2d.SunGraphics2D;
76
import sun.java2d.SunGraphicsEnvironment;
77
import sun.java2d.SurfaceData;
78
import sun.java2d.loops.Blit;
79
import sun.java2d.loops.CompositeType;
80
import sun.java2d.pipe.Region;
81
import sun.util.logging.PlatformLogger;
82
83
public class LWWindowPeer
84
extends LWContainerPeer<Window, JComponent>
85
implements FramePeer, DialogPeer, FullScreenCapable, DisplayChangedListener, PlatformEventNotifier
86
{
87
public enum PeerType {
88
SIMPLEWINDOW,
89
FRAME,
90
DIALOG,
91
EMBEDDED_FRAME,
92
VIEW_EMBEDDED_FRAME,
93
LW_FRAME
94
}
95
96
private static final PlatformLogger focusLog = PlatformLogger.getLogger("sun.lwawt.focus.LWWindowPeer");
97
98
private final PlatformWindow platformWindow;
99
100
private static final int MINIMUM_WIDTH = 1;
101
private static final int MINIMUM_HEIGHT = 1;
102
103
private Insets insets = new Insets(0, 0, 0, 0);
104
private Rectangle maximizedBounds;
105
106
private GraphicsDevice graphicsDevice;
107
private GraphicsConfiguration graphicsConfig;
108
109
private SurfaceData surfaceData;
110
private final Object surfaceDataLock = new Object();
111
112
private volatile int windowState = Frame.NORMAL;
113
114
// check that the mouse is over the window
115
private volatile boolean isMouseOver = false;
116
117
// A peer where the last mouse event came to. Used by cursor manager to
118
// find the component under cursor
119
private static volatile LWComponentPeer<?, ?> lastCommonMouseEventPeer;
120
121
// A peer where the last mouse event came to. Used to generate
122
// MOUSE_ENTERED/EXITED notifications
123
private volatile LWComponentPeer<?, ?> lastMouseEventPeer;
124
125
// Peers where all dragged/released events should come to,
126
// depending on what mouse button is being dragged according to Cocoa
127
private static final LWComponentPeer<?, ?>[] mouseDownTarget = new LWComponentPeer<?, ?>[3];
128
129
// A bitmask that indicates what mouse buttons produce MOUSE_CLICKED events
130
// on MOUSE_RELEASE. Click events are only generated if there were no drag
131
// events between MOUSE_PRESSED and MOUSE_RELEASED for particular button
132
private static int mouseClickButtons = 0;
133
134
private volatile boolean isOpaque = true;
135
136
private static final Font DEFAULT_FONT = new Font("Lucida Grande", Font.PLAIN, 13);
137
138
private static LWWindowPeer grabbingWindow;
139
140
private volatile boolean skipNextFocusChange;
141
142
private static final Color nonOpaqueBackground = new Color(0, 0, 0, 0);
143
144
private volatile boolean textured;
145
146
private final PeerType peerType;
147
148
private final SecurityWarningWindow warningWindow;
149
150
private volatile boolean targetFocusable;
151
152
/**
153
* Current modal blocker or null.
154
*
155
* Synchronization: peerTreeLock.
156
*/
157
private LWWindowPeer blocker;
158
159
public LWWindowPeer(Window target, PlatformComponent platformComponent,
160
PlatformWindow platformWindow, PeerType peerType)
161
{
162
super(target, platformComponent);
163
this.platformWindow = platformWindow;
164
this.peerType = peerType;
165
166
Window owner = target.getOwner();
167
LWWindowPeer ownerPeer = owner == null ? null :
168
(LWWindowPeer) AWTAccessor.getComponentAccessor().getPeer(owner);
169
PlatformWindow ownerDelegate = (ownerPeer != null) ? ownerPeer.getPlatformWindow() : null;
170
171
// The delegate.initialize() needs a non-null GC on X11.
172
GraphicsConfiguration gc = getTarget().getGraphicsConfiguration();
173
synchronized (getStateLock()) {
174
// graphicsConfig should be updated according to the real window
175
// bounds when the window is shown, see 4868278
176
this.graphicsConfig = gc;
177
}
178
179
if (!target.isFontSet()) {
180
target.setFont(DEFAULT_FONT);
181
}
182
183
if (!target.isBackgroundSet()) {
184
target.setBackground(SystemColor.window);
185
} else {
186
// first we check if user provided alpha for background. This is
187
// similar to what Apple's Java do.
188
// Since JDK7 we should rely on setOpacity() only.
189
// this.opacity = c.getAlpha();
190
}
191
192
if (!target.isForegroundSet()) {
193
target.setForeground(SystemColor.windowText);
194
// we should not call setForeground because it will call a repaint
195
// which the peer may not be ready to do yet.
196
}
197
198
platformWindow.initialize(target, this, ownerDelegate);
199
200
// Init warning window(for applets)
201
SecurityWarningWindow warn = null;
202
if (target.getWarningString() != null) {
203
// accessSystemTray permission allows to display TrayIcon, TrayIcon tooltip
204
// and TrayIcon balloon windows without a warning window.
205
if (!AWTAccessor.getWindowAccessor().isTrayIconWindow(target)) {
206
LWToolkit toolkit = (LWToolkit)Toolkit.getDefaultToolkit();
207
warn = toolkit.createSecurityWarning(target, this);
208
}
209
}
210
211
warningWindow = warn;
212
}
213
214
@Override
215
void initializeImpl() {
216
super.initializeImpl();
217
218
219
if (getTarget() instanceof Frame) {
220
Frame frame = (Frame) getTarget();
221
setTitle(frame.getTitle());
222
setState(frame.getExtendedState());
223
setMaximizedBounds(frame.getMaximizedBounds());
224
} else if (getTarget() instanceof Dialog) {
225
setTitle(((Dialog) getTarget()).getTitle());
226
}
227
228
updateAlwaysOnTopState();
229
updateMinimumSize();
230
updateFocusableWindowState();
231
232
final Shape shape = getTarget().getShape();
233
if (shape != null) {
234
applyShape(Region.getInstance(shape, null));
235
}
236
237
final float opacity = getTarget().getOpacity();
238
if (opacity < 1.0f) {
239
setOpacity(opacity);
240
}
241
242
setOpaque(getTarget().isOpaque());
243
244
updateInsets(platformWindow.getInsets());
245
if (getSurfaceData() == null) {
246
replaceSurfaceData(false);
247
}
248
activateDisplayListener();
249
}
250
251
// Just a helper method
252
@Override
253
public PlatformWindow getPlatformWindow() {
254
return platformWindow;
255
}
256
257
@Override
258
protected LWWindowPeer getWindowPeerOrSelf() {
259
return this;
260
}
261
262
// ---- PEER METHODS ---- //
263
264
@Override
265
protected void disposeImpl() {
266
deactivateDisplayListener();
267
SurfaceData oldData = getSurfaceData();
268
synchronized (surfaceDataLock){
269
surfaceData = null;
270
}
271
if (oldData != null) {
272
oldData.invalidate();
273
}
274
if (isGrabbing()) {
275
ungrab();
276
}
277
if (warningWindow != null) {
278
warningWindow.dispose();
279
}
280
281
platformWindow.dispose();
282
super.disposeImpl();
283
}
284
285
@Override
286
protected void setVisibleImpl(final boolean visible) {
287
if (!visible && warningWindow != null) {
288
warningWindow.setVisible(false, false);
289
}
290
updateFocusableWindowState();
291
super.setVisibleImpl(visible);
292
// TODO: update graphicsConfig, see 4868278
293
platformWindow.setVisible(visible);
294
if (isSimpleWindow()) {
295
KeyboardFocusManagerPeer kfmPeer = LWKeyboardFocusManagerPeer.getInstance();
296
if (visible) {
297
if (!getTarget().isAutoRequestFocus()) {
298
return;
299
} else {
300
requestWindowFocus(FocusEvent.Cause.ACTIVATION);
301
}
302
// Focus the owner in case this window is focused.
303
} else if (kfmPeer.getCurrentFocusedWindow() == getTarget()) {
304
// Transfer focus to the owner.
305
LWWindowPeer owner = getOwnerFrameDialog(LWWindowPeer.this);
306
if (owner != null) {
307
owner.requestWindowFocus(FocusEvent.Cause.ACTIVATION);
308
}
309
}
310
}
311
}
312
313
@Override
314
public final GraphicsConfiguration getGraphicsConfiguration() {
315
synchronized (getStateLock()) {
316
return graphicsConfig;
317
}
318
}
319
320
@Override
321
public boolean updateGraphicsData(GraphicsConfiguration gc) {
322
setGraphicsConfig(gc);
323
return false;
324
}
325
326
protected final Graphics getOnscreenGraphics(Color fg, Color bg, Font f) {
327
SurfaceData surfaceData = getSurfaceData();
328
if (surfaceData == null) {
329
return null;
330
}
331
if (fg == null) {
332
fg = SystemColor.windowText;
333
}
334
if (bg == null) {
335
bg = SystemColor.window;
336
}
337
if (f == null) {
338
f = DEFAULT_FONT;
339
}
340
return new SunGraphics2D(surfaceData, fg, bg, f);
341
}
342
343
@Override
344
public void setBounds(int x, int y, int w, int h, int op) {
345
346
if((op & NO_EMBEDDED_CHECK) == 0 && getPeerType() == PeerType.VIEW_EMBEDDED_FRAME) {
347
return;
348
}
349
350
if ((op & SET_CLIENT_SIZE) != 0) {
351
// SET_CLIENT_SIZE is only applicable to window peers, so handle it here
352
// instead of pulling 'insets' field up to LWComponentPeer
353
// no need to add insets since Window's notion of width and height includes insets.
354
op &= ~SET_CLIENT_SIZE;
355
op |= SET_SIZE;
356
}
357
358
// Don't post ComponentMoved/Resized and Paint events
359
// until we've got a notification from the delegate
360
Rectangle cb = constrainBounds(x, y, w, h);
361
362
Rectangle newBounds = new Rectangle(getBounds());
363
if ((op & (SET_LOCATION | SET_BOUNDS)) != 0) {
364
newBounds.x = cb.x;
365
newBounds.y = cb.y;
366
}
367
if ((op & (SET_SIZE | SET_BOUNDS)) != 0) {
368
newBounds.width = cb.width;
369
newBounds.height = cb.height;
370
}
371
// Native system could constraint bounds, so the peer wold be updated in the callback
372
platformWindow.setBounds(newBounds.x, newBounds.y, newBounds.width, newBounds.height);
373
}
374
375
public Rectangle constrainBounds(Rectangle bounds) {
376
return constrainBounds(bounds.x, bounds.y, bounds.width, bounds.height);
377
}
378
379
public Rectangle constrainBounds(int x, int y, int w, int h) {
380
381
if (w < MINIMUM_WIDTH) {
382
w = MINIMUM_WIDTH;
383
}
384
385
if (h < MINIMUM_HEIGHT) {
386
h = MINIMUM_HEIGHT;
387
}
388
389
final int maxW = getLWGC().getMaxTextureWidth();
390
final int maxH = getLWGC().getMaxTextureHeight();
391
392
if (w > maxW) {
393
w = maxW;
394
}
395
if (h > maxH) {
396
h = maxH;
397
}
398
399
return new Rectangle(x, y, w, h);
400
}
401
402
@Override
403
public Point getLocationOnScreen() {
404
return platformWindow.getLocationOnScreen();
405
}
406
407
/**
408
* Overridden from LWContainerPeer to return the correct insets.
409
* Insets are queried from the delegate and are kept up to date by
410
* requiering when needed (i.e. when the window geometry is changed).
411
*/
412
@Override
413
public Insets getInsets() {
414
synchronized (getStateLock()) {
415
return insets;
416
}
417
}
418
419
@Override
420
public FontMetrics getFontMetrics(Font f) {
421
// TODO: check for "use platform metrics" settings
422
return platformWindow.getFontMetrics(f);
423
}
424
425
@Override
426
public void toFront() {
427
platformWindow.toFront();
428
}
429
430
@Override
431
public void toBack() {
432
platformWindow.toBack();
433
}
434
435
@Override
436
public void setZOrder(ComponentPeer above) {
437
throw new RuntimeException("not implemented");
438
}
439
440
@Override
441
public void updateAlwaysOnTopState() {
442
platformWindow.setAlwaysOnTop(getTarget().isAlwaysOnTop());
443
}
444
445
@Override
446
public void updateFocusableWindowState() {
447
targetFocusable = getTarget().isFocusableWindow();
448
platformWindow.updateFocusableWindowState();
449
}
450
451
@Override
452
public void setModalBlocked(Dialog blocker, boolean blocked) {
453
synchronized (getPeerTreeLock()) {
454
ComponentPeer peer = AWTAccessor.getComponentAccessor().getPeer(blocker);
455
if (blocked && (peer instanceof LWWindowPeer)) {
456
this.blocker = (LWWindowPeer) peer;
457
} else {
458
this.blocker = null;
459
}
460
}
461
462
platformWindow.setModalBlocked(blocked);
463
}
464
465
@Override
466
public void updateMinimumSize() {
467
final Dimension min;
468
if (getTarget().isMinimumSizeSet()) {
469
min = getTarget().getMinimumSize();
470
min.width = Math.max(min.width, MINIMUM_WIDTH);
471
min.height = Math.max(min.height, MINIMUM_HEIGHT);
472
} else {
473
min = new Dimension(MINIMUM_WIDTH, MINIMUM_HEIGHT);
474
}
475
476
final Dimension max;
477
if (getTarget().isMaximumSizeSet()) {
478
max = getTarget().getMaximumSize();
479
max.width = Math.min(max.width, getLWGC().getMaxTextureWidth());
480
max.height = Math.min(max.height, getLWGC().getMaxTextureHeight());
481
} else {
482
max = new Dimension(getLWGC().getMaxTextureWidth(),
483
getLWGC().getMaxTextureHeight());
484
}
485
486
platformWindow.setSizeConstraints(min.width, min.height, max.width, max.height);
487
}
488
489
@Override
490
public void updateIconImages() {
491
getPlatformWindow().updateIconImages();
492
}
493
494
@Override
495
public void setOpacity(float opacity) {
496
getPlatformWindow().setOpacity(opacity);
497
repaintPeer();
498
}
499
500
@Override
501
public final void setOpaque(final boolean isOpaque) {
502
if (this.isOpaque != isOpaque) {
503
this.isOpaque = isOpaque;
504
updateOpaque();
505
}
506
}
507
508
private void updateOpaque() {
509
getPlatformWindow().setOpaque(!isTranslucent());
510
replaceSurfaceData(false);
511
repaintPeer();
512
}
513
514
@Override
515
public void updateWindow() {
516
}
517
518
public final boolean isTextured() {
519
return textured;
520
}
521
522
public final void setTextured(final boolean isTextured) {
523
textured = isTextured;
524
}
525
526
@Override
527
public final boolean isTranslucent() {
528
synchronized (getStateLock()) {
529
/*
530
* Textured window is a special case of translucent window.
531
* The difference is only in nswindow background. So when we set
532
* texture property our peer became fully translucent. It doesn't
533
* fill background, create non opaque backbuffers and layer etc.
534
*/
535
return !isOpaque || isShaped() || isTextured();
536
}
537
}
538
539
@Override
540
final void applyShapeImpl(final Region shape) {
541
super.applyShapeImpl(shape);
542
updateOpaque();
543
}
544
545
@Override
546
public void repositionSecurityWarning() {
547
if (warningWindow != null) {
548
ComponentAccessor compAccessor = AWTAccessor.getComponentAccessor();
549
Window target = getTarget();
550
int x = compAccessor.getX(target);
551
int y = compAccessor.getY(target);
552
int width = compAccessor.getWidth(target);
553
int height = compAccessor.getHeight(target);
554
warningWindow.reposition(x, y, width, height);
555
}
556
}
557
558
// ---- FRAME PEER METHODS ---- //
559
560
@Override // FramePeer and DialogPeer
561
public void setTitle(String title) {
562
platformWindow.setTitle(title == null ? "" : title);
563
}
564
565
@Override
566
public void setMenuBar(MenuBar mb) {
567
platformWindow.setMenuBar(mb);
568
}
569
570
@Override // FramePeer and DialogPeer
571
public void setResizable(boolean resizable) {
572
platformWindow.setResizable(resizable);
573
}
574
575
@Override
576
public void setState(int state) {
577
platformWindow.setWindowState(state);
578
}
579
580
@Override
581
public int getState() {
582
return windowState;
583
}
584
585
private boolean isMaximizedBoundsSet() {
586
synchronized (getStateLock()) {
587
return maximizedBounds != null;
588
}
589
}
590
591
private Rectangle getDefaultMaximizedBounds() {
592
GraphicsConfiguration config = getGraphicsConfiguration();
593
Insets screenInsets = ((CGraphicsDevice) config.getDevice())
594
.getScreenInsets();
595
Rectangle gcBounds = config.getBounds();
596
return new Rectangle(
597
gcBounds.x + screenInsets.left,
598
gcBounds.y + screenInsets.top,
599
gcBounds.width - screenInsets.left - screenInsets.right,
600
gcBounds.height - screenInsets.top - screenInsets.bottom);
601
}
602
603
@Override
604
public void setMaximizedBounds(Rectangle bounds) {
605
boolean isMaximizedBoundsSet;
606
synchronized (getStateLock()) {
607
this.maximizedBounds = (isMaximizedBoundsSet = (bounds != null))
608
? constrainBounds(bounds) : null;
609
}
610
611
setPlatformMaximizedBounds(isMaximizedBoundsSet ? maximizedBounds
612
: getDefaultMaximizedBounds());
613
}
614
615
public Rectangle getMaximizedBounds() {
616
synchronized (getStateLock()) {
617
return (maximizedBounds == null)
618
? getDefaultMaximizedBounds()
619
: maximizedBounds;
620
}
621
}
622
623
private void setPlatformMaximizedBounds(Rectangle bounds) {
624
platformWindow.setMaximizedBounds(
625
bounds.x, bounds.y,
626
bounds.width, bounds.height);
627
}
628
629
@Override
630
public void setBoundsPrivate(int x, int y, int width, int height) {
631
setBounds(x, y, width, height, SET_BOUNDS | NO_EMBEDDED_CHECK);
632
}
633
634
@Override
635
public Rectangle getBoundsPrivate() {
636
throw new RuntimeException("not implemented");
637
}
638
639
// ---- DIALOG PEER METHODS ---- //
640
641
@Override
642
public void blockWindows(List<Window> windows) {
643
//TODO: LWX will probably need some collectJavaToplevels to speed this up
644
for (Window w : windows) {
645
WindowPeer wp = AWTAccessor.getComponentAccessor().getPeer(w);
646
if (wp != null) {
647
wp.setModalBlocked((Dialog)getTarget(), true);
648
}
649
}
650
}
651
652
// ---- PEER NOTIFICATIONS ---- //
653
654
@Override
655
public void notifyIconify(boolean iconify) {
656
//The toplevel target is Frame and states are applicable to it.
657
//Otherwise, the target is Window and it don't have state property.
658
//Hopefully, no such events are posted in the queue so consider the
659
//target as Frame in all cases.
660
661
// REMIND: should we send it anyway if the state not changed since last
662
// time?
663
WindowEvent iconifyEvent = new WindowEvent(getTarget(),
664
iconify ? WindowEvent.WINDOW_ICONIFIED
665
: WindowEvent.WINDOW_DEICONIFIED);
666
postEvent(iconifyEvent);
667
668
int newWindowState = iconify ? Frame.ICONIFIED : Frame.NORMAL;
669
postWindowStateChangedEvent(newWindowState);
670
671
// REMIND: RepaintManager doesn't repaint iconified windows and
672
// hence ignores any repaint request during deiconification.
673
// So, we need to repaint window explicitly when it becomes normal.
674
if (!iconify) {
675
repaintPeer();
676
}
677
}
678
679
@Override
680
public void notifyZoom(boolean isZoomed) {
681
int newWindowState = isZoomed ? Frame.MAXIMIZED_BOTH : Frame.NORMAL;
682
postWindowStateChangedEvent(newWindowState);
683
}
684
685
/**
686
* Called by the {@code PlatformWindow} when any part of the window should
687
* be repainted.
688
*/
689
@Override
690
public void notifyExpose(final Rectangle r) {
691
repaintPeer(r);
692
}
693
694
/**
695
* Called by the {@code PlatformWindow} when this window is moved/resized by
696
* user or window insets are changed. There's no notifyReshape() in
697
* LWComponentPeer as the only components which could be resized by user are
698
* top-level windows.
699
* <p>
700
* We need to update the target and post the events, if the peer was moved
701
* or resized, or if the target is out of sync with this peer.
702
*/
703
@Override
704
public void notifyReshape(int x, int y, int w, int h) {
705
final Rectangle pBounds = getBounds();
706
final boolean invalid = updateInsets(platformWindow.getInsets());
707
final boolean pMoved = (x != pBounds.x) || (y != pBounds.y);
708
final boolean pResized = (w != pBounds.width) || (h != pBounds.height);
709
710
final ComponentAccessor accessor = AWTAccessor.getComponentAccessor();
711
final Rectangle tBounds = accessor.getBounds(getTarget());
712
final boolean tMoved = (x != tBounds.x) || (y != tBounds.y);
713
final boolean tResized = (w != tBounds.width) || (h != tBounds.height);
714
715
// Check if anything changed
716
if (!tMoved && !tResized && !pMoved && !pResized && !invalid) {
717
// Native window(NSWindow)/LWWindowPeer/Target are in sync
718
return;
719
}
720
// First, update peer's bounds
721
setBounds(x, y, w, h, SET_BOUNDS, false, false);
722
723
// Second, update the graphics config and surface data
724
final boolean isNewDevice = updateGraphicsDevice();
725
if (isNewDevice && !isMaximizedBoundsSet()) {
726
setPlatformMaximizedBounds(getDefaultMaximizedBounds());
727
}
728
729
if (pResized || isNewDevice || invalid) {
730
replaceSurfaceData();
731
updateMinimumSize();
732
}
733
734
// Third, COMPONENT_MOVED/COMPONENT_RESIZED/PAINT events
735
if (tMoved || pMoved || invalid) {
736
handleMove(x, y, true);
737
}
738
if (tResized || pResized || invalid || isNewDevice) {
739
handleResize(w, h, true);
740
repaintPeer();
741
}
742
743
repositionSecurityWarning();
744
}
745
746
private void clearBackground(final int w, final int h) {
747
final Graphics g = getOnscreenGraphics(getForeground(), getBackground(),
748
getFont());
749
if (g != null) {
750
try {
751
if (g instanceof Graphics2D) {
752
((Graphics2D) g).setComposite(AlphaComposite.Src);
753
}
754
if (isTranslucent()) {
755
g.setColor(nonOpaqueBackground);
756
g.fillRect(0, 0, w, h);
757
}
758
if (!isTextured()) {
759
if (g instanceof SunGraphics2D) {
760
((SunGraphics2D) g).constrain(0, 0, w, h, getRegion());
761
}
762
g.setColor(getBackground());
763
g.fillRect(0, 0, w, h);
764
}
765
} finally {
766
g.dispose();
767
}
768
}
769
}
770
771
@Override
772
public void notifyUpdateCursor() {
773
getLWToolkit().getCursorManager().updateCursorLater(this);
774
}
775
776
@Override
777
public void notifyActivation(boolean activation, LWWindowPeer opposite) {
778
Window oppositeWindow = (opposite == null)? null : opposite.getTarget();
779
changeFocusedWindow(activation, oppositeWindow);
780
}
781
782
// MouseDown in non-client area
783
@Override
784
public void notifyNCMouseDown() {
785
// Ungrab except for a click on a Dialog with the grabbing owner
786
if (grabbingWindow != null &&
787
!grabbingWindow.isOneOfOwnersOf(this))
788
{
789
grabbingWindow.ungrab();
790
}
791
}
792
793
// ---- EVENTS ---- //
794
795
/*
796
* Called by the delegate to dispatch the event to Java. Event
797
* coordinates are relative to non-client window are, i.e. the top-left
798
* point of the client area is (insets.top, insets.left).
799
*/
800
@Override
801
public void notifyMouseEvent(int id, long when, int button,
802
int x, int y, int absX, int absY,
803
int modifiers, int clickCount, boolean popupTrigger,
804
byte[] bdata)
805
{
806
// TODO: fill "bdata" member of AWTEvent
807
Rectangle r = getBounds();
808
// findPeerAt() expects parent coordinates
809
LWComponentPeer<?, ?> targetPeer = findPeerAt(r.x + x, r.y + y);
810
811
if (id == MouseEvent.MOUSE_EXITED) {
812
isMouseOver = false;
813
if (lastMouseEventPeer != null) {
814
if (lastMouseEventPeer.isEnabled()) {
815
Point lp = lastMouseEventPeer.windowToLocal(x, y,
816
this);
817
Component target = lastMouseEventPeer.getTarget();
818
postMouseExitedEvent(target, when, modifiers, lp,
819
absX, absY, clickCount, popupTrigger, button);
820
}
821
822
// Sometimes we may get MOUSE_EXITED after lastCommonMouseEventPeer is switched
823
// to a peer from another window. So we must first check if this peer is
824
// the same as lastWindowPeer
825
if (lastCommonMouseEventPeer != null && lastCommonMouseEventPeer.getWindowPeerOrSelf() == this) {
826
lastCommonMouseEventPeer = null;
827
}
828
lastMouseEventPeer = null;
829
}
830
} else if(id == MouseEvent.MOUSE_ENTERED) {
831
isMouseOver = true;
832
if (targetPeer != null) {
833
if (targetPeer.isEnabled()) {
834
Point lp = targetPeer.windowToLocal(x, y, this);
835
Component target = targetPeer.getTarget();
836
postMouseEnteredEvent(target, when, modifiers, lp,
837
absX, absY, clickCount, popupTrigger, button);
838
}
839
lastCommonMouseEventPeer = targetPeer;
840
lastMouseEventPeer = targetPeer;
841
}
842
} else {
843
PlatformWindow topmostPlatformWindow = LWToolkit.getLWToolkit().getPlatformWindowUnderMouse();
844
845
LWWindowPeer topmostWindowPeer =
846
topmostPlatformWindow != null ? topmostPlatformWindow.getPeer() : null;
847
848
// topmostWindowPeer == null condition is added for the backward
849
// compatibility with applets. It can be removed when the
850
// getTopmostPlatformWindowUnderMouse() method will be properly
851
// implemented in CPlatformEmbeddedFrame class
852
if (topmostWindowPeer == this || topmostWindowPeer == null) {
853
generateMouseEnterExitEventsForComponents(when, button, x, y,
854
absX, absY, modifiers, clickCount, popupTrigger,
855
targetPeer);
856
} else {
857
LWComponentPeer<?, ?> topmostTargetPeer = topmostWindowPeer.findPeerAt(r.x + x, r.y + y);
858
topmostWindowPeer.generateMouseEnterExitEventsForComponents(when, button, x, y,
859
absX, absY, modifiers, clickCount, popupTrigger,
860
topmostTargetPeer);
861
}
862
863
// TODO: fill "bdata" member of AWTEvent
864
865
int eventButtonMask = (button > 0)? MouseEvent.getMaskForButton(button) : 0;
866
int otherButtonsPressed = modifiers & ~eventButtonMask;
867
868
// For pressed/dragged/released events OS X treats other
869
// mouse buttons as if they were BUTTON2, so we do the same
870
int targetIdx = (button > 3) ? MouseEvent.BUTTON2 - 1 : button - 1;
871
872
// MOUSE_ENTERED/EXITED are generated for the components strictly under
873
// mouse even when dragging. That's why we first update lastMouseEventPeer
874
// based on initial targetPeer value and only then recalculate targetPeer
875
// for MOUSE_DRAGGED/RELEASED events
876
if (id == MouseEvent.MOUSE_PRESSED) {
877
878
// Ungrab only if this window is not an owned window of the grabbing one.
879
if (!isGrabbing() && grabbingWindow != null &&
880
!grabbingWindow.isOneOfOwnersOf(this))
881
{
882
grabbingWindow.ungrab();
883
}
884
if (otherButtonsPressed == 0) {
885
mouseClickButtons = eventButtonMask;
886
} else {
887
mouseClickButtons |= eventButtonMask;
888
}
889
890
// The window should be focused on mouse click. If it gets activated by the native platform,
891
// this request will be no op. It will take effect when:
892
// 1. A simple not focused window is clicked.
893
// 2. An active but not focused owner frame/dialog is clicked.
894
// The mouse event then will trigger a focus request "in window" to the component, so the window
895
// should gain focus before.
896
requestWindowFocus(FocusEvent.Cause.MOUSE_EVENT);
897
898
mouseDownTarget[targetIdx] = targetPeer;
899
} else if (id == MouseEvent.MOUSE_DRAGGED) {
900
// Cocoa dragged event has the information about which mouse
901
// button is being dragged. Use it to determine the peer that
902
// should receive the dragged event.
903
targetPeer = mouseDownTarget[targetIdx];
904
mouseClickButtons &= ~modifiers;
905
} else if (id == MouseEvent.MOUSE_RELEASED) {
906
// TODO: currently, mouse released event goes to the same component
907
// that received corresponding mouse pressed event. For most cases,
908
// it's OK, however, we need to make sure that our behavior is consistent
909
// with 1.6 for cases where component in question have been
910
// hidden/removed in between of mouse pressed/released events.
911
targetPeer = mouseDownTarget[targetIdx];
912
913
if ((modifiers & eventButtonMask) == 0) {
914
mouseDownTarget[targetIdx] = null;
915
}
916
917
// mouseClickButtons is updated below, after MOUSE_CLICK is sent
918
}
919
920
if (targetPeer == null) {
921
//TODO This can happen if this window is invisible. this is correct behavior in this case?
922
targetPeer = this;
923
}
924
925
926
Point lp = targetPeer.windowToLocal(x, y, this);
927
if (targetPeer.isEnabled()) {
928
MouseEvent event = new MouseEvent(targetPeer.getTarget(), id,
929
when, modifiers, lp.x, lp.y,
930
absX, absY, clickCount,
931
popupTrigger, button);
932
postEvent(event);
933
}
934
935
if (id == MouseEvent.MOUSE_RELEASED) {
936
if ((mouseClickButtons & eventButtonMask) != 0
937
&& targetPeer.isEnabled()) {
938
postEvent(new MouseEvent(targetPeer.getTarget(),
939
MouseEvent.MOUSE_CLICKED,
940
when, modifiers,
941
lp.x, lp.y, absX, absY,
942
clickCount, popupTrigger, button));
943
}
944
mouseClickButtons &= ~eventButtonMask;
945
}
946
}
947
notifyUpdateCursor();
948
}
949
950
private void generateMouseEnterExitEventsForComponents(long when,
951
int button, int x, int y, int screenX, int screenY,
952
int modifiers, int clickCount, boolean popupTrigger,
953
final LWComponentPeer<?, ?> targetPeer) {
954
955
if (!isMouseOver || targetPeer == lastMouseEventPeer) {
956
return;
957
}
958
959
// Generate Mouse Exit for components
960
if (lastMouseEventPeer != null && lastMouseEventPeer.isEnabled()) {
961
Point oldp = lastMouseEventPeer.windowToLocal(x, y, this);
962
Component target = lastMouseEventPeer.getTarget();
963
postMouseExitedEvent(target, when, modifiers, oldp, screenX, screenY,
964
clickCount, popupTrigger, button);
965
}
966
lastCommonMouseEventPeer = targetPeer;
967
lastMouseEventPeer = targetPeer;
968
969
// Generate Mouse Enter for components
970
if (targetPeer != null && targetPeer.isEnabled()) {
971
Point newp = targetPeer.windowToLocal(x, y, this);
972
Component target = targetPeer.getTarget();
973
postMouseEnteredEvent(target, when, modifiers, newp, screenX, screenY, clickCount, popupTrigger, button);
974
}
975
}
976
977
private void postMouseEnteredEvent(Component target, long when, int modifiers,
978
Point loc, int xAbs, int yAbs,
979
int clickCount, boolean popupTrigger, int button) {
980
981
updateSecurityWarningVisibility();
982
983
postEvent(new MouseEvent(target,
984
MouseEvent.MOUSE_ENTERED,
985
when, modifiers,
986
loc.x, loc.y, xAbs, yAbs,
987
clickCount, popupTrigger, button));
988
}
989
990
private void postMouseExitedEvent(Component target, long when, int modifiers,
991
Point loc, int xAbs, int yAbs,
992
int clickCount, boolean popupTrigger, int button) {
993
994
updateSecurityWarningVisibility();
995
996
postEvent(new MouseEvent(target,
997
MouseEvent.MOUSE_EXITED,
998
when, modifiers,
999
loc.x, loc.y, xAbs, yAbs,
1000
clickCount, popupTrigger, button));
1001
}
1002
1003
@Override
1004
public void notifyMouseWheelEvent(long when, int x, int y, int absX,
1005
int absY, int modifiers, int scrollType,
1006
int scrollAmount, int wheelRotation,
1007
double preciseWheelRotation, byte[] bdata)
1008
{
1009
// TODO: could we just use the last mouse event target here?
1010
Rectangle r = getBounds();
1011
// findPeerAt() expects parent coordinates
1012
final LWComponentPeer<?, ?> targetPeer = findPeerAt(r.x + x, r.y + y);
1013
if (targetPeer == null || !targetPeer.isEnabled()) {
1014
return;
1015
}
1016
1017
Point lp = targetPeer.windowToLocal(x, y, this);
1018
// TODO: fill "bdata" member of AWTEvent
1019
postEvent(new MouseWheelEvent(targetPeer.getTarget(),
1020
MouseEvent.MOUSE_WHEEL,
1021
when, modifiers,
1022
lp.x, lp.y,
1023
absX, absY, /* absX, absY */
1024
0 /* clickCount */, false /* popupTrigger */,
1025
scrollType, scrollAmount,
1026
wheelRotation, preciseWheelRotation));
1027
}
1028
1029
/*
1030
* Called by the delegate when a key is pressed.
1031
*/
1032
@Override
1033
public void notifyKeyEvent(int id, long when, int modifiers,
1034
int keyCode, char keyChar, int keyLocation)
1035
{
1036
LWKeyboardFocusManagerPeer kfmPeer = LWKeyboardFocusManagerPeer.getInstance();
1037
Component focusOwner = kfmPeer.getCurrentFocusOwner();
1038
1039
if (focusOwner == null) {
1040
focusOwner = kfmPeer.getCurrentFocusedWindow();
1041
if (focusOwner == null) {
1042
focusOwner = this.getTarget();
1043
}
1044
}
1045
1046
KeyEvent keyEvent = new KeyEvent(focusOwner, id, when, modifiers,
1047
keyCode, keyChar, keyLocation);
1048
AWTAccessor.getKeyEventAccessor().setExtendedKeyCode(keyEvent,
1049
(keyChar == KeyEvent.CHAR_UNDEFINED) ? keyCode
1050
: ExtendedKeyCodes.getExtendedKeyCodeForChar(keyChar));
1051
postEvent(keyEvent);
1052
}
1053
1054
// ---- UTILITY METHODS ---- //
1055
1056
private void activateDisplayListener() {
1057
final GraphicsEnvironment ge =
1058
GraphicsEnvironment.getLocalGraphicsEnvironment();
1059
((SunGraphicsEnvironment) ge).addDisplayChangedListener(this);
1060
}
1061
1062
private void deactivateDisplayListener() {
1063
final GraphicsEnvironment ge =
1064
GraphicsEnvironment.getLocalGraphicsEnvironment();
1065
((SunGraphicsEnvironment) ge).removeDisplayChangedListener(this);
1066
}
1067
1068
private void postWindowStateChangedEvent(int newWindowState) {
1069
if (getTarget() instanceof Frame) {
1070
AWTAccessor.getFrameAccessor().setExtendedState(
1071
(Frame)getTarget(), newWindowState);
1072
}
1073
1074
WindowEvent stateChangedEvent = new WindowEvent(getTarget(),
1075
WindowEvent.WINDOW_STATE_CHANGED,
1076
windowState, newWindowState);
1077
postEvent(stateChangedEvent);
1078
windowState = newWindowState;
1079
1080
updateSecurityWarningVisibility();
1081
}
1082
1083
private static int getGraphicsConfigScreen(GraphicsConfiguration gc) {
1084
// TODO: this method can be implemented in a more
1085
// efficient way by forwarding to the delegate
1086
GraphicsDevice gd = gc.getDevice();
1087
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
1088
GraphicsDevice[] gds = ge.getScreenDevices();
1089
for (int i = 0; i < gds.length; i++) {
1090
if (gds[i] == gd) {
1091
return i;
1092
}
1093
}
1094
// Should never happen if gc is a screen device config
1095
return 0;
1096
}
1097
1098
/*
1099
* This method is called when window's graphics config is changed from
1100
* the app code (e.g. when the window is made non-opaque) or when
1101
* the window is moved to another screen by user.
1102
*
1103
* Returns true if the graphics config has been changed, false otherwise.
1104
*/
1105
private boolean setGraphicsConfig(GraphicsConfiguration gc) {
1106
synchronized (getStateLock()) {
1107
if (graphicsConfig == gc) {
1108
return false;
1109
}
1110
// If window's graphics config is changed from the app code, the
1111
// config correspond to the same device as before; when the window
1112
// is moved by user, graphicsDevice is updated in notifyReshape().
1113
// In either case, there's nothing to do with screenOn here
1114
graphicsConfig = gc;
1115
}
1116
// SurfaceData is replaced later in updateGraphicsData()
1117
return true;
1118
}
1119
1120
/**
1121
* Returns true if the GraphicsDevice has been changed, false otherwise.
1122
*/
1123
public boolean updateGraphicsDevice() {
1124
GraphicsDevice newGraphicsDevice = platformWindow.getGraphicsDevice();
1125
synchronized (getStateLock()) {
1126
if (graphicsDevice == newGraphicsDevice) {
1127
return false;
1128
}
1129
graphicsDevice = newGraphicsDevice;
1130
}
1131
1132
final GraphicsConfiguration newGC = newGraphicsDevice.getDefaultConfiguration();
1133
1134
if (!setGraphicsConfig(newGC)) return false;
1135
1136
SunToolkit.executeOnEventHandlerThread(getTarget(), new Runnable() {
1137
public void run() {
1138
AWTAccessor.getComponentAccessor().setGraphicsConfiguration(getTarget(), newGC);
1139
}
1140
});
1141
return true;
1142
}
1143
1144
@Override
1145
public final void displayChanged() {
1146
if (updateGraphicsDevice()) {
1147
updateMinimumSize();
1148
if (!isMaximizedBoundsSet()) {
1149
setPlatformMaximizedBounds(getDefaultMaximizedBounds());
1150
}
1151
}
1152
// Replace surface unconditionally, because internal state of the
1153
// GraphicsDevice could be changed.
1154
replaceSurfaceData();
1155
repaintPeer();
1156
}
1157
1158
@Override
1159
public final void paletteChanged() {
1160
// components do not need to react to this event.
1161
}
1162
1163
/*
1164
* May be called by delegate to provide SD to Java2D code.
1165
*/
1166
public SurfaceData getSurfaceData() {
1167
synchronized (surfaceDataLock) {
1168
return surfaceData;
1169
}
1170
}
1171
1172
private void replaceSurfaceData() {
1173
replaceSurfaceData(true);
1174
}
1175
1176
private void replaceSurfaceData(final boolean blit) {
1177
synchronized (surfaceDataLock) {
1178
final SurfaceData oldData = getSurfaceData();
1179
surfaceData = platformWindow.replaceSurfaceData();
1180
final Rectangle size = getSize();
1181
if (getSurfaceData() != null && oldData != getSurfaceData()) {
1182
clearBackground(size.width, size.height);
1183
}
1184
1185
if (blit) {
1186
blitSurfaceData(oldData, getSurfaceData());
1187
}
1188
1189
if (oldData != null && oldData != getSurfaceData()) {
1190
// TODO: drop oldData for D3D/WGL pipelines
1191
// This can only happen when this peer is being created
1192
oldData.flush();
1193
}
1194
}
1195
flushOnscreenGraphics();
1196
}
1197
1198
private void blitSurfaceData(final SurfaceData src, final SurfaceData dst) {
1199
//TODO blit. proof-of-concept
1200
if (src != dst && src != null && dst != null
1201
&& !(dst instanceof NullSurfaceData)
1202
&& !(src instanceof NullSurfaceData)
1203
&& src.getSurfaceType().equals(dst.getSurfaceType())
1204
&& src.getDefaultScaleX() == dst.getDefaultScaleX()
1205
&& src.getDefaultScaleY() == dst.getDefaultScaleY())
1206
{
1207
final Rectangle size = src.getBounds();
1208
final Blit blit = Blit.locate(src.getSurfaceType(),
1209
CompositeType.Src,
1210
dst.getSurfaceType());
1211
if (blit != null) {
1212
blit.Blit(src, dst, AlphaComposite.Src, null, 0, 0, 0, 0,
1213
size.width, size.height);
1214
}
1215
}
1216
}
1217
1218
/**
1219
* Request the window insets from the delegate and compares it with the
1220
* current one. This method is mostly called by the delegate, e.g. when the
1221
* window state is changed and insets should be recalculated.
1222
* <p/>
1223
* This method may be called on the toolkit thread.
1224
*/
1225
public final boolean updateInsets(final Insets newInsets) {
1226
synchronized (getStateLock()) {
1227
if (insets.equals(newInsets)) {
1228
return false;
1229
}
1230
insets = newInsets;
1231
}
1232
return true;
1233
}
1234
1235
public static LWWindowPeer getWindowUnderCursor() {
1236
return lastCommonMouseEventPeer != null ? lastCommonMouseEventPeer.getWindowPeerOrSelf() : null;
1237
}
1238
1239
public static LWComponentPeer<?, ?> getPeerUnderCursor() {
1240
return lastCommonMouseEventPeer;
1241
}
1242
1243
/*
1244
* Requests platform to set native focus on a frame/dialog.
1245
* In case of a simple window, triggers appropriate java focus change.
1246
*/
1247
public boolean requestWindowFocus(FocusEvent.Cause cause) {
1248
if (focusLog.isLoggable(PlatformLogger.Level.FINE)) {
1249
focusLog.fine("requesting native focus to " + this);
1250
}
1251
1252
if (!focusAllowedFor()) {
1253
focusLog.fine("focus is not allowed");
1254
return false;
1255
}
1256
1257
if (platformWindow.rejectFocusRequest(cause)) {
1258
return false;
1259
}
1260
1261
AppContext targetAppContext = AWTAccessor.getComponentAccessor().getAppContext(getTarget());
1262
KeyboardFocusManager kfm = AWTAccessor.getKeyboardFocusManagerAccessor()
1263
.getCurrentKeyboardFocusManager(targetAppContext);
1264
Window currentActive = kfm.getActiveWindow();
1265
1266
1267
Window opposite = LWKeyboardFocusManagerPeer.getInstance().
1268
getCurrentFocusedWindow();
1269
1270
// Make the owner active window.
1271
if (isSimpleWindow()) {
1272
LWWindowPeer owner = getOwnerFrameDialog(this);
1273
1274
// If owner is not natively active, request native
1275
// activation on it w/o sending events up to java.
1276
if (owner != null && !owner.platformWindow.isActive()) {
1277
if (focusLog.isLoggable(PlatformLogger.Level.FINE)) {
1278
focusLog.fine("requesting native focus to the owner " + owner);
1279
}
1280
LWWindowPeer currentActivePeer = currentActive == null ? null :
1281
(LWWindowPeer) AWTAccessor.getComponentAccessor().getPeer(
1282
currentActive);
1283
1284
// Ensure the opposite is natively active and suppress sending events.
1285
if (currentActivePeer != null && currentActivePeer.platformWindow.isActive()) {
1286
if (focusLog.isLoggable(PlatformLogger.Level.FINE)) {
1287
focusLog.fine("the opposite is " + currentActivePeer);
1288
}
1289
currentActivePeer.skipNextFocusChange = true;
1290
}
1291
owner.skipNextFocusChange = true;
1292
1293
owner.platformWindow.requestWindowFocus();
1294
}
1295
1296
// DKFM will synthesize all the focus/activation events correctly.
1297
changeFocusedWindow(true, opposite);
1298
return true;
1299
1300
// In case the toplevel is active but not focused, change focus directly,
1301
// as requesting native focus on it will not have effect.
1302
} else if (getTarget() == currentActive && !getTarget().hasFocus()) {
1303
1304
changeFocusedWindow(true, opposite);
1305
return true;
1306
}
1307
1308
return platformWindow.requestWindowFocus();
1309
}
1310
1311
protected boolean focusAllowedFor() {
1312
Window window = getTarget();
1313
// TODO: check if modal blocked
1314
return window.isVisible() && window.isEnabled() && isFocusableWindow();
1315
}
1316
1317
private boolean isFocusableWindow() {
1318
boolean focusable = targetFocusable;
1319
if (isSimpleWindow()) {
1320
LWWindowPeer ownerPeer = getOwnerFrameDialog(this);
1321
if (ownerPeer == null) {
1322
return false;
1323
}
1324
return focusable && ownerPeer.targetFocusable;
1325
}
1326
return focusable;
1327
}
1328
1329
public boolean isSimpleWindow() {
1330
Window window = getTarget();
1331
return !(window instanceof Dialog || window instanceof Frame);
1332
}
1333
1334
@Override
1335
public void emulateActivation(boolean activate) {
1336
changeFocusedWindow(activate, null);
1337
}
1338
1339
@SuppressWarnings("deprecation")
1340
private boolean isOneOfOwnersOf(LWWindowPeer peer) {
1341
Window owner = (peer != null ? peer.getTarget().getOwner() : null);
1342
while (owner != null) {
1343
final ComponentAccessor acc = AWTAccessor.getComponentAccessor();
1344
if (acc.getPeer(owner) == this) {
1345
return true;
1346
}
1347
owner = owner.getOwner();
1348
}
1349
return false;
1350
}
1351
1352
/*
1353
* Changes focused window on java level.
1354
*/
1355
protected void changeFocusedWindow(boolean becomesFocused, Window opposite) {
1356
if (focusLog.isLoggable(PlatformLogger.Level.FINE)) {
1357
focusLog.fine((becomesFocused?"gaining":"loosing") + " focus window: " + this);
1358
}
1359
if (skipNextFocusChange) {
1360
focusLog.fine("skipping focus change");
1361
skipNextFocusChange = false;
1362
return;
1363
}
1364
if (!isFocusableWindow() && becomesFocused) {
1365
focusLog.fine("the window is not focusable");
1366
return;
1367
}
1368
if (becomesFocused) {
1369
synchronized (getPeerTreeLock()) {
1370
if (blocker != null) {
1371
if (focusLog.isLoggable(PlatformLogger.Level.FINEST)) {
1372
focusLog.finest("the window is blocked by " + blocker);
1373
}
1374
return;
1375
}
1376
}
1377
}
1378
1379
// Note, the method is not called:
1380
// - when the opposite (gaining focus) window is an owned/owner window.
1381
// - for a simple window in any case.
1382
if (!becomesFocused &&
1383
(isGrabbing() || this.isOneOfOwnersOf(grabbingWindow)))
1384
{
1385
if (focusLog.isLoggable(PlatformLogger.Level.FINE)) {
1386
focusLog.fine("ungrabbing on " + grabbingWindow);
1387
}
1388
// ungrab a simple window if its owner looses activation.
1389
grabbingWindow.ungrab();
1390
}
1391
1392
KeyboardFocusManagerPeer kfmPeer = LWKeyboardFocusManagerPeer.getInstance();
1393
1394
if (!becomesFocused && kfmPeer.getCurrentFocusedWindow() != getTarget()) {
1395
// late window focus lost event - ingoring
1396
return;
1397
}
1398
1399
kfmPeer.setCurrentFocusedWindow(becomesFocused ? getTarget() : null);
1400
1401
int eventID = becomesFocused ? WindowEvent.WINDOW_GAINED_FOCUS : WindowEvent.WINDOW_LOST_FOCUS;
1402
WindowEvent windowEvent = new TimedWindowEvent(getTarget(), eventID, opposite, System.currentTimeMillis());
1403
1404
// TODO: wrap in SequencedEvent
1405
postEvent(windowEvent);
1406
}
1407
1408
/*
1409
* Retrieves the owner of the peer.
1410
* Note: this method returns the owner which can be activated, (i.e. the instance
1411
* of Frame or Dialog may be returned).
1412
*/
1413
static LWWindowPeer getOwnerFrameDialog(LWWindowPeer peer) {
1414
Window owner = (peer != null ? peer.getTarget().getOwner() : null);
1415
while (owner != null && !(owner instanceof Frame || owner instanceof Dialog)) {
1416
owner = owner.getOwner();
1417
}
1418
return owner == null ? null : AWTAccessor.getComponentAccessor()
1419
.getPeer(owner);
1420
}
1421
1422
/**
1423
* Returns the foremost modal blocker of this window, or null.
1424
*/
1425
public LWWindowPeer getBlocker() {
1426
synchronized (getPeerTreeLock()) {
1427
LWWindowPeer blocker = this.blocker;
1428
if (blocker == null) {
1429
return null;
1430
}
1431
while (blocker.blocker != null) {
1432
blocker = blocker.blocker;
1433
}
1434
return blocker;
1435
}
1436
}
1437
1438
@Override
1439
public void enterFullScreenMode() {
1440
platformWindow.enterFullScreenMode();
1441
updateSecurityWarningVisibility();
1442
}
1443
1444
@Override
1445
public void exitFullScreenMode() {
1446
platformWindow.exitFullScreenMode();
1447
updateSecurityWarningVisibility();
1448
}
1449
1450
public long getLayerPtr() {
1451
return getPlatformWindow().getLayerPtr();
1452
}
1453
1454
void grab() {
1455
if (grabbingWindow != null && !isGrabbing()) {
1456
grabbingWindow.ungrab();
1457
}
1458
grabbingWindow = this;
1459
}
1460
1461
final void ungrab(boolean doPost) {
1462
if (isGrabbing()) {
1463
grabbingWindow = null;
1464
if (doPost) {
1465
postEvent(new UngrabEvent(getTarget()));
1466
}
1467
}
1468
}
1469
1470
void ungrab() {
1471
ungrab(true);
1472
}
1473
1474
private boolean isGrabbing() {
1475
return this == grabbingWindow;
1476
}
1477
1478
public PeerType getPeerType() {
1479
return peerType;
1480
}
1481
1482
public void updateSecurityWarningVisibility() {
1483
if (warningWindow == null) {
1484
return;
1485
}
1486
1487
if (!isVisible()) {
1488
return; // The warning window should already be hidden.
1489
}
1490
1491
boolean show = false;
1492
1493
if (!platformWindow.isFullScreenMode()) {
1494
if (isVisible()) {
1495
if (LWKeyboardFocusManagerPeer.getInstance().getCurrentFocusedWindow() ==
1496
getTarget()) {
1497
show = true;
1498
}
1499
1500
if (platformWindow.isUnderMouse() || warningWindow.isUnderMouse()) {
1501
show = true;
1502
}
1503
}
1504
}
1505
1506
warningWindow.setVisible(show, true);
1507
}
1508
1509
@Override
1510
public String toString() {
1511
return super.toString() + " [target is " + getTarget() + "]";
1512
}
1513
}
1514
1515