PageRenderTime 58ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/java/awt/Container.java

https://github.com/penberg/classpath
Java | 1609 lines | 819 code | 138 blank | 652 comment | 215 complexity | d6d2a9f379dc4956ca3c17bc97e03079 MD5 | raw file
  1. /* Container.java -- parent container class in AWT
  2. Copyright (C) 1999, 2000, 2002, 2003, 2004, 2005, 2006
  3. Free Software Foundation
  4. This file is part of GNU Classpath.
  5. GNU Classpath is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2, or (at your option)
  8. any later version.
  9. GNU Classpath is distributed in the hope that it will be useful, but
  10. WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with GNU Classpath; see the file COPYING. If not, write to the
  15. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  16. 02110-1301 USA.
  17. Linking this library statically or dynamically with other modules is
  18. making a combined work based on this library. Thus, the terms and
  19. conditions of the GNU General Public License cover the whole
  20. combination.
  21. As a special exception, the copyright holders of this library give you
  22. permission to link this library with independent modules to produce an
  23. executable, regardless of the license terms of these independent
  24. modules, and to copy and distribute the resulting executable under
  25. terms of your choice, provided that you also meet, for each linked
  26. independent module, the terms and conditions of the license of that
  27. module. An independent module is a module which is not derived from
  28. or based on this library. If you modify this library, you may extend
  29. this exception to your version of the library, but you are not
  30. obligated to do so. If you do not wish to do so, delete this
  31. exception statement from your version. */
  32. package java.awt;
  33. import gnu.java.lang.CPStringBuilder;
  34. import java.awt.event.ContainerEvent;
  35. import java.awt.event.ContainerListener;
  36. import java.awt.event.HierarchyEvent;
  37. import java.awt.event.KeyEvent;
  38. import java.awt.event.MouseEvent;
  39. import java.awt.peer.ComponentPeer;
  40. import java.awt.peer.ContainerPeer;
  41. import java.awt.peer.LightweightPeer;
  42. import java.beans.PropertyChangeListener;
  43. import java.io.IOException;
  44. import java.io.ObjectInputStream;
  45. import java.io.ObjectOutputStream;
  46. import java.io.PrintStream;
  47. import java.io.PrintWriter;
  48. import java.io.Serializable;
  49. import java.util.Collections;
  50. import java.util.EventListener;
  51. import java.util.HashSet;
  52. import java.util.Iterator;
  53. import java.util.Set;
  54. import javax.accessibility.Accessible;
  55. /**
  56. * A generic window toolkit object that acts as a container for other objects.
  57. * Components are tracked in a list, and new elements are at the end of the
  58. * list or bottom of the stacking order.
  59. *
  60. * @author original author unknown
  61. * @author Eric Blake (ebb9@email.byu.edu)
  62. * @author Andrew John Hughes (gnu_andrew@member.fsf.org)
  63. *
  64. * @since 1.0
  65. *
  66. * @status still missing 1.4 support, some generics from 1.5
  67. */
  68. public class Container extends Component
  69. {
  70. /**
  71. * Compatible with JDK 1.0+.
  72. */
  73. private static final long serialVersionUID = 4613797578919906343L;
  74. /* Serialized fields from the serialization spec. */
  75. int ncomponents;
  76. Component[] component;
  77. LayoutManager layoutMgr;
  78. /**
  79. * @since 1.4
  80. */
  81. boolean focusCycleRoot;
  82. /**
  83. * Indicates if this container provides a focus traversal policy.
  84. *
  85. * @since 1.5
  86. */
  87. private boolean focusTraversalPolicyProvider;
  88. int containerSerializedDataVersion;
  89. /* Anything else is non-serializable, and should be declared "transient". */
  90. transient ContainerListener containerListener;
  91. /** The focus traversal policy that determines how focus is
  92. transferred between this Container and its children. */
  93. private FocusTraversalPolicy focusTraversalPolicy;
  94. /**
  95. * The focus traversal keys, if not inherited from the parent or default
  96. * keyboard manager. These sets will contain only AWTKeyStrokes that
  97. * represent press and release events to use as focus control.
  98. *
  99. * @see #getFocusTraversalKeys(int)
  100. * @see #setFocusTraversalKeys(int, Set)
  101. * @since 1.4
  102. */
  103. transient Set[] focusTraversalKeys;
  104. /**
  105. * Default constructor for subclasses.
  106. */
  107. public Container()
  108. {
  109. // Nothing to do here.
  110. }
  111. /**
  112. * Returns the number of components in this container.
  113. *
  114. * @return The number of components in this container.
  115. */
  116. public int getComponentCount()
  117. {
  118. return countComponents ();
  119. }
  120. /**
  121. * Returns the number of components in this container.
  122. *
  123. * @return The number of components in this container.
  124. *
  125. * @deprecated use {@link #getComponentCount()} instead
  126. */
  127. public int countComponents()
  128. {
  129. return ncomponents;
  130. }
  131. /**
  132. * Returns the component at the specified index.
  133. *
  134. * @param n The index of the component to retrieve.
  135. *
  136. * @return The requested component.
  137. *
  138. * @throws ArrayIndexOutOfBoundsException If the specified index is invalid
  139. */
  140. public Component getComponent(int n)
  141. {
  142. synchronized (getTreeLock ())
  143. {
  144. if (n < 0 || n >= ncomponents)
  145. throw new ArrayIndexOutOfBoundsException("no such component");
  146. return component[n];
  147. }
  148. }
  149. /**
  150. * Returns an array of the components in this container.
  151. *
  152. * @return The components in this container.
  153. */
  154. public Component[] getComponents()
  155. {
  156. synchronized (getTreeLock ())
  157. {
  158. Component[] result = new Component[ncomponents];
  159. if (ncomponents > 0)
  160. System.arraycopy(component, 0, result, 0, ncomponents);
  161. return result;
  162. }
  163. }
  164. /**
  165. * Returns the insets for this container, which is the space used for
  166. * borders, the margin, etc.
  167. *
  168. * @return The insets for this container.
  169. */
  170. public Insets getInsets()
  171. {
  172. return insets ();
  173. }
  174. /**
  175. * Returns the insets for this container, which is the space used for
  176. * borders, the margin, etc.
  177. *
  178. * @return The insets for this container.
  179. * @deprecated use {@link #getInsets()} instead
  180. */
  181. public Insets insets()
  182. {
  183. Insets i;
  184. if (peer == null || peer instanceof LightweightPeer)
  185. i = new Insets (0, 0, 0, 0);
  186. else
  187. i = ((ContainerPeer) peer).getInsets ();
  188. return i;
  189. }
  190. /**
  191. * Adds the specified component to this container at the end of the
  192. * component list.
  193. *
  194. * @param comp The component to add to the container.
  195. *
  196. * @return The same component that was added.
  197. */
  198. public Component add(Component comp)
  199. {
  200. addImpl(comp, null, -1);
  201. return comp;
  202. }
  203. /**
  204. * Adds the specified component to the container at the end of the
  205. * component list. This method should not be used. Instead, use
  206. * <code>add(Component, Object)</code>.
  207. *
  208. * @param name The name of the component to be added.
  209. * @param comp The component to be added.
  210. *
  211. * @return The same component that was added.
  212. *
  213. * @see #add(Component,Object)
  214. */
  215. public Component add(String name, Component comp)
  216. {
  217. addImpl(comp, name, -1);
  218. return comp;
  219. }
  220. /**
  221. * Adds the specified component to this container at the specified index
  222. * in the component list.
  223. *
  224. * @param comp The component to be added.
  225. * @param index The index in the component list to insert this child
  226. * at, or -1 to add at the end of the list.
  227. *
  228. * @return The same component that was added.
  229. *
  230. * @throws ArrayIndexOutOfBoundsException If the specified index is invalid.
  231. */
  232. public Component add(Component comp, int index)
  233. {
  234. addImpl(comp, null, index);
  235. return comp;
  236. }
  237. /**
  238. * Adds the specified component to this container at the end of the
  239. * component list. The layout manager will use the specified constraints
  240. * when laying out this component.
  241. *
  242. * @param comp The component to be added to this container.
  243. * @param constraints The layout constraints for this component.
  244. */
  245. public void add(Component comp, Object constraints)
  246. {
  247. addImpl(comp, constraints, -1);
  248. }
  249. /**
  250. * Adds the specified component to this container at the specified index
  251. * in the component list. The layout manager will use the specified
  252. * constraints when layout out this component.
  253. *
  254. * @param comp The component to be added.
  255. * @param constraints The layout constraints for this component.
  256. * @param index The index in the component list to insert this child
  257. * at, or -1 to add at the end of the list.
  258. *
  259. * @throws ArrayIndexOutOfBoundsException If the specified index is invalid.
  260. */
  261. public void add(Component comp, Object constraints, int index)
  262. {
  263. addImpl(comp, constraints, index);
  264. }
  265. /**
  266. * This method is called by all the <code>add()</code> methods to perform
  267. * the actual adding of the component. Subclasses who wish to perform
  268. * their own processing when a component is added should override this
  269. * method. Any subclass doing this must call the superclass version of
  270. * this method in order to ensure proper functioning of the container.
  271. *
  272. * @param comp The component to be added.
  273. * @param constraints The layout constraints for this component, or
  274. * <code>null</code> if there are no constraints.
  275. * @param index The index in the component list to insert this child
  276. * at, or -1 to add at the end of the list.
  277. *
  278. * @throws ArrayIndexOutOfBoundsException If the specified index is invalid.
  279. */
  280. protected void addImpl(Component comp, Object constraints, int index)
  281. {
  282. synchronized (getTreeLock ())
  283. {
  284. if (index > ncomponents
  285. || (index < 0 && index != -1)
  286. || comp instanceof Window
  287. || (comp instanceof Container
  288. && ((Container) comp).isAncestorOf(this)))
  289. throw new IllegalArgumentException();
  290. // Reparent component, and make sure component is instantiated if
  291. // we are.
  292. if (comp.parent != null)
  293. comp.parent.remove(comp);
  294. if (component == null)
  295. component = new Component[4]; // FIXME, better initial size?
  296. // This isn't the most efficient implementation. We could do less
  297. // copying when growing the array. It probably doesn't matter.
  298. if (ncomponents >= component.length)
  299. {
  300. int nl = component.length * 2;
  301. Component[] c = new Component[nl];
  302. System.arraycopy(component, 0, c, 0, ncomponents);
  303. component = c;
  304. }
  305. if (index == -1)
  306. component[ncomponents++] = comp;
  307. else
  308. {
  309. System.arraycopy(component, index, component, index + 1,
  310. ncomponents - index);
  311. component[index] = comp;
  312. ++ncomponents;
  313. }
  314. // Give the new component a parent.
  315. comp.parent = this;
  316. // Update the counter for Hierarchy(Bounds)Listeners.
  317. int childHierarchyListeners = comp.numHierarchyListeners;
  318. if (childHierarchyListeners > 0)
  319. updateHierarchyListenerCount(AWTEvent.HIERARCHY_EVENT_MASK,
  320. childHierarchyListeners);
  321. int childHierarchyBoundsListeners = comp.numHierarchyBoundsListeners;
  322. if (childHierarchyBoundsListeners > 0)
  323. updateHierarchyListenerCount(AWTEvent.HIERARCHY_BOUNDS_EVENT_MASK,
  324. childHierarchyListeners);
  325. // Invalidate the layout of this container.
  326. if (valid)
  327. invalidate();
  328. // Create the peer _after_ the component has been added, so that
  329. // the peer gets to know about the component hierarchy.
  330. if (peer != null)
  331. {
  332. // Notify the component that it has a new parent.
  333. comp.addNotify();
  334. }
  335. // Notify the layout manager.
  336. if (layoutMgr != null)
  337. {
  338. // If we have a LayoutManager2 the constraints are "real",
  339. // otherwise they are the "name" of the Component to add.
  340. if (layoutMgr instanceof LayoutManager2)
  341. {
  342. LayoutManager2 lm2 = (LayoutManager2) layoutMgr;
  343. lm2.addLayoutComponent(comp, constraints);
  344. }
  345. else if (constraints instanceof String)
  346. layoutMgr.addLayoutComponent((String) constraints, comp);
  347. else
  348. layoutMgr.addLayoutComponent("", comp);
  349. }
  350. // We previously only sent an event when this container is showing.
  351. // Also, the event was posted to the event queue. A Mauve test shows
  352. // that this event is not delivered using the event queue and it is
  353. // also sent when the container is not showing.
  354. if (containerListener != null
  355. || (eventMask & AWTEvent.CONTAINER_EVENT_MASK) != 0)
  356. {
  357. ContainerEvent ce = new ContainerEvent(this,
  358. ContainerEvent.COMPONENT_ADDED,
  359. comp);
  360. dispatchEvent(ce);
  361. }
  362. // Notify hierarchy listeners.
  363. comp.fireHierarchyEvent(HierarchyEvent.HIERARCHY_CHANGED, comp,
  364. this, HierarchyEvent.PARENT_CHANGED);
  365. }
  366. }
  367. /**
  368. * Removes the component at the specified index from this container.
  369. *
  370. * @param index The index of the component to remove.
  371. */
  372. public void remove(int index)
  373. {
  374. synchronized (getTreeLock ())
  375. {
  376. if (index < 0 || index >= ncomponents)
  377. throw new ArrayIndexOutOfBoundsException();
  378. Component r = component[index];
  379. if (peer != null)
  380. r.removeNotify();
  381. if (layoutMgr != null)
  382. layoutMgr.removeLayoutComponent(r);
  383. // Update the counter for Hierarchy(Bounds)Listeners.
  384. int childHierarchyListeners = r.numHierarchyListeners;
  385. if (childHierarchyListeners > 0)
  386. updateHierarchyListenerCount(AWTEvent.HIERARCHY_EVENT_MASK,
  387. -childHierarchyListeners);
  388. int childHierarchyBoundsListeners = r.numHierarchyBoundsListeners;
  389. if (childHierarchyBoundsListeners > 0)
  390. updateHierarchyListenerCount(AWTEvent.HIERARCHY_BOUNDS_EVENT_MASK,
  391. -childHierarchyListeners);
  392. r.parent = null;
  393. System.arraycopy(component, index + 1, component, index,
  394. ncomponents - index - 1);
  395. component[--ncomponents] = null;
  396. if (valid)
  397. invalidate();
  398. if (containerListener != null
  399. || (eventMask & AWTEvent.CONTAINER_EVENT_MASK) != 0)
  400. {
  401. // Post event to notify of removing the component.
  402. ContainerEvent ce = new ContainerEvent(this,
  403. ContainerEvent.COMPONENT_REMOVED,
  404. r);
  405. dispatchEvent(ce);
  406. }
  407. // Notify hierarchy listeners.
  408. r.fireHierarchyEvent(HierarchyEvent.HIERARCHY_CHANGED, r,
  409. this, HierarchyEvent.PARENT_CHANGED);
  410. }
  411. }
  412. /**
  413. * Removes the specified component from this container.
  414. *
  415. * @param comp The component to remove from this container.
  416. */
  417. public void remove(Component comp)
  418. {
  419. synchronized (getTreeLock ())
  420. {
  421. for (int i = 0; i < ncomponents; ++i)
  422. {
  423. if (component[i] == comp)
  424. {
  425. remove(i);
  426. break;
  427. }
  428. }
  429. }
  430. }
  431. /**
  432. * Removes all components from this container.
  433. */
  434. public void removeAll()
  435. {
  436. synchronized (getTreeLock ())
  437. {
  438. // In order to allow the same bad tricks to be used as in RI
  439. // this code has to stay exactly that way: In a real-life app
  440. // a Container subclass implemented its own vector for
  441. // subcomponents, supplied additional addXYZ() methods
  442. // and overrode remove(int) and removeAll (the latter calling
  443. // super.removeAll() ).
  444. // By doing it this way, user code cannot prevent the correct
  445. // removal of components.
  446. while (ncomponents > 0)
  447. {
  448. ncomponents--;
  449. Component r = component[ncomponents];
  450. component[ncomponents] = null;
  451. if (peer != null)
  452. r.removeNotify();
  453. if (layoutMgr != null)
  454. layoutMgr.removeLayoutComponent(r);
  455. r.parent = null;
  456. // Send ContainerEvent if necessary.
  457. if (containerListener != null
  458. || (eventMask & AWTEvent.CONTAINER_EVENT_MASK) != 0)
  459. {
  460. // Post event to notify of removing the component.
  461. ContainerEvent ce
  462. = new ContainerEvent(this,
  463. ContainerEvent.COMPONENT_REMOVED,
  464. r);
  465. dispatchEvent(ce);
  466. }
  467. // Update the counter for Hierarchy(Bounds)Listeners.
  468. int childHierarchyListeners = r.numHierarchyListeners;
  469. if (childHierarchyListeners > 0)
  470. updateHierarchyListenerCount(AWTEvent.HIERARCHY_EVENT_MASK,
  471. -childHierarchyListeners);
  472. int childHierarchyBoundsListeners = r.numHierarchyBoundsListeners;
  473. if (childHierarchyBoundsListeners > 0)
  474. updateHierarchyListenerCount(AWTEvent.HIERARCHY_BOUNDS_EVENT_MASK,
  475. -childHierarchyListeners);
  476. // Send HierarchyEvent if necessary.
  477. fireHierarchyEvent(HierarchyEvent.HIERARCHY_CHANGED, r, this,
  478. HierarchyEvent.PARENT_CHANGED);
  479. }
  480. if (valid)
  481. invalidate();
  482. }
  483. }
  484. /**
  485. * Returns the current layout manager for this container.
  486. *
  487. * @return The layout manager for this container.
  488. */
  489. public LayoutManager getLayout()
  490. {
  491. return layoutMgr;
  492. }
  493. /**
  494. * Sets the layout manager for this container to the specified layout
  495. * manager.
  496. *
  497. * @param mgr The new layout manager for this container.
  498. */
  499. public void setLayout(LayoutManager mgr)
  500. {
  501. layoutMgr = mgr;
  502. if (valid)
  503. invalidate();
  504. }
  505. /**
  506. * Layout the components in this container.
  507. */
  508. public void doLayout()
  509. {
  510. layout ();
  511. }
  512. /**
  513. * Layout the components in this container.
  514. *
  515. * @deprecated use {@link #doLayout()} instead
  516. */
  517. public void layout()
  518. {
  519. if (layoutMgr != null)
  520. layoutMgr.layoutContainer (this);
  521. }
  522. /**
  523. * Invalidates this container to indicate that it (and all parent
  524. * containers) need to be laid out.
  525. */
  526. public void invalidate()
  527. {
  528. super.invalidate();
  529. if (layoutMgr != null && layoutMgr instanceof LayoutManager2)
  530. {
  531. LayoutManager2 lm2 = (LayoutManager2) layoutMgr;
  532. lm2.invalidateLayout(this);
  533. }
  534. }
  535. /**
  536. * Re-lays out the components in this container.
  537. */
  538. public void validate()
  539. {
  540. ComponentPeer p = peer;
  541. if (! valid && p != null)
  542. {
  543. ContainerPeer cPeer = null;
  544. if (p instanceof ContainerPeer)
  545. cPeer = (ContainerPeer) peer;
  546. synchronized (getTreeLock ())
  547. {
  548. if (cPeer != null)
  549. cPeer.beginValidate();
  550. validateTree();
  551. valid = true;
  552. if (cPeer != null)
  553. cPeer.endValidate();
  554. }
  555. }
  556. }
  557. /**
  558. * Recursively invalidates the container tree.
  559. */
  560. private final void invalidateTree()
  561. {
  562. synchronized (getTreeLock())
  563. {
  564. for (int i = 0; i < ncomponents; i++)
  565. {
  566. Component comp = component[i];
  567. if (comp instanceof Container)
  568. ((Container) comp).invalidateTree();
  569. else if (comp.valid)
  570. comp.invalidate();
  571. }
  572. if (valid)
  573. invalidate();
  574. }
  575. }
  576. /**
  577. * Recursively validates the container tree, recomputing any invalid
  578. * layouts.
  579. */
  580. protected void validateTree()
  581. {
  582. if (!valid)
  583. {
  584. ContainerPeer cPeer = null;
  585. if (peer instanceof ContainerPeer)
  586. {
  587. cPeer = (ContainerPeer) peer;
  588. cPeer.beginLayout();
  589. }
  590. doLayout ();
  591. for (int i = 0; i < ncomponents; ++i)
  592. {
  593. Component comp = component[i];
  594. if (comp instanceof Container && ! (comp instanceof Window)
  595. && ! comp.valid)
  596. {
  597. ((Container) comp).validateTree();
  598. }
  599. else
  600. {
  601. comp.validate();
  602. }
  603. }
  604. if (cPeer != null)
  605. {
  606. cPeer = (ContainerPeer) peer;
  607. cPeer.endLayout();
  608. }
  609. }
  610. /* children will call invalidate() when they are layed out. It
  611. is therefore important that valid is not set to true
  612. until after the children have been layed out. */
  613. valid = true;
  614. }
  615. public void setFont(Font f)
  616. {
  617. Font oldFont = getFont();
  618. super.setFont(f);
  619. Font newFont = getFont();
  620. if (newFont != oldFont && (oldFont == null || ! oldFont.equals(newFont)))
  621. {
  622. invalidateTree();
  623. }
  624. }
  625. /**
  626. * Returns the preferred size of this container.
  627. *
  628. * @return The preferred size of this container.
  629. */
  630. public Dimension getPreferredSize()
  631. {
  632. return preferredSize ();
  633. }
  634. /**
  635. * Returns the preferred size of this container.
  636. *
  637. * @return The preferred size of this container.
  638. *
  639. * @deprecated use {@link #getPreferredSize()} instead
  640. */
  641. public Dimension preferredSize()
  642. {
  643. Dimension size = prefSize;
  644. // Try to return cached value if possible.
  645. if (size == null || !(prefSizeSet || valid))
  646. {
  647. // Need to lock here.
  648. synchronized (getTreeLock())
  649. {
  650. LayoutManager l = layoutMgr;
  651. if (l != null)
  652. prefSize = l.preferredLayoutSize(this);
  653. else
  654. prefSize = super.preferredSizeImpl();
  655. size = prefSize;
  656. }
  657. }
  658. if (size != null)
  659. return new Dimension(size);
  660. else
  661. return size;
  662. }
  663. /**
  664. * Returns the minimum size of this container.
  665. *
  666. * @return The minimum size of this container.
  667. */
  668. public Dimension getMinimumSize()
  669. {
  670. return minimumSize ();
  671. }
  672. /**
  673. * Returns the minimum size of this container.
  674. *
  675. * @return The minimum size of this container.
  676. *
  677. * @deprecated use {@link #getMinimumSize()} instead
  678. */
  679. public Dimension minimumSize()
  680. {
  681. Dimension size = minSize;
  682. // Try to return cached value if possible.
  683. if (size == null || !(minSizeSet || valid))
  684. {
  685. // Need to lock here.
  686. synchronized (getTreeLock())
  687. {
  688. LayoutManager l = layoutMgr;
  689. if (l != null)
  690. minSize = l.minimumLayoutSize(this);
  691. else
  692. minSize = super.minimumSizeImpl();
  693. size = minSize;
  694. }
  695. }
  696. if (size != null)
  697. return new Dimension(size);
  698. else
  699. return size;
  700. }
  701. /**
  702. * Returns the maximum size of this container.
  703. *
  704. * @return The maximum size of this container.
  705. */
  706. public Dimension getMaximumSize()
  707. {
  708. Dimension size = maxSize;
  709. // Try to return cached value if possible.
  710. if (size == null || !(maxSizeSet || valid))
  711. {
  712. // Need to lock here.
  713. synchronized (getTreeLock())
  714. {
  715. LayoutManager l = layoutMgr;
  716. if (l instanceof LayoutManager2)
  717. maxSize = ((LayoutManager2) l).maximumLayoutSize(this);
  718. else {
  719. maxSize = super.maximumSizeImpl();
  720. }
  721. size = maxSize;
  722. }
  723. }
  724. if (size != null)
  725. return new Dimension(size);
  726. else
  727. return size;
  728. }
  729. /**
  730. * Returns the preferred alignment along the X axis. This is a value
  731. * between 0 and 1 where 0 represents alignment flush left and
  732. * 1 means alignment flush right, and 0.5 means centered.
  733. *
  734. * @return The preferred alignment along the X axis.
  735. */
  736. public float getAlignmentX()
  737. {
  738. LayoutManager layout = getLayout();
  739. float alignmentX = 0.0F;
  740. if (layout != null && layout instanceof LayoutManager2)
  741. {
  742. synchronized (getTreeLock())
  743. {
  744. LayoutManager2 lm2 = (LayoutManager2) layout;
  745. alignmentX = lm2.getLayoutAlignmentX(this);
  746. }
  747. }
  748. else
  749. alignmentX = super.getAlignmentX();
  750. return alignmentX;
  751. }
  752. /**
  753. * Returns the preferred alignment along the Y axis. This is a value
  754. * between 0 and 1 where 0 represents alignment flush top and
  755. * 1 means alignment flush bottom, and 0.5 means centered.
  756. *
  757. * @return The preferred alignment along the Y axis.
  758. */
  759. public float getAlignmentY()
  760. {
  761. LayoutManager layout = getLayout();
  762. float alignmentY = 0.0F;
  763. if (layout != null && layout instanceof LayoutManager2)
  764. {
  765. synchronized (getTreeLock())
  766. {
  767. LayoutManager2 lm2 = (LayoutManager2) layout;
  768. alignmentY = lm2.getLayoutAlignmentY(this);
  769. }
  770. }
  771. else
  772. alignmentY = super.getAlignmentY();
  773. return alignmentY;
  774. }
  775. /**
  776. * Paints this container. The implementation of this method in this
  777. * class forwards to any lightweight components in this container. If
  778. * this method is subclassed, this method should still be invoked as
  779. * a superclass method so that lightweight components are properly
  780. * drawn.
  781. *
  782. * @param g - The graphics context for this paint job.
  783. */
  784. public void paint(Graphics g)
  785. {
  786. if (isShowing())
  787. {
  788. visitChildren(g, GfxPaintVisitor.INSTANCE, true);
  789. }
  790. }
  791. /**
  792. * Updates this container. The implementation of this method in this
  793. * class forwards to any lightweight components in this container. If
  794. * this method is subclassed, this method should still be invoked as
  795. * a superclass method so that lightweight components are properly
  796. * drawn.
  797. *
  798. * @param g The graphics context for this update.
  799. *
  800. * @specnote The specification suggests that this method forwards the
  801. * update() call to all its lightweight children. Tests show
  802. * that this is not done either in the JDK. The exact behaviour
  803. * seems to be that the background is cleared in heavyweight
  804. * Containers, and all other containers
  805. * directly call paint(), causing the (lightweight) children to
  806. * be painted.
  807. */
  808. public void update(Graphics g)
  809. {
  810. // It seems that the JDK clears the background of containers like Panel
  811. // and Window (within this method) but not of 'plain' Containers or
  812. // JComponents. This could
  813. // lead to the assumption that it only clears heavyweight containers.
  814. // However that is not quite true. In a test with a custom Container
  815. // that overrides isLightweight() to return false, the background is
  816. // also not cleared. So we do a check on !(peer instanceof LightweightPeer)
  817. // instead.
  818. if (isShowing())
  819. {
  820. ComponentPeer p = peer;
  821. if (! (p instanceof LightweightPeer))
  822. {
  823. g.clearRect(0, 0, getWidth(), getHeight());
  824. }
  825. paint(g);
  826. }
  827. }
  828. /**
  829. * Prints this container. The implementation of this method in this
  830. * class forwards to any lightweight components in this container. If
  831. * this method is subclassed, this method should still be invoked as
  832. * a superclass method so that lightweight components are properly
  833. * drawn.
  834. *
  835. * @param g The graphics context for this print job.
  836. */
  837. public void print(Graphics g)
  838. {
  839. super.print(g);
  840. visitChildren(g, GfxPrintVisitor.INSTANCE, true);
  841. }
  842. /**
  843. * Paints all of the components in this container.
  844. *
  845. * @param g The graphics context for this paint job.
  846. */
  847. public void paintComponents(Graphics g)
  848. {
  849. if (isShowing())
  850. visitChildren(g, GfxPaintAllVisitor.INSTANCE, false);
  851. }
  852. /**
  853. * Prints all of the components in this container.
  854. *
  855. * @param g The graphics context for this print job.
  856. */
  857. public void printComponents(Graphics g)
  858. {
  859. super.paint(g);
  860. visitChildren(g, GfxPrintAllVisitor.INSTANCE, true);
  861. }
  862. /**
  863. * Adds the specified container listener to this object's list of
  864. * container listeners.
  865. *
  866. * @param listener The listener to add.
  867. */
  868. public synchronized void addContainerListener(ContainerListener listener)
  869. {
  870. if (listener != null)
  871. {
  872. containerListener = AWTEventMulticaster.add(containerListener,
  873. listener);
  874. newEventsOnly = true;
  875. }
  876. }
  877. /**
  878. * Removes the specified container listener from this object's list of
  879. * container listeners.
  880. *
  881. * @param listener The listener to remove.
  882. */
  883. public synchronized void removeContainerListener(ContainerListener listener)
  884. {
  885. containerListener = AWTEventMulticaster.remove(containerListener, listener);
  886. }
  887. /**
  888. * @since 1.4
  889. */
  890. public synchronized ContainerListener[] getContainerListeners()
  891. {
  892. return (ContainerListener[])
  893. AWTEventMulticaster.getListeners(containerListener,
  894. ContainerListener.class);
  895. }
  896. /**
  897. * Returns all registered {@link EventListener}s of the given
  898. * <code>listenerType</code>.
  899. *
  900. * @param listenerType the class of listeners to filter (<code>null</code>
  901. * not permitted).
  902. *
  903. * @return An array of registered listeners.
  904. *
  905. * @throws ClassCastException if <code>listenerType</code> does not implement
  906. * the {@link EventListener} interface.
  907. * @throws NullPointerException if <code>listenerType</code> is
  908. * <code>null</code>.
  909. *
  910. * @see #getContainerListeners()
  911. *
  912. * @since 1.3
  913. */
  914. public <T extends EventListener> T[] getListeners(Class<T> listenerType)
  915. {
  916. if (listenerType == ContainerListener.class)
  917. return (T[]) getContainerListeners();
  918. return super.getListeners(listenerType);
  919. }
  920. /**
  921. * Processes the specified event. This method calls
  922. * <code>processContainerEvent()</code> if this method is a
  923. * <code>ContainerEvent</code>, otherwise it calls the superclass
  924. * method.
  925. *
  926. * @param e The event to be processed.
  927. */
  928. protected void processEvent(AWTEvent e)
  929. {
  930. if (e instanceof ContainerEvent)
  931. processContainerEvent((ContainerEvent) e);
  932. else
  933. super.processEvent(e);
  934. }
  935. /**
  936. * Called when a container event occurs if container events are enabled.
  937. * This method calls any registered listeners.
  938. *
  939. * @param e The event that occurred.
  940. */
  941. protected void processContainerEvent(ContainerEvent e)
  942. {
  943. if (containerListener == null)
  944. return;
  945. switch (e.id)
  946. {
  947. case ContainerEvent.COMPONENT_ADDED:
  948. containerListener.componentAdded(e);
  949. break;
  950. case ContainerEvent.COMPONENT_REMOVED:
  951. containerListener.componentRemoved(e);
  952. break;
  953. }
  954. }
  955. /**
  956. * AWT 1.0 event processor.
  957. *
  958. * @param e The event that occurred.
  959. *
  960. * @deprecated use {@link #dispatchEvent(AWTEvent)} instead
  961. */
  962. public void deliverEvent(Event e)
  963. {
  964. if (!handleEvent (e))
  965. {
  966. synchronized (getTreeLock ())
  967. {
  968. Component parent = getParent ();
  969. if (parent != null)
  970. parent.deliverEvent (e);
  971. }
  972. }
  973. }
  974. /**
  975. * Returns the component located at the specified point. This is done
  976. * by checking whether or not a child component claims to contain this
  977. * point. The first child component that does is returned. If no
  978. * child component claims the point, the container itself is returned,
  979. * unless the point does not exist within this container, in which
  980. * case <code>null</code> is returned.
  981. *
  982. * When components overlap, the first component is returned. The component
  983. * that is closest to (x, y), containing that location, is returned.
  984. * Heavyweight components take precedence of lightweight components.
  985. *
  986. * This function does not ignore invisible components. If there is an invisible
  987. * component at (x,y), it will be returned.
  988. *
  989. * @param x The X coordinate of the point.
  990. * @param y The Y coordinate of the point.
  991. *
  992. * @return The component containing the specified point, or
  993. * <code>null</code> if there is no such point.
  994. */
  995. public Component getComponentAt(int x, int y)
  996. {
  997. return locate (x, y);
  998. }
  999. /**
  1000. * Returns the mouse pointer position relative to this Container's
  1001. * top-left corner. If allowChildren is false, the mouse pointer
  1002. * must be directly over this container. If allowChildren is true,
  1003. * the mouse pointer may be over this container or any of its
  1004. * descendents.
  1005. *
  1006. * @param allowChildren true to allow descendents, false if pointer
  1007. * must be directly over Container.
  1008. *
  1009. * @return relative mouse pointer position
  1010. *
  1011. * @throws HeadlessException if in a headless environment
  1012. */
  1013. public Point getMousePosition(boolean allowChildren) throws HeadlessException
  1014. {
  1015. return super.getMousePositionHelper(allowChildren);
  1016. }
  1017. boolean mouseOverComponent(Component component, boolean allowChildren)
  1018. {
  1019. if (allowChildren)
  1020. return isAncestorOf(component);
  1021. else
  1022. return component == this;
  1023. }
  1024. /**
  1025. * Returns the component located at the specified point. This is done
  1026. * by checking whether or not a child component claims to contain this
  1027. * point. The first child component that does is returned. If no
  1028. * child component claims the point, the container itself is returned,
  1029. * unless the point does not exist within this container, in which
  1030. * case <code>null</code> is returned.
  1031. *
  1032. * When components overlap, the first component is returned. The component
  1033. * that is closest to (x, y), containing that location, is returned.
  1034. * Heavyweight components take precedence of lightweight components.
  1035. *
  1036. * This function does not ignore invisible components. If there is an invisible
  1037. * component at (x,y), it will be returned.
  1038. *
  1039. * @param x The x position of the point to return the component at.
  1040. * @param y The y position of the point to return the component at.
  1041. *
  1042. * @return The component containing the specified point, or <code>null</code>
  1043. * if there is no such point.
  1044. *
  1045. * @deprecated use {@link #getComponentAt(int, int)} instead
  1046. */
  1047. public Component locate(int x, int y)
  1048. {
  1049. synchronized (getTreeLock ())
  1050. {
  1051. if (!contains (x, y))
  1052. return null;
  1053. // First find the component closest to (x,y) that is a heavyweight.
  1054. for (int i = 0; i < ncomponents; ++i)
  1055. {
  1056. Component comp = component[i];
  1057. int x2 = x - comp.x;
  1058. int y2 = y - comp.y;
  1059. if (comp.contains (x2, y2) && !comp.isLightweight())
  1060. return comp;
  1061. }
  1062. // if a heavyweight component is not found, look for a lightweight
  1063. // closest to (x,y).
  1064. for (int i = 0; i < ncomponents; ++i)
  1065. {
  1066. Component comp = component[i];
  1067. int x2 = x - comp.x;
  1068. int y2 = y - comp.y;
  1069. if (comp.contains (x2, y2) && comp.isLightweight())
  1070. return comp;
  1071. }
  1072. return this;
  1073. }
  1074. }
  1075. /**
  1076. * Returns the component located at the specified point. This is done
  1077. * by checking whether or not a child component claims to contain this
  1078. * point. The first child component that does is returned. If no
  1079. * child component claims the point, the container itself is returned,
  1080. * unless the point does not exist within this container, in which
  1081. * case <code>null</code> is returned.
  1082. *
  1083. * The top-most child component is returned in the case where components overlap.
  1084. * This is determined by finding the component closest to (x,y) and contains
  1085. * that location. Heavyweight components take precedence of lightweight components.
  1086. *
  1087. * This function does not ignore invisible components. If there is an invisible
  1088. * component at (x,y), it will be returned.
  1089. *
  1090. * @param p The point to return the component at.
  1091. * @return The component containing the specified point, or <code>null</code>
  1092. * if there is no such point.
  1093. */
  1094. public Component getComponentAt(Point p)
  1095. {
  1096. return getComponentAt (p.x, p.y);
  1097. }
  1098. /**
  1099. * Locates the visible child component that contains the specified position.
  1100. * The top-most child component is returned in the case where there is overlap
  1101. * in the components. If the containing child component is a Container,
  1102. * this method will continue searching for the deepest nested child
  1103. * component. Components which are not visible are ignored during the search.
  1104. *
  1105. * findComponentAt differs from getComponentAt, because it recursively
  1106. * searches a Container's children.
  1107. *
  1108. * @param x - x coordinate
  1109. * @param y - y coordinate
  1110. * @return null if the component does not contain the position.
  1111. * If there is no child component at the requested point and the point is
  1112. * within the bounds of the container the container itself is returned.
  1113. */
  1114. public Component findComponentAt(int x, int y)
  1115. {
  1116. synchronized (getTreeLock ())
  1117. {
  1118. if (! contains(x, y))
  1119. return null;
  1120. for (int i = 0; i < ncomponents; ++i)
  1121. {
  1122. // Ignore invisible children...
  1123. if (!component[i].isVisible())
  1124. continue;
  1125. int x2 = x - component[i].x;
  1126. int y2 = y - component[i].y;
  1127. // We don't do the contains() check right away because
  1128. // findComponentAt would redundantly do it first thing.
  1129. if (component[i] instanceof Container)
  1130. {
  1131. Container k = (Container) component[i];
  1132. Component r = k.findComponentAt(x2, y2);
  1133. if (r != null)
  1134. return r;
  1135. }
  1136. else if (component[i].contains(x2, y2))
  1137. return component[i];
  1138. }
  1139. return this;
  1140. }
  1141. }
  1142. /**
  1143. * Locates the visible child component that contains the specified position.
  1144. * The top-most child component is returned in the case where there is overlap
  1145. * in the components. If the containing child component is a Container,
  1146. * this method will continue searching for the deepest nested child
  1147. * component. Components which are not visible are ignored during the search.
  1148. *
  1149. * findComponentAt differs from getComponentAt, because it recursively
  1150. * searches a Container's children.
  1151. *
  1152. * @param p - the component's location
  1153. * @return null if the component does not contain the position.
  1154. * If there is no child component at the requested point and the point is
  1155. * within the bounds of the container the container itself is returned.
  1156. */
  1157. public Component findComponentAt(Point p)
  1158. {
  1159. return findComponentAt(p.x, p.y);
  1160. }
  1161. /**
  1162. * Called when this container is added to another container to inform it
  1163. * to create its peer. Peers for any child components will also be
  1164. * created.
  1165. */
  1166. public void addNotify()
  1167. {
  1168. synchronized (getTreeLock())
  1169. {
  1170. super.addNotify();
  1171. addNotifyContainerChildren();
  1172. }
  1173. }
  1174. /**
  1175. * Called when this container is removed from its parent container to
  1176. * inform it to destroy its peer. This causes the peers of all child
  1177. * component to be destroyed as well.
  1178. */
  1179. public void removeNotify()
  1180. {
  1181. synchronized (getTreeLock ())
  1182. {
  1183. int ncomps = ncomponents;
  1184. Component[] comps = component;
  1185. for (int i = ncomps - 1; i >= 0; --i)
  1186. {
  1187. Component comp = comps[i];
  1188. if (comp != null)
  1189. comp.removeNotify();
  1190. }
  1191. super.removeNotify();
  1192. }
  1193. }
  1194. /**
  1195. * Tests whether or not the specified component is contained within
  1196. * this components subtree.
  1197. *
  1198. * @param comp The component to test.
  1199. *
  1200. * @return <code>true</code> if this container is an ancestor of the
  1201. * specified component, <code>false</code> otherwise.
  1202. */
  1203. public boolean isAncestorOf(Component comp)
  1204. {
  1205. synchronized (getTreeLock ())
  1206. {
  1207. while (true)
  1208. {
  1209. if (comp == null)
  1210. return false;
  1211. comp = comp.getParent();
  1212. if (comp == this)
  1213. return true;
  1214. }
  1215. }
  1216. }
  1217. /**
  1218. * Returns a string representing the state of this container for
  1219. * debugging purposes.
  1220. *
  1221. * @return A string representing the state of this container.
  1222. */
  1223. protected String paramString()
  1224. {
  1225. if (layoutMgr == null)
  1226. return super.paramString();
  1227. CPStringBuilder sb = new CPStringBuilder();
  1228. sb.append(super.paramString());
  1229. sb.append(",layout=");
  1230. sb.append(layoutMgr.getClass().getName());
  1231. return sb.toString();
  1232. }
  1233. /**
  1234. * Writes a listing of this container to the specified stream starting
  1235. * at the specified indentation point.
  1236. *
  1237. * @param out The <code>PrintStream</code> to write to.
  1238. * @param indent The indentation point.
  1239. */
  1240. public void list(PrintStream out, int indent)
  1241. {
  1242. synchronized (getTreeLock ())
  1243. {
  1244. super.list(out, indent);
  1245. for (int i = 0; i < ncomponents; ++i)
  1246. component[i].list(out, indent + 2);
  1247. }
  1248. }
  1249. /**
  1250. * Writes a listing of this container to the specified stream starting
  1251. * at the specified indentation point.
  1252. *
  1253. * @param out The <code>PrintWriter</code> to write to.
  1254. * @param indent The indentation point.
  1255. */
  1256. public void list(PrintWriter out, int indent)
  1257. {
  1258. synchronized (getTreeLock ())
  1259. {
  1260. super.list(out, indent);
  1261. for (int i = 0; i < ncomponents; ++i)
  1262. component[i].list(out, indent + 2);
  1263. }
  1264. }
  1265. /**
  1266. * Sets the focus traversal keys for a given traversal operation for this
  1267. * Container.
  1268. *
  1269. * @exception IllegalArgumentException If id is not one of
  1270. * KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS,
  1271. * KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS,
  1272. * KeyboardFocusManager.UP_CYCLE_TRAVERSAL_KEYS,
  1273. * or KeyboardFocusManager.DOWN_CYCLE_TRAVERSAL_KEYS,
  1274. * or if keystrokes contains null, or if any Object in keystrokes is not an
  1275. * AWTKeyStroke, or if any keystroke represents a KEY_TYPED event, or if any
  1276. * keystroke already maps to another focus traversal operation for this
  1277. * Container.
  1278. *
  1279. * @since 1.4
  1280. */
  1281. public void setFocusTraversalKeys(int id,
  1282. Set<? extends AWTKeyStroke> keystrokes)
  1283. {
  1284. if (id != KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS &&
  1285. id != KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS &&
  1286. id != KeyboardFocusManager.UP_CYCLE_TRAVERSAL_KEYS &&
  1287. id != KeyboardFocusManager.DOWN_CYCLE_TRAVERSAL_KEYS)
  1288. throw new IllegalArgumentException ();
  1289. if (keystrokes == null)
  1290. {
  1291. Container parent = getParent ();
  1292. while (parent != null)
  1293. {
  1294. if (parent.areFocusTraversalKeysSet (id))
  1295. {
  1296. keystrokes = parent.getFocusTraversalKeys (id);
  1297. break;
  1298. }
  1299. parent = parent.getParent ();
  1300. }
  1301. if (keystrokes == null)
  1302. keystrokes = KeyboardFocusManager.getCurrentKeyboardFocusManager ().
  1303. getDefaultFocusTraversalKeys (id);
  1304. }
  1305. Set sa;
  1306. Set sb;
  1307. Set sc;
  1308. String name;
  1309. switch (id)
  1310. {
  1311. case KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS:
  1312. sa = getFocusTraversalKeys
  1313. (KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS);
  1314. sb = getFocusTraversalKeys
  1315. (KeyboardFocusManager.UP_CYCLE_TRAVERSAL_KEYS);
  1316. sc = getFocusTraversalKeys
  1317. (KeyboardFocusManager.DOWN_CYCLE_TRAVERSAL_KEYS);
  1318. name = "forwardFocusTraversalKeys";
  1319. break;
  1320. case KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS:
  1321. sa = getFocusTraversalKeys
  1322. (KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS);
  1323. sb = getFocusTraversalKeys
  1324. (KeyboardFocusManager.UP_CYCLE_TRAVERSAL_KEYS);
  1325. sc = getFocusTraversalKeys
  1326. (KeyboardFocusManager.DOWN_CYCLE_TRAVERSAL_KEYS);
  1327. name = "backwardFocusTraversalKeys";
  1328. break;
  1329. case KeyboardFocusManager.UP_CYCLE_TRAVERSAL_KEYS:
  1330. sa = getFocusTraversalKeys
  1331. (KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS);
  1332. sb = getFocusTraversalKeys
  1333. (KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS);
  1334. sc = getFocusTraversalKeys
  1335. (KeyboardFocusManager.DOWN_CYCLE_TRAVERSAL_KEYS);
  1336. name = "upCycleFocusTraversalKeys";
  1337. break;
  1338. case KeyboardFocusManager.DOWN_CYCLE_TRAVERSAL_KEYS:
  1339. sa = getFocusTraversalKeys
  1340. (KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS);
  1341. sb = getFocusTraversalKeys
  1342. (KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS);
  1343. sc = getFocusTraversalKeys
  1344. (KeyboardFocusManager.UP_CYCLE_TRAVERSAL_KEYS);
  1345. name = "downCycleFocusTraversalKeys";
  1346. break;
  1347. default:
  1348. throw new IllegalArgumentException ();
  1349. }
  1350. int i = keystrokes.size ();
  1351. Iterator iter = keystrokes.iterator ();
  1352. while (--i >= 0)
  1353. {
  1354. Object o = iter.next ();
  1355. if (!(o instanceof AWTKeyStroke)
  1356. || sa.contains (o) || sb.contains (o) || sc.contains (o)
  1357. || ((AWTKeyStroke) o).keyCode == KeyEvent.VK_UNDEFINED)
  1358. throw new IllegalArgumentException ();
  1359. }
  1360. if (focusTraversalKeys == null)
  1361. focusTraversalKeys = new Set[4];
  1362. keystrokes =
  1363. Collections.unmodifiableSet(new HashSet<AWTKeyStroke>(keystrokes));
  1364. firePropertyChange (name, focusTraversalKeys[id], keystrokes);
  1365. focusTraversalKeys[id] = keystrokes;
  1366. }
  1367. /**
  1368. * Returns the Set of focus traversal keys for a given traversal operation for
  1369. * this Container.
  1370. *
  1371. * @exception IllegalArgumentException If id is not one of
  1372. * KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS,
  1373. * KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS,
  1374. * KeyboardFocusManager.UP_CYCLE_TRAVERSAL_KEYS,
  1375. * or KeyboardFocusManager.DOWN_CYCLE_TRAVERSAL_KEYS.
  1376. *
  1377. * @since 1.4
  1378. */
  1379. public Set<AWTKeyStroke> getFocusTraversalKeys (int id)
  1380. {
  1381. if (id != KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS &&
  1382. id != KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS &&
  1383. id != KeyboardFocusManager.UP_CYCLE_TRAVERSAL_KEYS &&
  1384. id != KeyboardFocusManager.DOWN_CYCLE_TRAVERSAL_KEYS)
  1385. throw new IllegalArgumentException ();
  1386. Set s = null;
  1387. if (focusTraversalKeys != null)
  1388. s = focusTraversalKeys[id];
  1389. if (s == null && parent != null)
  1390. s = parent.getFocusTraversalKeys (id);
  1391. return s == null ? (KeyboardFocusManager.getCurrentKeyboardFocusManager()
  1392. .getDefaultFocusTraversalKeys(id)) : s;
  1393. }
  1394. /**
  1395. * Returns whether the Set of focus traversal keys for the given focus
  1396. * traversal operation has been explicitly defined for this Container.
  1397. * If this method returns false, this Container is inheriting the Set from
  1398. * an ancestor, or from the current KeyboardFocusManager.
  1399. *
  1400. * @exception IllegalArgumentException If id is not one of
  1401. * KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS,
  1402. * KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS,
  1403. * KeyboardFocusManager.UP_CYCLE_TRAVERSAL_KEYS,
  1404. * or KeyboardFocusManager.DOWN_CYCLE_TRAVERSAL_KEYS.
  1405. *
  1406. * @since 1.4
  1407. */
  1408. public boolean areFocusTraversalKeysSet (int id)
  1409. {
  1410. if (id != KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS &&
  1411. id != KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS &&
  1412. id != KeyboardFocusManager.UP_CYCLE_TRAVERSAL_KEYS &&
  1413. id != KeyboardFocusManager.DOWN_CYCLE_TRAVERSAL_KEYS)
  1414. throw new IllegalArgumentException ();
  1415. return focusTraversalKeys != null && focusTraversalKeys[id] != null;
  1416. }
  1417. /**
  1418. * Check whether the given Container is the focus cycle root of this
  1419. * Container's focus traversal cycle. If this Container is a focus
  1420. * cycle root itself, then it will be in two different focus cycles
  1421. * -- it's own, and that of its ancestor focus cycle root's. In
  1422. * that case, if <code>c</code> is either of those containers, this
  1423. * method will return true.
  1424. *
  1425. * @param c the candidate Container
  1426. *
  1427. * @return true if c is the focus cycle root of the focus traversal
  1428. * cycle to which this Container belongs, false otherwise
  1429. *
  1430. * @since 1.4
  1431. */
  1432. public boolean isFocusCycleRoot (Container c)
  1433. {
  1434. if (this == c
  1435. && isFocusCycleRoot ())
  1436. return true;
  1437. Container ancestor = getFocusCycleRootAncestor ();
  1438. if (c == ancestor)
  1439. return true;
  1440. return false;
  1441. }
  1442. /**
  1443. * If this Container is a focus cycle root, set the focus traversal
  1444. * policy that determines the focus traversal order for its
  1445. * children. If non-null, this policy will be inherited by all
  1446. * inferior focus cycle roots. If <code>policy</code> is null, this
  1447. * Container will inherit its policy from the closest ancestor focus
  1448. * cycle root that's had its policy set.
  1449. *
  1450. * @param policy the new focus traversal policy for this Container or null
  1451. *
  1452. * @since 1.4
  1453. */
  1454. public void setFocusTraversalPolicy (FocusTraversalPolicy policy)
  1455. {
  1456. focusTraversalPolicy = policy;
  1457. }
  1458. /**
  1459. * Return the focus traversal policy that determines the focus
  1460. * traversal order for this Container's children. This method
  1461. * returns null if this Container is not a focus cycle root. If the
  1462. * focus traversal policy has not been set explicitly, then this
  1463. * method will return an ancestor focus cycle root's policy instead.
  1464. *
  1465. * @return this Container's focus