5454
5555
5656/**
57+ *
58+ * SwingJS note: THIS CLASS SHOULD NEVER BE DIRECTLY SUBCLASSED BY THE DEVELOPER IN SwingJS
59+ *
5760 * A generic Abstract Window Toolkit(AWT) container object is a component
5861 * that can contain other AWT components.
5962 * <p>
@@ -82,14 +85,38 @@ public class Container extends JSComponent {
8285// private static final Logger log = Logger.getLogger("java.awt.Container");
8386// private static final Logger eventLog = Logger.getLogger("java.awt.event.Container");
8487//
85- private static final Component [] EMPTY_ARRAY = new Component [0 ];
88+ public static final Component [] EMPTY_ARRAY = new Component [0 ];
8689
8790 /**
8891 * The components in this container.
92+ *
93+ * BH - sorry, for my sanity I changed this name from "component" to "children"
94+ *
8995 * @see #add
9096 * @see #getComponents
9197 */
92- private Lst <Component > children ;
98+ private Lst <Component > component ;
99+
100+ private Component [] _childArray ;
101+
102+ private boolean _childTainted ;
103+
104+
105+ /**
106+ * Fast access to stable array of children; last element is null
107+ *
108+ * @return
109+ */
110+ Component [] getChildArray () {
111+ int n = component .size ();
112+ if (n == 0 )
113+ return EMPTY_ARRAY ;
114+ if (_childArray != null && !_childTainted )
115+ return _childArray ;
116+ _childTainted = false ;
117+ return component .toArray (_childArray != null
118+ && _childArray .length > n ? _childArray : (_childArray = new Component [n * 2 ]));
119+ }
93120
94121 /**
95122 * Layout manager for this container.
@@ -260,7 +287,7 @@ public class Container extends JSComponent {
260287 *
261288 */
262289 public Container () {
263- children = new Lst <Component >();
290+ component = new Lst <Component >();
264291 }
265292
266293 void initializeFocusTraversalKeys () {
@@ -284,7 +311,7 @@ public int getComponentCount() {
284311 @ Deprecated
285312 public int countComponents () {
286313 //synchronized (getTreeLock()) {
287- return children .size ();
314+ return component .size ();
288315 //}
289316 }
290317
@@ -297,10 +324,10 @@ public int countComponents() {
297324 */
298325 public Component getComponent (int n ) {
299326 //synchronized (getTreeLock()) {
300- if ((n < 0 ) || (n >= children .size ())) {
327+ if ((n < 0 ) || (n >= component .size ())) {
301328 throw new ArrayIndexOutOfBoundsException ("No such child: " + n );
302329 }
303- return children .get (n );
330+ return component .get (n );
304331 //}
305332 }
306333
@@ -317,10 +344,10 @@ public Component[] getComponents() {
317344 // DO NOT INVOKE CLIENT CODE ON THIS THREAD!
318345 final Component [] getComponents_NoClientCode () {
319346 // synchronized (getTreeLock()) {
320- return children .toArray (EMPTY_ARRAY );
347+ return component .toArray (EMPTY_ARRAY );
321348 // }
322349 } // getComponents_NoClientCode()
323-
350+
324351 /**
325352 * Determines the insets of this container, which indicate the size
326353 * of the container's border.
@@ -499,7 +526,8 @@ private boolean removeDelicately(Component comp, Container newParent, int newInd
499526 adjustDescendants (-(comp .countHierarchyMembers ()));
500527
501528 comp .parent = null ;
502- children .removeItemAt (index );
529+ component .removeItemAt (index );
530+ _childTainted = true ;
503531
504532 invalidateIfValid ();
505533 } else {
@@ -508,8 +536,9 @@ private boolean removeDelicately(Component comp, Container newParent, int newInd
508536 // after remove. Consult the rules below:
509537 // 2->4: 012345 -> 013425, 2->5: 012345 -> 013452
510538 // 4->2: 012345 -> 014235
511- children .removeItemAt (index );
512- children .add (newIndex , comp );
539+ component .removeItemAt (index );
540+ component .add (newIndex , comp );
541+ _childTainted = true ;
513542 }
514543 if (comp .parent == null ) { // was actually removed
515544 if (containerListener != null ||
@@ -716,7 +745,7 @@ public void setComponentZOrder(Component comp, int index) {
716745 comp .mixOnZOrderChanging (oldZindex , index );
717746 }
718747
719- updateUIZOrder (( JSComponent []) getComponents () );
748+ updateUIZOrder ();
720749
721750 }
722751 }
@@ -782,10 +811,11 @@ private void addDelicately(Component comp, Container curParent, int index) {
782811 if (curParent != this ) {
783812 //index == -1 means add to the end.
784813 if (index == -1 ) {
785- children .add (comp );
814+ component .add (comp );
786815 } else {
787- children .add (index , comp );
816+ component .add (index , comp );
788817 }
818+ _childTainted = true ;
789819 comp .parent = this ;
790820
791821 adjustListeningChildren (AWTEvent .HIERARCHY_EVENT_MASK ,
@@ -794,8 +824,9 @@ private void addDelicately(Component comp, Container curParent, int index) {
794824 comp .numListening (AWTEvent .HIERARCHY_BOUNDS_EVENT_MASK ));
795825 adjustDescendants (comp .countHierarchyMembers ());
796826 } else {
797- if (index < children .size ()) {
798- children .set (index , comp );
827+ if (index < component .size ()) {
828+ component .set (index , comp );
829+ _childTainted = true ;
799830 }
800831 }
801832
@@ -901,7 +932,7 @@ public int getComponentZOrder(Component comp) {
901932 if (comp .parent != this ) {
902933 return -1 ;
903934 }
904- return children .indexOf (comp );
935+ return component .indexOf (comp );
905936 }
906937 }
907938
@@ -1046,7 +1077,7 @@ protected Component addImplCont(Component comp, Object constraints, int index) {
10461077 */
10471078// GraphicsConfiguration thisGC = this.getGraphicsConfiguration();
10481079
1049- if (index > children .size () || (index < 0 && index != -1 )) {
1080+ if (index > component .size () || (index < 0 && index != -1 )) {
10501081 throw new IllegalArgumentException (
10511082 "illegal component position" );
10521083 }
@@ -1059,17 +1090,18 @@ protected Component addImplCont(Component comp, Object constraints, int index) {
10591090 /* Reparent the component and tidy up the tree's state. */
10601091 if (comp .parent != null ) {
10611092 comp .parent .remove (comp );
1062- if (index > children .size ()) {
1093+ if (index > component .size ()) {
10631094 throw new IllegalArgumentException ("illegal component position" );
10641095 }
10651096 }
10661097
10671098 //index == -1 means add to the end.
10681099 if (index == -1 ) {
1069- children .add (comp );
1100+ component .add (comp );
10701101 } else {
1071- children .add (index , comp );
1102+ component .add (index , comp );
10721103 }
1104+ _childTainted = true ;
10731105 comp .parent = this ;
10741106
10751107 adjustListeningChildren (AWTEvent .HIERARCHY_EVENT_MASK ,
@@ -1149,10 +1181,10 @@ void checkGD(String stringID) {
11491181 */
11501182 public void remove (int index ) {
11511183 synchronized (getTreeLock ()) {
1152- if (index < 0 || index >= children .size ()) {
1184+ if (index < 0 || index >= component .size ()) {
11531185 throw new ArrayIndexOutOfBoundsException (index );
11541186 }
1155- Component comp = children .get (index );
1187+ Component comp = component .get (index );
11561188 if (peer != null ) {
11571189 comp .removeNotify ();
11581190 }
@@ -1167,7 +1199,8 @@ public void remove(int index) {
11671199 adjustDescendants (-(comp .countHierarchyMembers ()));
11681200
11691201 comp .parent = null ;
1170- children .removeItemAt (index );
1202+ component .removeItemAt (index );
1203+ _childTainted = true ;
11711204
11721205 invalidateIfValid ();
11731206 if (containerListener != null
@@ -1209,7 +1242,7 @@ public void remove(int index) {
12091242 public void remove (Component comp ) {
12101243 synchronized (getTreeLock ()) {
12111244 if (comp .parent == this ) {
1212- int index = children .indexOf (comp );
1245+ int index = component .indexOf (comp );
12131246 if (index >= 0 ) {
12141247 remove (index );
12151248 }
@@ -1233,8 +1266,8 @@ public void removeAll() {
12331266 -listeningBoundsChildren );
12341267 adjustDescendants (-descendantsCount );
12351268
1236- while (!children .isEmpty ()) {
1237- Component comp = children .removeItemAt (children .size ()-1 );
1269+ while (!component .isEmpty ()) {
1270+ Component comp = component .removeItemAt (component .size ()-1 );
12381271
12391272 if (peer != null ) {
12401273 comp .removeNotify ();
@@ -1384,7 +1417,7 @@ final int createHierarchyEvents(int id, Component changed,
13841417 int listeners = getListenersCount (id , enabledOnToolkit );
13851418
13861419 for (int count = listeners , i = 0 ; count > 0 ; i ++) {
1387- count -= children .get (i ).createHierarchyEvents (id , changed ,
1420+ count -= component .get (i ).createHierarchyEvents (id , changed ,
13881421 changedParent , changeFlags , enabledOnToolkit );
13891422 }
13901423 return listeners +
@@ -1396,13 +1429,13 @@ final void createChildHierarchyEvents(int id, long changeFlags,
13961429 boolean enabledOnToolkit )
13971430 {
13981431 //assert Thread.holdsLock(getTreeLock());
1399- if (children .isEmpty ()) {
1432+ if (component .isEmpty ()) {
14001433 return ;
14011434 }
14021435 int listeners = getListenersCount (id , enabledOnToolkit );
14031436
14041437 for (int count = listeners , i = 0 ; count > 0 ; i ++) {
1405- count -= children .get (i ).createHierarchyEvents (id , this , parent ,
1438+ count -= component .get (i ).createHierarchyEvents (id , this , parent ,
14061439 changeFlags , enabledOnToolkit );
14071440 }
14081441 }
@@ -1515,7 +1548,7 @@ public void validate() {
15151548 // getting its peer in time for validation.
15161549 if (peer == null )
15171550 peer = getToolkit ().createComponent (this );
1518- int n = children .size ();
1551+ int n = component .size ();
15191552 if (!isValid () && peer != null && n > 0 ) {
15201553 ContainerPeer p = null ;
15211554 if (peer instanceof ContainerPeer )
@@ -1566,8 +1599,8 @@ protected void validateTree() {
15661599 ((ContainerPeer )peer ).beginLayout ();
15671600 }
15681601 doLayout ();
1569- for (int i = 0 ; i < children .size (); i ++) {
1570- Component comp = children .get (i );
1602+ for (int i = 0 ; i < component .size (); i ++) {
1603+ Component comp = component .get (i );
15711604 if ( (comp instanceof Container )
15721605 // SwingJS needs to create all DIV elements
15731606 // && !(comp instanceof Window)
@@ -1590,8 +1623,8 @@ protected void validateTree() {
15901623 */
15911624 public void invalidateTree () { // SwingJS -- need this public for ToolTipManager PopupFactory
15921625 synchronized (getTreeLock ()) {
1593- for (int i = 0 ; i < children .size (); i ++) {
1594- Component comp = children .get (i );
1626+ for (int i = 0 ; i < component .size (); i ++) {
1627+ Component comp = component .get (i );
15951628 if (comp instanceof Container ) {
15961629 ((Container )comp ).invalidateTree ();
15971630 }
@@ -1852,7 +1885,7 @@ public void paintContainer(Graphics g) {
18521885// // super.paint(); -- Don't bother, since it's a NOP.
18531886//
18541887 GraphicsCallback .PaintCallback .getInstance ().
1855- runComponents (children . toArray ( EMPTY_ARRAY ), g , SunGraphicsCallback .LIGHTWEIGHTS );
1888+ runComponents (getComponentCount (), getChildArray ( ), g , SunGraphicsCallback .LIGHTWEIGHTS );
18561889// }
18571890 }
18581891
@@ -1926,7 +1959,7 @@ protected void updateContainer(Graphics g) {
19261959 public void paintComponents (Graphics g ) {
19271960 if (isShowing ()) {
19281961 GraphicsCallback .PaintAllCallback .getInstance ().
1929- runComponents (children . toArray ( EMPTY_ARRAY ), g , SunGraphicsCallback .TWO_PASSES );
1962+ runComponents (getComponentCount (), getChildArray ( ), g , SunGraphicsCallback .TWO_PASSES );
19301963 }
19311964 }
19321965
@@ -1950,7 +1983,7 @@ void lightweightPaint(Graphics g) {
19501983 void paintHeavyweightComponents (Graphics g ) {
19511984 if (isShowing ()) {
19521985 GraphicsCallback .PaintHeavyweightComponentsCallback .getInstance ().
1953- runComponents (children . toArray ( EMPTY_ARRAY ), g , SunGraphicsCallback .LIGHTWEIGHTS |
1986+ runComponents (getComponentCount (), getChildArray ( ), g , SunGraphicsCallback .LIGHTWEIGHTS |
19541987 SunGraphicsCallback .HEAVYWEIGHTS );
19551988 }
19561989 }
@@ -2296,8 +2329,8 @@ private Component getMouseEventTargetImpl(int x, int y, boolean includeSelf,
22962329 boolean searchHeavyweightDescendants ) {
22972330 synchronized (getTreeLock ()) {
22982331
2299- for (int i = 0 ; i < children .size (); i ++) {
2300- Component comp = children .get (i );
2332+ for (int i = 0 ; i < component .size (); i ++) {
2333+ Component comp = component .get (i );
23012334
23022335 // comp != null && comp.visible && searchNeavyweightChildren != (comp.peer instanceof LightweightPeer)
23032336 // && comp.contains(x - comp.x, y - comp.y)
@@ -2680,8 +2713,8 @@ public void addNotify() {
26802713 // the menu is being assigned as a child to JLayeredPane
26812714 // instead of particular component so always affect
26822715 // collection of component if menu is becoming shown or hidden.
2683- for (int i = 0 ; i < children .size (); i ++) {
2684- children .get (i ).addNotify ();
2716+ for (int i = 0 ; i < component .size (); i ++) {
2717+ component .get (i ).addNotify ();
26852718 }
26862719// SwingJS // Update stacking order if native platform allows
26872720// ContainerPeer cpeer = (ContainerPeer)peer;
@@ -2719,8 +2752,8 @@ public void removeNotify() {
27192752 // the menu is being assigned as a child to JLayeredPane
27202753 // instead of particular component so always affect
27212754 // collection of component if menu is becoming shown or hidden.
2722- for (int i = children .size (); --i >= 0 ;) {
2723- Component comp = children .get (i );
2755+ for (int i = component .size (); --i >= 0 ;) {
2756+ Component comp = component .get (i );
27242757 if (comp != null ) {
27252758 // Fix for 6607170.
27262759 // We want to suppress focus change on disposal
@@ -3485,8 +3518,8 @@ boolean postsOldMouseEvents() {
34853518 public void applyComponentOrientation (ComponentOrientation o ) {
34863519 applyCompOrientComp (o );
34873520 synchronized (getTreeLock ()) {
3488- for (int i = 0 ; i < children .size (); i ++) {
3489- Component comp = children .get (i );
3521+ for (int i = 0 ; i < component .size (); i ++) {
3522+ Component comp = component .get (i );
34903523 comp .applyComponentOrientation (o );
34913524 }
34923525 }
0 commit comments