Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.desktop/share/classes/java/awt/Frame.java
41152 views
1
/*
2
* Copyright (c) 1995, 2021, 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 java.awt;
27
28
import java.awt.event.KeyEvent;
29
import java.awt.event.WindowEvent;
30
import java.awt.peer.FramePeer;
31
import java.io.IOException;
32
import java.io.ObjectInputStream;
33
import java.io.ObjectOutputStream;
34
import java.io.Serial;
35
import java.io.Serializable;
36
import java.util.ArrayList;
37
import java.util.Vector;
38
39
import javax.accessibility.AccessibleContext;
40
import javax.accessibility.AccessibleRole;
41
import javax.accessibility.AccessibleState;
42
import javax.accessibility.AccessibleStateSet;
43
44
import sun.awt.AWTAccessor;
45
import sun.awt.SunToolkit;
46
47
/**
48
* A {@code Frame} is a top-level window with a title and a border.
49
* <p>
50
* The size of the frame includes any area designated for the
51
* border. The dimensions of the border area may be obtained
52
* using the {@code getInsets} method, however, since
53
* these dimensions are platform-dependent, a valid insets
54
* value cannot be obtained until the frame is made displayable
55
* by either calling {@code pack} or {@code show}.
56
* Since the border area is included in the overall size of the
57
* frame, the border effectively obscures a portion of the frame,
58
* constraining the area available for rendering and/or displaying
59
* subcomponents to the rectangle which has an upper-left corner
60
* location of {@code (insets.left, insets.top)}, and has a size of
61
* {@code width - (insets.left + insets.right)} by
62
* {@code height - (insets.top + insets.bottom)}.
63
* <p>
64
* The default layout for a frame is {@code BorderLayout}.
65
* <p>
66
* A frame may have its native decorations (i.e. {@code Frame}
67
* and {@code Titlebar}) turned off
68
* with {@code setUndecorated}. This can only be done while the frame
69
* is not {@link Component#isDisplayable() displayable}.
70
* <p>
71
* In a multi-screen environment, you can create a {@code Frame}
72
* on a different screen device by constructing the {@code Frame}
73
* with {@link #Frame(GraphicsConfiguration)} or
74
* {@link #Frame(String title, GraphicsConfiguration)}. The
75
* {@code GraphicsConfiguration} object is one of the
76
* {@code GraphicsConfiguration} objects of the target screen
77
* device.
78
* <p>
79
* In a virtual device multi-screen environment in which the desktop
80
* area could span multiple physical screen devices, the bounds of all
81
* configurations are relative to the virtual-coordinate system. The
82
* origin of the virtual-coordinate system is at the upper left-hand
83
* corner of the primary physical screen. Depending on the location
84
* of the primary screen in the virtual device, negative coordinates
85
* are possible, as shown in the following figure.
86
* <p>
87
* <img src="doc-files/MultiScreen.gif" alt="Diagram of virtual device
88
* encompassing three physical screens and one primary physical screen. The
89
* primary physical screen shows (0,0) coords while a different physical screen
90
* shows (-80,-100) coords." style="margin: 7px 10px;">
91
* <p>
92
* In such an environment, when calling {@code setLocation},
93
* you must pass a virtual coordinate to this method. Similarly,
94
* calling {@code getLocationOnScreen} on a {@code Frame}
95
* returns virtual device coordinates. Call the {@code getBounds}
96
* method of a {@code GraphicsConfiguration} to find its origin in
97
* the virtual coordinate system.
98
* <p>
99
* The following code sets the
100
* location of the {@code Frame} at (10, 10) relative
101
* to the origin of the physical screen of the corresponding
102
* {@code GraphicsConfiguration}. If the bounds of the
103
* {@code GraphicsConfiguration} is not taken into account, the
104
* {@code Frame} location would be set at (10, 10) relative to the
105
* virtual-coordinate system and would appear on the primary physical
106
* screen, which might be different from the physical screen of the
107
* specified {@code GraphicsConfiguration}.
108
*
109
* <pre>
110
* Frame f = new Frame(GraphicsConfiguration gc);
111
* Rectangle bounds = gc.getBounds();
112
* f.setLocation(10 + bounds.x, 10 + bounds.y);
113
* </pre>
114
*
115
* <p>
116
* Frames are capable of generating the following types of
117
* {@code WindowEvent}s:
118
* <ul>
119
* <li>{@code WINDOW_OPENED}
120
* <li>{@code WINDOW_CLOSING}:
121
* <br>If the program doesn't
122
* explicitly hide or dispose the window while processing
123
* this event, the window close operation is canceled.
124
* <li>{@code WINDOW_CLOSED}
125
* <li>{@code WINDOW_ICONIFIED}
126
* <li>{@code WINDOW_DEICONIFIED}
127
* <li>{@code WINDOW_ACTIVATED}
128
* <li>{@code WINDOW_DEACTIVATED}
129
* <li>{@code WINDOW_GAINED_FOCUS}
130
* <li>{@code WINDOW_LOST_FOCUS}
131
* <li>{@code WINDOW_STATE_CHANGED}
132
* </ul>
133
*
134
* @author Sami Shaio
135
* @see WindowEvent
136
* @see Window#addWindowListener
137
* @since 1.0
138
*/
139
public class Frame extends Window implements MenuContainer {
140
141
/* Note: These are being obsoleted; programs should use the Cursor class
142
* variables going forward. See Cursor and Component.setCursor.
143
*/
144
145
/**
146
* @deprecated replaced by {@code Cursor.DEFAULT_CURSOR}.
147
*/
148
@Deprecated
149
public static final int DEFAULT_CURSOR = Cursor.DEFAULT_CURSOR;
150
151
152
/**
153
* @deprecated replaced by {@code Cursor.CROSSHAIR_CURSOR}.
154
*/
155
@Deprecated
156
public static final int CROSSHAIR_CURSOR = Cursor.CROSSHAIR_CURSOR;
157
158
/**
159
* @deprecated replaced by {@code Cursor.TEXT_CURSOR}.
160
*/
161
@Deprecated
162
public static final int TEXT_CURSOR = Cursor.TEXT_CURSOR;
163
164
/**
165
* @deprecated replaced by {@code Cursor.WAIT_CURSOR}.
166
*/
167
@Deprecated
168
public static final int WAIT_CURSOR = Cursor.WAIT_CURSOR;
169
170
/**
171
* @deprecated replaced by {@code Cursor.SW_RESIZE_CURSOR}.
172
*/
173
@Deprecated
174
public static final int SW_RESIZE_CURSOR = Cursor.SW_RESIZE_CURSOR;
175
176
/**
177
* @deprecated replaced by {@code Cursor.SE_RESIZE_CURSOR}.
178
*/
179
@Deprecated
180
public static final int SE_RESIZE_CURSOR = Cursor.SE_RESIZE_CURSOR;
181
182
/**
183
* @deprecated replaced by {@code Cursor.NW_RESIZE_CURSOR}.
184
*/
185
@Deprecated
186
public static final int NW_RESIZE_CURSOR = Cursor.NW_RESIZE_CURSOR;
187
188
/**
189
* @deprecated replaced by {@code Cursor.NE_RESIZE_CURSOR}.
190
*/
191
@Deprecated
192
public static final int NE_RESIZE_CURSOR = Cursor.NE_RESIZE_CURSOR;
193
194
/**
195
* @deprecated replaced by {@code Cursor.N_RESIZE_CURSOR}.
196
*/
197
@Deprecated
198
public static final int N_RESIZE_CURSOR = Cursor.N_RESIZE_CURSOR;
199
200
/**
201
* @deprecated replaced by {@code Cursor.S_RESIZE_CURSOR}.
202
*/
203
@Deprecated
204
public static final int S_RESIZE_CURSOR = Cursor.S_RESIZE_CURSOR;
205
206
/**
207
* @deprecated replaced by {@code Cursor.W_RESIZE_CURSOR}.
208
*/
209
@Deprecated
210
public static final int W_RESIZE_CURSOR = Cursor.W_RESIZE_CURSOR;
211
212
/**
213
* @deprecated replaced by {@code Cursor.E_RESIZE_CURSOR}.
214
*/
215
@Deprecated
216
public static final int E_RESIZE_CURSOR = Cursor.E_RESIZE_CURSOR;
217
218
/**
219
* @deprecated replaced by {@code Cursor.HAND_CURSOR}.
220
*/
221
@Deprecated
222
public static final int HAND_CURSOR = Cursor.HAND_CURSOR;
223
224
/**
225
* @deprecated replaced by {@code Cursor.MOVE_CURSOR}.
226
*/
227
@Deprecated
228
public static final int MOVE_CURSOR = Cursor.MOVE_CURSOR;
229
230
231
/**
232
* Frame is in the "normal" state. This symbolic constant names a
233
* frame state with all state bits cleared.
234
* @see #setExtendedState(int)
235
* @see #getExtendedState
236
*/
237
public static final int NORMAL = 0;
238
239
/**
240
* This state bit indicates that frame is iconified.
241
* @see #setExtendedState(int)
242
* @see #getExtendedState
243
*/
244
public static final int ICONIFIED = 1;
245
246
/**
247
* This state bit indicates that frame is maximized in the
248
* horizontal direction.
249
* @see #setExtendedState(int)
250
* @see #getExtendedState
251
* @since 1.4
252
*/
253
public static final int MAXIMIZED_HORIZ = 2;
254
255
/**
256
* This state bit indicates that frame is maximized in the
257
* vertical direction.
258
* @see #setExtendedState(int)
259
* @see #getExtendedState
260
* @since 1.4
261
*/
262
public static final int MAXIMIZED_VERT = 4;
263
264
/**
265
* This state bit mask indicates that frame is fully maximized
266
* (that is both horizontally and vertically). It is just a
267
* convenience alias for
268
* <code>MAXIMIZED_VERT&nbsp;|&nbsp;MAXIMIZED_HORIZ</code>.
269
*
270
* <p>Note that the correct test for frame being fully maximized is
271
* <pre>
272
* (state &amp; Frame.MAXIMIZED_BOTH) == Frame.MAXIMIZED_BOTH
273
* </pre>
274
*
275
* <p>To test is frame is maximized in <em>some</em> direction use
276
* <pre>
277
* (state &amp; Frame.MAXIMIZED_BOTH) != 0
278
* </pre>
279
*
280
* @see #setExtendedState(int)
281
* @see #getExtendedState
282
* @since 1.4
283
*/
284
public static final int MAXIMIZED_BOTH = MAXIMIZED_VERT | MAXIMIZED_HORIZ;
285
286
/**
287
* Maximized bounds for this frame.
288
* @see #setMaximizedBounds(Rectangle)
289
* @see #getMaximizedBounds
290
* @serial
291
* @since 1.4
292
*/
293
Rectangle maximizedBounds;
294
295
296
/**
297
* This is the title of the frame. It can be changed
298
* at any time. {@code title} can be null and if
299
* this is the case the {@code title} = "".
300
*
301
* @serial
302
* @see #getTitle
303
* @see #setTitle(String)
304
*/
305
String title = "Untitled";
306
307
/**
308
* The frames menubar. If {@code menuBar} = null
309
* the frame will not have a menubar.
310
*
311
* @serial
312
* @see #getMenuBar
313
* @see #setMenuBar(MenuBar)
314
*/
315
MenuBar menuBar;
316
317
/**
318
* This field indicates whether the frame is resizable.
319
* This property can be changed at any time.
320
* {@code resizable} will be true if the frame is
321
* resizable, otherwise it will be false.
322
*
323
* @serial
324
* @see #isResizable()
325
*/
326
boolean resizable = true;
327
328
/**
329
* This field indicates whether the frame is undecorated.
330
* This property can only be changed while the frame is not displayable.
331
* {@code undecorated} will be true if the frame is
332
* undecorated, otherwise it will be false.
333
*
334
* @serial
335
* @see #setUndecorated(boolean)
336
* @see #isUndecorated()
337
* @see Component#isDisplayable()
338
* @since 1.4
339
*/
340
boolean undecorated = false;
341
342
/**
343
* {@code mbManagement} is only used by the Motif implementation.
344
*
345
* @serial
346
*/
347
boolean mbManagement = false; /* used only by the Motif impl. */
348
349
/**
350
* The bitwise mask of frame state constants.
351
*/
352
// XXX: uwe: abuse old field for now
353
// will need to take care of serialization
354
private int state = NORMAL;
355
356
/**
357
* The Windows owned by the Frame.
358
* Note: in 1.2 this has been superseded by Window.ownedWindowList
359
*
360
* @serial
361
* @see java.awt.Window#ownedWindowList
362
*/
363
Vector<Window> ownedWindows;
364
365
private static final String base = "frame";
366
private static int nameCounter = 0;
367
368
/**
369
* Use serialVersionUID from JDK 1.1 for interoperability.
370
*/
371
@Serial
372
private static final long serialVersionUID = 2673458971256075116L;
373
374
static {
375
/* ensure that the necessary native libraries are loaded */
376
Toolkit.loadLibraries();
377
if (!GraphicsEnvironment.isHeadless()) {
378
initIDs();
379
}
380
}
381
382
/**
383
* Constructs a new instance of {@code Frame} that is
384
* initially invisible. The title of the {@code Frame}
385
* is empty.
386
* @exception HeadlessException when
387
* {@code GraphicsEnvironment.isHeadless()} returns {@code true}
388
* @see java.awt.GraphicsEnvironment#isHeadless()
389
* @see Component#setSize
390
* @see Component#setVisible(boolean)
391
*/
392
public Frame() throws HeadlessException {
393
this("");
394
}
395
396
/**
397
* Constructs a new, initially invisible {@code Frame} with the
398
* specified {@code GraphicsConfiguration}.
399
*
400
* @param gc the {@code GraphicsConfiguration}
401
* of the target screen device. If {@code gc}
402
* is {@code null}, the system default
403
* {@code GraphicsConfiguration} is assumed.
404
* @exception IllegalArgumentException if
405
* {@code gc} is not from a screen device.
406
* @exception HeadlessException when
407
* {@code GraphicsEnvironment.isHeadless()} returns {@code true}
408
* @see java.awt.GraphicsEnvironment#isHeadless()
409
* @since 1.3
410
*/
411
public Frame(GraphicsConfiguration gc) {
412
this("", gc);
413
}
414
415
/**
416
* Constructs a new, initially invisible {@code Frame} object
417
* with the specified title.
418
* @param title the title to be displayed in the frame's border.
419
* A {@code null} value
420
* is treated as an empty string, "".
421
* @exception HeadlessException when
422
* {@code GraphicsEnvironment.isHeadless()} returns {@code true}
423
* @see java.awt.GraphicsEnvironment#isHeadless()
424
* @see java.awt.Component#setSize
425
* @see java.awt.Component#setVisible(boolean)
426
* @see java.awt.GraphicsConfiguration#getBounds
427
*/
428
public Frame(String title) throws HeadlessException {
429
init(title, null);
430
}
431
432
/**
433
* Constructs a new, initially invisible {@code Frame} object
434
* with the specified title and a
435
* {@code GraphicsConfiguration}.
436
* @param title the title to be displayed in the frame's border.
437
* A {@code null} value
438
* is treated as an empty string, "".
439
* @param gc the {@code GraphicsConfiguration}
440
* of the target screen device. If {@code gc} is
441
* {@code null}, the system default
442
* {@code GraphicsConfiguration} is assumed.
443
* @exception IllegalArgumentException if {@code gc}
444
* is not from a screen device.
445
* @exception HeadlessException when
446
* {@code GraphicsEnvironment.isHeadless()} returns {@code true}
447
* @see java.awt.GraphicsEnvironment#isHeadless()
448
* @see java.awt.Component#setSize
449
* @see java.awt.Component#setVisible(boolean)
450
* @see java.awt.GraphicsConfiguration#getBounds
451
* @since 1.3
452
*/
453
public Frame(String title, GraphicsConfiguration gc) {
454
super(gc);
455
init(title, gc);
456
}
457
458
private void init(String title, GraphicsConfiguration gc) {
459
this.title = title;
460
SunToolkit.checkAndSetPolicy(this);
461
}
462
463
/**
464
* Construct a name for this component. Called by getName() when the
465
* name is null.
466
*/
467
String constructComponentName() {
468
synchronized (Frame.class) {
469
return base + nameCounter++;
470
}
471
}
472
473
/**
474
* Makes this Frame displayable by connecting it to
475
* a native screen resource. Making a frame displayable will
476
* cause any of its children to be made displayable.
477
* This method is called internally by the toolkit and should
478
* not be called directly by programs.
479
* @see Component#isDisplayable
480
* @see #removeNotify
481
*/
482
public void addNotify() {
483
synchronized (getTreeLock()) {
484
if (peer == null) {
485
peer = getComponentFactory().createFrame(this);
486
}
487
FramePeer p = (FramePeer)peer;
488
MenuBar menuBar = this.menuBar;
489
if (menuBar != null) {
490
mbManagement = true;
491
menuBar.addNotify();
492
p.setMenuBar(menuBar);
493
}
494
p.setMaximizedBounds(maximizedBounds);
495
super.addNotify();
496
}
497
}
498
499
/**
500
* Gets the title of the frame. The title is displayed in the
501
* frame's border.
502
* @return the title of this frame, or an empty string ("")
503
* if this frame doesn't have a title.
504
* @see #setTitle(String)
505
*/
506
public String getTitle() {
507
return title;
508
}
509
510
/**
511
* Sets the title for this frame to the specified string.
512
* @param title the title to be displayed in the frame's border.
513
* A {@code null} value
514
* is treated as an empty string, "".
515
* @see #getTitle
516
*/
517
public void setTitle(String title) {
518
String oldTitle = this.title;
519
if (title == null) {
520
title = "";
521
}
522
523
524
synchronized(this) {
525
this.title = title;
526
FramePeer peer = (FramePeer)this.peer;
527
if (peer != null) {
528
peer.setTitle(title);
529
}
530
}
531
firePropertyChange("title", oldTitle, title);
532
}
533
534
/**
535
* Returns the image to be displayed as the icon for this frame.
536
* <p>
537
* This method is obsolete and kept for backward compatibility
538
* only. Use {@link Window#getIconImages Window.getIconImages()} instead.
539
* <p>
540
* If a list of several images was specified as a Window's icon,
541
* this method will return the first item of the list.
542
*
543
* @return the icon image for this frame, or {@code null}
544
* if this frame doesn't have an icon image.
545
* @see #setIconImage(Image)
546
* @see Window#getIconImages()
547
* @see Window#setIconImages
548
*/
549
public Image getIconImage() {
550
java.util.List<Image> icons = this.icons;
551
if (icons != null) {
552
if (icons.size() > 0) {
553
return icons.get(0);
554
}
555
}
556
return null;
557
}
558
559
/**
560
* {@inheritDoc}
561
*/
562
public void setIconImage(Image image) {
563
super.setIconImage(image);
564
}
565
566
/**
567
* Gets the menu bar for this frame.
568
* @return the menu bar for this frame, or {@code null}
569
* if this frame doesn't have a menu bar.
570
* @see #setMenuBar(MenuBar)
571
*/
572
public MenuBar getMenuBar() {
573
return menuBar;
574
}
575
576
/**
577
* Sets the menu bar for this frame to the specified menu bar.
578
* @param mb the menu bar being set.
579
* If this parameter is {@code null} then any
580
* existing menu bar on this frame is removed.
581
* @see #getMenuBar
582
*/
583
public void setMenuBar(MenuBar mb) {
584
synchronized (getTreeLock()) {
585
if (menuBar == mb) {
586
return;
587
}
588
if ((mb != null) && (mb.parent != null)) {
589
mb.parent.remove(mb);
590
}
591
if (menuBar != null) {
592
remove(menuBar);
593
}
594
menuBar = mb;
595
if (menuBar != null) {
596
menuBar.parent = this;
597
598
FramePeer peer = (FramePeer)this.peer;
599
if (peer != null) {
600
mbManagement = true;
601
menuBar.addNotify();
602
invalidateIfValid();
603
peer.setMenuBar(menuBar);
604
}
605
}
606
}
607
}
608
609
/**
610
* Indicates whether this frame is resizable by the user.
611
* By default, all frames are initially resizable.
612
* @return {@code true} if the user can resize this frame;
613
* {@code false} otherwise.
614
* @see java.awt.Frame#setResizable(boolean)
615
*/
616
public boolean isResizable() {
617
return resizable;
618
}
619
620
/**
621
* Sets whether this frame is resizable by the user.
622
* @param resizable {@code true} if this frame is resizable;
623
* {@code false} otherwise.
624
* @see java.awt.Frame#isResizable
625
*/
626
public void setResizable(boolean resizable) {
627
boolean oldResizable = this.resizable;
628
boolean testvalid = false;
629
630
synchronized (this) {
631
this.resizable = resizable;
632
FramePeer peer = (FramePeer)this.peer;
633
if (peer != null) {
634
peer.setResizable(resizable);
635
testvalid = true;
636
}
637
}
638
639
// On some platforms, changing the resizable state affects
640
// the insets of the Frame. If we could, we'd call invalidate()
641
// from the peer, but we need to guarantee that we're not holding
642
// the Frame lock when we call invalidate().
643
if (testvalid) {
644
invalidateIfValid();
645
}
646
firePropertyChange("resizable", oldResizable, resizable);
647
}
648
649
650
/**
651
* Sets the state of this frame (obsolete).
652
* <p>
653
* In older versions of JDK a frame state could only be NORMAL or
654
* ICONIFIED. Since JDK 1.4 set of supported frame states is
655
* expanded and frame state is represented as a bitwise mask.
656
* <p>
657
* For compatibility with applications developed
658
* earlier this method still accepts
659
* {@code Frame.NORMAL} and
660
* {@code Frame.ICONIFIED} only. The iconic
661
* state of the frame is only changed, other aspects
662
* of frame state are not affected by this method. If
663
* the state passed to this method is neither {@code
664
* Frame.NORMAL} nor {@code Frame.ICONIFIED} the
665
* method performs no actions at all.
666
* <p>Note that if the state is not supported on a
667
* given platform, neither the state nor the return
668
* value of the {@link #getState} method will be
669
* changed. The application may determine whether a
670
* specific state is supported via the {@link
671
* java.awt.Toolkit#isFrameStateSupported} method.
672
* <p><b>If the frame is currently visible on the
673
* screen</b> (the {@link #isShowing} method returns
674
* {@code true}), the developer should examine the
675
* return value of the {@link
676
* java.awt.event.WindowEvent#getNewState} method of
677
* the {@code WindowEvent} received through the
678
* {@link java.awt.event.WindowStateListener} to
679
* determine that the state has actually been
680
* changed.
681
* <p><b>If the frame is not visible on the
682
* screen</b>, the events may or may not be
683
* generated. In this case the developer may assume
684
* that the state changes immediately after this
685
* method returns. Later, when the {@code
686
* setVisible(true)} method is invoked, the frame
687
* will attempt to apply this state. Receiving any
688
* {@link
689
* java.awt.event.WindowEvent#WINDOW_STATE_CHANGED}
690
* events is not guaranteed in this case also.
691
*
692
* @param state either {@code Frame.NORMAL} or
693
* {@code Frame.ICONIFIED}.
694
* @see #setExtendedState(int)
695
* @see java.awt.Window#addWindowStateListener
696
*/
697
public synchronized void setState(int state) {
698
int current = getExtendedState();
699
if (state == ICONIFIED && (current & ICONIFIED) == 0) {
700
setExtendedState(current | ICONIFIED);
701
}
702
else if (state == NORMAL && (current & ICONIFIED) != 0) {
703
setExtendedState(current & ~ICONIFIED);
704
}
705
}
706
707
/**
708
* Sets the state of this frame. The state is
709
* represented as a bitwise mask.
710
* <ul>
711
* <li>{@code NORMAL}
712
* <br>Indicates that no state bits are set.
713
* <li>{@code ICONIFIED}
714
* <li>{@code MAXIMIZED_HORIZ}
715
* <li>{@code MAXIMIZED_VERT}
716
* <li>{@code MAXIMIZED_BOTH}
717
* <br>Concatenates {@code MAXIMIZED_HORIZ}
718
* and {@code MAXIMIZED_VERT}.
719
* </ul>
720
* <p>Note that if the state is not supported on a
721
* given platform, neither the state nor the return
722
* value of the {@link #getExtendedState} method will
723
* be changed. The application may determine whether
724
* a specific state is supported via the {@link
725
* java.awt.Toolkit#isFrameStateSupported} method.
726
* <p><b>If the frame is currently visible on the
727
* screen</b> (the {@link #isShowing} method returns
728
* {@code true}), the developer should examine the
729
* return value of the {@link
730
* java.awt.event.WindowEvent#getNewState} method of
731
* the {@code WindowEvent} received through the
732
* {@link java.awt.event.WindowStateListener} to
733
* determine that the state has actually been
734
* changed.
735
* <p><b>If the frame is not visible on the
736
* screen</b>, the events may or may not be
737
* generated. In this case the developer may assume
738
* that the state changes immediately after this
739
* method returns. Later, when the {@code
740
* setVisible(true)} method is invoked, the frame
741
* will attempt to apply this state. Receiving any
742
* {@link
743
* java.awt.event.WindowEvent#WINDOW_STATE_CHANGED}
744
* events is not guaranteed in this case also.
745
*
746
* @param state a bitwise mask of frame state constants
747
* @since 1.4
748
* @see java.awt.Window#addWindowStateListener
749
*/
750
public void setExtendedState(int state) {
751
if ( !isFrameStateSupported( state ) ) {
752
return;
753
}
754
synchronized (getObjectLock()) {
755
this.state = state;
756
}
757
// peer.setState must be called outside of object lock
758
// synchronization block to avoid possible deadlock
759
FramePeer peer = (FramePeer)this.peer;
760
if (peer != null) {
761
peer.setState(state);
762
}
763
}
764
private boolean isFrameStateSupported(int state) {
765
if( !getToolkit().isFrameStateSupported( state ) ) {
766
// * Toolkit.isFrameStateSupported returns always false
767
// on compound state even if all parts are supported;
768
// * if part of state is not supported, state is not supported;
769
// * MAXIMIZED_BOTH is not a compound state.
770
if( ((state & ICONIFIED) != 0) &&
771
!getToolkit().isFrameStateSupported( ICONIFIED )) {
772
return false;
773
}else {
774
state &= ~ICONIFIED;
775
}
776
return getToolkit().isFrameStateSupported( state );
777
}
778
return true;
779
}
780
781
/**
782
* Gets the state of this frame (obsolete).
783
* <p>
784
* In older versions of JDK a frame state could only be NORMAL or
785
* ICONIFIED. Since JDK 1.4 set of supported frame states is
786
* expanded and frame state is represented as a bitwise mask.
787
* <p>
788
* For compatibility with old programs this method still returns
789
* {@code Frame.NORMAL} and {@code Frame.ICONIFIED} but
790
* it only reports the iconic state of the frame, other aspects of
791
* frame state are not reported by this method.
792
*
793
* @return {@code Frame.NORMAL} or {@code Frame.ICONIFIED}.
794
* @see #setState(int)
795
* @see #getExtendedState
796
*/
797
public synchronized int getState() {
798
return (getExtendedState() & ICONIFIED) != 0 ? ICONIFIED : NORMAL;
799
}
800
801
802
/**
803
* Gets the state of this frame. The state is
804
* represented as a bitwise mask.
805
* <ul>
806
* <li>{@code NORMAL}
807
* <br>Indicates that no state bits are set.
808
* <li>{@code ICONIFIED}
809
* <li>{@code MAXIMIZED_HORIZ}
810
* <li>{@code MAXIMIZED_VERT}
811
* <li>{@code MAXIMIZED_BOTH}
812
* <br>Concatenates {@code MAXIMIZED_HORIZ}
813
* and {@code MAXIMIZED_VERT}.
814
* </ul>
815
*
816
* @return a bitwise mask of frame state constants
817
* @see #setExtendedState(int)
818
* @since 1.4
819
*/
820
public int getExtendedState() {
821
synchronized (getObjectLock()) {
822
return state;
823
}
824
}
825
826
static {
827
AWTAccessor.setFrameAccessor(
828
new AWTAccessor.FrameAccessor() {
829
public void setExtendedState(Frame frame, int state) {
830
synchronized(frame.getObjectLock()) {
831
frame.state = state;
832
}
833
}
834
public int getExtendedState(Frame frame) {
835
synchronized(frame.getObjectLock()) {
836
return frame.state;
837
}
838
}
839
public Rectangle getMaximizedBounds(Frame frame) {
840
synchronized(frame.getObjectLock()) {
841
return frame.maximizedBounds;
842
}
843
}
844
}
845
);
846
}
847
848
/**
849
* Sets the maximized bounds for this frame.
850
* <p>
851
* When a frame is in maximized state the system supplies some
852
* defaults bounds. This method allows some or all of those
853
* system supplied values to be overridden.
854
* <p>
855
* If {@code bounds} is {@code null}, accept bounds
856
* supplied by the system. If non-{@code null} you can
857
* override some of the system supplied values while accepting
858
* others by setting those fields you want to accept from system
859
* to {@code Integer.MAX_VALUE}.
860
* <p>
861
* Note, the given maximized bounds are used as a hint for the native
862
* system, because the underlying platform may not support setting the
863
* location and/or size of the maximized windows. If that is the case, the
864
* provided values do not affect the appearance of the frame in the
865
* maximized state.
866
*
867
* @param bounds bounds for the maximized state
868
* @see #getMaximizedBounds()
869
* @since 1.4
870
*/
871
public void setMaximizedBounds(Rectangle bounds) {
872
synchronized(getObjectLock()) {
873
this.maximizedBounds = bounds;
874
}
875
FramePeer peer = (FramePeer)this.peer;
876
if (peer != null) {
877
peer.setMaximizedBounds(bounds);
878
}
879
}
880
881
/**
882
* Gets maximized bounds for this frame.
883
* Some fields may contain {@code Integer.MAX_VALUE} to indicate
884
* that system supplied values for this field must be used.
885
*
886
* @return maximized bounds for this frame; may be {@code null}
887
* @see #setMaximizedBounds(Rectangle)
888
* @since 1.4
889
*/
890
public Rectangle getMaximizedBounds() {
891
synchronized(getObjectLock()) {
892
return maximizedBounds;
893
}
894
}
895
896
897
/**
898
* Disables or enables decorations for this frame.
899
* <p>
900
* This method can only be called while the frame is not displayable. To
901
* make this frame decorated, it must be opaque and have the default shape,
902
* otherwise the {@code IllegalComponentStateException} will be thrown.
903
* Refer to {@link Window#setShape}, {@link Window#setOpacity} and {@link
904
* Window#setBackground} for details
905
*
906
* @param undecorated {@code true} if no frame decorations are to be
907
* enabled; {@code false} if frame decorations are to be enabled
908
*
909
* @throws IllegalComponentStateException if the frame is displayable
910
* @throws IllegalComponentStateException if {@code undecorated} is
911
* {@code false}, and this frame does not have the default shape
912
* @throws IllegalComponentStateException if {@code undecorated} is
913
* {@code false}, and this frame opacity is less than {@code 1.0f}
914
* @throws IllegalComponentStateException if {@code undecorated} is
915
* {@code false}, and the alpha value of this frame background
916
* color is less than {@code 1.0f}
917
*
918
* @see #isUndecorated
919
* @see Component#isDisplayable
920
* @see Window#getShape
921
* @see Window#getOpacity
922
* @see Window#getBackground
923
* @see javax.swing.JFrame#setDefaultLookAndFeelDecorated(boolean)
924
*
925
* @since 1.4
926
*/
927
public void setUndecorated(boolean undecorated) {
928
/* Make sure we don't run in the middle of peer creation.*/
929
synchronized (getTreeLock()) {
930
if (isDisplayable()) {
931
throw new IllegalComponentStateException("The frame is displayable.");
932
}
933
if (!undecorated) {
934
if (getOpacity() < 1.0f) {
935
throw new IllegalComponentStateException("The frame is not opaque");
936
}
937
if (getShape() != null) {
938
throw new IllegalComponentStateException("The frame does not have a default shape");
939
}
940
Color bg = getBackground();
941
if ((bg != null) && (bg.getAlpha() < 255)) {
942
throw new IllegalComponentStateException("The frame background color is not opaque");
943
}
944
}
945
this.undecorated = undecorated;
946
}
947
}
948
949
/**
950
* Indicates whether this frame is undecorated.
951
* By default, all frames are initially decorated.
952
* @return {@code true} if frame is undecorated;
953
* {@code false} otherwise.
954
* @see java.awt.Frame#setUndecorated(boolean)
955
* @since 1.4
956
*/
957
public boolean isUndecorated() {
958
return undecorated;
959
}
960
961
/**
962
* {@inheritDoc}
963
*/
964
@Override
965
public void setOpacity(float opacity) {
966
synchronized (getTreeLock()) {
967
if ((opacity < 1.0f) && !isUndecorated()) {
968
throw new IllegalComponentStateException("The frame is decorated");
969
}
970
super.setOpacity(opacity);
971
}
972
}
973
974
/**
975
* {@inheritDoc}
976
*/
977
@Override
978
public void setShape(Shape shape) {
979
synchronized (getTreeLock()) {
980
if ((shape != null) && !isUndecorated()) {
981
throw new IllegalComponentStateException("The frame is decorated");
982
}
983
super.setShape(shape);
984
}
985
}
986
987
/**
988
* {@inheritDoc}
989
*/
990
@Override
991
public void setBackground(Color bgColor) {
992
synchronized (getTreeLock()) {
993
if ((bgColor != null) && (bgColor.getAlpha() < 255) && !isUndecorated()) {
994
throw new IllegalComponentStateException("The frame is decorated");
995
}
996
super.setBackground(bgColor);
997
}
998
}
999
1000
/**
1001
* Removes the specified menu bar from this frame.
1002
* @param m the menu component to remove.
1003
* If {@code m} is {@code null}, then
1004
* no action is taken
1005
*/
1006
public void remove(MenuComponent m) {
1007
if (m == null) {
1008
return;
1009
}
1010
synchronized (getTreeLock()) {
1011
if (m == menuBar) {
1012
menuBar = null;
1013
FramePeer peer = (FramePeer)this.peer;
1014
if (peer != null) {
1015
mbManagement = true;
1016
invalidateIfValid();
1017
peer.setMenuBar(null);
1018
m.removeNotify();
1019
}
1020
m.parent = null;
1021
} else {
1022
super.remove(m);
1023
}
1024
}
1025
}
1026
1027
/**
1028
* Makes this Frame undisplayable by removing its connection
1029
* to its native screen resource. Making a Frame undisplayable
1030
* will cause any of its children to be made undisplayable.
1031
* This method is called by the toolkit internally and should
1032
* not be called directly by programs.
1033
* @see Component#isDisplayable
1034
* @see #addNotify
1035
*/
1036
public void removeNotify() {
1037
synchronized (getTreeLock()) {
1038
FramePeer peer = (FramePeer)this.peer;
1039
if (peer != null) {
1040
// get the latest Frame state before disposing
1041
getState();
1042
1043
if (menuBar != null) {
1044
mbManagement = true;
1045
peer.setMenuBar(null);
1046
menuBar.removeNotify();
1047
}
1048
}
1049
super.removeNotify();
1050
}
1051
}
1052
1053
void postProcessKeyEvent(KeyEvent e) {
1054
if (menuBar != null && menuBar.handleShortcut(e)) {
1055
e.consume();
1056
return;
1057
}
1058
super.postProcessKeyEvent(e);
1059
}
1060
1061
/**
1062
* Returns a string representing the state of this {@code Frame}.
1063
* This method is intended to be used only for debugging purposes, and the
1064
* content and format of the returned string may vary between
1065
* implementations. The returned string may be empty but may not be
1066
* {@code null}.
1067
*
1068
* @return the parameter string of this frame
1069
*/
1070
protected String paramString() {
1071
String str = super.paramString();
1072
if (title != null) {
1073
str += ",title=" + title;
1074
}
1075
if (resizable) {
1076
str += ",resizable";
1077
}
1078
int state = getExtendedState();
1079
if (state == NORMAL) {
1080
str += ",normal";
1081
}
1082
else {
1083
if ((state & ICONIFIED) != 0) {
1084
str += ",iconified";
1085
}
1086
if ((state & MAXIMIZED_BOTH) == MAXIMIZED_BOTH) {
1087
str += ",maximized";
1088
}
1089
else if ((state & MAXIMIZED_HORIZ) != 0) {
1090
str += ",maximized_horiz";
1091
}
1092
else if ((state & MAXIMIZED_VERT) != 0) {
1093
str += ",maximized_vert";
1094
}
1095
}
1096
return str;
1097
}
1098
1099
/**
1100
* Sets the cursor for this frame to the specified type.
1101
*
1102
* @param cursorType the cursor type
1103
* @deprecated As of JDK version 1.1,
1104
* replaced by {@code Component.setCursor(Cursor)}.
1105
*/
1106
@Deprecated
1107
public void setCursor(int cursorType) {
1108
if (cursorType < DEFAULT_CURSOR || cursorType > MOVE_CURSOR) {
1109
throw new IllegalArgumentException("illegal cursor type");
1110
}
1111
setCursor(Cursor.getPredefinedCursor(cursorType));
1112
}
1113
1114
/**
1115
* @deprecated As of JDK version 1.1,
1116
* replaced by {@code Component.getCursor()}.
1117
* @return the cursor type for this frame
1118
*/
1119
@Deprecated
1120
public int getCursorType() {
1121
return (getCursor().getType());
1122
}
1123
1124
/**
1125
* Returns an array of all {@code Frame}s created by this application.
1126
* If called from an applet, the array includes only the {@code Frame}s
1127
* accessible by that applet.
1128
* <p>
1129
* <b>Warning:</b> this method may return system created frames, such
1130
* as a shared, hidden frame which is used by Swing. Applications
1131
* should not assume the existence of these frames, nor should an
1132
* application assume anything about these frames such as component
1133
* positions, {@code LayoutManager}s or serialization.
1134
* <p>
1135
* <b>Note</b>: To obtain a list of all ownerless windows, including
1136
* ownerless {@code Dialog}s (introduced in release 1.6), use {@link
1137
* Window#getOwnerlessWindows Window.getOwnerlessWindows}.
1138
*
1139
* @return the array of all {@code Frame}s created by this application
1140
*
1141
* @see Window#getWindows()
1142
* @see Window#getOwnerlessWindows
1143
*
1144
* @since 1.2
1145
*/
1146
public static Frame[] getFrames() {
1147
Window[] allWindows = Window.getWindows();
1148
1149
int frameCount = 0;
1150
for (Window w : allWindows) {
1151
if (w instanceof Frame) {
1152
frameCount++;
1153
}
1154
}
1155
1156
Frame[] frames = new Frame[frameCount];
1157
int c = 0;
1158
for (Window w : allWindows) {
1159
if (w instanceof Frame) {
1160
frames[c++] = (Frame)w;
1161
}
1162
}
1163
1164
return frames;
1165
}
1166
1167
/* Serialization support. If there's a MenuBar we restore
1168
* its (transient) parent field here. Likewise for top level
1169
* windows that are "owned" by this frame.
1170
*/
1171
1172
/**
1173
* {@code Frame}'s Serialized Data Version.
1174
*
1175
* @serial
1176
*/
1177
private int frameSerializedDataVersion = 1;
1178
1179
/**
1180
* Writes default serializable fields to stream. Writes
1181
* an optional serializable icon {@code Image}, which is
1182
* available as of 1.4.
1183
*
1184
* @param s the {@code ObjectOutputStream} to write
1185
* @throws IOException if an I/O error occurs
1186
* @serialData an optional icon {@code Image}
1187
* @see java.awt.Image
1188
* @see #getIconImage
1189
* @see #setIconImage(Image)
1190
* @see #readObject(ObjectInputStream)
1191
*/
1192
@Serial
1193
private void writeObject(ObjectOutputStream s)
1194
throws IOException
1195
{
1196
s.defaultWriteObject();
1197
if (icons != null && icons.size() > 0) {
1198
Image icon1 = icons.get(0);
1199
if (icon1 instanceof Serializable) {
1200
s.writeObject(icon1);
1201
return;
1202
}
1203
}
1204
s.writeObject(null);
1205
}
1206
1207
/**
1208
* Reads the {@code ObjectInputStream}. Tries
1209
* to read an icon {@code Image}, which is optional
1210
* data available as of 1.4. If an icon {@code Image}
1211
* is not available, but anything other than an EOF
1212
* is detected, an {@code OptionalDataException}
1213
* will be thrown.
1214
* Unrecognized keys or values will be ignored.
1215
*
1216
* @param s the {@code ObjectInputStream} to read
1217
* @throws ClassNotFoundException if the class of a serialized object could
1218
* not be found
1219
* @throws java.io.OptionalDataException if an icon {@code Image} is not
1220
* available, but anything other than an EOF is detected
1221
* @throws HeadlessException if {@code GraphicsEnvironment.isHeadless()}
1222
* returns {@code true}
1223
* @see java.awt.GraphicsEnvironment#isHeadless()
1224
* @see java.awt.Image
1225
* @see #getIconImage
1226
* @see #setIconImage(Image)
1227
* @see #writeObject(ObjectOutputStream)
1228
*/
1229
@Serial
1230
private void readObject(ObjectInputStream s)
1231
throws ClassNotFoundException, IOException, HeadlessException
1232
{
1233
// HeadlessException is thrown by Window's readObject
1234
s.defaultReadObject();
1235
try {
1236
Image icon = (Image) s.readObject();
1237
if (icons == null) {
1238
icons = new ArrayList<Image>();
1239
icons.add(icon);
1240
}
1241
} catch (java.io.OptionalDataException e) {
1242
// pre-1.4 instances will not have this optional data.
1243
// 1.6 and later instances serialize icons in the Window class
1244
// e.eof will be true to indicate that there is no more
1245
// data available for this object.
1246
1247
// If e.eof is not true, throw the exception as it
1248
// might have been caused by unrelated reasons.
1249
if (!e.eof) {
1250
throw (e);
1251
}
1252
}
1253
1254
if (menuBar != null)
1255
menuBar.parent = this;
1256
1257
// Ensure 1.1 serialized Frames can read & hook-up
1258
// owned windows properly
1259
//
1260
if (ownedWindows != null) {
1261
for (int i = 0; i < ownedWindows.size(); i++) {
1262
connectOwnedWindow(ownedWindows.elementAt(i));
1263
}
1264
ownedWindows = null;
1265
}
1266
}
1267
1268
/**
1269
* Initialize JNI field and method IDs
1270
*/
1271
private static native void initIDs();
1272
1273
/*
1274
* --- Accessibility Support ---
1275
*
1276
*/
1277
1278
/**
1279
* Gets the AccessibleContext associated with this Frame.
1280
* For frames, the AccessibleContext takes the form of an
1281
* AccessibleAWTFrame.
1282
* A new AccessibleAWTFrame instance is created if necessary.
1283
*
1284
* @return an AccessibleAWTFrame that serves as the
1285
* AccessibleContext of this Frame
1286
* @since 1.3
1287
*/
1288
public AccessibleContext getAccessibleContext() {
1289
if (accessibleContext == null) {
1290
accessibleContext = new AccessibleAWTFrame();
1291
}
1292
return accessibleContext;
1293
}
1294
1295
/**
1296
* This class implements accessibility support for the
1297
* {@code Frame} class. It provides an implementation of the
1298
* Java Accessibility API appropriate to frame user-interface elements.
1299
* @since 1.3
1300
*/
1301
protected class AccessibleAWTFrame extends AccessibleAWTWindow
1302
{
1303
/**
1304
* Use serialVersionUID from JDK 1.3 for interoperability.
1305
*/
1306
@Serial
1307
private static final long serialVersionUID = -6172960752956030250L;
1308
1309
/**
1310
* Constructs an {@code AccessibleAWTFrame}.
1311
*/
1312
protected AccessibleAWTFrame() {}
1313
1314
/**
1315
* Get the role of this object.
1316
*
1317
* @return an instance of AccessibleRole describing the role of the
1318
* object
1319
* @see AccessibleRole
1320
*/
1321
public AccessibleRole getAccessibleRole() {
1322
return AccessibleRole.FRAME;
1323
}
1324
1325
/**
1326
* Get the state of this object.
1327
*
1328
* @return an instance of AccessibleStateSet containing the current
1329
* state set of the object
1330
* @see AccessibleState
1331
*/
1332
public AccessibleStateSet getAccessibleStateSet() {
1333
AccessibleStateSet states = super.getAccessibleStateSet();
1334
if (getFocusOwner() != null) {
1335
states.add(AccessibleState.ACTIVE);
1336
}
1337
if (isResizable()) {
1338
states.add(AccessibleState.RESIZABLE);
1339
}
1340
return states;
1341
}
1342
1343
1344
} // inner class AccessibleAWTFrame
1345
1346
}
1347
1348