1:
37:
38:
39: package ;
40:
41: import ;
42: import ;
43: import ;
44: import ;
45: import ;
46: import ;
47: import ;
48: import ;
49: import ;
50: import ;
51: import ;
52: import ;
53: import ;
54: import ;
55: import ;
56: import ;
57: import ;
58: import ;
59: import ;
60: import ;
61: import ;
62:
63: import ;
64:
65: import ;
66:
67:
79: public class Container extends Component
80: {
81:
84: private static final long serialVersionUID = 4613797578919906343L;
85:
86:
87: int ncomponents;
88: Component[] component;
89: LayoutManager layoutMgr;
90:
91: LightweightDispatcher dispatcher;
92:
93: Dimension maxSize;
94:
95:
98: boolean focusCycleRoot;
99:
100: int containerSerializedDataVersion;
101:
102:
103: transient ContainerListener containerListener;
104: transient PropertyChangeSupport changeSupport;
105:
106:
108: private FocusTraversalPolicy focusTraversalPolicy;
109:
110:
119: transient Set[] focusTraversalKeys;
120:
121:
124: public Container()
125: {
126: }
127:
128:
133: public int getComponentCount()
134: {
135: return countComponents ();
136: }
137:
138:
145: public int countComponents()
146: {
147: return ncomponents;
148: }
149:
150:
159: public Component getComponent(int n)
160: {
161: synchronized (getTreeLock ())
162: {
163: if (n < 0 || n >= ncomponents)
164: throw new ArrayIndexOutOfBoundsException("no such component");
165:
166: return component[n];
167: }
168: }
169:
170:
175: public Component[] getComponents()
176: {
177: synchronized (getTreeLock ())
178: {
179: Component[] result = new Component[ncomponents];
180:
181: if (ncomponents > 0)
182: System.arraycopy(component, 0, result, 0, ncomponents);
183:
184: return result;
185: }
186: }
187:
188:
191:
192: protected void swapComponents (int i, int j)
193: {
194: synchronized (getTreeLock ())
195: {
196: if (i < 0
197: || i >= component.length
198: || j < 0
199: || j >= component.length)
200: throw new ArrayIndexOutOfBoundsException ();
201: Component tmp = component[i];
202: component[i] = component[j];
203: component[j] = tmp;
204: }
205: }
206:
207:
213: public Insets getInsets()
214: {
215: return insets ();
216: }
217:
218:
225: public Insets insets()
226: {
227: if (peer == null)
228: return new Insets (0, 0, 0, 0);
229:
230: return ((ContainerPeer) peer).getInsets ();
231: }
232:
233:
241: public Component add(Component comp)
242: {
243: addImpl(comp, null, -1);
244: return comp;
245: }
246:
247:
259: public Component add(String name, Component comp)
260: {
261: addImpl(comp, name, -1);
262: return comp;
263: }
264:
265:
277: public Component add(Component comp, int index)
278: {
279: addImpl(comp, null, index);
280: return comp;
281: }
282:
283:
291: public void add(Component comp, Object constraints)
292: {
293: addImpl(comp, constraints, -1);
294: }
295:
296:
308: public void add(Component comp, Object constraints, int index)
309: {
310: addImpl(comp, constraints, index);
311: }
312:
313:
328: protected void addImpl(Component comp, Object constraints, int index)
329: {
330: synchronized (getTreeLock ())
331: {
332: if (index > ncomponents
333: || (index < 0 && index != -1)
334: || comp instanceof Window
335: || (comp instanceof Container
336: && ((Container) comp).isAncestorOf(this)))
337: throw new IllegalArgumentException();
338:
339:
340:
341: if (comp.parent != null)
342: comp.parent.remove(comp);
343: comp.parent = this;
344:
345: if (peer != null)
346: {
347:
348: comp.addNotify();
349:
350: if (comp.isLightweight ())
351: {
352: enableEvents (comp.eventMask);
353: if (!isLightweight ())
354: enableEvents (AWTEvent.PAINT_EVENT_MASK);
355: }
356: }
357:
358:
359: comp.invalidate();
360:
361: if (component == null)
362: component = new Component[4];
363:
364:
365:
366: if (ncomponents >= component.length)
367: {
368: int nl = component.length * 2;
369: Component[] c = new Component[nl];
370: System.arraycopy(component, 0, c, 0, ncomponents);
371: component = c;
372: }
373:
374: if (index == -1)
375: component[ncomponents++] = comp;
376: else
377: {
378: System.arraycopy(component, index, component, index + 1,
379: ncomponents - index);
380: component[index] = comp;
381: ++ncomponents;
382: }
383:
384:
385: if (layoutMgr != null)
386: {
387: if (layoutMgr instanceof LayoutManager2)
388: {
389: LayoutManager2 lm2 = (LayoutManager2) layoutMgr;
390: lm2.addLayoutComponent(comp, constraints);
391: }
392: else if (constraints instanceof String)
393: layoutMgr.addLayoutComponent((String) constraints, comp);
394: else
395: layoutMgr.addLayoutComponent(null, comp);
396: }
397:
398:
399:
400:
401:
402: ContainerEvent ce = new ContainerEvent(this,
403: ContainerEvent.COMPONENT_ADDED,
404: comp);
405: ContainerListener[] listeners = getContainerListeners();
406: for (int i = 0; i < listeners.length; i++)
407: listeners[i].componentAdded(ce);
408:
409:
410: repaint(comp.getX(), comp.getY(), comp.getWidth(),
411: comp.getHeight());
412: }
413: }
414:
415:
420: public void remove(int index)
421: {
422: synchronized (getTreeLock ())
423: {
424: Component r = component[index];
425:
426: ComponentListener[] list = r.getComponentListeners();
427: for (int j = 0; j < list.length; j++)
428: r.removeComponentListener(list[j]);
429:
430: r.removeNotify();
431:
432: System.arraycopy(component, index + 1, component, index,
433: ncomponents - index - 1);
434: component[--ncomponents] = null;
435:
436: invalidate();
437:
438: if (layoutMgr != null)
439: layoutMgr.removeLayoutComponent(r);
440:
441: r.parent = null;
442:
443: if (isShowing ())
444: {
445:
446: ContainerEvent ce = new ContainerEvent(this,
447: ContainerEvent.COMPONENT_REMOVED,
448: r);
449: getToolkit().getSystemEventQueue().postEvent(ce);
450:
451:
452: repaint();
453: }
454: }
455: }
456:
457:
462: public void remove(Component comp)
463: {
464: synchronized (getTreeLock ())
465: {
466: for (int i = 0; i < ncomponents; ++i)
467: {
468: if (component[i] == comp)
469: {
470: remove(i);
471: break;
472: }
473: }
474: }
475: }
476:
477:
480: public void removeAll()
481: {
482: synchronized (getTreeLock ())
483: {
484: while (ncomponents > 0)
485: remove(0);
486: }
487: }
488:
489:
494: public LayoutManager getLayout()
495: {
496: return layoutMgr;
497: }
498:
499:
505: public void setLayout(LayoutManager mgr)
506: {
507: layoutMgr = mgr;
508: invalidate();
509: }
510:
511:
514: public void doLayout()
515: {
516: layout ();
517: }
518:
519:
524: public void layout()
525: {
526: if (layoutMgr != null)
527: layoutMgr.layoutContainer (this);
528: }
529:
530:
534: public void invalidate()
535: {
536: super.invalidate();
537: if (layoutMgr != null && layoutMgr instanceof LayoutManager2)
538: {
539: LayoutManager2 lm2 = (LayoutManager2) layoutMgr;
540: lm2.invalidateLayout(this);
541: }
542: }
543:
544:
547: public void validate()
548: {
549: synchronized (getTreeLock ())
550: {
551: if (! isValid() && peer != null)
552: {
553: validateTree();
554: }
555: }
556: }
557:
558:
561: void invalidateTree()
562: {
563: super.invalidate();
564: for (int i = 0; i < ncomponents; i++)
565: {
566: Component comp = component[i];
567: comp.invalidate();
568: if (comp instanceof Container)
569: ((Container) comp).invalidateTree();
570: }
571:
572: if (layoutMgr != null && layoutMgr instanceof LayoutManager2)
573: {
574: LayoutManager2 lm2 = (LayoutManager2) layoutMgr;
575: lm2.invalidateLayout(this);
576: }
577: }
578:
579:
583: protected void validateTree()
584: {
585: if (valid)
586: return;
587:
588: ContainerPeer cPeer = null;
589: if (peer != null && ! (peer instanceof LightweightPeer))
590: {
591: cPeer = (ContainerPeer) peer;
592: cPeer.beginValidate();
593: }
594:
595: for (int i = 0; i < ncomponents; ++i)
596: {
597: Component comp = component[i];
598:
599: if (comp.getPeer () == null)
600: comp.addNotify();
601: }
602:
603: doLayout ();
604: for (int i = 0; i < ncomponents; ++i)
605: {
606: Component comp = component[i];
607:
608: if (! comp.isValid())
609: {
610: if (comp instanceof Container)
611: {
612: ((Container) comp).validateTree();
613: }
614: else
615: {
616: component[i].validate();
617: }
618: }
619: }
620:
621:
624: valid = true;
625:
626: if (cPeer != null)
627: cPeer.endValidate();
628: }
629:
630: public void setFont(Font f)
631: {
632: if( (f != null && (font == null || !font.equals(f)))
633: || f == null)
634: {
635: super.setFont(f);
636:
637:
638:
639: invalidateTree();
640: }
641: }
642:
643:
648: public Dimension getPreferredSize()
649: {
650: return preferredSize ();
651: }
652:
653:
660: public Dimension preferredSize()
661: {
662: synchronized(treeLock)
663: {
664: if(valid && prefSize != null)
665: return new Dimension(prefSize);
666: LayoutManager layout = getLayout();
667: if (layout != null)
668: {
669: Dimension layoutSize = layout.preferredLayoutSize(this);
670: if(valid)
671: prefSize = layoutSize;
672: return new Dimension(layoutSize);
673: }
674: else
675: return super.preferredSize ();
676: }
677: }
678:
679:
684: public Dimension getMinimumSize()
685: {
686: return minimumSize ();
687: }
688:
689:
696: public Dimension minimumSize()
697: {
698: if(valid && minSize != null)
699: return new Dimension(minSize);
700:
701: LayoutManager layout = getLayout();
702: if (layout != null)
703: {
704: minSize = layout.minimumLayoutSize (this);
705: return minSize;
706: }
707: else
708: return super.minimumSize ();
709: }
710:
711:
716: public Dimension getMaximumSize()
717: {
718: if (valid && maxSize != null)
719: return new Dimension(maxSize);
720:
721: LayoutManager layout = getLayout();
722: if (layout != null && layout instanceof LayoutManager2)
723: {
724: LayoutManager2 lm2 = (LayoutManager2) layout;
725: maxSize = lm2.maximumLayoutSize(this);
726: return maxSize;
727: }
728: else
729: return super.getMaximumSize();
730: }
731:
732:
739: public float getAlignmentX()
740: {
741: return super.getAlignmentX();
742: }
743:
744:
751: public float getAlignmentY()
752: {
753: return super.getAlignmentY();
754: }
755:
756:
765: public void paint(Graphics g)
766: {
767: if (!isShowing())
768: return;
769:
770:
771:
772: visitChildren(g, GfxPaintVisitor.INSTANCE, false);
773: }
774:
775:
792: public void update(Graphics g)
793: {
794:
795:
796:
797:
798:
799:
800:
801:
802: ComponentPeer p = peer;
803: if (p != null && !(p instanceof LightweightPeer))
804: g.clearRect(0, 0, getWidth(), getHeight());
805:
806: paint(g);
807: }
808:
809:
818: public void print(Graphics g)
819: {
820: super.print(g);
821: visitChildren(g, GfxPrintVisitor.INSTANCE, true);
822: }
823:
824:
829: public void paintComponents(Graphics g)
830: {
831: super.paint(g);
832: visitChildren(g, GfxPaintAllVisitor.INSTANCE, true);
833: }
834:
835:
840: public void printComponents(Graphics g)
841: {
842: super.paint(g);
843: visitChildren(g, GfxPrintAllVisitor.INSTANCE, true);
844: }
845:
846:
852: public synchronized void addContainerListener(ContainerListener listener)
853: {
854: containerListener = AWTEventMulticaster.add(containerListener, listener);
855: }
856:
857:
863: public synchronized void removeContainerListener(ContainerListener listener)
864: {
865: containerListener = AWTEventMulticaster.remove(containerListener, listener);
866: }
867:
868:
871: public synchronized ContainerListener[] getContainerListeners()
872: {
873: return (ContainerListener[])
874: AWTEventMulticaster.getListeners(containerListener,
875: ContainerListener.class);
876: }
877:
878:
888: public EventListener[] getListeners(Class listenerType)
889: {
890: if (listenerType == ContainerListener.class)
891: return getContainerListeners();
892: return super.getListeners(listenerType);
893: }
894:
895:
903: protected void processEvent(AWTEvent e)
904: {
905: if (e instanceof ContainerEvent)
906: processContainerEvent((ContainerEvent) e);
907: else
908: super.processEvent(e);
909: }
910:
911:
917: protected void processContainerEvent(ContainerEvent e)
918: {
919: if (containerListener == null)
920: return;
921: switch (e.id)
922: {
923: case ContainerEvent.COMPONENT_ADDED:
924: containerListener.componentAdded(e);
925: break;
926:
927: case ContainerEvent.COMPONENT_REMOVED:
928: containerListener.componentRemoved(e);
929: break;
930: }
931: }
932:
933:
940: public void deliverEvent(Event e)
941: {
942: if (!handleEvent (e))
943: {
944: synchronized (getTreeLock ())
945: {
946: Component parent = getParent ();
947:
948: if (parent != null)
949: parent.deliverEvent (e);
950: }
951: }
952: }
953:
954:
968: public Component getComponentAt(int x, int y)
969: {
970: return locate (x, y);
971: }
972:
973:
989: public Component locate(int x, int y)
990: {
991: synchronized (getTreeLock ())
992: {
993: if (!contains (x, y))
994: return null;
995: for (int i = 0; i < ncomponents; ++i)
996: {
997:
998: if (!component[i].isVisible ())
999: continue;
1000:
1001: int x2 = x - component[i].x;
1002: int y2 = y - component[i].y;
1003: if (component[i].contains (x2, y2))
1004: return component[i];
1005: }
1006: return this;
1007: }
1008: }
1009:
1010:
1022: public Component getComponentAt(Point p)
1023: {
1024: return getComponentAt (p.x, p.y);
1025: }
1026:
1027: public Component findComponentAt(int x, int y)
1028: {
1029: synchronized (getTreeLock ())
1030: {
1031: if (! contains(x, y))
1032: return null;
1033:
1034: for (int i = 0; i < ncomponents; ++i)
1035: {
1036:
1037: if (!component[i].isVisible())
1038: continue;
1039:
1040: int x2 = x - component[i].x;
1041: int y2 = y - component[i].y;
1042:
1043:
1044: if (component[i] instanceof Container)
1045: {
1046: Container k = (Container) component[i];
1047: Component r = k.findComponentAt(x2, y2);
1048: if (r != null)
1049: return r;
1050: }
1051: else if (component[i].contains(x2, y2))
1052: return component[i];
1053: }
1054:
1055: return this;
1056: }
1057: }
1058:
1059:
1071: Component findComponentForMouseEventAt(int x, int y)
1072: {
1073: synchronized (getTreeLock())
1074: {
1075: if (!contains(x, y))
1076: return null;
1077:
1078: for (int i = 0; i < ncomponents; ++i)
1079: {
1080:
1081: if (!component[i].isVisible())
1082: continue;
1083:
1084: int x2 = x - component[i].x;
1085: int y2 = y - component[i].y;
1086:
1087:
1088: if (component[i] instanceof Container)
1089: {
1090: Container k = (Container) component[i];
1091: Component r = k.findComponentForMouseEventAt(x2, y2);
1092: if (r != null)
1093: return r;
1094: }
1095: else if (component[i].contains(x2, y2))
1096: return component[i];
1097: }
1098:
1099:
1100: if (this.getMouseListeners().length == 0)
1101: return null;
1102: return this;
1103: }
1104: }
1105:
1106: public Component findComponentAt(Point p)
1107: {
1108: return findComponentAt(p.x, p.y);
1109: }
1110:
1111:
1116: public void addNotify()
1117: {
1118: super.addNotify();
1119: addNotifyContainerChildren();
1120: }
1121:
1122:
1127: public void removeNotify()
1128: {
1129: synchronized (getTreeLock ())
1130: {
1131: for (int i = 0; i < ncomponents; ++i)
1132: component[i].removeNotify();
1133: super.removeNotify();
1134: }
1135: }
1136:
1137:
1146: public boolean isAncestorOf(Component comp)
1147: {
1148: synchronized (getTreeLock ())
1149: {
1150: while (true)
1151: {
1152: if (comp == null)
1153: return false;
1154: if (comp == this)
1155: return true;
1156: comp = comp.getParent();
1157: }
1158: }
1159: }
1160:
1161:
1167: protected String paramString()
1168: {
1169: if (layoutMgr == null)
1170: return super.paramString();
1171:
1172: StringBuffer sb = new StringBuffer();
1173: sb.append(super.paramString());
1174: sb.append(",layout=");
1175: sb.append(layoutMgr.getClass().getName());
1176: return sb.toString();
1177: }
1178:
1179:
1186: public void list(PrintStream out, int indent)
1187: {
1188: synchronized (getTreeLock ())
1189: {
1190: super.list(out, indent);
1191: for (int i = 0; i < ncomponents; ++i)
1192: component[i].list(out, indent + 2);
1193: }
1194: }
1195:
1196:
1203: public void list(PrintWriter out, int indent)
1204: {
1205: synchronized (getTreeLock ())
1206: {
1207: super.list(out, indent);
1208: for (int i = 0; i < ncomponents; ++i)
1209: component[i].list(out, indent + 2);
1210: }
1211: }
1212:
1213:
1229: public void setFocusTraversalKeys(int id, Set keystrokes)
1230: {
1231: if (id != KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS &&
1232: id != KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS &&
1233: id != KeyboardFocusManager.UP_CYCLE_TRAVERSAL_KEYS &&
1234: id != KeyboardFocusManager.DOWN_CYCLE_TRAVERSAL_KEYS)
1235: throw new IllegalArgumentException ();
1236:
1237: if (keystrokes == null)
1238: {
1239: Container parent = getParent ();
1240:
1241: while (parent != null)
1242: {
1243: if (parent.areFocusTraversalKeysSet (id))
1244: {
1245: keystrokes = parent.getFocusTraversalKeys (id);
1246: break;
1247: }
1248: parent = parent.getParent ();
1249: }
1250:
1251: if (keystrokes == null)
1252: keystrokes = KeyboardFocusManager.getCurrentKeyboardFocusManager ().
1253: getDefaultFocusTraversalKeys (id);
1254: }
1255:
1256: Set sa;
1257: Set sb;
1258: Set sc;
1259: String name;
1260: switch (id)
1261: {
1262: case KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS:
1263: sa = getFocusTraversalKeys
1264: (KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS);
1265: sb = getFocusTraversalKeys
1266: (KeyboardFocusManager.UP_CYCLE_TRAVERSAL_KEYS);
1267: sc = getFocusTraversalKeys
1268: (KeyboardFocusManager.DOWN_CYCLE_TRAVERSAL_KEYS);
1269: name = "forwardFocusTraversalKeys";
1270: break;
1271: case KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS:
1272: sa = getFocusTraversalKeys
1273: (KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS);
1274: sb = getFocusTraversalKeys
1275: (KeyboardFocusManager.UP_CYCLE_TRAVERSAL_KEYS);
1276: sc = getFocusTraversalKeys
1277: (KeyboardFocusManager.DOWN_CYCLE_TRAVERSAL_KEYS);
1278: name = "backwardFocusTraversalKeys";
1279: break;
1280: case KeyboardFocusManager.UP_CYCLE_TRAVERSAL_KEYS:
1281: sa = getFocusTraversalKeys
1282: (KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS);
1283: sb = getFocusTraversalKeys
1284: (KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS);
1285: sc = getFocusTraversalKeys
1286: (KeyboardFocusManager.DOWN_CYCLE_TRAVERSAL_KEYS);
1287: name = "upCycleFocusTraversalKeys";
1288: break;
1289: case KeyboardFocusManager.DOWN_CYCLE_TRAVERSAL_KEYS:
1290: sa = getFocusTraversalKeys
1291: (KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS);
1292: sb = getFocusTraversalKeys
1293: (KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS);
1294: sc = getFocusTraversalKeys
1295: (KeyboardFocusManager.UP_CYCLE_TRAVERSAL_KEYS);
1296: name = "downCycleFocusTraversalKeys";
1297: break;
1298: default:
1299: throw new IllegalArgumentException ();
1300: }
1301:
1302: int i = keystrokes.size ();
1303: Iterator iter = keystrokes.iterator ();
1304:
1305: while (--i >= 0)
1306: {
1307: Object o = iter.next ();
1308: if (!(o instanceof AWTKeyStroke)
1309: || sa.contains (o) || sb.contains (o) || sc.contains (o)
1310: || ((AWTKeyStroke) o).keyCode == KeyEvent.VK_UNDEFINED)
1311: throw new IllegalArgumentException ();
1312: }
1313:
1314: if (focusTraversalKeys == null)
1315: focusTraversalKeys = new Set[4];
1316:
1317: keystrokes = Collections.unmodifiableSet (new HashSet (keystrokes));
1318: firePropertyChange (name, focusTraversalKeys[id], keystrokes);
1319:
1320: focusTraversalKeys[id] = keystrokes;
1321: }
1322:
1323:
1335: public Set getFocusTraversalKeys (int id)
1336: {
1337: if (id != KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS &&
1338: id != KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS &&
1339: id != KeyboardFocusManager.UP_CYCLE_TRAVERSAL_KEYS &&
1340: id != KeyboardFocusManager.DOWN_CYCLE_TRAVERSAL_KEYS)
1341: throw new IllegalArgumentException ();
1342:
1343: Set s = null;
1344:
1345: if (focusTraversalKeys != null)
1346: s = focusTraversalKeys[id];
1347:
1348: if (s == null && parent != null)
1349: s = parent.getFocusTraversalKeys (id);
1350:
1351: return s == null ? (KeyboardFocusManager.getCurrentKeyboardFocusManager()
1352: .getDefaultFocusTraversalKeys(id)) : s;
1353: }
1354:
1355:
1369: public boolean areFocusTraversalKeysSet (int id)
1370: {
1371: if (id != KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS &&
1372: id != KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS &&
1373: id != KeyboardFocusManager.UP_CYCLE_TRAVERSAL_KEYS &&
1374: id != KeyboardFocusManager.DOWN_CYCLE_TRAVERSAL_KEYS)
1375: throw new IllegalArgumentException ();
1376:
1377: return focusTraversalKeys != null && focusTraversalKeys[id] != null;
1378: }
1379:
1380:
1395: public boolean isFocusCycleRoot (Container c)
1396: {
1397: if (this == c
1398: && isFocusCycleRoot ())
1399: return true;
1400:
1401: Container ancestor = getFocusCycleRootAncestor ();
1402:
1403: if (c == ancestor)
1404: return true;
1405:
1406: return false;
1407: }
1408:
1409:
1421: public void setFocusTraversalPolicy (FocusTraversalPolicy policy)
1422: {
1423: focusTraversalPolicy = policy;
1424: }
1425:
1426:
1437: public FocusTraversalPolicy getFocusTraversalPolicy ()
1438: {
1439: if (!isFocusCycleRoot ())
1440: return null;
1441:
1442: if (focusTraversalPolicy == null)
1443: {
1444: Container ancestor = getFocusCycleRootAncestor ();
1445:
1446: if (ancestor != this)
1447: return ancestor.getFocusTraversalPolicy ();
1448: else
1449: {
1450: KeyboardFocusManager manager = KeyboardFocusManager.getCurrentKeyboardFocusManager ();
1451:
1452: return manager.getDefaultFocusTraversalPolicy ();
1453: }
1454: }
1455: else
1456: return focusTraversalPolicy;
1457: }
1458:
1459:
1467: public boolean isFocusTraversalPolicySet ()
1468: {
1469: return focusTraversalPolicy == null;
1470: }
1471:
1472:
1488: public void setFocusCycleRoot (boolean focusCycleRoot)
1489: {
1490: this.focusCycleRoot = focusCycleRoot;
1491: }
1492:
1493:
1500: public boolean isFocusCycleRoot ()
1501: {
1502: return focusCycleRoot;
1503: }
1504:
1505:
1514: public void transferFocusDownCycle ()
1515: {
1516: KeyboardFocusManager manager = KeyboardFocusManager.getCurrentKeyboardFocusManager ();
1517:
1518: manager.downFocusCycle (this);
1519: }
1520:
1521:
1529: public void applyComponentOrientation (ComponentOrientation orientation)
1530: {
1531: if (orientation == null)
1532: throw new NullPointerException ();
1533: }
1534:
1535: public void addPropertyChangeListener (PropertyChangeListener listener)
1536: {
1537: if (listener == null)
1538: return;
1539:
1540: if (changeSupport == null)
1541: changeSupport = new PropertyChangeSupport (this);
1542:
1543: changeSupport.addPropertyChangeListener (listener);
1544: }
1545:
1546: public void addPropertyChangeListener (String name,
1547: PropertyChangeListener listener)
1548: {
1549: if (listener == null)
1550: return;
1551:
1552: if (changeSupport == null)
1553: changeSupport = new PropertyChangeSupport (this);
1554:
1555: changeSupport.addPropertyChangeListener (name, listener);
1556: }
1557:
1558:
1559:
1560:
1574: private void visitChildren(Graphics gfx, GfxVisitor visitor,
1575: boolean lightweightOnly)
1576: {
1577: synchronized (getTreeLock ())
1578: {
1579: for (int i = ncomponents - 1; i >= 0; --i)
1580: {
1581: Component comp = component[i];
1582: boolean applicable = comp.isVisible()
1583: && (comp.isLightweight() || !lightweightOnly);
1584:
1585: if (applicable)
1586: visitChild(gfx, visitor, comp);
1587: }
1588: }
1589: }
1590:
1591:
1604: private void visitChild(Graphics gfx, GfxVisitor visitor,
1605: Component comp)
1606: {
1607: Rectangle bounds = comp.getBounds();
1608: Rectangle oldClip = gfx.getClipBounds();
1609: if (oldClip == null)
1610: oldClip = bounds;
1611:
1612: Rectangle clip = oldClip.intersection(bounds);
1613:
1614: if (clip.isEmpty()) return;
1615:
1616: boolean clipped = false;
1617: boolean translated = false;
1618: try
1619: {
1620: gfx.setClip(clip.x, clip.y, clip.width, clip.height);
1621: clipped = true;
1622: gfx.translate(bounds.x, bounds.y);
1623: translated = true;
1624: visitor.visit(comp, gfx);
1625: }
1626: finally
1627: {
1628: if (translated)
1629: gfx.translate (-bounds.x, -bounds.y);
1630: if (clipped)
1631: gfx.setClip (oldClip.x, oldClip.y, oldClip.width, oldClip.height);
1632: }
1633: }
1634:
1635: void dispatchEventImpl(AWTEvent e)
1636: {
1637:
1638: if (dispatcher != null && dispatcher.handleEvent (e))
1639: return;
1640:
1641: if ((e.id <= ContainerEvent.CONTAINER_LAST
1642: && e.id >= ContainerEvent.CONTAINER_FIRST)
1643: && (containerListener != null
1644: || (eventMask & AWTEvent.CONTAINER_EVENT_MASK) != 0))
1645: processEvent(e);
1646: else
1647: super.dispatchEventImpl(e);
1648: }
1649:
1650:
1660: boolean eventTypeEnabled(int eventId)
1661: {
1662: if(eventId <= ContainerEvent.CONTAINER_LAST
1663: && eventId >= ContainerEvent.CONTAINER_FIRST)
1664: return containerListener != null
1665: || (eventMask & AWTEvent.CONTAINER_EVENT_MASK) != 0;
1666: else
1667: return super.eventTypeEnabled(eventId);
1668: }
1669:
1670:
1671: Component findNextFocusComponent(Component child)
1672: {
1673: synchronized (getTreeLock ())
1674: {
1675: int start, end;
1676: if (child != null)
1677: {
1678: for (start = 0; start < ncomponents; ++start)
1679: {
1680: if (component[start] == child)
1681: break;
1682: }
1683: end = start;
1684:
1685: if (end == 0)
1686: end = ncomponents;
1687: ++start;
1688: }
1689: else
1690: {
1691: start = 0;
1692: end = ncomponents;
1693: }
1694:
1695: for (int j = start; j != end; ++j)
1696: {
1697: if (j >= ncomponents)
1698: {
1699:
1700:
1701:
1702:
1703: if (parent != null)
1704: return parent.findNextFocusComponent(this);
1705: j -= ncomponents;
1706: }
1707: if (component[j] instanceof Container)
1708: {
1709: Component c = component[j];
1710: c = c.findNextFocusComponent(null);
1711: if (c != null)
1712: return c;
1713: }
1714: else if (component[j].isFocusTraversable())
1715: return component[j];
1716: }
1717:
1718: return null;
1719: }
1720: }
1721:
1722: private void addNotifyContainerChildren()
1723: {
1724: synchronized (getTreeLock ())
1725: {
1726: for (int i = ncomponents; --i >= 0; )
1727: {
1728: component[i].addNotify();
1729: if (component[i].isLightweight ())
1730: {
1731:
1732:
1733:
1734: if (!this.isLightweight() && dispatcher == null)
1735: dispatcher = new LightweightDispatcher (this);
1736:
1737: if (dispatcher != null)
1738: dispatcher.enableEvents(component[i].eventMask);
1739:
1740: enableEvents(component[i].eventMask);
1741: if (peer != null && !isLightweight ())
1742: enableEvents (AWTEvent.PAINT_EVENT_MASK);
1743: }
1744: }
1745: }
1746: }
1747:
1748:
1763: private void readObject (ObjectInputStream s)
1764: throws ClassNotFoundException, IOException
1765: {
1766: s.defaultReadObject ();
1767: String key = (String) s.readObject ();
1768: while (key != null)
1769: {
1770: Object object = s.readObject ();
1771: if ("containerL".equals (key))
1772: addContainerListener((ContainerListener) object);
1773:
1774: else if ("focusTraversalPolicy".equals (key))
1775: setFocusTraversalPolicy ((FocusTraversalPolicy) object);
1776:
1777: key = (String) s.readObject();
1778: }
1779: }
1780:
1781:
1793: private void writeObject (ObjectOutputStream s) throws IOException
1794: {
1795: s.defaultWriteObject ();
1796: AWTEventMulticaster.save (s, "containerL", containerListener);
1797: if (focusTraversalPolicy instanceof Serializable)
1798: s.writeObject (focusTraversalPolicy);
1799: else
1800: s.writeObject (null);
1801: }
1802:
1803:
1804:
1805:
1808:
1809: abstract static class GfxVisitor
1810: {
1811: public abstract void visit(Component c, Graphics gfx);
1812: }
1813:
1814: static class GfxPaintVisitor extends GfxVisitor
1815: {
1816: public static final GfxVisitor INSTANCE = new GfxPaintVisitor();
1817:
1818: public void visit(Component c, Graphics gfx)
1819: {
1820: c.paint(gfx);
1821: }
1822: }
1823:
1824: static class GfxPrintVisitor extends GfxVisitor
1825: {
1826: public static final GfxVisitor INSTANCE = new GfxPrintVisitor();
1827:
1828: public void visit(Component c, Graphics gfx)
1829: {
1830: c.print(gfx);
1831: }
1832: }
1833:
1834: static class GfxPaintAllVisitor extends GfxVisitor
1835: {
1836: public static final GfxVisitor INSTANCE = new GfxPaintAllVisitor();
1837:
1838: public void visit(Component c, Graphics gfx)
1839: {
1840: c.paintAll(gfx);
1841: }
1842: }
1843:
1844: static class GfxPrintAllVisitor extends GfxVisitor
1845: {
1846: public static final GfxVisitor INSTANCE = new GfxPrintAllVisitor();
1847:
1848: public void visit(Component c, Graphics gfx)
1849: {
1850: c.printAll(gfx);
1851: }
1852: }
1853:
1854:
1861: protected class AccessibleAWTContainer extends AccessibleAWTComponent
1862: {
1863:
1866: private static final long serialVersionUID = 5081320404842566097L;
1867:
1868:
1873: protected ContainerListener accessibleContainerHandler
1874: = new AccessibleContainerHandler();
1875:
1876:
1879: protected AccessibleAWTContainer()
1880: {
1881: Container.this.addContainerListener(accessibleContainerHandler);
1882: }
1883:
1884:
1890: public int getAccessibleChildrenCount()
1891: {
1892: synchronized (getTreeLock ())
1893: {
1894: int count = 0;
1895: int i = component == null ? 0 : component.length;
1896: while (--i >= 0)
1897: if (component[i] instanceof Accessible)
1898: count++;
1899: return count;
1900: }
1901: }
1902:
1903:
1909: public Accessible getAccessibleChild(int i)
1910: {
1911: synchronized (getTreeLock ())
1912: {
1913: if (component == null)
1914: return null;
1915: int index = -1;
1916: while (i >= 0 && ++index < component.length)
1917: if (component[index] instanceof Accessible)
1918: i--;
1919: if (i < 0)
1920: return (Accessible) component[index];
1921: return null;
1922: }
1923: }
1924:
1925:
1935: public Accessible getAccessibleAt(Point p)
1936: {
1937: Component c = getComponentAt(p.x, p.y);
1938: return c != Container.this && c instanceof Accessible ? (Accessible) c
1939: : null;
1940: }
1941:
1942:
1950: protected class AccessibleContainerHandler implements ContainerListener
1951: {
1952:
1955: protected AccessibleContainerHandler()
1956: {
1957: }
1958:
1959:
1965: public void componentAdded(ContainerEvent e)
1966: {
1967: AccessibleAWTContainer.this.firePropertyChange
1968: (ACCESSIBLE_CHILD_PROPERTY, null, e.getChild());
1969: }
1970:
1971:
1977: public void componentRemoved(ContainerEvent e)
1978: {
1979: AccessibleAWTContainer.this.firePropertyChange
1980: (ACCESSIBLE_CHILD_PROPERTY, e.getChild(), null);
1981: }
1982: }
1983: }
1984: }
1985:
1986:
1992: class LightweightDispatcher implements Serializable
1993: {
1994: private static final long serialVersionUID = 5184291520170872969L;
1995: private Container nativeContainer;
1996: private Cursor nativeCursor;
1997: private long eventMask;
1998:
1999: private transient Component pressedComponent;
2000: private transient Component lastComponentEntered;
2001: private transient int pressCount;
2002:
2003: LightweightDispatcher(Container c)
2004: {
2005: nativeContainer = c;
2006: }
2007:
2008: void enableEvents(long l)
2009: {
2010: eventMask |= l;
2011: }
2012:
2013:
2024: Component getDeepestComponentForMouseEventAt (
2025: Component parent, int x, int y)
2026: {
2027: if (parent == null || (! parent.contains(x, y)))
2028: return null;
2029:
2030: if (! (parent instanceof Container))
2031: return parent;
2032:
2033: Container c = (Container) parent;
2034: return c.findComponentForMouseEventAt(x, y);
2035: }
2036:
2037: Component acquireComponentForMouseEvent(MouseEvent me)
2038: {
2039: int x = me.getX ();
2040: int y = me.getY ();
2041:
2042: Component mouseEventTarget = null;
2043:
2044: Component parent = nativeContainer;
2045: Component candidate = null;
2046: Point p = me.getPoint();
2047: while (candidate == null && parent != null)
2048: {
2049: candidate =
2050: getDeepestComponentForMouseEventAt(parent, p.x, p.y);
2051: if (candidate == null || (candidate.eventMask & me.getID()) == 0)
2052: {
2053: candidate = null;
2054: p = AWTUtilities.convertPoint(parent, p.x, p.y, parent.parent);
2055: parent = parent.parent;
2056: }
2057: }
2058:
2059:
2060:
2061:
2062: if (candidate == nativeContainer)
2063: candidate = null;
2064:
2065:
2066: if (lastComponentEntered != null
2067: && lastComponentEntered.isShowing()
2068: && lastComponentEntered != candidate)
2069: {
2070:
2071:
2072: if (AWTUtilities.isDescendingFrom(lastComponentEntered,
2073: nativeContainer))
2074: {
2075: Point tp = AWTUtilities.convertPoint(nativeContainer,
2076: x, y, lastComponentEntered);
2077: MouseEvent exited = new MouseEvent (lastComponentEntered,
2078: MouseEvent.MOUSE_EXITED,
2079: me.getWhen (),
2080: me.getModifiersEx (),
2081: tp.x, tp.y,
2082: me.getClickCount (),
2083: me.isPopupTrigger (),
2084: me.getButton ());
2085: lastComponentEntered.dispatchEvent (exited);
2086: }
2087: lastComponentEntered = null;
2088: }
2089:
2090:
2091: if (candidate != null)
2092: {
2093: mouseEventTarget = candidate;
2094: if (candidate.isLightweight()
2095: && candidate.isShowing()
2096: && candidate != nativeContainer
2097: && candidate != lastComponentEntered)
2098: {
2099: lastComponentEntered = mouseEventTarget;
2100: Point cp = AWTUtilities.convertPoint(nativeContainer,
2101: x, y, lastComponentEntered);
2102: MouseEvent entered = new MouseEvent (lastComponentEntered,
2103: MouseEvent.MOUSE_ENTERED,
2104: me.getWhen (),
2105: me.getModifiersEx (),
2106: cp.x, cp.y,
2107: me.getClickCount (),
2108: me.isPopupTrigger (),
2109: me.getButton ());
2110: lastComponentEntered.dispatchEvent (entered);
2111: }
2112: }
2113:
2114:
2115:
2116: int modifiers = me.getModifiersEx() & (MouseEvent.BUTTON1_DOWN_MASK
2117: | MouseEvent.BUTTON2_DOWN_MASK
2118: | MouseEvent.BUTTON3_DOWN_MASK);
2119: switch(me.getButton())
2120: {
2121: case MouseEvent.BUTTON1:
2122: modifiers &= ~MouseEvent.BUTTON1_DOWN_MASK;
2123: break;
2124: case MouseEvent.BUTTON2:
2125: modifiers &= ~MouseEvent.BUTTON2_DOWN_MASK;
2126: break;
2127: case MouseEvent.BUTTON3:
2128: modifiers &= ~MouseEvent.BUTTON3_DOWN_MASK;
2129: break;
2130: }
2131:
2132: if (me.getID() == MouseEvent.MOUSE_RELEASED
2133: || me.getID() == MouseEvent.MOUSE_PRESSED && modifiers > 0
2134: || me.getID() == MouseEvent.MOUSE_DRAGGED)
2135: {
2136:
2137:
2138:
2139:
2140:
2141:
2142:
2143: if (AWTUtilities.isDescendingFrom(pressedComponent, nativeContainer))
2144: mouseEventTarget = pressedComponent;
2145: }
2146: else if (me.getID() == MouseEvent.MOUSE_CLICKED)
2147: {
2148:
2149:
2150: if (candidate != pressedComponent)
2151: mouseEventTarget = null;
2152: else if (pressCount == 0)
2153: pressedComponent = null;
2154: }
2155: return mouseEventTarget;
2156: }
2157:
2158: boolean handleEvent(AWTEvent e)
2159: {
2160: if (e instanceof MouseEvent)
2161: {
2162: MouseEvent me = (MouseEvent) e;
2163:
2164:
2165:
2166: Component mouseEventTarget = acquireComponentForMouseEvent(me);
2167:
2168:
2169: if (mouseEventTarget != null
2170: && mouseEventTarget.isShowing()
2171: && e.getID() != MouseEvent.MOUSE_ENTERED
2172: && e.getID() != MouseEvent.MOUSE_EXITED)
2173: {
2174: switch (e.getID())
2175: {
2176: case MouseEvent.MOUSE_PRESSED:
2177: if (pressCount++ == 0)
2178: pressedComponent = mouseEventTarget;
2179: break;
2180: case MouseEvent.MOUSE_RELEASED:
2181:
2182:
2183:
2184: if (--pressCount == 0
2185: && mouseEventTarget != pressedComponent)
2186: pressedComponent = null;
2187: break;
2188: }
2189:
2190: MouseEvent newEvt =
2191: AWTUtilities.convertMouseEvent(nativeContainer, me,
2192: mouseEventTarget);
2193: mouseEventTarget.dispatchEvent(newEvt);
2194:
2195: if (newEvt.isConsumed())
2196: e.consume();
2197: }
2198: }
2199:
2200: return e.isConsumed();
2201: }
2202: }