1 7 package java.awt; 8 9 import java.io.PrintStream ; 10 import java.io.PrintWriter ; 11 import java.awt.peer.ContainerPeer; 12 import java.awt.peer.ComponentPeer; 13 import java.awt.peer.LightweightPeer; 14 import sun.awt.PeerEvent; 15 import java.awt.event.ComponentEvent ; 16 import java.awt.event.ContainerEvent ; 17 import java.awt.event.FocusEvent ; 18 import java.awt.event.HierarchyEvent ; 19 import java.awt.event.InputEvent ; 20 import java.awt.event.KeyEvent ; 21 import java.awt.event.MouseEvent ; 22 import java.awt.event.MouseWheelEvent ; 23 import java.awt.event.ContainerListener ; 24 import java.util.EventListener ; 25 import java.io.ObjectStreamField ; 26 import java.io.ObjectOutputStream ; 27 import java.io.ObjectInputStream ; 28 import java.io.IOException ; 29 import java.awt.event.AWTEventListener ; 30 import java.awt.event.WindowAdapter ; 31 import java.awt.event.WindowListener ; 32 import java.awt.event.WindowEvent ; 33 import java.awt.dnd.DropTarget ; 34 import java.util.HashSet ; 35 import java.util.LinkedList ; 36 import java.util.Set ; 37 import java.util.Iterator ; 38 import javax.accessibility.*; 39 import java.beans.PropertyChangeListener ; 40 import javax.swing.JRootPane ; 41 42 import sun.awt.AppContext; 43 import sun.awt.DebugHelper; 44 import sun.awt.SunToolkit; 45 import sun.awt.dnd.SunDropTargetEvent; 46 47 72 public class Container extends Component { 73 74 81 int ncomponents; 82 83 88 Component component[] = new Component [0]; 89 90 96 LayoutManager layoutMgr; 97 98 104 private LightweightDispatcher dispatcher; 105 106 124 private transient FocusTraversalPolicy focusTraversalPolicy; 125 126 138 private boolean focusCycleRoot = false; 139 140 141 146 private boolean focusTraversalPolicyProvider; 147 148 private transient Set printingThreads; 150 private transient boolean printing = false; 152 153 transient ContainerListener containerListener; 154 155 156 transient int listeningChildren; 157 transient int listeningBoundsChildren; 158 transient int descendantsCount; 159 160 163 private static final long serialVersionUID = 4613797578919906343L; 164 165 private static final DebugHelper dbg = DebugHelper.create(Container .class); 166 167 175 static final boolean INCLUDE_SELF = true; 176 177 184 static final boolean SEARCH_HEAVYWEIGHTS = true; 185 186 213 private static final ObjectStreamField [] serialPersistentFields = { 214 new ObjectStreamField ("ncomponents", Integer.TYPE), 215 new ObjectStreamField ("component", Component [].class), 216 new ObjectStreamField ("layoutMgr", LayoutManager .class), 217 new ObjectStreamField ("dispatcher", LightweightDispatcher.class), 218 new ObjectStreamField ("maxSize", Dimension .class), 219 new ObjectStreamField ("focusCycleRoot", Boolean.TYPE), 220 new ObjectStreamField ("containerSerializedDataVersion", Integer.TYPE), 221 new ObjectStreamField ("focusTraversalPolicyProvider", Boolean.TYPE), 222 }; 223 224 static { 225 226 Toolkit.loadLibraries(); 227 if (!GraphicsEnvironment.isHeadless()) { 228 initIDs(); 229 } 230 } 231 232 236 private static native void initIDs(); 237 238 244 public Container() { 245 } 246 247 void initializeFocusTraversalKeys() { 248 focusTraversalKeys = new Set [4]; 249 } 250 251 257 public int getComponentCount() { 258 return countComponents(); 259 } 260 261 265 @Deprecated 266 public int countComponents() { 267 return ncomponents; 268 } 269 270 277 public Component getComponent(int n) { 278 synchronized (getTreeLock()) { 279 if ((n < 0) || (n >= ncomponents)) { 280 throw new ArrayIndexOutOfBoundsException ("No such child: " + n); 281 } 282 return component[n]; 283 } 284 } 285 286 290 public Component [] getComponents() { 291 return getComponents_NoClientCode(); 292 } 293 final Component [] getComponents_NoClientCode() { 298 synchronized (getTreeLock()) { 299 Component list[] = new Component [ncomponents]; 300 System.arraycopy(component, 0, list, 0, ncomponents); 301 return list; 302 } 303 } 305 316 public Insets getInsets() { 317 return insets(); 318 } 319 320 324 @Deprecated 325 public Insets insets() { 326 if (this.peer != null && this.peer instanceof ContainerPeer) { 327 ContainerPeer peer = (ContainerPeer)this.peer; 328 return (Insets )peer.insets().clone(); 329 } 330 return new Insets (0, 0, 0, 0); 331 } 332 333 350 public Component add(Component comp) { 351 addImpl(comp, null, -1); 352 return comp; 353 } 354 355 363 public Component add(String name, Component comp) { 364 addImpl(comp, name, -1); 365 return comp; 366 } 367 368 389 public Component add(Component comp, int index) { 390 addImpl(comp, null, index); 391 return comp; 392 } 393 394 void checkTreeLock() { 395 if (!Thread.holdsLock(getTreeLock())) { 396 throw new IllegalStateException ("This function should be called while holding treeLock"); 397 } 398 } 399 410 private void checkAdding(Component comp, int index) { 411 checkTreeLock(); 412 413 GraphicsConfiguration thisGC = getGraphicsConfiguration(); 414 415 if (index > ncomponents || index < 0) { 416 throw new IllegalArgumentException ("illegal component position"); 417 } 418 if (comp.parent == this) { 419 if (index == ncomponents) { 420 throw new IllegalArgumentException ("illegal component position " + 421 index + " should be less then " + ncomponents); 422 } 423 } 424 if (comp instanceof Container ) { 425 for (Container cn = this; cn != null; cn=cn.parent) { 426 if (cn == comp) { 427 throw new IllegalArgumentException ("adding container's parent to itself"); 428 } 429 } 430 431 if (comp instanceof Window ) { 432 throw new IllegalArgumentException ("adding a window to a container"); 433 } 434 } 435 Window thisTopLevel = getContainingWindow(); 436 Window compTopLevel = comp.getContainingWindow(); 437 if (thisTopLevel != compTopLevel) { 438 throw new IllegalArgumentException ("component and container should be in the same top-level window"); 439 } 440 if (thisGC != null) { 441 comp.checkGD(thisGC.getDevice().getIDstring()); 442 } 443 } 444 445 453 private void removeDelicately(Component comp, Container newParent, int newIndex) { 454 checkTreeLock(); 455 456 int index = getComponentZOrder(comp); 457 if (isRemoveNotifyNeeded(comp, this, newParent)) { 458 comp.removeNotify(); 459 } 460 if (newParent != this) { 461 if (layoutMgr != null) { 462 layoutMgr.removeLayoutComponent(comp); 463 } 464 adjustListeningChildren(AWTEvent.HIERARCHY_EVENT_MASK, 465 -comp.numListening(AWTEvent.HIERARCHY_EVENT_MASK)); 466 adjustListeningChildren(AWTEvent.HIERARCHY_BOUNDS_EVENT_MASK, 467 -comp.numListening(AWTEvent.HIERARCHY_BOUNDS_EVENT_MASK)); 468 adjustDescendants(-(comp.countHierarchyMembers())); 469 470 comp.parent = null; 471 System.arraycopy(component, index + 1, 472 component, index, 473 ncomponents - index - 1); 474 component[--ncomponents] = null; 475 476 if (valid) { 477 invalidate(); 478 } 479 } else { 480 if (newIndex > index) { if (newIndex-index > 0) { 482 System.arraycopy(component, index+1, component, index, newIndex-index); 483 } 484 } else { if (index-newIndex > 0) { 486 System.arraycopy(component, newIndex, component, newIndex+1, index-newIndex); 487 } 488 } 489 component[newIndex] = comp; 490 } 491 if (comp.parent == null) { if (containerListener != null || 493 (eventMask & AWTEvent.CONTAINER_EVENT_MASK) != 0 || 494 Toolkit.enabledOnToolkit(AWTEvent.CONTAINER_EVENT_MASK)) { 495 ContainerEvent e = new ContainerEvent (this, 496 ContainerEvent.COMPONENT_REMOVED, 497 comp); 498 dispatchEvent(e); 499 500 } 501 comp.createHierarchyEvents(HierarchyEvent.HIERARCHY_CHANGED, comp, 502 this, HierarchyEvent.PARENT_CHANGED, 503 Toolkit.enabledOnToolkit(AWTEvent.HIERARCHY_EVENT_MASK)); 504 if (peer != null && layoutMgr == null && isVisible()) { 505 updateCursorImmediately(); 506 } 507 } 508 } 509 510 516 boolean canContainFocusOwner(Component focusOwnerCandidate) { 517 if (!(isEnabled() && isDisplayable() 518 && isVisible() && isFocusable())) 519 { 520 return false; 521 } 522 if (isFocusCycleRoot()) { 523 FocusTraversalPolicy policy = getFocusTraversalPolicy(); 524 if (policy instanceof DefaultFocusTraversalPolicy ) { 525 if (!((DefaultFocusTraversalPolicy )policy).accept(focusOwnerCandidate)) { 526 return false; 527 } 528 } 529 } 530 synchronized(getTreeLock()) { 531 if (parent != null) { 532 return parent.canContainFocusOwner(focusOwnerCandidate); 533 } 534 } 535 return true; 536 } 537 538 544 private boolean hasHeavyweightChildren() { 545 checkTreeLock(); 546 boolean res = true; for (int i = 0; i < getComponentCount() && res; i++) { 548 Component child = getComponent(i); 549 res &= child.isLightweight(); 550 if (res && child instanceof Container ) { 551 res &= !((Container )child).hasHeavyweightChildren(); 552 } 553 } 554 return !res; 555 } 556 557 562 Container getHeavyweightContainer() { 563 checkTreeLock(); 564 if (peer != null && !(peer instanceof LightweightPeer)) { 565 return this; 566 } else { 567 return getNativeContainer(); 568 } 569 } 570 571 578 private static boolean isRemoveNotifyNeeded(Component comp, Container oldContainer, Container newContainer) { 579 if (oldContainer == null) { return false; 581 } 582 if (comp.peer == null) { return false; 584 } 585 if (newContainer.peer == null) { 586 return true; 588 } 589 590 if (comp.isLightweight()) { 593 if (comp instanceof Container ) { 594 return ((Container )comp).hasHeavyweightChildren(); 596 } else { 597 return false; 599 } 600 } 601 602 Container newNativeContainer = oldContainer.getHeavyweightContainer(); 604 Container oldNativeContainer = newContainer.getHeavyweightContainer(); 605 if (newNativeContainer != oldNativeContainer) { 606 return !comp.peer.isReparentSupported(); 609 } else { 610 return !comp.isLightweight() && 614 !((ContainerPeer)(newNativeContainer.peer)).isRestackSupported(); 615 } 616 } 617 618 664 public final void setComponentZOrder(Component comp, int index) { 665 synchronized (getTreeLock()) { 666 Container curParent = comp.parent; 668 if (curParent == this && index == getComponentZOrder(comp)) { 669 return; 670 } 671 checkAdding(comp, index); 672 if (curParent != null) { 673 curParent.removeDelicately(comp, this, index); 674 } 675 676 addDelicately(comp, curParent, index); 677 } 678 } 679 680 685 private void reparentTraverse(ContainerPeer parentPeer, Container child) { 686 checkTreeLock(); 687 688 for (int i = 0; i < child.getComponentCount(); i++) { 689 Component comp = child.getComponent(i); 690 if (comp.isLightweight()) { 691 if (comp instanceof Container ) { 694 reparentTraverse(parentPeer, (Container )comp); 695 } 696 } else { 697 comp.getPeer().reparent(parentPeer); 699 } 700 } 701 } 702 703 708 private void reparentChild(Component comp) { 709 checkTreeLock(); 710 if (comp == null) { 711 return; 712 } 713 if (comp.isLightweight()) { 714 if (comp instanceof Container ) { 716 reparentTraverse((ContainerPeer)getPeer(), (Container )comp); 718 } 719 } else { 720 comp.getPeer().reparent((ContainerPeer)getPeer()); 721 } 722 } 723 724 729 private void addDelicately(Component comp, Container curParent, int index) { 730 checkTreeLock(); 731 732 if (curParent != this) { 734 735 if (ncomponents == component.length) { 736 Component newcomponents[] = new Component [ncomponents * 2 + 1]; 737 System.arraycopy(component, 0, newcomponents, 0, ncomponents); 738 component = newcomponents; 739 } 740 if (index == -1 || index == ncomponents) { 741 component[ncomponents++] = comp; 742 } else { 743 System.arraycopy(component, index, component, 744 index + 1, ncomponents - index); 745 component[index] = comp; 746 ncomponents++; 747 } 748 comp.parent = this; 749 750 adjustListeningChildren(AWTEvent.HIERARCHY_EVENT_MASK, 751 comp.numListening(AWTEvent.HIERARCHY_EVENT_MASK)); 752 adjustListeningChildren(AWTEvent.HIERARCHY_BOUNDS_EVENT_MASK, 753 comp.numListening(AWTEvent.HIERARCHY_BOUNDS_EVENT_MASK)); 754 adjustDescendants(comp.countHierarchyMembers()); 755 } else { 756 if (index < ncomponents) { 757 component[index] = comp; 758 } 759 } 760 761 if (valid) { 762 invalidate(); 763 } 764 if (peer != null) { 765 if (comp.peer == null) { comp.addNotify(); 767 Container newNativeContainer = getHeavyweightContainer(); 769 if (((ContainerPeer)newNativeContainer.getPeer()).isRestackSupported()) { 770 ((ContainerPeer)newNativeContainer.getPeer()).restack(); 771 } 772 } else { Container newNativeContainer = getHeavyweightContainer(); 775 Container oldNativeContainer = curParent.getHeavyweightContainer(); 776 if (oldNativeContainer != newNativeContainer) { 777 newNativeContainer.reparentChild(comp); 779 } 780 if ((!comp.isLightweight() || (comp instanceof Container )) 783 && ((ContainerPeer)newNativeContainer.getPeer()).isRestackSupported()) 784 { 785 ((ContainerPeer)newNativeContainer.getPeer()).restack(); 786 } 787 if (!comp.isLightweight() && isLightweight()) { 788 if (!curParent.isLightweight()) { 791 comp.nativeInLightFixer = new NativeInLightFixer(); 794 } else { 795 comp.nativeInLightFixer.install(this); 798 } 799 } 800 } 801 } 802 if (curParent != this) { 803 804 if (layoutMgr != null) { 805 if (layoutMgr instanceof LayoutManager2 ) { 806 ((LayoutManager2 )layoutMgr).addLayoutComponent(comp, null); 807 } else { 808 layoutMgr.addLayoutComponent(null, comp); 809 } 810 } 811 if (containerListener != null || 812 (eventMask & AWTEvent.CONTAINER_EVENT_MASK) != 0 || 813 Toolkit.enabledOnToolkit(AWTEvent.CONTAINER_EVENT_MASK)) { 814 ContainerEvent e = new ContainerEvent (this, 815 ContainerEvent.COMPONENT_ADDED, 816 comp); 817 dispatchEvent(e); 818 } 819 comp.createHierarchyEvents(HierarchyEvent.HIERARCHY_CHANGED, comp, 820 this, HierarchyEvent.PARENT_CHANGED, 821 Toolkit.enabledOnToolkit(AWTEvent.HIERARCHY_EVENT_MASK)); 822 823 if (comp.isFocusOwner() && !comp.canBeFocusOwner()) { 826 comp.transferFocus(); 827 } else if (comp instanceof Container ) { 828 Component focusOwner = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner(); 829 if (focusOwner != null && isParentOf(focusOwner) && !focusOwner.canBeFocusOwner()) { 830 focusOwner.transferFocus(); 831 } 832 } 833 } else { 834 comp.createHierarchyEvents(HierarchyEvent.HIERARCHY_CHANGED, comp, 835 this, HierarchyEvent.HIERARCHY_CHANGED, 836 Toolkit.enabledOnToolkit(AWTEvent.HIERARCHY_EVENT_MASK)); 837 } 838 839 if (peer != null && layoutMgr == null && isVisible()) { 840 updateCursorImmediately(); 841 } 842 } 843 844 857 public final int getComponentZOrder(Component comp) { 858 if (comp == null) { 859 return -1; 860 } 861 synchronized(getTreeLock()) { 862 if (comp.parent != this) { 864 return -1; 865 } 866 for (int i = 0; i < ncomponents; i++) { 867 if (component[i] == comp) { 868 return i; 869 } 870 } 871 } 872 return -1; 874 } 875 876 898 public void add(Component comp, Object constraints) { 899 addImpl(comp, constraints, -1); 900 } 901 902 927 public void add(Component comp, Object constraints, int index) { 928 addImpl(comp, constraints, index); 929 } 930 931 987 protected void addImpl(Component comp, Object constraints, int index) { 988 synchronized (getTreeLock()) { 989 996 GraphicsConfiguration thisGC = this.getGraphicsConfiguration(); 997 998 if (index > ncomponents || (index < 0 && index != -1)) { 999 throw new IllegalArgumentException ( 1000 "illegal component position"); 1001 } 1002 if (comp instanceof Container ) { 1003 for (Container cn = this; cn != null; cn=cn.parent) { 1004 if (cn == comp) { 1005 throw new IllegalArgumentException ( 1006 "adding container's parent to itself"); 1007 } 1008 } 1009 if (comp instanceof Window ) { 1010 throw new IllegalArgumentException ( 1011 "adding a window to a container"); 1012 } 1013 } 1014 if (thisGC != null) { 1015 comp.checkGD(thisGC.getDevice().getIDstring()); 1016 } 1017 1018 1019 if (comp.parent != null) { 1020 comp.parent.remove(comp); 1021 if (index > ncomponents) { 1022 throw new IllegalArgumentException ("illegal component position"); 1023 } 1024 } 1025 1026 1027 if (ncomponents == component.length) { 1028 Component newcomponents[] = new Component [ncomponents * 2 + 1]; 1029 System.arraycopy(component, 0, newcomponents, 0, ncomponents); 1030 component = newcomponents; 1031 } 1032 if (index == -1 || index == ncomponents) { 1033 component[ncomponents++] = comp; 1034 } else { 1035 System.arraycopy(component, index, component, 1036 index + 1, ncomponents - index); 1037 component[index] = comp; 1038 ncomponents++; 1039 } 1040 comp.parent = this; 1041 1042 adjustListeningChildren(AWTEvent.HIERARCHY_EVENT_MASK, 1043 comp.numListening(AWTEvent.HIERARCHY_EVENT_MASK)); 1044 adjustListeningChildren(AWTEvent.HIERARCHY_BOUNDS_EVENT_MASK, 1045 comp.numListening(AWTEvent.HIERARCHY_BOUNDS_EVENT_MASK)); 1046 adjustDescendants(comp.countHierarchyMembers()); 1047 1048 if (valid) { 1049 invalidate(); 1050 } 1051 if (peer != null) { 1052 comp.addNotify(); 1053 } 1054 1055 1056 if (layoutMgr != null) { 1057 if (layoutMgr instanceof LayoutManager2 ) { 1058 ((LayoutManager2 )layoutMgr).addLayoutComponent(comp, constraints); 1059 } else if (constraints instanceof String ) { 1060 layoutMgr.addLayoutComponent((String )constraints, comp); 1061 } 1062 } 1063 if (containerListener != null || 1064 (eventMask & AWTEvent.CONTAINER_EVENT_MASK) != 0 || 1065 Toolkit.enabledOnToolkit(AWTEvent.CONTAINER_EVENT_MASK)) { 1066 ContainerEvent e = new ContainerEvent (this, 1067 ContainerEvent.COMPONENT_ADDED, 1068 comp); 1069 dispatchEvent(e); 1070 } 1071 1072 comp.createHierarchyEvents(HierarchyEvent.HIERARCHY_CHANGED, comp, 1073 this, HierarchyEvent.PARENT_CHANGED, 1074 Toolkit.enabledOnToolkit(AWTEvent.HIERARCHY_EVENT_MASK)); 1075 if (peer != null && layoutMgr == null && isVisible()) { 1076 updateCursorImmediately(); 1077 } 1078 } 1079 } 1080 1081 1086 void checkGD(String stringID) { 1087 Component tempComp; 1088 for (int i = 0; i < component.length; i++) { 1089 tempComp= component[i]; 1090 if (tempComp != null) { 1091 tempComp.checkGD(stringID); 1092 } 1093 } 1094 } 1095 1096 1107 public void remove(int index) { 1108 synchronized (getTreeLock()) { 1109 if (index < 0 || index >= ncomponents) { 1110 throw new ArrayIndexOutOfBoundsException (index); 1111 } 1112 Component comp = component[index]; 1113 if (peer != null) { 1114 comp.removeNotify(); 1115 } 1116 if (layoutMgr != null) { 1117 layoutMgr.removeLayoutComponent(comp); 1118 } 1119 1120 adjustListeningChildren(AWTEvent.HIERARCHY_EVENT_MASK, 1121 -comp.numListening(AWTEvent.HIERARCHY_EVENT_MASK)); 1122 adjustListeningChildren(AWTEvent.HIERARCHY_BOUNDS_EVENT_MASK, 1123 -comp.numListening(AWTEvent.HIERARCHY_BOUNDS_EVENT_MASK)); 1124 adjustDescendants(-(comp.countHierarchyMembers())); 1125 1126 comp.parent = null; 1127 System.arraycopy(component, index + 1, 1128 component, index, 1129 ncomponents - index - 1); 1130 component[--ncomponents] = null; 1131 1132 if (valid) { 1133 invalidate(); 1134 } 1135 if (containerListener != null || 1136 (eventMask & AWTEvent.CONTAINER_EVENT_MASK) != 0 || 1137 Toolkit.enabledOnToolkit(AWTEvent.CONTAINER_EVENT_MASK)) { 1138 ContainerEvent e = new ContainerEvent (this, 1139 ContainerEvent.COMPONENT_REMOVED, 1140 comp); 1141 dispatchEvent(e); 1142 } 1143 1144 comp.createHierarchyEvents(HierarchyEvent.HIERARCHY_CHANGED, comp, 1145 this, HierarchyEvent.PARENT_CHANGED, 1146 Toolkit.enabledOnToolkit(AWTEvent.HIERARCHY_EVENT_MASK)); 1147 if (peer != null && layoutMgr == null && isVisible()) { 1148 updateCursorImmediately(); 1149 } 1150 } 1151 } 1152 1153 1163 public void remove(Component comp) { 1164 synchronized (getTreeLock()) { 1165 if (comp.parent == this) { 1166 1169 Component component[] = this.component; 1170 for (int i = ncomponents; --i >= 0; ) { 1171 if (component[i] == comp) { 1172 remove(i); 1173 } 1174 } 1175 } 1176 } 1177 } 1178 1179 1187 public void removeAll() { 1188 synchronized (getTreeLock()) { 1189 adjustListeningChildren(AWTEvent.HIERARCHY_EVENT_MASK, 1190 -listeningChildren); 1191 adjustListeningChildren(AWTEvent.HIERARCHY_BOUNDS_EVENT_MASK, 1192 -listeningBoundsChildren); 1193 adjustDescendants(-descendantsCount); 1194 1195 while (ncomponents > 0) { 1196 Component comp = component[--ncomponents]; 1197 component[ncomponents] = null; 1198 1199 if (peer != null) { 1200 comp.removeNotify(); 1201 } 1202 if (layoutMgr != null) { 1203 layoutMgr.removeLayoutComponent(comp); 1204 } 1205 comp.parent = null; 1206 if (containerListener != null || 1207 (eventMask & AWTEvent.CONTAINER_EVENT_MASK) != 0 || 1208 Toolkit.enabledOnToolkit(AWTEvent.CONTAINER_EVENT_MASK)) { 1209 ContainerEvent e = new ContainerEvent (this, 1210 ContainerEvent.COMPONENT_REMOVED, 1211 comp); 1212 dispatchEvent(e); 1213 } 1214 1215 comp.createHierarchyEvents(HierarchyEvent.HIERARCHY_CHANGED, 1216 comp, this, 1217 HierarchyEvent.PARENT_CHANGED, 1218 Toolkit.enabledOnToolkit(AWTEvent.HIERARCHY_EVENT_MASK)); 1219 } 1220 if (peer != null && layoutMgr == null && isVisible()) { 1221 updateCursorImmediately(); 1222 } 1223 if (valid) { 1224 invalidate(); 1225 } 1226 } 1227 } 1228 1229 int numListening(long mask) { 1231 int superListening = super.numListening(mask); 1232 1233 if (mask == AWTEvent.HIERARCHY_EVENT_MASK) { 1234 if (dbg.on) { 1235 int sum = 0; 1237 for (int i = 0; i < ncomponents; i++) { 1238 sum += component[i].numListening(mask); 1239 } 1240 dbg.assertion(listeningChildren == sum); 1241 } 1242 return listeningChildren + superListening; 1243 } else if (mask == AWTEvent.HIERARCHY_BOUNDS_EVENT_MASK) { 1244 if (dbg.on) { 1245 int sum = 0; 1247 for (int i = 0; i < ncomponents; i++) { 1248 sum += component[i].numListening(mask); 1249 } 1250 dbg.assertion(listeningBoundsChildren == sum); 1251 } 1252 return listeningBoundsChildren + superListening; 1253 } else { 1254 if (dbg.on) { 1255 dbg.assertion(false); 1256 } 1257 return superListening; 1258 } 1259 } 1260 1261 void adjustListeningChildren(long mask, int num) { 1263 if (dbg.on) { 1264 dbg.assertion(mask == AWTEvent.HIERARCHY_EVENT_MASK || 1265 mask == AWTEvent.HIERARCHY_BOUNDS_EVENT_MASK || 1266 mask == (AWTEvent.HIERARCHY_EVENT_MASK | 1267 AWTEvent.HIERARCHY_BOUNDS_EVENT_MASK)); 1268 } 1269 1270 if (num == 0) 1271 return; 1272 1273 if ((mask & AWTEvent.HIERARCHY_EVENT_MASK) != 0) { 1274 listeningChildren += num; 1275 } 1276 if ((mask & AWTEvent.HIERARCHY_BOUNDS_EVENT_MASK) != 0) { 1277 listeningBoundsChildren += num; 1278 } 1279 1280 adjustListeningChildrenOnParent(mask, num); 1281 } 1282 1283 void adjustDescendants(int num) { 1285 if (num == 0) 1286 return; 1287 1288 descendantsCount += num; 1289 adjustDecendantsOnParent(num); 1290 } 1291 1292 void adjustDecendantsOnParent(int num) { 1294 if (parent != null) { 1295 parent.adjustDescendants(num); 1296 } 1297 } 1298 1299 int countHierarchyMembers() { 1301 if (dbg.on) { 1302 int sum = 0; 1304 for (int i = 0; i < ncomponents; i++) { 1305 sum += component[i].countHierarchyMembers(); 1306 } 1307 dbg.assertion(descendantsCount == sum); 1308 } 1309 return descendantsCount + 1; 1310 } 1311 1312 private int getListenersCount(int id, boolean enabledOnToolkit) { 1313 assert Thread.holdsLock(getTreeLock()); 1314 if (enabledOnToolkit) { 1315 return descendantsCount; 1316 } 1317 switch (id) { 1318 case HierarchyEvent.HIERARCHY_CHANGED: 1319 return listeningChildren; 1320 case HierarchyEvent.ANCESTOR_MOVED: 1321 case HierarchyEvent.ANCESTOR_RESIZED: 1322 return listeningBoundsChildren; 1323 default: 1324 return 0; 1325 } 1326 } 1327 1328 final int createHierarchyEvents(int id, Component changed, 1329 Container changedParent, long changeFlags, boolean enabledOnToolkit) 1330 { 1331 assert Thread.holdsLock(getTreeLock()); 1332 int listeners = getListenersCount(id, enabledOnToolkit); 1333 1334 for (int count = listeners, i = 0; count > 0; i++) { 1335 count -= component[i].createHierarchyEvents(id, changed, 1336 changedParent, changeFlags, enabledOnToolkit); 1337 } 1338 return listeners + 1339 super.createHierarchyEvents(id, changed, changedParent, 1340 changeFlags, enabledOnToolkit); 1341 } 1342 1343 final void createChildHierarchyEvents(int id, long changeFlags, 1344 boolean enabledOnToolkit) 1345 { 1346 assert Thread.holdsLock(getTreeLock()); 1347 if (ncomponents == 0) { 1348 return; 1349 } 1350 int listeners = getListenersCount(id, enabledOnToolkit); 1351 1352 for (int count = listeners, i = 0; count > 0; i++) { 1353 count -= component[i].createHierarchyEvents(id, this, parent, 1354 changeFlags, enabledOnToolkit); 1355 } 1356 } 1357 1358 1363 public LayoutManager getLayout() { 1364 return layoutMgr; 1365 } 1366 1367 1373 public void setLayout(LayoutManager mgr) { 1374 layoutMgr = mgr; 1375 if (valid) { 1376 invalidate(); 1377 } 1378 } 1379 1380 1389 public void doLayout() { 1390 layout(); 1391 } 1392 1393 1397 @Deprecated 1398 public void layout() { 1399 LayoutManager layoutMgr = this.layoutMgr; 1400 if (layoutMgr != null) { 1401 layoutMgr.layoutContainer(this); 1402 } 1403 } 1404 1405 1413 public void invalidate() { 1414 LayoutManager layoutMgr = this.layoutMgr; 1415 if (layoutMgr instanceof LayoutManager2 ) { 1416 LayoutManager2 lm = (LayoutManager2 ) layoutMgr; 1417 lm.invalidateLayout(this); 1418 } 1419 super.invalidate(); 1420 } 1421 1422 1435 public void validate() { 1436 1437 if (!valid) { 1438 boolean updateCur = false; 1439 synchronized (getTreeLock()) { 1440 if (!valid && peer != null) { 1441 ContainerPeer p = null; 1442 if (peer instanceof ContainerPeer) { 1443 p = (ContainerPeer) peer; 1444 } 1445 if (p != null) { 1446 p.beginValidate(); 1447 } 1448 validateTree(); 1449 valid = true; 1450 if (p != null) { 1451 p.endValidate(); 1452 updateCur = isVisible(); 1453 } 1454 } 1455 } 1456 if (updateCur) { 1457 updateCursorImmediately(); 1458 } 1459 } 1460 } 1461 1462 1468 protected void validateTree() { 1469 if (!valid) { 1470 if (peer instanceof ContainerPeer) { 1471 ((ContainerPeer)peer).beginLayout(); 1472 } 1473 doLayout(); 1474 Component component[] = this.component; 1475 for (int i = 0 ; i < ncomponents ; ++i) { 1476 Component comp = component[i]; 1477 if ( (comp instanceof Container ) 1478 && !(comp instanceof Window ) 1479 && !comp.valid) { 1480 ((Container )comp).validateTree(); 1481 } else { 1482 comp.validate(); 1483 } 1484 } 1485 if (peer instanceof ContainerPeer) { 1486 ((ContainerPeer)peer).endLayout(); 1487 } 1488 } 1489 valid = true; 1490 } 1491 1492 1496 void invalidateTree() { 1497 synchronized (getTreeLock()) { 1498 for (int i = 0; i < ncomponents; ++i) { 1499 Component comp = component[i]; 1500 if (comp instanceof Container ) { 1501 ((Container )comp).invalidateTree(); 1502 } 1503 else { 1504 if (comp.valid) { 1505 comp.invalidate(); 1506 } 1507 } 1508 } 1509 if (valid) { 1510 invalidate(); 1511 } 1512 } 1513 } 1514 1515 1521 public void setFont(Font f) { 1522 boolean shouldinvalidate = false; 1523 1524 Font oldfont = getFont(); 1525 super.setFont(f); 1526 Font newfont = getFont(); 1527 if (newfont != oldfont && (oldfont == null || 1528 !oldfont.equals(newfont))) { 1529 invalidateTree(); 1530 } 1531 } 1532 1533 1542 public Dimension getPreferredSize() { 1543 return preferredSize(); 1544 } 1545 1546 1550 @Deprecated 1551 public Dimension preferredSize() { 1552 1555 Dimension dim = prefSize; 1556 if (dim == null || !(isPreferredSizeSet() || isValid())) { 1557 synchronized (getTreeLock()) { 1558 prefSize = (layoutMgr != null) ? 1559 layoutMgr.preferredLayoutSize(this) : 1560 super.preferredSize(); 1561 dim = prefSize; 1562 } 1563 } 1564 if (dim != null){ 1565 return new Dimension (dim); 1566 } 1567 else{ 1568 return dim; 1569 } 1570 } 1571 1572 1582 public Dimension getMinimumSize() { 1583 return minimumSize(); 1584 } 1585 1586 1590 @Deprecated 1591 public Dimension minimumSize() { 1592 1595 Dimension dim = minSize; 1596 if (dim == null || !(isMinimumSizeSet() || isValid())) { 1597 synchronized (getTreeLock()) { 1598 minSize = (layoutMgr != null) ? 1599 layoutMgr.minimumLayoutSize(this) : 1600 super.minimumSize(); 1601 dim = minSize; 1602 } 1603 } 1604 if (dim != null){ 1605 return new Dimension (dim); 1606 } 1607 else{ 1608 return dim; 1609 } 1610 } 1611 1612 1616 public Dimension getMaximumSize() { 1617 1620 Dimension dim = maxSize; 1621 if (dim == null || !(isMaximumSizeSet() || isValid())) { 1622 synchronized (getTreeLock()) { 1623 if (layoutMgr instanceof LayoutManager2 ) { 1624 LayoutManager2 lm = (LayoutManager2 ) layoutMgr; 1625 maxSize = lm.maximumLayoutSize(this); 1626 } else { 1627 maxSize = super.getMaximumSize(); 1628 } 1629 dim = maxSize; 1630 } 1631 } 1632 if (dim != null){ 1633 return new Dimension (dim); 1634 } 1635 else{ 1636 return dim; 1637 } 1638 } 1639 1640 1647 public float getAlignmentX() { 1648 float xAlign; 1649 if (layoutMgr instanceof LayoutManager2 ) { 1650 synchronized (getTreeLock()) { 1651 LayoutManager2 lm = (LayoutManager2 ) layoutMgr; 1652 xAlign = lm.getLayoutAlignmentX(this); 1653 } 1654 } else { 1655 xAlign = super.getAlignmentX(); 1656 } 1657 return xAlign; 1658 } 1659 1660 1667 public float getAlignmentY() { 1668 float yAlign; 1669 if (layoutMgr instanceof LayoutManager2 ) { 1670 synchronized (getTreeLock()) { 1671 LayoutManager2 lm = (LayoutManager2 ) layoutMgr; 1672 yAlign = lm.getLayoutAlignmentY(this); 1673 } 1674 } else { 1675 yAlign = super.getAlignmentY(); 1676 } 1677 return yAlign; 1678 } 1679 1680 1691 public void paint(Graphics g) { 1692 if (isShowing()) { 1693 if (printing) { 1694 synchronized (this) { 1695 if (printing) { 1696 if (printingThreads.contains(Thread.currentThread())) { 1697 return; 1698 } 1699 } 1700 } 1701 } 1702 1703 1707 1709 GraphicsCallback.PaintCallback.getInstance(). 1710 runComponents(component, g, GraphicsCallback.LIGHTWEIGHTS); 1711 } 1712 } 1713 1714 1725 public void update(Graphics g) { 1726 if (isShowing()) { 1727 if (! (peer instanceof LightweightPeer)) { 1728 g.clearRect(0, 0, width, height); 1729 } 1730 paint(g); 1731 } 1732 } 1733 1734 1745 public void print(Graphics g) { 1746 if (isShowing()) { 1747 Thread t = Thread.currentThread(); 1748 try { 1749 synchronized (this) { 1750 if (printingThreads == null) { 1751 printingThreads = new HashSet (); 1752 } 1753 printingThreads.add(t); 1754 printing = true; 1755 } 1756 super.print(g); } finally { 1758 synchronized (this) { 1759 printingThreads.remove(t); 1760 printing = !printingThreads.isEmpty(); 1761 } 1762 } 1763 1764 GraphicsCallback.PrintCallback.getInstance(). 1765 runComponents(component, g, GraphicsCallback.LIGHTWEIGHTS); 1766 } 1767 } 1768 1769 1775 public void paintComponents(Graphics g) { 1776 if (isShowing()) { 1777 GraphicsCallback.PaintAllCallback.getInstance(). 1778 runComponents(component, g, GraphicsCallback.TWO_PASSES); 1779 } 1780 } 1781 1782 1789 void lightweightPaint(Graphics g) { 1790 super.lightweightPaint(g); 1791 paintHeavyweightComponents(g); 1792 } 1793 1794 1797 void paintHeavyweightComponents(Graphics g) { 1798 if (isShowing()) { 1799 GraphicsCallback.PaintHeavyweightComponentsCallback.getInstance(). 1800 runComponents(component, g, GraphicsCallback.LIGHTWEIGHTS | 1801 GraphicsCallback.HEAVYWEIGHTS); 1802 } 1803 } 1804 1805 1811 public void printComponents(Graphics g) { 1812 if (isShowing()) { 1813 GraphicsCallback.PrintAllCallback.getInstance(). 1814 runComponents(component, g, GraphicsCallback.TWO_PASSES); 1815 } 1816 } 1817 1818 1825 void lightweightPrint(Graphics g) { 1826 super.lightweightPrint(g); 1827 printHeavyweightComponents(g); 1828 } 1829 1830 1833 void printHeavyweightComponents(Graphics g) { 1834 if (isShowing()) { 1835 GraphicsCallback.PrintHeavyweightComponentsCallback.getInstance(). 1836 runComponents(component, g, GraphicsCallback.LIGHTWEIGHTS | 1837 GraphicsCallback.HEAVYWEIGHTS); 1838 } 1839 } 1840 1841 1851 public synchronized void addContainerListener(ContainerListener l) { 1852 if (l == null) { 1853 return; 1854 } 1855 containerListener = AWTEventMulticaster.add(containerListener, l); 1856 newEventsOnly = true; 1857 } 1858 1859 1869 public synchronized void removeContainerListener(ContainerListener l) { 1870 if (l == null) { 1871 return; 1872 } 1873 containerListener = AWTEventMulticaster.remove(containerListener, l); 1874 } 1875 1876 1888 public synchronized ContainerListener [] getContainerListeners() { 1889 return (ContainerListener []) (getListeners(ContainerListener .class)); 1890 } 1891 1892 1925 public <T extends EventListener > T[] getListeners(Class <T> listenerType) { 1926 EventListener l = null; 1927 if (listenerType == ContainerListener .class) { 1928 l = containerListener; 1929 } else { 1930 return super.getListeners(listenerType); 1931 } 1932 return AWTEventMulticaster.getListeners(l, listenerType); 1933 } 1934 1935 boolean eventEnabled(AWTEvent e) { 1937 int id = e.getID(); 1938 1939 if (id == ContainerEvent.COMPONENT_ADDED || 1940 id == ContainerEvent.COMPONENT_REMOVED) { 1941 if ((eventMask & AWTEvent.CONTAINER_EVENT_MASK) != 0 || 1942 containerListener != null) { 1943 return true; 1944 } 1945 return false; 1946 } 1947 return super.eventEnabled(e); 1948 } 1949 1950 1961 protected void processEvent(AWTEvent e) { 1962 if (e instanceof ContainerEvent ) { 1963 processContainerEvent((ContainerEvent )e); 1964 return; 1965 } 1966 super.processEvent(e); 1967 } 1968 1969 1987 protected void processContainerEvent(ContainerEvent e) { 1988 ContainerListener listener = containerListener; 1989 if (listener != null) { 1990 switch(e.getID()) { 1991 case ContainerEvent.COMPONENT_ADDED: 1992 listener.componentAdded(e); 1993 break; 1994 case ContainerEvent.COMPONENT_REMOVED: 1995 listener.componentRemoved(e); 1996 break; 1997 } 1998 } 1999 } 2000 2001 2009 void dispatchEventImpl(AWTEvent e) { 2010 if ((dispatcher != null) && dispatcher.dispatchEvent(e)) { 2011 e.consume(); 2018 if (peer != null) { 2019 peer.handleEvent(e); 2020 } 2021 return; 2022 } 2023 2024 super.dispatchEventImpl(e); 2025 2026 synchronized (getTreeLock()) { 2027 switch (e.getID()) { 2028 case ComponentEvent.COMPONENT_RESIZED: 2029 createChildHierarchyEvents(HierarchyEvent.ANCESTOR_RESIZED, 0, 2030 Toolkit.enabledOnToolkit(AWTEvent.HIERARCHY_BOUNDS_EVENT_MASK)); 2031 break; 2032 case ComponentEvent.COMPONENT_MOVED: 2033 createChildHierarchyEvents(HierarchyEvent.ANCESTOR_MOVED, 0, 2034 Toolkit.enabledOnToolkit(AWTEvent.HIERARCHY_BOUNDS_EVENT_MASK)); 2035 break; 2036 default: 2037 break; 2038 } 2039 } 2040 } 2041 2042 2047 void dispatchEventToSelf(AWTEvent e) { 2048 super.dispatchEventImpl(e); 2049 } 2050 2051 2055 Component getMouseEventTarget(int x, int y, boolean includeSelf) { 2056 return getMouseEventTarget(x, y, includeSelf, 2057 MouseEventTargetFilter.FILTER, 2058 !SEARCH_HEAVYWEIGHTS); 2059 } 2060 2061 2064 Component getDropTargetEventTarget(int x, int y, boolean includeSelf) { 2065 return getMouseEventTarget(x, y, includeSelf, 2066 DropTargetEventTargetFilter.FILTER, 2067 SEARCH_HEAVYWEIGHTS); 2068 } 2069 2070 2082 private Component getMouseEventTarget(int x, int y, boolean includeSelf, 2083 EventTargetFilter filter, 2084 boolean searchHeavyweights) { 2085 Component comp = null; 2086 if (searchHeavyweights) { 2087 comp = getMouseEventTargetImpl(x, y, includeSelf, filter, 2088 SEARCH_HEAVYWEIGHTS, 2089 searchHeavyweights); 2090 } 2091 2092 if (comp == null || comp == this) { 2093 comp = getMouseEventTargetImpl(x, y, includeSelf, filter, 2094 !SEARCH_HEAVYWEIGHTS, 2095 searchHeavyweights); 2096 } 2097 2098 return comp; 2099 } 2100 2101 2123 private Component getMouseEventTargetImpl(int x, int y, boolean includeSelf, 2124 EventTargetFilter filter, 2125 boolean searchHeavyweightChildren, 2126 boolean searchHeavyweightDescendants) { 2127 int ncomponents = this.ncomponents; 2128 Component component[] = this.component; 2129 2130 for (int i = 0 ; i < ncomponents ; i++) { 2131 Component comp = component[i]; 2132 if (comp != null && comp.visible && 2133 ((!searchHeavyweightChildren && 2134 comp.peer instanceof LightweightPeer) || 2135 (searchHeavyweightChildren && 2136 !(comp.peer instanceof LightweightPeer))) && 2137 comp.contains(x - comp.x, y - comp.y)) { 2138 2139 if (comp instanceof Container ) { 2142 Container child = (Container ) comp; 2143 Component deeper = child.getMouseEventTarget(x - child.x, 2144 y - child.y, 2145 includeSelf, 2146 filter, 2147 searchHeavyweightDescendants); 2148 if (deeper != null) { 2149 return deeper; 2150 } 2151 } else { 2152 if (filter.accept(comp)) { 2153 return comp; 2156 } 2157 } 2158 } 2159 } 2160 2161 boolean isPeerOK; 2162 boolean isMouseOverMe; 2163 2164 isPeerOK = (peer instanceof LightweightPeer) || includeSelf; 2165 isMouseOverMe = contains(x,y); 2166 2167 if (isMouseOverMe && isPeerOK && filter.accept(this)) { 2170 return this; 2171 } 2172 return null; 2174 } 2175 2176 static interface EventTargetFilter { 2177 boolean accept(final Component comp); 2178 } 2179 2180 static class MouseEventTargetFilter implements EventTargetFilter { 2181 static final EventTargetFilter FILTER = new MouseEventTargetFilter(); 2182 2183 private MouseEventTargetFilter() {} 2184 2185 public boolean accept(final Component comp) { 2186 return (comp.eventMask & AWTEvent.MOUSE_MOTION_EVENT_MASK) != 0 2187 || (comp.eventMask & AWTEvent.MOUSE_EVENT_MASK) != 0 2188 || (comp.eventMask & AWTEvent.MOUSE_WHEEL_EVENT_MASK) != 0 2189 || comp.mouseListener != null 2190 || comp.mouseMotionListener != null 2191 || comp.mouseWheelListener != null; 2192 } 2193 } 2194 2195 static class DropTargetEventTargetFilter implements EventTargetFilter { 2196 static final EventTargetFilter FILTER = new DropTargetEventTargetFilter(); 2197 2198 private DropTargetEventTargetFilter() {} 2199 2200 public boolean accept(final Component comp) { 2201 DropTarget dt = comp.getDropTarget(); 2202 return dt != null && dt.isActive(); 2203 } 2204 } 2205 2206 2213 void proxyEnableEvents(long events) { 2214 if (peer instanceof LightweightPeer) { 2215 if (parent != null) { 2218 parent.proxyEnableEvents(events); 2219 } 2220 } else { 2221 if (dispatcher != null) { 2227 dispatcher.enableEvents(events); 2228 } 2229 } 2230 } 2231 2232 2236 @Deprecated 2237 public void deliverEvent(Event e) { 2238 Component comp = getComponentAt(e.x, e.y); 2239 if ((comp != null) && (comp != this)) { 2240 e.translate(-comp.x, -comp.y); 2241 comp.deliverEvent(e); 2242 } else { 2243 postEvent(e); 2244 } 2245 } 2246 2247 2265 public Component getComponentAt(int x, int y) { 2266 return locate(x, y); 2267 } 2268 2269 2273 @Deprecated 2274 public Component locate(int x, int y) { 2275 if (!contains(x, y)) { 2276 return null; 2277 } 2278 synchronized (getTreeLock()) { 2279 for (int i = 0 ; i < ncomponents ; i++) { 2281 Component comp = component[i]; 2282 if (comp != null && 2283 !(comp.peer instanceof LightweightPeer)) { 2284 if (comp.contains(x - comp.x, y - comp.y)) { 2285 return comp; 2286 } 2287 } 2288 } 2289 for (int i = 0 ; i < ncomponents ; i++) { 2290 Component comp = component[i]; 2291 if (comp != null && 2292 comp.peer instanceof LightweightPeer) { 2293 if (comp.contains(x - comp.x, y - comp.y)) { 2294 return comp; 2295 } 2296 } 2297 } 2298 } 2299 return this; 2300 } 2301 2302 2311 public Component getComponentAt(Point p) { 2312 return getComponentAt(p.x, p.y); 2313 } 2314 2315 2334 public Point getMousePosition(boolean allowChildren) throws HeadlessException { 2335 if (GraphicsEnvironment.isHeadless()) { 2336 throw new HeadlessException (); 2337 } 2338 PointerInfo pi = (PointerInfo )java.security.AccessController.doPrivileged( 2339 new java.security.PrivilegedAction () { 2340 public Object run() { 2341 return MouseInfo.getPointerInfo(); 2342 } 2343 } 2344 ); 2345 synchronized (getTreeLock()) { 2346 Component inTheSameWindow = findUnderMouseInWindow(pi); 2347 if (isSameOrAncestorOf(inTheSameWindow, allowChildren)) { 2348 return pointRelativeToComponent(pi.getLocation()); 2349 } 2350 return null; 2351 } 2352 } 2353 2354 boolean isSameOrAncestorOf(Component comp, boolean allowChildren) { 2355 return this == comp || (allowChildren && isParentOf(comp)); 2356 } 2357 2358 2381 public Component findComponentAt(int x, int y) { 2382 synchronized (getTreeLock()) { 2383 return findComponentAt(x, y, true); 2384 } 2385 } 2386 2387 2397 final Component findComponentAt(int x, int y, boolean ignoreEnabled) 2398 { 2399 if (isRecursivelyVisible()){ 2400 return findComponentAtImpl(x, y, ignoreEnabled); 2401 } 2402 return null; 2403 } 2404 2405 final Component findComponentAtImpl(int x, int y, boolean ignoreEnabled){ 2406 if (!(contains(x, y) && visible && (ignoreEnabled || enabled))) { 2407 return null; 2408 } 2409 int ncomponents = this.ncomponents; 2410 Component component[] = this.component; 2411 2412 for (int i = 0 ; i < ncomponents ; i++) { 2414 Component comp = component[i]; 2415 if (comp != null && 2416 !(comp.peer instanceof LightweightPeer)) { 2417 if (comp instanceof Container ) { 2418 comp = ((Container )comp).findComponentAtImpl(x - comp.x, 2419 y - comp.y, 2420 ignoreEnabled); 2421 } else { 2422 comp = comp.locate(x - comp.x, y - comp.y); 2423 } 2424 if (comp != null && comp.visible && 2425 (ignoreEnabled || comp.enabled)) 2426 { 2427 return comp; 2428 } 2429 } 2430 } 2431 for (int i = 0 ; i < ncomponents ; i++) { 2432 Component comp = component[i]; 2433 if (comp != null && 2434 comp.peer instanceof LightweightPeer) { 2435 if (comp instanceof Container ) { 2436 comp = ((Container )comp).findComponentAtImpl(x - comp.x, 2437 y - comp.y, 2438 ignoreEnabled); 2439 } else { 2440 comp = comp.locate(x - comp.x, y - comp.y); 2441 } 2442 if (comp != null && comp.visible && 2443 (ignoreEnabled || comp.enabled)) 2444 { 2445 return comp; 2446 } 2447 } 2448 } 2449 return this; 2450 } 2451 2452 2474 public Component findComponentAt(Point p) { 2475 return findComponentAt(p.x, p.y); 2476 } 2477 2478 2487 public void addNotify() { 2488 synchronized (getTreeLock()) { 2489 super.addNotify(); 2494 if (! (peer instanceof LightweightPeer)) { 2495 dispatcher = new LightweightDispatcher(this); 2496 } 2497 int ncomponents = this.ncomponents; 2498 Component component[] = this.component; 2499 for (int i = 0 ; i < ncomponents ; i++) { 2500 component[i].addNotify(); 2501 } 2502 ContainerPeer cpeer = (ContainerPeer)peer; 2504 if (cpeer.isRestackSupported()) { 2505 cpeer.restack(); 2506 } 2507 2508 2509 } 2510 } 2511 2512 2521 public void removeNotify() { 2522 synchronized (getTreeLock()) { 2523 int ncomponents = this.ncomponents; 2524 Component component[] = this.component; 2525 for (int i = ncomponents-1 ; i >= 0 ; i--) { 2526 if( component[i] != null ) 2527 component[i].removeNotify(); 2528 } 2529 if ( dispatcher != null ) { 2530 dispatcher.dispose(); 2531 dispatcher = null; 2532 } 2533 super.removeNotify(); 2534 } 2535 } 2536 2537 2545 public boolean isAncestorOf(Component c) { 2546 Container p; 2547 if (c == null || ((p = c.getParent()) == null)) { 2548 return false; 2549 } 2550 while (p != null) { 2551 if (p == this) { 2552 return true; 2553 } 2554 p = p.getParent(); 2555 } 2556 return false; 2557 } 2558 2559 2571 2572 transient Component modalComp; 2573 transient AppContext modalAppContext; 2574 2575 private void startLWModal() { 2576 modalAppContext = AppContext.getAppContext(); 2580 2581 long time = Toolkit.getEventQueue().getMostRecentEventTime(); 2584 Component predictedFocusOwner = (this instanceof javax.swing.JInternalFrame ) ? ((javax.swing.JInternalFrame )(this)).getMostRecentFocusOwner() : null; 2585 if (predictedFocusOwner != null) { 2586 KeyboardFocusManager.getCurrentKeyboardFocusManager(). 2587 enqueueKeyEvents(time, predictedFocusOwner); 2588 } 2589 final Container nativeContainer; 2593 synchronized (getTreeLock()) { 2594 nativeContainer = getHeavyweightContainer(); 2595 if (nativeContainer.modalComp != null) { 2596 this.modalComp = nativeContainer.modalComp; 2597 nativeContainer.modalComp = this; 2598 return; 2599 } 2600 else { 2601 nativeContainer.modalComp = this; 2602 } 2603 } 2604 2605 Runnable pumpEventsForHierarchy = new Runnable () { 2606 public void run() { 2607 EventDispatchThread dispatchThread = 2608 (EventDispatchThread )Thread.currentThread(); 2609 dispatchThread.pumpEventsForHierarchy( 2610 new Conditional () { 2611 public boolean evaluate() { 2612 return ((windowClosingException == null) && (nativeContainer.modalComp != null)) ; 2613 } 2614 }, Container.this); 2615 } 2616 }; 2617 2618 if (EventQueue.isDispatchThread()) { 2619 SequencedEvent currentSequencedEvent = 2620 KeyboardFocusManager.getCurrentKeyboardFocusManager(). 2621 getCurrentSequencedEvent(); 2622 if (currentSequencedEvent != null) { 2623 currentSequencedEvent.dispose(); 2624 } 2625 2626 pumpEventsForHierarchy.run(); 2627 } else { 2628 synchronized (getTreeLock()) { 2629 Toolkit.getEventQueue(). 2630 postEvent(new PeerEvent(this, 2631 pumpEventsForHierarchy, 2632 PeerEvent.PRIORITY_EVENT)); 2633 while (windowClosingException == null) { 2634 try { 2635 getTreeLock().wait(); 2636 } catch (InterruptedException e) { 2637 break; 2638 } 2639 } 2640 } 2641 } 2642 if (windowClosingException != null) { 2643 windowClosingException.fillInStackTrace(); 2644 throw windowClosingException; 2645 } 2646 if (predictedFocusOwner != null) { 2647 KeyboardFocusManager.getCurrentKeyboardFocusManager(). 2648 dequeueKeyEvents(time, predictedFocusOwner); 2649 } 2650 } 2651 2652 private void stopLWModal() { 2653 synchronized (getTreeLock()) { 2654 if (modalAppContext != null) { 2655 Container nativeContainer = getHeavyweightContainer(); 2656 if(nativeContainer != null) { 2657 if (this.modalComp != null) { 2658 nativeContainer.modalComp = this.modalComp; 2659 this.modalComp = null; 2660 return; 2661 } 2662 else { 2663 nativeContainer.modalComp = null; 2664 } 2665 } 2666 SunToolkit.postEvent(modalAppContext, 2669 new PeerEvent(this, 2670 new WakingRunnable(), 2671 PeerEvent.PRIORITY_EVENT)); 2672 } 2673 EventQueue.invokeLater(new WakingRunnable()); 2674 getTreeLock().notifyAll(); 2675 } 2676 } 2677 2678 final static class WakingRunnable implements Runnable { 2679 public void run() { 2680 } 2681 } 2682 2683 2684 2685 2694 protected String paramString() { 2695 String str = super.paramString(); 2696 LayoutManager layoutMgr = this.layoutMgr; 2697 if (layoutMgr != null) { 2698 str += ",layout=" + layoutMgr.getClass().getName(); 2699 } 2700 return str; 2701 } 2702 2703 2717 public void list(PrintStream out, int indent) { 2718 super.list(out, indent); 2719 int ncomponents = this.ncomponents; 2720 Component component[] = this.component; 2721 for (int i = 0 ; i < ncomponents ; i++) { 2722 Component comp = component[i]; 2723 if (comp != null) { 2724 comp.list(out, indent+1); 2725 } 2726 } 2727 } 2728 2729 2743 public void list(PrintWriter out, int indent) { 2744 super.list(out, indent); 2745 int ncomponents = this.ncomponents; 2746 Component component[] = this.component; 2747 for (int i = 0 ; i < ncomponents ; i++) { 2748 Component comp = component[i]; 2749 if (comp != null) { 2750 comp.list(out, indent+1); 2751 } 2752 } 2753 } 2754 2755 2833 public void setFocusTraversalKeys(int id, 2834 Set <? extends AWTKeyStroke > keystrokes) 2835 { 2836 if (id < 0 || id >= KeyboardFocusManager.TRAVERSAL_KEY_LENGTH) { 2837 throw new IllegalArgumentException ("invalid focus traversal key identifier"); 2838 } 2839 2840 setFocusTraversalKeys_NoIDCheck(id, keystrokes); 2843 } 2844 2845 2874 public Set <AWTKeyStroke > getFocusTraversalKeys(int id) { 2875 if (id < 0 || id >= KeyboardFocusManager.TRAVERSAL_KEY_LENGTH) { 2876 throw new IllegalArgumentException ("invalid focus traversal key identifier"); 2877 } 2878 2879 return getFocusTraversalKeys_NoIDCheck(id); 2882 } 2883 2884 2904 public boolean areFocusTraversalKeysSet(int id) { 2905 if (id < 0 || id >= KeyboardFocusManager.TRAVERSAL_KEY_LENGTH) { 2906 throw new IllegalArgumentException ("invalid focus traversal key identifier"); 2907 } 2908 2909 return (focusTraversalKeys != null && focusTraversalKeys[id] != null); 2910 } 2911 2912 2928 public boolean isFocusCycleRoot(Container container) { 2929 if (isFocusCycleRoot() && container == this) { 2930 return true; 2931 } else { 2932 return super.isFocusCycleRoot(container); 2933 } 2934 } 2935 2936 private Container findTraversalRoot() { 2937 2944 Container currentFocusCycleRoot = KeyboardFocusManager. 2945 getCurrentKeyboardFocusManager().getCurrentFocusCycleRoot(); 2946 Container root; 2947 2948 if (currentFocusCycleRoot == this) { 2949 root = this; 2950 } else { 2951 root = getFocusCycleRootAncestor(); 2952 if (root == null) { 2953 root = this; 2954 } 2955 } 2956 2957 if (root != currentFocusCycleRoot) { 2958 KeyboardFocusManager.getCurrentKeyboardFocusManager(). 2959 setGlobalCurrentFocusCycleRoot(root); 2960 } 2961 return root; 2962 } 2963 2964 final boolean containsFocus() { 2965 synchronized (getTreeLock()) { 2966 Component comp = KeyboardFocusManager. 2967 getCurrentKeyboardFocusManager().getFocusOwner(); 2968 while (comp != null && !(comp instanceof Window ) && comp != this) 2969 { 2970 comp = (Component ) comp.getParent(); 2971 } 2972 return (comp == this); 2973 } 2974 } 2975 2976 2982 boolean isParentOf(Component comp) { 2983 synchronized(getTreeLock()) { 2984 while (comp != null && comp != this && !(comp instanceof Window )) { 2985 comp = comp.getParent(); 2986 } 2987 return (comp == this); 2988 } 2989 } 2990 2991 void clearMostRecentFocusOwnerOnHide() { 2992 Component comp = null; 2993 Container window = this; 2994 2995 synchronized (getTreeLock()) { 2996 while (window != null && !(window instanceof Window )) { 2997 window = window.getParent(); 2998 } 2999 if (window != null) { 3000 comp = KeyboardFocusManager. 3001 getMostRecentFocusOwner((Window )window); 3002 while ((comp != null) && (comp != this) && !(comp instanceof Window )) { 3003 comp = comp.getParent(); 3004 } 3005 } 3006 } 3007 3008 if (comp == this) { 3009 KeyboardFocusManager.setMostRecentFocusOwner((Window )window, null); 3010 } 3011 3012 if (window != null) { 3013 Window myWindow = (Window )window; 3014 synchronized(getTreeLock()) { 3015 synchronized(KeyboardFocusManager .class) { 3017 Component storedComp = myWindow.getTemporaryLostComponent(); 3018 if (isParentOf(storedComp) || storedComp == this) { 3019 myWindow.setTemporaryLostComponent(null); 3020 } 3021 } 3022 } 3023 } 3024 } 3025 3026 void clearCurrentFocusCycleRootOnHide() { 3027 KeyboardFocusManager kfm = 3028 KeyboardFocusManager.getCurrentKeyboardFocusManager(); 3029 Container cont = kfm.getCurrentFocusCycleRoot(); 3030 3031 synchronized (getTreeLock()) { 3032 while (this != cont && !(cont instanceof Window ) && (cont != null)) { 3033 cont = cont.getParent(); 3034 } 3035 } 3036 3037 if (cont == this) { 3038 kfm.setGlobalCurrentFocusCycleRoot(null); 3039 } 3040 } 3041 3042 boolean nextFocusHelper() { 3043 if (isFocusCycleRoot()) { 3044 Container root = findTraversalRoot(); 3045 Component comp = this; 3046 Container anc; 3047 while (root != null && 3048 (anc = root.getFocusCycleRootAncestor()) != null && 3049 !(root.isShowing() && 3050 root.isFocusable() && 3051 root.isEnabled())) 3052 { 3053 comp = root; 3054 root = anc; 3055 } 3056 if (root != null) { 3057 FocusTraversalPolicy policy = root.getFocusTraversalPolicy(); 3058 Component toFocus = policy.getComponentAfter(root, comp); 3059 if (toFocus == null) { 3060 toFocus = policy.getDefaultComponent(root); 3061 } 3062 if (toFocus != null) { 3063 return toFocus.requestFocus(false); 3064 } 3065 } 3066 return false; 3067 } else { 3068 return super.nextFocusHelper(); 3070 } 3071 } 3072 3073 public void transferFocusBackward() { 3074 if (isFocusCycleRoot()) { 3075 Container root = findTraversalRoot(); 3076 Component comp = this; 3077 while (root != null && 3078 !(root.isShowing() && 3079 root.isFocusable() && 3080 root.isEnabled())) 3081 { 3082 comp = root; 3083 root = comp.getFocusCycleRootAncestor(); 3084 } 3085 if (root != null) { 3086 FocusTraversalPolicy policy = root.getFocusTraversalPolicy(); 3087 Component toFocus = policy.getComponentBefore(root, comp); 3088 if (toFocus == null) { 3089 toFocus = policy.getDefaultComponent(root); 3090 } 3091 if (toFocus != null) { 3092 toFocus.requestFocus(); 3093 } 3094 } 3095 } else { 3096 super.transferFocusBackward(); 3098 } 3099 } 3100 3101 3122 public void setFocusTraversalPolicy(FocusTraversalPolicy policy) { 3123 FocusTraversalPolicy oldPolicy; 3124 synchronized (this) { 3125 oldPolicy = this.focusTraversalPolicy; 3126 this.focusTraversalPolicy = policy; 3127 } 3128 firePropertyChange("focusTraversalPolicy", oldPolicy, policy); 3129 } 3130 3131 3145 public FocusTraversalPolicy getFocusTraversalPolicy() { 3146 if (!isFocusTraversalPolicyProvider() && !isFocusCycleRoot()) { 3147 return null; 3148 } 3149 3150 FocusTraversalPolicy policy = this.focusTraversalPolicy; 3151 if (policy != null) { 3152 return policy; 3153 } 3154 3155 Container rootAncestor = getFocusCycleRootAncestor(); 3156 if (rootAncestor != null) { 3157 return rootAncestor.getFocusTraversalPolicy(); 3158 } else { 3159 return KeyboardFocusManager.getCurrentKeyboardFocusManager(). 3160 getDefaultFocusTraversalPolicy(); 3161 } 3162 } 3163 3164 3173 public boolean isFocusTraversalPolicySet() { 3174 return (focusTraversalPolicy != null); 3175 } 3176 3177 3202 public void setFocusCycleRoot(boolean focusCycleRoot) { 3203 boolean oldFocusCycleRoot; 3204 synchronized (this) { 3205 oldFocusCycleRoot = this.focusCycleRoot; 3206 this.focusCycleRoot = focusCycleRoot; 3207 } 3208 firePropertyChange("focusCycleRoot", oldFocusCycleRoot, 3209 focusCycleRoot); 3210 } 3211 3212 3229 public boolean isFocusCycleRoot() { 3230 return focusCycleRoot; 3231 } 3232 3233 3247 public final void setFocusTraversalPolicyProvider(boolean provider) { 3248 boolean oldProvider; 3249 synchronized(this) { 3250 oldProvider = focusTraversalPolicyProvider; 3251 focusTraversalPolicyProvider = provider; 3252 } 3253 firePropertyChange("focusTraversalPolicyProvider", oldProvider, provider); 3254 } 3255 3256 3274 public final boolean isFocusTraversalPolicyProvider() { 3275 return focusTraversalPolicyProvider; 3276 } 3277 3278 3290 public void transferFocusDownCycle() { 3291 if (isFocusCycleRoot()) { 3292 KeyboardFocusManager.getCurrentKeyboardFocusManager(). 3293 setGlobalCurrentFocusCycleRoot(this); 3294 Component toFocus = getFocusTraversalPolicy(). 3295 getDefaultComponent(this); 3296 if (toFocus != null) { 3297 toFocus.requestFocus(); 3298 } 3299 } 3300 } 3301 3302 void preProcessKeyEvent(KeyEvent e) { 3303 Container parent = this.parent; 3304 if (parent != null) { 3305 parent.preProcessKeyEvent(e); 3306 } 3307 } 3308 3309 void postProcessKeyEvent(KeyEvent e) { 3310 Container parent = this.parent; 3311 if (parent != null) { 3312 parent.postProcessKeyEvent(e); 3313 } 3314 } 3315 3316 boolean postsOldMouseEvents() { 3317 return true; 3318 } 3319 3320 3331 public void applyComponentOrientation(ComponentOrientation o) { 3332 super.applyComponentOrientation(o); 3333 3334 for (int i = 0 ; i < ncomponents ; ++i) { 3335 component[i].applyComponentOrientation(o); 3336 } 3337 } 3338 3339 3372 public void addPropertyChangeListener(PropertyChangeListener listener) { 3373 super.addPropertyChangeListener(listener); 3374 } 3375 3376 3412 public void addPropertyChangeListener(String propertyName, 3413 PropertyChangeListener listener) { 3414 super.addPropertyChangeListener(propertyName, listener); 3415 } 3416 3417 3420 3423 private int containerSerializedDataVersion = 1; 3424 3425 3451 private void writeObject(ObjectOutputStream s) throws IOException { 3452 ObjectOutputStream.PutField f = s.putFields(); 3453 f.put("ncomponents", ncomponents); 3454 f.put("component", component); 3455 f.put("layoutMgr", layoutMgr); 3456 f.put("dispatcher", dispatcher); 3457 f.put("maxSize", maxSize); 3458 f.put("focusCycleRoot", focusCycleRoot); 3459 f.put("containerSerializedDataVersion", containerSerializedDataVersion); 3460 f.put("focusTraversalPolicyProvider", focusTraversalPolicyProvider); 3461 s.writeFields(); 3462 3463 AWTEventMulticaster.save(s, containerListenerK, containerListener); 3464 s.writeObject(null); 3465 3466 if (focusTraversalPolicy instanceof java.io.Serializable ) { 3467 s.writeObject(focusTraversalPolicy); 3468 } else { 3469 s.writeObject(null); 3470 } 3471 } 3472 3473 3489 private void readObject(ObjectInputStream s) 3490 throws ClassNotFoundException , IOException 3491 { 3492 ObjectInputStream.GetField f = s.readFields(); 3493 ncomponents = f.get("ncomponents", 0); 3494 component = (Component [])f.get("component", new Component [0]); 3495 layoutMgr = (LayoutManager )f.get("layoutMgr", null); 3496 dispatcher = (LightweightDispatcher)f.get("dispatcher", null); 3497 if (maxSize == null) { 3499 maxSize = (Dimension )f.get("maxSize", null); 3500 } 3501 focusCycleRoot = f.get("focusCycleRoot", false); 3502 containerSerializedDataVersion = f.get("containerSerializedDataVersion", 1); 3503 focusTraversalPolicyProvider = f.get("focusTraversalPolicyProvider", false); 3504 3505 Component component[] = this.component; 3506 for(int i = 0; i < ncomponents; i++) { 3507 component[i].parent = this; 3508 adjustListeningChildren(AWTEvent.HIERARCHY_EVENT_MASK, 3509 component[i].numListening(AWTEvent.HIERARCHY_EVENT_MASK)); 3510 adjustListeningChildren(AWTEvent.HIERARCHY_BOUNDS_EVENT_MASK, 3511 component[i].numListening( 3512 AWTEvent.HIERARCHY_BOUNDS_EVENT_MASK)); 3513 adjustDescendants(component[i].countHierarchyMembers()); 3514 } 3515 3516 Object keyOrNull; 3517 while(null != (keyOrNull = s.readObject())) { 3518 String key = ((String )keyOrNull).intern(); 3519 3520 if (containerListenerK == key) { 3521 addContainerListener((ContainerListener )(s.readObject())); 3522 } else { 3523 s.readObject(); 3525 } 3526 } 3527 3528 try { 3529 Object policy = s.readObject(); 3530 if (policy instanceof FocusTraversalPolicy ) { 3531 focusTraversalPolicy = (FocusTraversalPolicy )policy; 3532 } 3533 } catch (java.io.OptionalDataException e) { 3534 3540 if (!e.eof) { 3541 throw e; 3542 } 3543 } 3544 } 3545 3546 3549 3550 3560 protected class AccessibleAWTContainer extends AccessibleAWTComponent { 3561 3562 3565 private static final long serialVersionUID = 5081320404842566097L; 3566 3567 3574 public int getAccessibleChildrenCount() { 3575 return Container.this.getAccessibleChildrenCount(); 3576 } 3577 3578 3584 public Accessible getAccessibleChild(int i) { 3585 return Container.this.getAccessibleChild(i); 3586 } 3587 3588 3598 public Accessible getAccessibleAt(Point p) { 3599 return Container.this.getAccessibleAt(p); 3600 } 3601 3602 protected ContainerListener accessibleContainerHandler = null; 3603 3604 3608 protected class AccessibleContainerHandler 3609 implements ContainerListener { 3610 public void componentAdded(ContainerEvent e) { 3611 Component c = e.getChild(); 3612 if (c != null && c instanceof Accessible) { 3613 AccessibleAWTContainer.this.firePropertyChange( 3614 AccessibleContext.ACCESSIBLE_CHILD_PROPERTY, 3615 null, ((Accessible) c).getAccessibleContext()); 3616 } 3617 } 3618 public void componentRemoved(ContainerEvent e) { 3619 Component c = e.getChild(); 3620 if (c != null && c instanceof Accessible) { 3621 AccessibleAWTContainer.this.firePropertyChange( 3622 AccessibleContext.ACCESSIBLE_CHILD_PROPERTY, 3623 ((Accessible) c).getAccessibleContext(), null); 3624 } 3625 } 3626 } 3627 3628 3633 public void addPropertyChangeListener(PropertyChangeListener listener) { 3634 if (accessibleContainerHandler == null) { 3635 accessibleContainerHandler = new AccessibleContainerHandler(); 3636 Container.this.addContainerListener(accessibleContainerHandler); 3637 } 3638 super.addPropertyChangeListener(listener); 3639 } 3640 3641 } 3643 3654 Accessible getAccessibleAt(Point p) { 3655 synchronized (getTreeLock()) { 3656 if (this instanceof Accessible) { 3657 Accessible a = (Accessible)this; 3658 AccessibleContext ac = a.getAccessibleContext(); 3659 if (ac != null) { 3660 AccessibleComponent acmp; 3661 Point location; 3662 int nchildren = ac.getAccessibleChildrenCount(); 3663 for (int i=0; i < nchildren; i++) { 3664 a = ac.getAccessibleChild(i); 3665 if ((a != null)) { 3666 ac = a.getAccessibleContext(); 3667 if (ac != null) { 3668 acmp = ac.getAccessibleComponent(); 3669 if ((acmp != null) && (acmp.isShowing())) { 3670 location = acmp.getLocation(); 3671 Point np = new Point (p.x-location.x, 3672 p.y-location.y); 3673 if (acmp.contains(np)){ 3674 return a; 3675 } 3676 } 3677 } 3678 } 3679 } 3680 } 3681 return (Accessible)this; 3682 } else { 3683 Component ret = this; 3684 if (!this.contains(p.x,p.y)) { 3685 ret = null; 3686 } else { 3687 int ncomponents = this.getComponentCount(); 3688 for (int i=0; i < ncomponents; i++) { 3689 Component comp = this.getComponent(i); 3690 if ((comp != null) && comp.isShowing()) { 3691 Point location = comp.getLocation(); 3692 if (comp.contains(p.x-location.x,p.y-location.y)) { 3693 ret = comp; 3694 } 3695 } 3696 } 3697 } 3698 if (ret instanceof Accessible) { 3699 return (Accessible) ret; 3700 } 3701 } 3702 return null; 3703 } 3704 } 3705 3706 3713 int getAccessibleChildrenCount() { 3714 synchronized (getTreeLock()) { 3715 int count = 0; 3716 Component [] children = this.getComponents(); 3717 for (int i = 0; i < children.length; i++) { 3718 if (children[i] instanceof Accessible) { 3719 count++; 3720 } 3721 } 3722 return count; 3723 } 3724 } 3725 3726 3732 Accessible getAccessibleChild(int i) { 3733 synchronized (getTreeLock()) { 3734 Component [] children = this.getComponents(); 3735 int count = 0; 3736 for (int j = 0; j < children.length; j++) { 3737 if (children[j] instanceof Accessible) { 3738 if (count == i) { 3739 return (Accessible) children[j]; 3740 } else { 3741 count++; 3742 } 3743 } 3744 } 3745 return null; 3746 } 3747 } 3748 3749} 3750 3751 3752 3762class LightweightDispatcher implements java.io.Serializable , AWTEventListener { 3763 3764 3767 private static final long serialVersionUID = 5184291520170872969L; 3768 3772 private static final int LWD_MOUSE_DRAGGED_OVER = 1500; 3773 3774 private static final DebugHelper dbg = DebugHelper.create(LightweightDispatcher.class); 3775 3776 LightweightDispatcher(Container nativeContainer) { 3777 this.nativeContainer = nativeContainer; 3778 mouseEventTarget = null; 3779 eventMask = 0; 3780 } 3781 3782 3786 void dispose() { 3787 stopListeningForOtherDrags(); 3789 mouseEventTarget = null; 3790 } 3791 3792 3795 void enableEvents(long events) { 3796 eventMask |= events; 3797 } 3798 3799 3806 boolean dispatchEvent(AWTEvent e) { 3807 boolean ret = false; 3808 3809 3814 if (e instanceof SunDropTargetEvent) { 3815 3816 SunDropTargetEvent sdde = (SunDropTargetEvent) e; 3817 ret = processDropTargetEvent(sdde); 3818 3819 } else { 3820 if (e instanceof MouseEvent && (eventMask & MOUSE_MASK) != 0) { 3821 MouseEvent me = (MouseEvent ) e; 3822 ret = processMouseEvent(me); 3823 } 3824 3825 if (e.getID() == MouseEvent.MOUSE_MOVED) { 3826 nativeContainer.updateCursorImmediately(); 3827 } 3828 } 3829 3830 return ret; 3831 } 3832 3833 3837 private boolean isMouseGrab(MouseEvent e) { 3838 int modifiers = e.getModifiersEx(); 3839 3840 if(e.getID() == MouseEvent.MOUSE_PRESSED 3841 || e.getID() == MouseEvent.MOUSE_RELEASED) 3842 { 3843 switch (e.getButton()) { 3844 case MouseEvent.BUTTON1: 3845 modifiers ^= InputEvent.BUTTON1_DOWN_MASK; 3846 break; 3847 case MouseEvent.BUTTON2: 3848 modifiers ^= InputEvent.BUTTON2_DOWN_MASK; 3849 break; 3850 case MouseEvent.BUTTON3: 3851 modifiers ^= InputEvent.BUTTON3_DOWN_MASK; 3852 break; 3853 } 3854 } 3855 3856 return ((modifiers & (InputEvent.BUTTON1_DOWN_MASK 3857 | InputEvent.BUTTON2_DOWN_MASK 3858 | InputEvent.BUTTON3_DOWN_MASK)) != 0); 3859 } 3860 3861 3868 private boolean processMouseEvent(MouseEvent e) { 3869 int id = e.getID(); 3870 Component mouseOver = nativeContainer.getMouseEventTarget(e.getX(), e.getY(), 3872 Container.INCLUDE_SELF); 3873 3874 trackMouseEnterExit(mouseOver, e); 3875 3876 if (!isMouseGrab(e) && id != MouseEvent.MOUSE_CLICKED) { 3880 mouseEventTarget = (mouseOver != nativeContainer) ? mouseOver: null; 3881 } 3882 3883 if (mouseEventTarget != null) { 3884 switch (id) { 3885 case MouseEvent.MOUSE_ENTERED: 3886 case MouseEvent.MOUSE_EXITED: 3887 break; 3888 case MouseEvent.MOUSE_PRESSED: 3889 retargetMouseEvent(mouseEventTarget, id, e); 3890 break; 3891 case MouseEvent.MOUSE_RELEASED: 3892 retargetMouseEvent(mouseEventTarget, id, e); 3893 break; 3894 case MouseEvent.MOUSE_CLICKED: 3895 if (mouseOver == mouseEventTarget) { 3901 retargetMouseEvent(mouseOver, id, e); 3902 } 3903 break; 3904 case MouseEvent.MOUSE_MOVED: 3905 retargetMouseEvent(mouseEventTarget, id, e); 3906 break; 3907 case MouseEvent.MOUSE_DRAGGED: 3908 if (isMouseGrab(e)) { 3909 retargetMouseEvent(mouseEventTarget, id, e); 3910 } 3911 break; 3912 case MouseEvent.MOUSE_WHEEL: 3913 if (dbg.on && mouseOver != null) { 3917 dbg.println("LD retargeting mouse wheel to " + 3918 mouseOver.getName() + ", " + 3919 mouseOver.getClass()); 3920 } 3921 retargetMouseEvent(mouseOver, id, e); 3922 break; 3923 } 3924 e.consume(); 3925 } 3926 return e.isConsumed(); 3927 } 3928 3929 private boolean processDropTargetEvent(SunDropTargetEvent e) { 3930 int id = e.getID(); 3931 int x = e.getX(); 3932 int y = e.getY(); 3933 3934 3939 if (!nativeContainer.contains(x, y)) { 3940 final Dimension d = nativeContainer.getSize(); 3941 if (d.width <= x) { 3942 x = d.width - 1; 3943 } else if (x < 0) { 3944 x = 0; 3945 } 3946 if (d.height <= y) { 3947 y = d.height - 1; 3948 } else if (y < 0) { 3949 y = 0; 3950 } 3951 } 3952 Component mouseOver = nativeContainer.getDropTargetEventTarget(x, y, 3954 Container.INCLUDE_SELF); 3955 trackMouseEnterExit(mouseOver, e); 3956 3957 if (mouseOver != nativeContainer && mouseOver != null) { 3958 switch (id) { 3959 case SunDropTargetEvent.MOUSE_ENTERED: 3960 case SunDropTargetEvent.MOUSE_EXITED: 3961 break; 3962 default: 3963 retargetMouseEvent(mouseOver, id, e); 3964 e.consume(); 3965 break; 3966 } 3967 } 3968 return e.isConsumed(); 3969 } 3970 3971 3976 private void trackMouseEnterExit(Component targetOver, MouseEvent e) { 3977 Component targetEnter = null; 3978 int id = e.getID(); 3979 3980 if (e instanceof SunDropTargetEvent && 3981 id == MouseEvent.MOUSE_ENTERED && 3982 isMouseInNativeContainer == true) { 3983 targetLastEntered = null; 3988 } else if ( id != MouseEvent.MOUSE_EXITED && 3989 id != MouseEvent.MOUSE_DRAGGED && 3990 id != LWD_MOUSE_DRAGGED_OVER && 3991 isMouseInNativeContainer == false ) { 3992 isMouseInNativeContainer = true; 3994 startListeningForOtherDrags(); 3995 } else if ( id == MouseEvent.MOUSE_EXITED ) { 3996 isMouseInNativeContainer = false; 3997 stopListeningForOtherDrags(); 3998 } 3999 4000 if (isMouseInNativeContainer) { 4001 targetEnter = targetOver; 4002 } 4003 4004 if (targetLastEntered == targetEnter) { 4005 return; 4006 } 4007 4008 if (targetLastEntered != null) { 4009 retargetMouseEvent(targetLastEntered, MouseEvent.MOUSE_EXITED, e); 4010 } 4011 if (id == MouseEvent.MOUSE_EXITED) { 4012 e.consume(); 4014 } 4015 4016 if (targetEnter != null) { 4017 retargetMouseEvent(targetEnter, MouseEvent.MOUSE_ENTERED, e); 4018 } 4019 if (id == MouseEvent.MOUSE_ENTERED) { 4020 e.consume(); 4022 } 4023 4024 targetLastEntered = targetEnter; 4025 } 4026 4027 4032 private void startListeningForOtherDrags() { 4033 java.security.AccessController.doPrivileged( 4035 new java.security.PrivilegedAction () { 4036 public Object run() { 4037 nativeContainer.getToolkit().addAWTEventListener( 4038 LightweightDispatcher.this, 4039 AWTEvent.MOUSE_EVENT_MASK | 4040 AWTEvent.MOUSE_MOTION_EVENT_MASK); 4041 return null; 4042 } 4043 } 4044 ); 4045 } 4046 4047 private void stopListeningForOtherDrags() { 4048 java.security.AccessController.doPrivileged( 4050 new java.security.PrivilegedAction () { 4051 public Object run() { 4052 nativeContainer.getToolkit().removeAWTEventListener(LightweightDispatcher.this); 4053 return null; 4054 } 4055 } 4056 ); 4057 } 4058 4059 4064 public void eventDispatched(AWTEvent e) { 4065 boolean isForeignDrag = (e instanceof MouseEvent ) && 4066 !(e instanceof SunDropTargetEvent) && 4067 (e.id == MouseEvent.MOUSE_DRAGGED) && 4068 (e.getSource() != nativeContainer); 4069 4070 if (!isForeignDrag) { 4071 return; 4073 } 4074 4075 MouseEvent srcEvent = (MouseEvent )e; 4076 MouseEvent me; 4077 4078 synchronized (nativeContainer.getTreeLock()) { 4079 Component srcComponent = srcEvent.getComponent(); 4080 4081 if ( !srcComponent.isShowing() ) { 4084 return; 4085 } 4086 4087 me = new MouseEvent (nativeContainer, 4092 LWD_MOUSE_DRAGGED_OVER, 4093 srcEvent.getWhen(), 4094 srcEvent.getModifiersEx() | srcEvent.getModifiers(), 4095 srcEvent.getX(), 4096 srcEvent.getY(), 4097 srcEvent.getClickCount(), 4098 srcEvent.isPopupTrigger(), 4099 srcEvent.getButton()); 4100 ((AWTEvent )srcEvent).copyPrivateDataInto(me); 4101 final Point ptSrcOrigin = srcComponent.getLocationOnScreen(); 4103 4104 if (AppContext.getAppContext() != nativeContainer.appContext) { 4105 final MouseEvent mouseEvent = me; 4106 Runnable r = new Runnable () { 4107 public void run() { 4108 if (!nativeContainer.isShowing() ) { 4109 return; 4110 } 4111 4112 Point ptDstOrigin = nativeContainer.getLocationOnScreen(); 4113 mouseEvent.translatePoint(ptSrcOrigin.x - ptDstOrigin.x, 4114 ptSrcOrigin.y - ptDstOrigin.y ); 4115 Component targetOver = 4116 nativeContainer.getMouseEventTarget(mouseEvent.getX(), 4117 mouseEvent.getY(), 4118 Container.INCLUDE_SELF); 4119 trackMouseEnterExit(targetOver, mouseEvent); 4120 } 4121 }; 4122 SunToolkit.executeOnEventHandlerThread(nativeContainer, r); 4123 return; 4124 } else { 4125 if (!nativeContainer.isShowing() ) { 4126 return; 4127 } 4128 4129 Point ptDstOrigin = nativeContainer.getLocationOnScreen(); 4130 me.translatePoint( ptSrcOrigin.x - ptDstOrigin.x, ptSrcOrigin.y - ptDstOrigin.y ); 4131 } 4132 } 4133 Component targetOver = 4137 nativeContainer.getMouseEventTarget(me.getX(), me.getY(), 4138 Container.INCLUDE_SELF); 4139 trackMouseEnterExit(targetOver, me); 4140 } 4141 4142 4150 void retargetMouseEvent(Component target, int id, MouseEvent e) { 4151 if (target == null) { 4152 return; } 4154 4155 int x = e.getX(), y = e.getY(); 4156 Component component; 4157 4158 for(component = target; 4159 component != null && component != nativeContainer; 4160 component = component.getParent()) { 4161 x -= component.x; 4162 y -= component.y; 4163 } 4164 MouseEvent retargeted; 4165 if (component != null) { 4166 if (e instanceof SunDropTargetEvent) { 4167 retargeted = new SunDropTargetEvent(target, 4168 id, 4169 x, 4170 y, 4171 ((SunDropTargetEvent)e).getDispatcher()); 4172 } else if (id == MouseEvent.MOUSE_WHEEL) { 4173 retargeted = new MouseWheelEvent (target, 4174 id, 4175 e.getWhen(), 4176 e.getModifiersEx() | e.getModifiers(), 4177 x, 4178 y, 4179 e.getClickCount(), 4180 e.isPopupTrigger(), 4181 ((MouseWheelEvent )e).getScrollType(), 4182 ((MouseWheelEvent )e).getScrollAmount(), 4183 ((MouseWheelEvent )e).getWheelRotation()); 4184 } 4185 else { 4186 retargeted = new MouseEvent (target, 4187 id, 4188 e.getWhen(), 4189 e.getModifiersEx() | e.getModifiers(), 4190 x, 4191 y, 4192 e.getClickCount(), 4193 e.isPopupTrigger(), 4194 e.getButton()); 4195 } 4196 4197 ((AWTEvent )e).copyPrivateDataInto(retargeted); 4198 4199 if (target == nativeContainer) { 4200 ((Container )target).dispatchEventToSelf(retargeted); 4202 } else { 4203 assert AppContext.getAppContext() == target.appContext; 4204 4205 if (nativeContainer.modalComp != null) { 4206 if (((Container )nativeContainer.modalComp).isAncestorOf(target)) { 4207 target.dispatchEvent(retargeted); 4208 } else { 4209 e.consume(); 4210 } 4211 } else { 4212 target.dispatchEvent(retargeted); 4213 } 4214 } 4215 } 4216 } 4217 4218 4220 4224 private Container nativeContainer; 4225 4226 4229 private Component focus; 4230 4231 4237 private transient Component mouseEventTarget; 4238 4239 4242 private transient Component targetLastEntered; 4243 4244 4247 private transient boolean isMouseInNativeContainer = false; 4248 4249 4252 private Cursor nativeCursor; 4253 4254 4261 private long eventMask; 4262 4263 4267 private static final long PROXY_EVENT_MASK = 4268 AWTEvent.FOCUS_EVENT_MASK | 4269 AWTEvent.KEY_EVENT_MASK | 4270 AWTEvent.MOUSE_EVENT_MASK | 4271 AWTEvent.MOUSE_MOTION_EVENT_MASK | 4272 AWTEvent.MOUSE_WHEEL_EVENT_MASK; 4273 4274 private static final long MOUSE_MASK = 4275 AWTEvent.MOUSE_EVENT_MASK | 4276 AWTEvent.MOUSE_MOTION_EVENT_MASK | 4277 AWTEvent.MOUSE_WHEEL_EVENT_MASK; 4278} 4279 | Popular Tags |