Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
4970 lines (4580 loc) · 189 KB

File metadata and controls

4970 lines (4580 loc) · 189 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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) 1995, 2015, Oracle and/or its affiliates. All rights reserved.
* ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*/
package java.awt;
import java.awt.dnd.DropTarget;
import java.awt.event.*;
import java.awt.peer.ContainerPeer;
import java.awt.peer.ComponentPeer;
import java.awt.peer.LightweightPeer;
import java.beans.PropertyChangeListener;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.ObjectStreamField;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.lang.ref.WeakReference;
import java.security.AccessController;
import java.util.ArrayList;
import java.util.EventListener;
import java.util.HashSet;
import java.util.Set;
import javax.accessibility.*;
import sun.util.logging.PlatformLogger;
import sun.awt.AppContext;
import sun.awt.AWTAccessor;
import sun.awt.CausedFocusEvent;
import sun.awt.PeerEvent;
import sun.awt.SunToolkit;
import sun.awt.dnd.SunDropTargetEvent;
import sun.java2d.pipe.Region;
import sun.security.action.GetBooleanAction;
/**
* A generic Abstract Window Toolkit(AWT) container object is a component
* that can contain other AWT components.
* <p>
* Components added to a container are tracked in a list. The order
* of the list will define the components' front-to-back stacking order
* within the container. If no index is specified when adding a
* component to a container, it will be added to the end of the list
* (and hence to the bottom of the stacking order).
* <p>
* <b>Note</b>: For details on the focus subsystem, see
* <a href="https://docs.oracle.com/javase/tutorial/uiswing/misc/focus.html">
* How to Use the Focus Subsystem</a>,
* a section in <em>The Java Tutorial</em>, and the
* <a href="../../java/awt/doc-files/FocusSpec.html">Focus Specification</a>
* for more information.
*
* @author Arthur van Hoff
* @author Sami Shaio
* @see #add(java.awt.Component, int)
* @see #getComponent(int)
* @see LayoutManager
* @since JDK1.0
*/
public class Container extends Component {
private static final PlatformLogger log = PlatformLogger.getLogger("java.awt.Container");
private static final PlatformLogger eventLog = PlatformLogger.getLogger("java.awt.event.Container");
private static final Component[] EMPTY_ARRAY = new Component[0];
/**
* The components in this container.
* @see #add
* @see #getComponents
*/
private java.util.List<Component> component = new ArrayList<>();
/**
* Layout manager for this container.
* @see #doLayout
* @see #setLayout
* @see #getLayout
*/
LayoutManager layoutMgr;
/**
* Event router for lightweight components. If this container
* is native, this dispatcher takes care of forwarding and
* retargeting the events to lightweight components contained
* (if any).
*/
private LightweightDispatcher dispatcher;
/**
* The focus traversal policy that will manage keyboard traversal of this
* Container's children, if this Container is a focus cycle root. If the
* value is null, this Container inherits its policy from its focus-cycle-
* root ancestor. If all such ancestors of this Container have null
* policies, then the current KeyboardFocusManager's default policy is
* used. If the value is non-null, this policy will be inherited by all
* focus-cycle-root children that have no keyboard-traversal policy of
* their own (as will, recursively, their focus-cycle-root children).
* <p>
* If this Container is not a focus cycle root, the value will be
* remembered, but will not be used or inherited by this or any other
* Containers until this Container is made a focus cycle root.
*
* @see #setFocusTraversalPolicy
* @see #getFocusTraversalPolicy
* @since 1.4
*/
private transient FocusTraversalPolicy focusTraversalPolicy;
/**
* Indicates whether this Component is the root of a focus traversal cycle.
* Once focus enters a traversal cycle, typically it cannot leave it via
* focus traversal unless one of the up- or down-cycle keys is pressed.
* Normal traversal is limited to this Container, and all of this
* Container's descendants that are not descendants of inferior focus cycle
* roots.
*
* @see #setFocusCycleRoot
* @see #isFocusCycleRoot
* @since 1.4
*/
private boolean focusCycleRoot = false;
/**
* Stores the value of focusTraversalPolicyProvider property.
* @since 1.5
* @see #setFocusTraversalPolicyProvider
*/
private boolean focusTraversalPolicyProvider;
// keeps track of the threads that are printing this component
private transient Set<Thread> printingThreads;
// True if there is at least one thread that's printing this component
private transient boolean printing = false;
transient ContainerListener containerListener;
/* HierarchyListener and HierarchyBoundsListener support */
transient int listeningChildren;
transient int listeningBoundsChildren;
transient int descendantsCount;
/* Non-opaque window support -- see Window.setLayersOpaque */
transient Color preserveBackgroundColor = null;
/**
* JDK 1.1 serialVersionUID
*/
private static final long serialVersionUID = 4613797578919906343L;
/**
* A constant which toggles one of the controllable behaviors
* of <code>getMouseEventTarget</code>. It is used to specify whether
* the method can return the Container on which it is originally called
* in case if none of its children are the current mouse event targets.
*
* @see #getMouseEventTarget(int, int, boolean)
*/
static final boolean INCLUDE_SELF = true;
/**
* A constant which toggles one of the controllable behaviors
* of <code>getMouseEventTarget</code>. It is used to specify whether
* the method should search only lightweight components.
*
* @see #getMouseEventTarget(int, int, boolean)
*/
static final boolean SEARCH_HEAVYWEIGHTS = true;
/*
* Number of HW or LW components in this container (including
* all descendant containers).
*/
private transient int numOfHWComponents = 0;
private transient int numOfLWComponents = 0;
private static final PlatformLogger mixingLog = PlatformLogger.getLogger("java.awt.mixing.Container");
/**
* @serialField ncomponents int
* The number of components in this container.
* This value can be null.
* @serialField component Component[]
* The components in this container.
* @serialField layoutMgr LayoutManager
* Layout manager for this container.
* @serialField dispatcher LightweightDispatcher
* Event router for lightweight components. If this container
* is native, this dispatcher takes care of forwarding and
* retargeting the events to lightweight components contained
* (if any).
* @serialField maxSize Dimension
* Maximum size of this Container.
* @serialField focusCycleRoot boolean
* Indicates whether this Component is the root of a focus traversal cycle.
* Once focus enters a traversal cycle, typically it cannot leave it via
* focus traversal unless one of the up- or down-cycle keys is pressed.
* Normal traversal is limited to this Container, and all of this
* Container's descendants that are not descendants of inferior focus cycle
* roots.
* @serialField containerSerializedDataVersion int
* Container Serial Data Version.
* @serialField focusTraversalPolicyProvider boolean
* Stores the value of focusTraversalPolicyProvider property.
*/
private static final ObjectStreamField[] serialPersistentFields = {
new ObjectStreamField("ncomponents", Integer.TYPE),
new ObjectStreamField("component", Component[].class),
new ObjectStreamField("layoutMgr", LayoutManager.class),
new ObjectStreamField("dispatcher", LightweightDispatcher.class),
new ObjectStreamField("maxSize", Dimension.class),
new ObjectStreamField("focusCycleRoot", Boolean.TYPE),
new ObjectStreamField("containerSerializedDataVersion", Integer.TYPE),
new ObjectStreamField("focusTraversalPolicyProvider", Boolean.TYPE),
};
static {
/* ensure that the necessary native libraries are loaded */
Toolkit.loadLibraries();
if (!GraphicsEnvironment.isHeadless()) {
initIDs();
}
AWTAccessor.setContainerAccessor(new AWTAccessor.ContainerAccessor() {
@Override
public void validateUnconditionally(Container cont) {
cont.validateUnconditionally();
}
@Override
public Component findComponentAt(Container cont, int x, int y,
boolean ignoreEnabled) {
return cont.findComponentAt(x, y, ignoreEnabled);
}
});
}
/**
* Initialize JNI field and method IDs for fields that may be
called from C.
*/
private static native void initIDs();
/**
* Constructs a new Container. Containers can be extended directly,
* but are lightweight in this case and must be contained by a parent
* somewhere higher up in the component tree that is native.
* (such as Frame for example).
*/
public Container() {
}
@SuppressWarnings({"unchecked","rawtypes"})
void initializeFocusTraversalKeys() {
focusTraversalKeys = new Set[4];
}
/**
* Gets the number of components in this panel.
* <p>
* Note: This method should be called under AWT tree lock.
*
* @return the number of components in this panel.
* @see #getComponent
* @since JDK1.1
* @see Component#getTreeLock()
*/
public int getComponentCount() {
return countComponents();
}
/**
* @deprecated As of JDK version 1.1,
* replaced by getComponentCount().
*/
@Deprecated
public int countComponents() {
// This method is not synchronized under AWT tree lock.
// Instead, the calling code is responsible for the
// synchronization. See 6784816 for details.
return component.size();
}
/**
* Gets the nth component in this container.
* <p>
* Note: This method should be called under AWT tree lock.
*
* @param n the index of the component to get.
* @return the n<sup>th</sup> component in this container.
* @exception ArrayIndexOutOfBoundsException
* if the n<sup>th</sup> value does not exist.
* @see Component#getTreeLock()
*/
public Component getComponent(int n) {
// This method is not synchronized under AWT tree lock.
// Instead, the calling code is responsible for the
// synchronization. See 6784816 for details.
try {
return component.get(n);
} catch (IndexOutOfBoundsException z) {
throw new ArrayIndexOutOfBoundsException("No such child: " + n);
}
}
/**
* Gets all the components in this container.
* <p>
* Note: This method should be called under AWT tree lock.
*
* @return an array of all the components in this container.
* @see Component#getTreeLock()
*/
public Component[] getComponents() {
// This method is not synchronized under AWT tree lock.
// Instead, the calling code is responsible for the
// synchronization. See 6784816 for details.
return getComponents_NoClientCode();
}
// NOTE: This method may be called by privileged threads.
// This functionality is implemented in a package-private method
// to insure that it cannot be overridden by client subclasses.
// DO NOT INVOKE CLIENT CODE ON THIS THREAD!
final Component[] getComponents_NoClientCode() {
return component.toArray(EMPTY_ARRAY);
}
/*
* Wrapper for getComponents() method with a proper synchronization.
*/
Component[] getComponentsSync() {
synchronized (getTreeLock()) {
return getComponents();
}
}
/**
* Determines the insets of this container, which indicate the size
* of the container's border.
* <p>
* A <code>Frame</code> object, for example, has a top inset that
* corresponds to the height of the frame's title bar.
* @return the insets of this container.
* @see Insets
* @see LayoutManager
* @since JDK1.1
*/
public Insets getInsets() {
return insets();
}
/**
* @deprecated As of JDK version 1.1,
* replaced by <code>getInsets()</code>.
*/
@Deprecated
public Insets insets() {
ComponentPeer peer = this.peer;
if (peer instanceof ContainerPeer) {
ContainerPeer cpeer = (ContainerPeer)peer;
return (Insets)cpeer.getInsets().clone();
}
return new Insets(0, 0, 0, 0);
}
/**
* Appends the specified component to the end of this container.
* This is a convenience method for {@link #addImpl}.
* <p>
* This method changes layout-related information, and therefore,
* invalidates the component hierarchy. If the container has already been
* displayed, the hierarchy must be validated thereafter in order to
* display the added component.
*
* @param comp the component to be added
* @exception NullPointerException if {@code comp} is {@code null}
* @see #addImpl
* @see #invalidate
* @see #validate
* @see javax.swing.JComponent#revalidate()
* @return the component argument
*/
public Component add(Component comp) {
addImpl(comp, null, -1);
return comp;
}
/**
* Adds the specified component to this container.
* This is a convenience method for {@link #addImpl}.
* <p>
* This method is obsolete as of 1.1. Please use the
* method <code>add(Component, Object)</code> instead.
* <p>
* This method changes layout-related information, and therefore,
* invalidates the component hierarchy. If the container has already been
* displayed, the hierarchy must be validated thereafter in order to
* display the added component.
*
* @exception NullPointerException if {@code comp} is {@code null}
* @see #add(Component, Object)
* @see #invalidate
*/
public Component add(String name, Component comp) {
addImpl(comp, name, -1);
return comp;
}
/**
* Adds the specified component to this container at the given
* position.
* This is a convenience method for {@link #addImpl}.
* <p>
* This method changes layout-related information, and therefore,
* invalidates the component hierarchy. If the container has already been
* displayed, the hierarchy must be validated thereafter in order to
* display the added component.
*
*
* @param comp the component to be added
* @param index the position at which to insert the component,
* or <code>-1</code> to append the component to the end
* @exception NullPointerException if {@code comp} is {@code null}
* @exception IllegalArgumentException if {@code index} is invalid (see
* {@link #addImpl} for details)
* @return the component <code>comp</code>
* @see #addImpl
* @see #remove
* @see #invalidate
* @see #validate
* @see javax.swing.JComponent#revalidate()
*/
public Component add(Component comp, int index) {
addImpl(comp, null, index);
return comp;
}
/**
* Checks that the component
* isn't supposed to be added into itself.
*/
private void checkAddToSelf(Component comp){
if (comp instanceof Container) {
for (Container cn = this; cn != null; cn=cn.parent) {
if (cn == comp) {
throw new IllegalArgumentException("adding container's parent to itself");
}
}
}
}
/**
* Checks that the component is not a Window instance.
*/
private void checkNotAWindow(Component comp){
if (comp instanceof Window) {
throw new IllegalArgumentException("adding a window to a container");
}
}
/**
* Checks that the component comp can be added to this container
* Checks : index in bounds of container's size,
* comp is not one of this container's parents,
* and comp is not a window.
* Comp and container must be on the same GraphicsDevice.
* if comp is container, all sub-components must be on
* same GraphicsDevice.
*
* @since 1.5
*/
private void checkAdding(Component comp, int index) {
checkTreeLock();
GraphicsConfiguration thisGC = getGraphicsConfiguration();
if (index > component.size() || index < 0) {
throw new IllegalArgumentException("illegal component position");
}
if (comp.parent == this) {
if (index == component.size()) {
throw new IllegalArgumentException("illegal component position " +
index + " should be less then " + component.size());
}
}
checkAddToSelf(comp);
checkNotAWindow(comp);
Window thisTopLevel = getContainingWindow();
Window compTopLevel = comp.getContainingWindow();
if (thisTopLevel != compTopLevel) {
throw new IllegalArgumentException("component and container should be in the same top-level window");
}
if (thisGC != null) {
comp.checkGD(thisGC.getDevice().getIDstring());
}
}
/**
* Removes component comp from this container without making unneccessary changes
* and generating unneccessary events. This function intended to perform optimized
* remove, for example, if newParent and current parent are the same it just changes
* index without calling removeNotify.
* Note: Should be called while holding treeLock
* Returns whether removeNotify was invoked
* @since: 1.5
*/
private boolean removeDelicately(Component comp, Container newParent, int newIndex) {
checkTreeLock();
int index = getComponentZOrder(comp);
boolean needRemoveNotify = isRemoveNotifyNeeded(comp, this, newParent);
if (needRemoveNotify) {
comp.removeNotify();
}
if (newParent != this) {
if (layoutMgr != null) {
layoutMgr.removeLayoutComponent(comp);
}
adjustListeningChildren(AWTEvent.HIERARCHY_EVENT_MASK,
-comp.numListening(AWTEvent.HIERARCHY_EVENT_MASK));
adjustListeningChildren(AWTEvent.HIERARCHY_BOUNDS_EVENT_MASK,
-comp.numListening(AWTEvent.HIERARCHY_BOUNDS_EVENT_MASK));
adjustDescendants(-(comp.countHierarchyMembers()));
comp.parent = null;
if (needRemoveNotify) {
comp.setGraphicsConfiguration(null);
}
component.remove(index);
invalidateIfValid();
} else {
// We should remove component and then
// add it by the newIndex without newIndex decrement if even we shift components to the left
// after remove. Consult the rules below:
// 2->4: 012345 -> 013425, 2->5: 012345 -> 013452
// 4->2: 012345 -> 014235
component.remove(index);
component.add(newIndex, comp);
}
if (comp.parent == null) { // was actually removed
if (containerListener != null ||
(eventMask & AWTEvent.CONTAINER_EVENT_MASK) != 0 ||
Toolkit.enabledOnToolkit(AWTEvent.CONTAINER_EVENT_MASK)) {
ContainerEvent e = new ContainerEvent(this,
ContainerEvent.COMPONENT_REMOVED,
comp);
dispatchEvent(e);
}
comp.createHierarchyEvents(HierarchyEvent.HIERARCHY_CHANGED, comp,
this, HierarchyEvent.PARENT_CHANGED,
Toolkit.enabledOnToolkit(AWTEvent.HIERARCHY_EVENT_MASK));
if (peer != null && layoutMgr == null && isVisible()) {
updateCursorImmediately();
}
}
return needRemoveNotify;
}
/**
* Checks whether this container can contain component which is focus owner.
* Verifies that container is enable and showing, and if it is focus cycle root
* its FTP allows component to be focus owner
* @since 1.5
*/
boolean canContainFocusOwner(Component focusOwnerCandidate) {
if (!(isEnabled() && isDisplayable()
&& isVisible() && isFocusable()))
{
return false;
}
if (isFocusCycleRoot()) {
FocusTraversalPolicy policy = getFocusTraversalPolicy();
if (policy instanceof DefaultFocusTraversalPolicy) {
if (!((DefaultFocusTraversalPolicy)policy).accept(focusOwnerCandidate)) {
return false;
}
}
}
synchronized(getTreeLock()) {
if (parent != null) {
return parent.canContainFocusOwner(focusOwnerCandidate);
}
}
return true;
}
/**
* Checks whether or not this container has heavyweight children.
* Note: Should be called while holding tree lock
* @return true if there is at least one heavyweight children in a container, false otherwise
* @since 1.5
*/
final boolean hasHeavyweightDescendants() {
checkTreeLock();
return numOfHWComponents > 0;
}
/**
* Checks whether or not this container has lightweight children.
* Note: Should be called while holding tree lock
* @return true if there is at least one lightweight children in a container, false otherwise
* @since 1.7
*/
final boolean hasLightweightDescendants() {
checkTreeLock();
return numOfLWComponents > 0;
}
/**
* Returns closest heavyweight component to this container. If this container is heavyweight
* returns this.
* @since 1.5
*/
Container getHeavyweightContainer() {
checkTreeLock();
if (peer != null && !(peer instanceof LightweightPeer)) {
return this;
} else {
return getNativeContainer();
}
}
/**
* Detects whether or not remove from current parent and adding to new parent requires call of
* removeNotify on the component. Since removeNotify destroys native window this might (not)
* be required. For example, if new container and old containers are the same we don't need to
* destroy native window.
* @since: 1.5
*/
private static boolean isRemoveNotifyNeeded(Component comp, Container oldContainer, Container newContainer) {
if (oldContainer == null) { // Component didn't have parent - no removeNotify
return false;
}
if (comp.peer == null) { // Component didn't have peer - no removeNotify
return false;
}
if (newContainer.peer == null) {
// Component has peer but new Container doesn't - call removeNotify
return true;
}
// If component is lightweight non-Container or lightweight Container with all but heavyweight
// children there is no need to call remove notify
if (comp.isLightweight()) {
boolean isContainer = comp instanceof Container;
if (!isContainer || (isContainer && !((Container)comp).hasHeavyweightDescendants())) {
return false;
}
}
// If this point is reached, then the comp is either a HW or a LW container with HW descendants.
// All three components have peers, check for peer change
Container newNativeContainer = oldContainer.getHeavyweightContainer();
Container oldNativeContainer = newContainer.getHeavyweightContainer();
if (newNativeContainer != oldNativeContainer) {
// Native containers change - check whether or not current platform supports
// changing of widget hierarchy on native level without recreation.
// The current implementation forbids reparenting of LW containers with HW descendants
// into another native container w/o destroying the peers. Actually such an operation
// is quite rare. If we ever need to save the peers, we'll have to slightly change the
// addDelicately() method in order to handle such LW containers recursively, reparenting
// each HW descendant independently.
return !comp.peer.isReparentSupported();
} else {
return false;
}
}
/**
* Moves the specified component to the specified z-order index in
* the container. The z-order determines the order that components
* are painted; the component with the highest z-order paints first
* and the component with the lowest z-order paints last.
* Where components overlap, the component with the lower
* z-order paints over the component with the higher z-order.
* <p>
* If the component is a child of some other container, it is
* removed from that container before being added to this container.
* The important difference between this method and
* <code>java.awt.Container.add(Component, int)</code> is that this method
* doesn't call <code>removeNotify</code> on the component while
* removing it from its previous container unless necessary and when
* allowed by the underlying native windowing system. This way, if the
* component has the keyboard focus, it maintains the focus when
* moved to the new position.
* <p>
* This property is guaranteed to apply only to lightweight
* non-<code>Container</code> components.
* <p>
* This method changes layout-related information, and therefore,
* invalidates the component hierarchy.
* <p>
* <b>Note</b>: Not all platforms support changing the z-order of
* heavyweight components from one container into another without
* the call to <code>removeNotify</code>. There is no way to detect
* whether a platform supports this, so developers shouldn't make
* any assumptions.
*
* @param comp the component to be moved
* @param index the position in the container's list to
* insert the component, where <code>getComponentCount()</code>
* appends to the end
* @exception NullPointerException if <code>comp</code> is
* <code>null</code>
* @exception IllegalArgumentException if <code>comp</code> is one of the
* container's parents
* @exception IllegalArgumentException if <code>index</code> is not in
* the range <code>[0, getComponentCount()]</code> for moving
* between containers, or not in the range
* <code>[0, getComponentCount()-1]</code> for moving inside
* a container
* @exception IllegalArgumentException if adding a container to itself
* @exception IllegalArgumentException if adding a <code>Window</code>
* to a container
* @see #getComponentZOrder(java.awt.Component)
* @see #invalidate
* @since 1.5
*/
public void setComponentZOrder(Component comp, int index) {
synchronized (getTreeLock()) {
// Store parent because remove will clear it
Container curParent = comp.parent;
int oldZindex = getComponentZOrder(comp);
if (curParent == this && index == oldZindex) {
return;
}
checkAdding(comp, index);
boolean peerRecreated = (curParent != null) ?
curParent.removeDelicately(comp, this, index) : false;
addDelicately(comp, curParent, index);
// If the oldZindex == -1, the component gets inserted,
// rather than it changes its z-order.
if (!peerRecreated && oldZindex != -1) {
// The new 'index' cannot be == -1.
// It gets checked at the checkAdding() method.
// Therefore both oldZIndex and index denote
// some existing positions at this point and
// this is actually a Z-order changing.
comp.mixOnZOrderChanging(oldZindex, index);
}
}
}
/**
* Traverses the tree of components and reparents children heavyweight component
* to new heavyweight parent.
* @since 1.5
*/
private void reparentTraverse(ContainerPeer parentPeer, Container child) {
checkTreeLock();
for (int i = 0; i < child.getComponentCount(); i++) {
Component comp = child.getComponent(i);
if (comp.isLightweight()) {
// If components is lightweight check if it is container
// If it is container it might contain heavyweight children we need to reparent
if (comp instanceof Container) {
reparentTraverse(parentPeer, (Container)comp);
}
} else {
// Q: Need to update NativeInLightFixer?
comp.getPeer().reparent(parentPeer);
}
}
}
/**
* Reparents child component peer to this container peer.
* Container must be heavyweight.
* @since 1.5
*/
private void reparentChild(Component comp) {
checkTreeLock();
if (comp == null) {
return;
}
if (comp.isLightweight()) {
// If component is lightweight container we need to reparent all its explicit heavyweight children
if (comp instanceof Container) {
// Traverse component's tree till depth-first until encountering heavyweight component
reparentTraverse((ContainerPeer)getPeer(), (Container)comp);
}
} else {
comp.getPeer().reparent((ContainerPeer)getPeer());
}
}
/**
* Adds component to this container. Tries to minimize side effects of this adding -
* doesn't call remove notify if it is not required.
* @since 1.5
*/
private void addDelicately(Component comp, Container curParent, int index) {
checkTreeLock();
// Check if moving between containers
if (curParent != this) {
//index == -1 means add to the end.
if (index == -1) {
component.add(comp);
} else {
component.add(index, comp);
}
comp.parent = this;
comp.setGraphicsConfiguration(getGraphicsConfiguration());
adjustListeningChildren(AWTEvent.HIERARCHY_EVENT_MASK,
comp.numListening(AWTEvent.HIERARCHY_EVENT_MASK));
adjustListeningChildren(AWTEvent.HIERARCHY_BOUNDS_EVENT_MASK,
comp.numListening(AWTEvent.HIERARCHY_BOUNDS_EVENT_MASK));
adjustDescendants(comp.countHierarchyMembers());
} else {
if (index < component.size()) {
component.set(index, comp);
}
}
invalidateIfValid();
if (peer != null) {
if (comp.peer == null) { // Remove notify was called or it didn't have peer - create new one
comp.addNotify();
} else { // Both container and child have peers, it means child peer should be reparented.
// In both cases we need to reparent native widgets.
Container newNativeContainer = getHeavyweightContainer();
Container oldNativeContainer = curParent.getHeavyweightContainer();
if (oldNativeContainer != newNativeContainer) {
// Native container changed - need to reparent native widgets
newNativeContainer.reparentChild(comp);
}
comp.updateZOrder();
if (!comp.isLightweight() && isLightweight()) {
// If component is heavyweight and one of the containers is lightweight
// the location of the component should be fixed.
comp.relocateComponent();
}
}
}
if (curParent != this) {
/* Notify the layout manager of the added component. */
if (layoutMgr != null) {
if (layoutMgr instanceof LayoutManager2) {
((LayoutManager2)layoutMgr).addLayoutComponent(comp, null);
} else {
layoutMgr.addLayoutComponent(null, comp);
}
}
if (containerListener != null ||
(eventMask & AWTEvent.CONTAINER_EVENT_MASK) != 0 ||
Toolkit.enabledOnToolkit(AWTEvent.CONTAINER_EVENT_MASK)) {
ContainerEvent e = new ContainerEvent(this,
ContainerEvent.COMPONENT_ADDED,
comp);
dispatchEvent(e);
}
comp.createHierarchyEvents(HierarchyEvent.HIERARCHY_CHANGED, comp,
this, HierarchyEvent.PARENT_CHANGED,
Toolkit.enabledOnToolkit(AWTEvent.HIERARCHY_EVENT_MASK));
// If component is focus owner or parent container of focus owner check that after reparenting
// focus owner moved out if new container prohibit this kind of focus owner.
if (comp.isFocusOwner() && !comp.canBeFocusOwnerRecursively()) {
comp.transferFocus();
} else if (comp instanceof Container) {
Component focusOwner = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner();
if (focusOwner != null && isParentOf(focusOwner) && !focusOwner.canBeFocusOwnerRecursively()) {
focusOwner.transferFocus();
}
}
} else {
comp.createHierarchyEvents(HierarchyEvent.HIERARCHY_CHANGED, comp,
this, HierarchyEvent.HIERARCHY_CHANGED,
Toolkit.enabledOnToolkit(AWTEvent.HIERARCHY_EVENT_MASK));
}
if (peer != null && layoutMgr == null && isVisible()) {
updateCursorImmediately();
}
}
/**
* Returns the z-order index of the component inside the container.
* The higher a component is in the z-order hierarchy, the lower
* its index. The component with the lowest z-order index is
* painted last, above all other child components.
*
* @param comp the component being queried
* @return the z-order index of the component; otherwise
* returns -1 if the component is <code>null</code>
* or doesn't belong to the container
* @see #setComponentZOrder(java.awt.Component, int)
* @since 1.5
*/
public int getComponentZOrder(Component comp) {
if (comp == null) {
return -1;
}
synchronized(getTreeLock()) {
// Quick check - container should be immediate parent of the component
if (comp.parent != this) {
return -1;
}
return component.indexOf(comp);
}
}
/**
* Adds the specified component to the end of this container.
* Also notifies the layout manager to add the component to
* this container's layout using the specified constraints object.
* This is a convenience method for {@link #addImpl}.
* <p>
* This method changes layout-related information, and therefore,
* invalidates the component hierarchy. If the container has already been
* displayed, the hierarchy must be validated thereafter in order to
* display the added component.
*
*
* @param comp the component to be added
* @param constraints an object expressing
* layout constraints for this component
* @exception NullPointerException if {@code comp} is {@code null}
* @see #addImpl
* @see #invalidate
* @see #validate
* @see javax.swing.JComponent#revalidate()
* @see LayoutManager
* @since JDK1.1
*/
public void add(Component comp, Object constraints) {
addImpl(comp, constraints, -1);
}
/**
* Adds the specified component to this container with the specified
* constraints at the specified index. Also notifies the layout
* manager to add the component to the this container's layout using
* the specified constraints object.
* This is a convenience method for {@link #addImpl}.
* <p>
* This method changes layout-related information, and therefore,
* invalidates the component hierarchy. If the container has already been
* displayed, the hierarchy must be validated thereafter in order to
* display the added component.
*
*
* @param comp the component to be added
* @param constraints an object expressing layout constraints for this
* @param index the position in the container's list at which to insert
* the component; <code>-1</code> means insert at the end
* component
* @exception NullPointerException if {@code comp} is {@code null}
* @exception IllegalArgumentException if {@code index} is invalid (see
* {@link #addImpl} for details)
* @see #addImpl
* @see #invalidate
* @see #validate
* @see javax.swing.JComponent#revalidate()
Morty Proxy This is a proxified and sanitized view of the page, visit original site.