Exemplo n.º 1
0
 /** {@inheritDoc} */
 @Override
 public @Nonnull Collection<Container> roots() {
   List<Container> roots = newArrayList();
   for (Window w : windowMonitor.rootWindows()) {
     roots.add(w);
   }
   return roots;
 }
 /* Usually only needed when dealing with Applets. */
 private @Nullable EventQueue eventQueueFor(@Nullable Component c) {
   return c != null ? windowMonitor.eventQueueFor(c) : toolkit.getSystemEventQueue();
 }
Exemplo n.º 3
0
/**
 * Provides access to the current AWT hierarchy.
 *
 * @author Alex Ruiz
 * @author Yvonne Wang
 */
public class ExistingHierarchy implements ComponentHierarchy {
  private static WindowMonitor windowMonitor = WindowMonitor.instance();

  private final ParentFinder parentFinder;
  private final ChildrenFinder childrenFinder;

  public ExistingHierarchy() {
    this(new ParentFinder(), new ChildrenFinder());
  }

  @VisibleForTesting
  ExistingHierarchy(@Nonnull ParentFinder parentFinder, @Nonnull ChildrenFinder childrenFinder) {
    this.parentFinder = parentFinder;
    this.childrenFinder = childrenFinder;
  }

  /** {@inheritDoc} */
  @Override
  public @Nonnull Collection<Container> roots() {
    List<Container> roots = newArrayList();
    for (Window w : windowMonitor.rootWindows()) {
      roots.add(w);
    }
    return roots;
  }

  /**
   * Returns the parent for the given AWT or Swing {@code Component}.
   *
   * <p><b>Note:</b> This method is accessed in the current executing thread. Such thread may or may
   * not be the event dispatch thread (EDT.) Client code must call this method from the EDT.
   *
   * @param c the given {@code Component}.
   * @return the parent for the given {@code Component}.
   */
  @RunsInCurrentThread
  @Override
  public Container parentOf(@Nonnull Component c) {
    return parentFinder.parentOf(c);
  }

  /**
   * Returns whether the given AWT or Swing {@code Component} is reachable from any of the root
   * windows. The default is to consider all components to be contained in the hierarchy, whether
   * they are reachable or not.
   *
   * @param c the given {@code Component}.
   * @return {@code true}.
   */
  @Override
  public boolean contains(@Nonnull Component c) {
    return true;
  }

  /**
   * Returns all descendants of interest of the given AWT or Swing {@code Component}.
   *
   * <p><b>Note:</b> This method is accessed in the current executing thread. Such thread may or may
   * not be the event dispatch thread (EDT.) Client code must call this method from the EDT.
   *
   * @param c the given {@code Component}.
   * @return all descendants of interest of the given {@code Component}.
   */
  @RunsInCurrentThread
  @Override
  public @Nonnull Collection<Component> childrenOf(@Nonnull Component c) {
    return childrenFinder.childrenOf(c);
  }

  /**
   * Properly disposes of the given {@code Window}, making it and its native resources available for
   * garbage collection.
   *
   * <p><b>Note:</b> This method is accessed in the current executing thread. Such thread may or may
   * not be the event dispatch thread (EDT.) Client code must call this method from the EDT.
   *
   * @param w the {@code Window} to dispose.
   */
  @Override
  @RunsInCurrentThread
  public void dispose(@Nonnull Window w) {
    if (isAppletViewer(w)) {
      return;
    }
    for (Window owned : w.getOwnedWindows()) {
      if (owned != null) {
        dispose(owned);
      }
    }
    if (isSharedInvisibleFrame(w)) {
      return;
    }
    w.dispose();
  }

  @Nonnull
  ParentFinder parentFinder() {
    return parentFinder;
  }

  @Nonnull
  ChildrenFinder childrenFinder() {
    return childrenFinder;
  }
}