Container.java 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444
  1. /* Container.java -- parent container class in AWT
  2. Copyright (C) 1999, 2000, 2002 Free Software Foundation
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  15. 02111-1307 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package java.awt;
  32. import java.awt.event.AWTEventListener;
  33. import java.awt.event.ContainerEvent;
  34. import java.awt.event.ContainerListener;
  35. import java.awt.event.MouseEvent;
  36. import java.awt.peer.ComponentPeer;
  37. import java.awt.peer.ContainerPeer;
  38. import java.awt.peer.LightweightPeer;
  39. import java.beans.PropertyChangeListener;
  40. import java.io.PrintStream;
  41. import java.io.PrintWriter;
  42. import java.io.Serializable;
  43. import java.util.EventListener;
  44. import java.util.Set;
  45. import javax.accessibility.Accessible;
  46. /**
  47. * A generic window toolkit object that acts as a container for other objects.
  48. * Components are tracked in a list, and new elements are at the end of the
  49. * list or bottom of the stacking order.
  50. *
  51. * @author original author unknown
  52. * @author Eric Blake <ebb9@email.byu.edu>
  53. *
  54. * @since 1.0
  55. *
  56. * @status still missing 1.4 support
  57. */
  58. public class Container extends Component
  59. {
  60. /**
  61. * Compatible with JDK 1.0+.
  62. */
  63. private static final long serialVersionUID = 4613797578919906343L;
  64. /* Serialized fields from the serialization spec. */
  65. int ncomponents;
  66. Component[] component;
  67. LayoutManager layoutMgr;
  68. LightweightDispatcher dispatcher;
  69. Dimension maxSize;
  70. /**
  71. * @since 1.4
  72. */
  73. boolean focusCycleRoot;
  74. int containerSerializedDataVersion;
  75. /* Anything else is non-serializable, and should be declared "transient". */
  76. transient ContainerListener containerListener;
  77. /**
  78. * Default constructor for subclasses.
  79. */
  80. public Container()
  81. {
  82. }
  83. /**
  84. * Returns the number of components in this container.
  85. *
  86. * @return The number of components in this container.
  87. */
  88. public int getComponentCount()
  89. {
  90. return ncomponents;
  91. }
  92. /**
  93. * Returns the number of components in this container.
  94. *
  95. * @return The number of components in this container.
  96. *
  97. * @deprecated use {@link #getComponentCount()} instead
  98. */
  99. public int countComponents()
  100. {
  101. return getComponentCount();
  102. }
  103. /**
  104. * Returns the component at the specified index.
  105. *
  106. * @param index The index of the component to retrieve.
  107. *
  108. * @return The requested component.
  109. *
  110. * @throws ArrayIndexOutOfBoundsException If the specified index is invalid
  111. */
  112. public Component getComponent(int n)
  113. {
  114. synchronized (getTreeLock ())
  115. {
  116. if (n < 0 || n >= ncomponents)
  117. throw new ArrayIndexOutOfBoundsException("no such component");
  118. return component[n];
  119. }
  120. }
  121. /**
  122. * Returns an array of the components in this container.
  123. *
  124. * @return The components in this container.
  125. */
  126. public Component[] getComponents()
  127. {
  128. synchronized (getTreeLock ())
  129. {
  130. Component[] result = new Component[ncomponents];
  131. if (ncomponents > 0)
  132. System.arraycopy(component, 0, result, 0, ncomponents);
  133. return result;
  134. }
  135. }
  136. /**
  137. * Returns the insets for this container, which is the space used for
  138. * borders, the margin, etc.
  139. *
  140. * @return The insets for this container.
  141. */
  142. public Insets getInsets()
  143. {
  144. if (peer == null)
  145. return new Insets(0, 0, 0, 0);
  146. return ((ContainerPeer) peer).getInsets();
  147. }
  148. /**
  149. * Returns the insets for this container, which is the space used for
  150. * borders, the margin, etc.
  151. *
  152. * @return The insets for this container.
  153. * @deprecated use {@link #getInsets()} instead
  154. */
  155. public Insets insets()
  156. {
  157. return getInsets();
  158. }
  159. /**
  160. * Adds the specified component to this container at the end of the
  161. * component list.
  162. *
  163. * @param component The component to add to the container.
  164. *
  165. * @return The same component that was added.
  166. */
  167. public Component add(Component comp)
  168. {
  169. addImpl(comp, null, -1);
  170. return comp;
  171. }
  172. /**
  173. * Adds the specified component to the container at the end of the
  174. * component list. This method should not be used. Instead, use
  175. * <code>add(Component, Object</code>.
  176. *
  177. * @param name The name of the component to be added.
  178. * @param component The component to be added.
  179. *
  180. * @return The same component that was added.
  181. */
  182. public Component add(String name, Component comp)
  183. {
  184. addImpl(comp, name, -1);
  185. return comp;
  186. }
  187. /**
  188. * Adds the specified component to this container at the specified index
  189. * in the component list.
  190. *
  191. * @param component The component to be added.
  192. * @param index The index in the component list to insert this child
  193. * at, or -1 to add at the end of the list.
  194. *
  195. * @return The same component that was added.
  196. *
  197. * @throws ArrayIndexOutOfBounds If the specified index is invalid.
  198. */
  199. public Component add(Component comp, int index)
  200. {
  201. addImpl(comp, null, index);
  202. return comp;
  203. }
  204. /**
  205. * Adds the specified component to this container at the end of the
  206. * component list. The layout manager will use the specified constraints
  207. * when laying out this component.
  208. *
  209. * @param component The component to be added to this container.
  210. * @param constraints The layout constraints for this component.
  211. */
  212. public void add(Component comp, Object constraints)
  213. {
  214. addImpl(comp, constraints, -1);
  215. }
  216. /**
  217. * Adds the specified component to this container at the specified index
  218. * in the component list. The layout manager will use the specified
  219. * constraints when layout out this component.
  220. *
  221. * @param component The component to be added.
  222. * @param constraints The layout constraints for this component.
  223. * @param index The index in the component list to insert this child
  224. * at, or -1 to add at the end of the list.
  225. *
  226. * @throws ArrayIndexOutOfBounds If the specified index is invalid.
  227. */
  228. public void add(Component comp, Object constraints, int index)
  229. {
  230. addImpl(comp, constraints, index);
  231. }
  232. /**
  233. * This method is called by all the <code>add()</code> methods to perform
  234. * the actual adding of the component. Subclasses who wish to perform
  235. * their own processing when a component is added should override this
  236. * method. Any subclass doing this must call the superclass version of
  237. * this method in order to ensure proper functioning of the container.
  238. *
  239. * @param component The component to be added.
  240. * @param constraints The layout constraints for this component, or
  241. * <code>null</code> if there are no constraints.
  242. * @param index The index in the component list to insert this child
  243. * at, or -1 to add at the end of the list.
  244. *
  245. * @throws ArrayIndexOutOfBounds If the specified index is invalid.
  246. */
  247. protected void addImpl(Component comp, Object constraints, int index)
  248. {
  249. synchronized (getTreeLock ())
  250. {
  251. if (index > ncomponents
  252. || (index < 0 && index != -1)
  253. || comp instanceof Window
  254. || (comp instanceof Container
  255. && ((Container) comp).isAncestorOf(this)))
  256. throw new IllegalArgumentException();
  257. // Reparent component, and make sure component is instantiated if
  258. // we are.
  259. if (comp.parent != null)
  260. comp.parent.remove(comp);
  261. comp.parent = this;
  262. if (peer != null)
  263. {
  264. comp.addNotify();
  265. if (comp.isLightweight())
  266. enableEvents(comp.eventMask);
  267. }
  268. invalidate();
  269. if (component == null)
  270. component = new Component[4]; // FIXME, better initial size?
  271. // This isn't the most efficient implementation. We could do less
  272. // copying when growing the array. It probably doesn't matter.
  273. if (ncomponents >= component.length)
  274. {
  275. int nl = component.length * 2;
  276. Component[] c = new Component[nl];
  277. System.arraycopy(component, 0, c, 0, ncomponents);
  278. component = c;
  279. }
  280. if (index == -1)
  281. component[ncomponents++] = comp;
  282. else
  283. {
  284. System.arraycopy(component, index, component, index + 1,
  285. ncomponents - index);
  286. component[index] = comp;
  287. ++ncomponents;
  288. }
  289. // Notify the layout manager.
  290. if (layoutMgr != null)
  291. {
  292. if (layoutMgr instanceof LayoutManager2)
  293. {
  294. LayoutManager2 lm2 = (LayoutManager2) layoutMgr;
  295. lm2.addLayoutComponent(comp, constraints);
  296. }
  297. else if (constraints instanceof String)
  298. layoutMgr.addLayoutComponent((String) constraints, comp);
  299. else
  300. layoutMgr.addLayoutComponent(null, comp);
  301. }
  302. // Post event to notify of adding the container.
  303. ContainerEvent ce = new ContainerEvent(this,
  304. ContainerEvent.COMPONENT_ADDED,
  305. comp);
  306. getToolkit().getSystemEventQueue().postEvent(ce);
  307. }
  308. }
  309. /**
  310. * Removes the component at the specified index from this container.
  311. *
  312. * @param index The index of the component to remove.
  313. */
  314. public void remove(int index)
  315. {
  316. synchronized (getTreeLock ())
  317. {
  318. Component r = component[index];
  319. r.removeNotify();
  320. System.arraycopy(component, index + 1, component, index,
  321. ncomponents - index - 1);
  322. component[--ncomponents] = null;
  323. invalidate();
  324. if (layoutMgr != null)
  325. layoutMgr.removeLayoutComponent(r);
  326. // Post event to notify of adding the container.
  327. ContainerEvent ce = new ContainerEvent(this,
  328. ContainerEvent.COMPONENT_REMOVED,
  329. r);
  330. getToolkit().getSystemEventQueue().postEvent(ce);
  331. }
  332. }
  333. /**
  334. * Removes the specified component from this container.
  335. *
  336. * @return component The component to remove from this container.
  337. */
  338. public void remove(Component comp)
  339. {
  340. synchronized (getTreeLock ())
  341. {
  342. for (int i = 0; i < ncomponents; ++i)
  343. {
  344. if (component[i] == comp)
  345. {
  346. remove(i);
  347. break;
  348. }
  349. }
  350. }
  351. }
  352. /**
  353. * Removes all components from this container.
  354. */
  355. public void removeAll()
  356. {
  357. synchronized (getTreeLock ())
  358. {
  359. while (ncomponents > 0)
  360. remove(0);
  361. }
  362. }
  363. /**
  364. * Returns the current layout manager for this container.
  365. *
  366. * @return The layout manager for this container.
  367. */
  368. public LayoutManager getLayout()
  369. {
  370. return layoutMgr;
  371. }
  372. /**
  373. * Sets the layout manager for this container to the specified layout
  374. * manager.
  375. *
  376. * @param mgr The new layout manager for this container.
  377. */
  378. public void setLayout(LayoutManager mgr)
  379. {
  380. layoutMgr = mgr;
  381. invalidate();
  382. }
  383. /**
  384. * Layout the components in this container.
  385. */
  386. public void doLayout()
  387. {
  388. if (layoutMgr != null)
  389. layoutMgr.layoutContainer(this);
  390. }
  391. /**
  392. * Layout the components in this container.
  393. *
  394. * @deprecated use {@link #doLayout()} instead
  395. */
  396. public void layout()
  397. {
  398. doLayout();
  399. }
  400. /**
  401. * Invalidates this container to indicate that it (and all parent
  402. * containers) need to be laid out.
  403. */
  404. public void invalidate()
  405. {
  406. super.invalidate();
  407. }
  408. /**
  409. * Re-lays out the components in this container.
  410. */
  411. public void validate()
  412. {
  413. synchronized (getTreeLock ())
  414. {
  415. if (! isValid())
  416. {
  417. validateTree();
  418. }
  419. }
  420. }
  421. /**
  422. * Recursively validates the container tree, recomputing any invalid
  423. * layouts.
  424. */
  425. protected void validateTree()
  426. {
  427. if (valid)
  428. return;
  429. ContainerPeer cPeer = null;
  430. if (peer != null && ! (peer instanceof LightweightPeer))
  431. {
  432. cPeer = (ContainerPeer) peer;
  433. cPeer.beginValidate();
  434. }
  435. doLayout();
  436. for (int i = 0; i < ncomponents; ++i)
  437. {
  438. Component comp = component[i];
  439. if (! comp.isValid())
  440. {
  441. if (comp instanceof Container)
  442. {
  443. ((Container) comp).validateTree();
  444. }
  445. else
  446. {
  447. component[i].validate();
  448. }
  449. }
  450. }
  451. /* children will call invalidate() when they are layed out. It
  452. is therefore imporant that valid is not set to true
  453. before after the children has been layed out. */
  454. valid = true;
  455. if (cPeer != null)
  456. cPeer.endValidate();
  457. }
  458. public void setFont(Font f)
  459. {
  460. super.setFont(f);
  461. // FIXME, should invalidate all children with font == null
  462. }
  463. /**
  464. * Returns the preferred size of this container.
  465. *
  466. * @return The preferred size of this container.
  467. */
  468. public Dimension getPreferredSize()
  469. {
  470. if (layoutMgr != null)
  471. return layoutMgr.preferredLayoutSize(this);
  472. else
  473. return super.getPreferredSize();
  474. }
  475. /**
  476. * Returns the preferred size of this container.
  477. *
  478. * @return The preferred size of this container.
  479. *
  480. * @deprecated use {@link #getPreferredSize()} instead
  481. */
  482. public Dimension preferredSize()
  483. {
  484. return getPreferredSize();
  485. }
  486. /**
  487. * Returns the minimum size of this container.
  488. *
  489. * @return The minimum size of this container.
  490. */
  491. public Dimension getMinimumSize()
  492. {
  493. if (layoutMgr != null)
  494. return layoutMgr.minimumLayoutSize(this);
  495. else
  496. return super.getMinimumSize();
  497. }
  498. /**
  499. * Returns the minimum size of this container.
  500. *
  501. * @return The minimum size of this container.
  502. *
  503. * @deprecated use {@link #getMinimumSize()} instead
  504. */
  505. public Dimension minimumSize()
  506. {
  507. return getMinimumSize();
  508. }
  509. /**
  510. * Returns the maximum size of this container.
  511. *
  512. * @return The maximum size of this container.
  513. */
  514. public Dimension getMaximumSize()
  515. {
  516. if (layoutMgr != null && layoutMgr instanceof LayoutManager2)
  517. {
  518. LayoutManager2 lm2 = (LayoutManager2) layoutMgr;
  519. return lm2.maximumLayoutSize(this);
  520. }
  521. else
  522. return super.getMaximumSize();
  523. }
  524. /**
  525. * Returns the preferred alignment along the X axis. This is a value
  526. * between 0 and 1 where 0 represents alignment flush left and
  527. * 1 means alignment flush right, and 0.5 means centered.
  528. *
  529. * @return The preferred alignment along the X axis.
  530. */
  531. public float getAlignmentX()
  532. {
  533. if (layoutMgr instanceof LayoutManager2)
  534. {
  535. LayoutManager2 lm2 = (LayoutManager2) layoutMgr;
  536. return lm2.getLayoutAlignmentX(this);
  537. }
  538. else
  539. return super.getAlignmentX();
  540. }
  541. /**
  542. * Returns the preferred alignment along the Y axis. This is a value
  543. * between 0 and 1 where 0 represents alignment flush top and
  544. * 1 means alignment flush bottom, and 0.5 means centered.
  545. *
  546. * @return The preferred alignment along the Y axis.
  547. */
  548. public float getAlignmentY()
  549. {
  550. if (layoutMgr instanceof LayoutManager2)
  551. {
  552. LayoutManager2 lm2 = (LayoutManager2) layoutMgr;
  553. return lm2.getLayoutAlignmentY(this);
  554. }
  555. else
  556. return super.getAlignmentY();
  557. }
  558. /**
  559. * Paints this container. The implementation of this method in this
  560. * class forwards to any lightweight components in this container. If
  561. * this method is subclassed, this method should still be invoked as
  562. * a superclass method so that lightweight components are properly
  563. * drawn.
  564. *
  565. * @param graphics The graphics context for this paint job.
  566. */
  567. public void paint(Graphics g)
  568. {
  569. if (!isShowing())
  570. return;
  571. super.paint(g);
  572. visitChildren(g, GfxPaintVisitor.INSTANCE, true);
  573. }
  574. /**
  575. * Updates this container. The implementation of this method in this
  576. * class forwards to any lightweight components in this container. If
  577. * this method is subclassed, this method should still be invoked as
  578. * a superclass method so that lightweight components are properly
  579. * drawn.
  580. *
  581. * @param graphics The graphics context for this update.
  582. */
  583. public void update(Graphics g)
  584. {
  585. super.update(g);
  586. }
  587. /**
  588. * Prints this container. The implementation of this method in this
  589. * class forwards to any lightweight components in this container. If
  590. * this method is subclassed, this method should still be invoked as
  591. * a superclass method so that lightweight components are properly
  592. * drawn.
  593. *
  594. * @param graphics The graphics context for this print job.
  595. */
  596. public void print(Graphics g)
  597. {
  598. super.print(g);
  599. visitChildren(g, GfxPrintVisitor.INSTANCE, true);
  600. }
  601. /**
  602. * Paints all of the components in this container.
  603. *
  604. * @param graphics The graphics context for this paint job.
  605. */
  606. public void paintComponents(Graphics g)
  607. {
  608. super.paint(g);
  609. visitChildren(g, GfxPaintAllVisitor.INSTANCE, true);
  610. }
  611. /**
  612. * Prints all of the components in this container.
  613. *
  614. * @param graphics The graphics context for this print job.
  615. */
  616. public void printComponents(Graphics g)
  617. {
  618. super.paint(g);
  619. visitChildren(g, GfxPrintAllVisitor.INSTANCE, true);
  620. }
  621. /**
  622. * Adds the specified container listener to this object's list of
  623. * container listeners.
  624. *
  625. * @param listener The listener to add.
  626. */
  627. public synchronized void addContainerListener(ContainerListener l)
  628. {
  629. containerListener = AWTEventMulticaster.add(containerListener, l);
  630. }
  631. /**
  632. * Removes the specified container listener from this object's list of
  633. * container listeners.
  634. *
  635. * @param listener The listener to remove.
  636. */
  637. public synchronized void removeContainerListener(ContainerListener l)
  638. {
  639. containerListener = AWTEventMulticaster.remove(containerListener, l);
  640. }
  641. /**
  642. * @since 1.4
  643. */
  644. public synchronized ContainerListener[] getContainerListeners()
  645. {
  646. return (ContainerListener[])
  647. AWTEventMulticaster.getListeners(containerListener,
  648. ContainerListener.class);
  649. }
  650. /**
  651. * Returns an array of all the objects currently registered as FooListeners
  652. * upon this Container. FooListeners are registered using the addFooListener
  653. * method.
  654. *
  655. * @since 1.3
  656. */
  657. public EventListener[] getListeners(Class listenerType)
  658. {
  659. if (listenerType == ContainerListener.class)
  660. return getContainerListeners();
  661. return super.getListeners(listenerType);
  662. }
  663. /**
  664. * Processes the specified event. This method calls
  665. * <code>processContainerEvent()</code> if this method is a
  666. * <code>ContainerEvent</code>, otherwise it calls the superclass
  667. * method.
  668. *
  669. * @param event The event to be processed.
  670. */
  671. protected void processEvent(AWTEvent e)
  672. {
  673. if (e instanceof ContainerEvent)
  674. processContainerEvent((ContainerEvent) e);
  675. else
  676. super.processEvent(e);
  677. }
  678. /**
  679. * Called when a container event occurs if container events are enabled.
  680. * This method calls any registered listeners.
  681. *
  682. * @param event The event that occurred.
  683. */
  684. protected void processContainerEvent(ContainerEvent e)
  685. {
  686. if (containerListener == null)
  687. return;
  688. switch (e.id)
  689. {
  690. case ContainerEvent.COMPONENT_ADDED:
  691. containerListener.componentAdded(e);
  692. break;
  693. case ContainerEvent.COMPONENT_REMOVED:
  694. containerListener.componentRemoved(e);
  695. break;
  696. }
  697. }
  698. /**
  699. * AWT 1.0 event processor.
  700. *
  701. * @param event The event that occurred.
  702. *
  703. * @deprecated use {@link #dispatchEvent(AWTEvent)} instead
  704. */
  705. public void deliverEvent(Event e)
  706. {
  707. }
  708. /**
  709. * Returns the component located at the specified point. This is done
  710. * by checking whether or not a child component claims to contain this
  711. * point. The first child component that does is returned. If no
  712. * child component claims the point, the container itself is returned,
  713. * unless the point does not exist within this container, in which
  714. * case <code>null</code> is returned.
  715. *
  716. * @param x The X coordinate of the point.
  717. * @param y The Y coordinate of the point.
  718. *
  719. * @return The component containing the specified point, or
  720. * <code>null</code> if there is no such point.
  721. */
  722. public Component getComponentAt(int x, int y)
  723. {
  724. synchronized (getTreeLock ())
  725. {
  726. if (! contains(x, y))
  727. return null;
  728. for (int i = 0; i < ncomponents; ++i)
  729. {
  730. // Ignore invisible children...
  731. if (!component[i].isVisible())
  732. continue;
  733. int x2 = x - component[i].x;
  734. int y2 = y - component[i].y;
  735. if (component[i].contains(x2, y2))
  736. return component[i];
  737. }
  738. return this;
  739. }
  740. }
  741. /**
  742. * Returns the component located at the specified point. This is done
  743. * by checking whether or not a child component claims to contain this
  744. * point. The first child component that does is returned. If no
  745. * child component claims the point, the container itself is returned,
  746. * unless the point does not exist within this container, in which
  747. * case <code>null</code> is returned.
  748. *
  749. * @param point The point to return the component at.
  750. *
  751. * @return The component containing the specified point, or <code>null</code>
  752. * if there is no such point.
  753. *
  754. * @deprecated use {@link #getComponentAt(int, int)} instead
  755. */
  756. public Component locate(int x, int y)
  757. {
  758. return getComponentAt(x, y);
  759. }
  760. /**
  761. * Returns the component located at the specified point. This is done
  762. * by checking whether or not a child component claims to contain this
  763. * point. The first child component that does is returned. If no
  764. * child component claims the point, the container itself is returned,
  765. * unless the point does not exist within this container, in which
  766. * case <code>null</code> is returned.
  767. *
  768. * @param point The point to return the component at.
  769. * @return The component containing the specified point, or <code>null</code>
  770. * if there is no such point.
  771. */
  772. public Component getComponentAt(Point p)
  773. {
  774. return getComponentAt(p.x, p.y);
  775. }
  776. public Component findComponentAt(int x, int y)
  777. {
  778. synchronized (getTreeLock ())
  779. {
  780. if (! contains(x, y))
  781. return null;
  782. for (int i = 0; i < ncomponents; ++i)
  783. {
  784. // Ignore invisible children...
  785. if (!component[i].isVisible())
  786. continue;
  787. int x2 = x - component[i].x;
  788. int y2 = y - component[i].y;
  789. // We don't do the contains() check right away because
  790. // findComponentAt would redundantly do it first thing.
  791. if (component[i] instanceof Container)
  792. {
  793. Container k = (Container) component[i];
  794. Component r = k.findComponentAt(x2, y2);
  795. if (r != null)
  796. return r;
  797. }
  798. else if (component[i].contains(x2, y2))
  799. return component[i];
  800. }
  801. return this;
  802. }
  803. }
  804. public Component findComponentAt(Point p)
  805. {
  806. return findComponentAt(p.x, p.y);
  807. }
  808. /**
  809. * Called when this container is added to another container to inform it
  810. * to create its peer. Peers for any child components will also be
  811. * created.
  812. */
  813. public void addNotify()
  814. {
  815. addNotifyContainerChildren();
  816. super.addNotify();
  817. }
  818. /**
  819. * Called when this container is removed from its parent container to
  820. * inform it to destroy its peer. This causes the peers of all child
  821. * component to be destroyed as well.
  822. */
  823. public void removeNotify()
  824. {
  825. synchronized (getTreeLock ())
  826. {
  827. for (int i = 0; i < ncomponents; ++i)
  828. component[i].removeNotify();
  829. super.removeNotify();
  830. }
  831. }
  832. /**
  833. * Tests whether or not the specified component is contained within
  834. * this components subtree.
  835. *
  836. * @param component The component to test.
  837. *
  838. * @return <code>true</code> if this container is an ancestor of the
  839. * specified component, <code>false</code> otherwise.
  840. */
  841. public boolean isAncestorOf(Component comp)
  842. {
  843. synchronized (getTreeLock ())
  844. {
  845. while (true)
  846. {
  847. if (comp == null)
  848. return false;
  849. if (comp == this)
  850. return true;
  851. comp = comp.getParent();
  852. }
  853. }
  854. }
  855. /**
  856. * Returns a string representing the state of this container for
  857. * debugging purposes.
  858. *
  859. * @return A string representing the state of this container.
  860. */
  861. protected String paramString()
  862. {
  863. String param = super.paramString();
  864. if (layoutMgr != null)
  865. param = param + "," + layoutMgr.getClass().getName();
  866. return param;
  867. }
  868. /**
  869. * Writes a listing of this container to the specified stream starting
  870. * at the specified indentation point.
  871. *
  872. * @param stream The <code>PrintStream</code> to write to.
  873. * @param indent The indentation point.
  874. */
  875. public void list(PrintStream out, int indent)
  876. {
  877. synchronized (getTreeLock ())
  878. {
  879. super.list(out, indent);
  880. for (int i = 0; i < ncomponents; ++i)
  881. component[i].list(out, indent + 2);
  882. }
  883. }
  884. /**
  885. * Writes a listing of this container to the specified stream starting
  886. * at the specified indentation point.
  887. *
  888. * @param stream The <code>PrintWriter</code> to write to.
  889. * @param indent The indentation point.
  890. */
  891. public void list(PrintWriter out, int indent)
  892. {
  893. synchronized (getTreeLock ())
  894. {
  895. super.list(out, indent);
  896. for (int i = 0; i < ncomponents; ++i)
  897. component[i].list(out, indent + 2);
  898. }
  899. }
  900. public void setFocusTraversalKeys(int id, Set keys)
  901. {
  902. if (id != KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS &&
  903. id != KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS &&
  904. id != KeyboardFocusManager.UP_CYCLE_TRAVERSAL_KEYS &&
  905. id != KeyboardFocusManager.DOWN_CYCLE_TRAVERSAL_KEYS)
  906. throw new IllegalArgumentException ();
  907. }
  908. public Set getFocusTraversalKeys(int id)
  909. {
  910. if (id != KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS &&
  911. id != KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS &&
  912. id != KeyboardFocusManager.UP_CYCLE_TRAVERSAL_KEYS &&
  913. id != KeyboardFocusManager.DOWN_CYCLE_TRAVERSAL_KEYS)
  914. throw new IllegalArgumentException ();
  915. return null;
  916. }
  917. public boolean areFocusTraversalKeysSet(int id)
  918. {
  919. if (id != KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS &&
  920. id != KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS &&
  921. id != KeyboardFocusManager.UP_CYCLE_TRAVERSAL_KEYS &&
  922. id != KeyboardFocusManager.DOWN_CYCLE_TRAVERSAL_KEYS)
  923. throw new IllegalArgumentException ();
  924. return false;
  925. }
  926. public boolean isFocusCycleRoot(Container c)
  927. {
  928. return false;
  929. }
  930. public void transferFocusBackward()
  931. {
  932. }
  933. public void setFocusTraversalPolicy(FocusTraversalPolicy policy)
  934. {
  935. }
  936. public FocusTraversalPolicy getFocusTraversalPolicy()
  937. {
  938. return null;
  939. }
  940. public boolean isFocusTraversalPolicySet()
  941. {
  942. return false;
  943. }
  944. public void setFocusCycleRoot(boolean focusCycleRoot)
  945. {
  946. }
  947. public boolean isFocusCycleRoot()
  948. {
  949. return false;
  950. }
  951. public void transferFocusDownCycle()
  952. {
  953. }
  954. public void applyComponentOrientation(ComponentOrientation o)
  955. {
  956. if (orientation == null)
  957. throw new NullPointerException ();
  958. }
  959. public void addPropertyChangeListener(PropertyChangeListener l)
  960. {
  961. }
  962. public void addPropertyChangeListener(String name, PropertyChangeListener l)
  963. {
  964. }
  965. // Hidden helper methods.
  966. /**
  967. * Perform a graphics operation on the children of this container.
  968. * For each applicable child, the visitChild() method will be called
  969. * to perform the graphics operation.
  970. *
  971. * @param gfx The graphics object that will be used to derive new
  972. * graphics objects for the children.
  973. *
  974. * @param visitor Object encapsulating the graphics operation that
  975. * should be performed.
  976. *
  977. * @param lightweightOnly If true, only lightweight components will
  978. * be visited.
  979. */
  980. private void visitChildren(Graphics gfx, GfxVisitor visitor,
  981. boolean lightweightOnly)
  982. {
  983. synchronized (getTreeLock ())
  984. {
  985. for (int i = 0; i < ncomponents; ++i)
  986. {
  987. Component comp = component[i];
  988. boolean applicable = comp.isVisible()
  989. && (comp.isLightweight() || !lightweightOnly);
  990. if (applicable)
  991. visitChild(gfx, visitor, comp);
  992. }
  993. }
  994. }
  995. /**
  996. * Perform a graphics operation on a child. A translated and clipped
  997. * graphics object will be created, and the visit() method of the
  998. * visitor will be called to perform the operation.
  999. *
  1000. * @param gfx The graphics object that will be used to derive new
  1001. * graphics objects for the child.
  1002. *
  1003. * @param visitor Object encapsulating the graphics operation that
  1004. * should be performed.
  1005. *
  1006. * @param comp The child component that should be visited.
  1007. */
  1008. private void visitChild(Graphics gfx, GfxVisitor visitor,
  1009. Component comp)
  1010. {
  1011. Rectangle bounds = comp.getBounds();
  1012. Rectangle clip = gfx.getClipBounds().intersection(bounds);
  1013. if (clip.isEmpty()) return;
  1014. Graphics gfx2 = gfx.create();
  1015. gfx2.setClip(clip.x, clip.y, clip.width, clip.height);
  1016. gfx2.translate(bounds.x, bounds.y);
  1017. visitor.visit(comp, gfx2);
  1018. }
  1019. void dispatchEventImpl(AWTEvent e)
  1020. {
  1021. if ((e.id <= ContainerEvent.CONTAINER_LAST
  1022. && e.id >= ContainerEvent.CONTAINER_FIRST)
  1023. && (containerListener != null
  1024. || (eventMask & AWTEvent.CONTAINER_EVENT_MASK) != 0))
  1025. processEvent(e);
  1026. else
  1027. super.dispatchEventImpl(e);
  1028. }
  1029. // This is used to implement Component.transferFocus.
  1030. Component findNextFocusComponent(Component child)
  1031. {
  1032. synchronized (getTreeLock ())
  1033. {
  1034. int start, end;
  1035. if (child != null)
  1036. {
  1037. for (start = 0; start < ncomponents; ++start)
  1038. {
  1039. if (component[start] == child)
  1040. break;
  1041. }
  1042. end = start;
  1043. // This special case lets us be sure to terminate.
  1044. if (end == 0)
  1045. end = ncomponents;
  1046. ++start;
  1047. }
  1048. else
  1049. {
  1050. start = 0;
  1051. end = ncomponents;
  1052. }
  1053. for (int j = start; j != end; ++j)
  1054. {
  1055. if (j >= ncomponents)
  1056. {
  1057. // The JCL says that we should wrap here. However, that
  1058. // seems wrong. To me it seems that focus order should be
  1059. // global within in given window. So instead if we reach
  1060. // the end we try to look in our parent, if we have one.
  1061. if (parent != null)
  1062. return parent.findNextFocusComponent(this);
  1063. j -= ncomponents;
  1064. }
  1065. if (component[j] instanceof Container)
  1066. {
  1067. Component c = component[j];
  1068. c = c.findNextFocusComponent(null);
  1069. if (c != null)
  1070. return c;
  1071. }
  1072. else if (component[j].isFocusTraversable())
  1073. return component[j];
  1074. }
  1075. return null;
  1076. }
  1077. }
  1078. private void addNotifyContainerChildren()
  1079. {
  1080. synchronized (getTreeLock ())
  1081. {
  1082. for (int i = ncomponents; --i >= 0; )
  1083. {
  1084. component[i].addNotify();
  1085. if (component[i].isLightweight())
  1086. enableEvents(component[i].eventMask);
  1087. }
  1088. }
  1089. }
  1090. // Nested classes.
  1091. /* The following classes are used in concert with the
  1092. visitChildren() method to implement all the graphics operations
  1093. that requires traversal of the containment hierarchy. */
  1094. abstract static class GfxVisitor
  1095. {
  1096. public abstract void visit(Component c, Graphics gfx);
  1097. }
  1098. static class GfxPaintVisitor extends GfxVisitor
  1099. {
  1100. public void visit(Component c, Graphics gfx) { c.paint(gfx); }
  1101. public static final GfxVisitor INSTANCE = new GfxPaintVisitor();
  1102. }
  1103. static class GfxPrintVisitor extends GfxVisitor
  1104. {
  1105. public void visit(Component c, Graphics gfx) { c.print(gfx); }
  1106. public static final GfxVisitor INSTANCE = new GfxPrintVisitor();
  1107. }
  1108. static class GfxPaintAllVisitor extends GfxVisitor
  1109. {
  1110. public void visit(Component c, Graphics gfx) { c.paintAll(gfx); }
  1111. public static final GfxVisitor INSTANCE = new GfxPaintAllVisitor();
  1112. }
  1113. static class GfxPrintAllVisitor extends GfxVisitor
  1114. {
  1115. public void visit(Component c, Graphics gfx) { c.printAll(gfx); }
  1116. public static final GfxVisitor INSTANCE = new GfxPrintAllVisitor();
  1117. }
  1118. /**
  1119. * This class provides accessibility support for subclasses of container.
  1120. *
  1121. * @author Eric Blake <ebb9@email.byu.edu>
  1122. *
  1123. * @since 1.3
  1124. */
  1125. protected class AccessibleAWTContainer extends AccessibleAWTComponent
  1126. {
  1127. /**
  1128. * Compatible with JDK 1.4+.
  1129. */
  1130. private static final long serialVersionUID = 5081320404842566097L;
  1131. /**
  1132. * The handler to fire PropertyChange when children are added or removed.
  1133. *
  1134. * @serial the handler for property changes
  1135. */
  1136. protected ContainerListener accessibleContainerHandler
  1137. = new AccessibleContainerHandler();
  1138. /**
  1139. * The default constructor.
  1140. */
  1141. protected AccessibleAWTContainer()
  1142. {
  1143. Container.this.addContainerListener(accessibleContainerHandler);
  1144. }
  1145. /**
  1146. * Return the number of accessible children of the containing accessible
  1147. * object (at most the total number of its children).
  1148. *
  1149. * @return the number of accessible children
  1150. */
  1151. public int getAccessibleChildrenCount()
  1152. {
  1153. synchronized (getTreeLock ())
  1154. {
  1155. int count = 0;
  1156. int i = component == null ? 0 : component.length;
  1157. while (--i >= 0)
  1158. if (component[i] instanceof Accessible)
  1159. count++;
  1160. return count;
  1161. }
  1162. }
  1163. /**
  1164. * Return the nth accessible child of the containing accessible object.
  1165. *
  1166. * @param i the child to grab, zero-based
  1167. * @return the accessible child, or null
  1168. */
  1169. public Accessible getAccessibleChild(int i)
  1170. {
  1171. synchronized (getTreeLock ())
  1172. {
  1173. if (component == null)
  1174. return null;
  1175. int index = -1;
  1176. while (i >= 0 && ++index < component.length)
  1177. if (component[index] instanceof Accessible)
  1178. i--;
  1179. if (i < 0)
  1180. return (Accessible) component[index];
  1181. return null;
  1182. }
  1183. }
  1184. /**
  1185. * Return the accessible child located at point (in the parent's
  1186. * coordinates), if one exists.
  1187. *
  1188. * @param p the point to look at
  1189. *
  1190. * @return an accessible object at that point, or null
  1191. *
  1192. * @throws NullPointerException if p is null
  1193. */
  1194. public Accessible getAccessibleAt(Point p)
  1195. {
  1196. Component c = getComponentAt(p.x, p.y);
  1197. return c != Container.this && c instanceof Accessible ? (Accessible) c
  1198. : null;
  1199. }
  1200. /**
  1201. * This class fires a <code>PropertyChange</code> listener, if registered,
  1202. * when children are added or removed from the enclosing accessible object.
  1203. *
  1204. * @author Eric Blake <ebb9@email.byu.edu>
  1205. *
  1206. * @since 1.3
  1207. */
  1208. protected class AccessibleContainerHandler implements ContainerListener
  1209. {
  1210. /**
  1211. * Default constructor.
  1212. */
  1213. protected AccessibleContainerHandler()
  1214. {
  1215. }
  1216. /**
  1217. * Fired when a component is added; forwards to the PropertyChange
  1218. * listener.
  1219. *
  1220. * @param e the container event for adding
  1221. */
  1222. public void componentAdded(ContainerEvent e)
  1223. {
  1224. AccessibleAWTContainer.this.firePropertyChange
  1225. (ACCESSIBLE_CHILD_PROPERTY, null, e.getChild());
  1226. }
  1227. /**
  1228. * Fired when a component is removed; forwards to the PropertyChange
  1229. * listener.
  1230. *
  1231. * @param e the container event for removing
  1232. */
  1233. public void componentRemoved(ContainerEvent e)
  1234. {
  1235. AccessibleAWTContainer.this.firePropertyChange
  1236. (ACCESSIBLE_CHILD_PROPERTY, e.getChild(), null);
  1237. }
  1238. } // class AccessibleContainerHandler
  1239. } // class AccessibleAWTPanel
  1240. } // class Container
  1241. /**
  1242. * Undocumented helper class.
  1243. * STUBBED
  1244. */
  1245. class LightweightDispatcher implements Serializable, AWTEventListener
  1246. {
  1247. private static final long serialVersionUID = 5184291520170872969L;
  1248. private Container nativeContainer;
  1249. private Component focus;
  1250. private transient Component mouseEventTarget;
  1251. private transient Component targetLastEntered;
  1252. private transient boolean isMouseInNativeContainer;
  1253. private Cursor nativeCursor;
  1254. private long eventMask;
  1255. LightweightDispatcher(Container c)
  1256. {
  1257. }
  1258. void dispose()
  1259. {
  1260. }
  1261. void enableEvents(long l)
  1262. {
  1263. }
  1264. boolean dispatchEvent(AWTEvent e)
  1265. {
  1266. return true;
  1267. }
  1268. boolean isMouseGrab(MouseEvent e)
  1269. {
  1270. return true;
  1271. }
  1272. boolean processMouseEvent(MouseEvent e)
  1273. {
  1274. return true;
  1275. }
  1276. void trackMouseEnterExit(Component c, MouseEvent e)
  1277. {
  1278. }
  1279. void startListeningForOtherDrags()
  1280. {
  1281. }
  1282. void stopListeningForOtherDrags()
  1283. {
  1284. }
  1285. public void eventDispatched(AWTEvent e)
  1286. {
  1287. }
  1288. void retargetMouseEvent(Component c, int i, MouseEvent e)
  1289. {
  1290. }
  1291. } // class LightweightDispatcher