Exemplo n.º 1
0
  /**
   * Convenience method for searching above the given component in the component hierarchy and
   * returns the first object of the given type it finds, or <code>null</code> if no such parent was
   * found.
   *
   * <p>The reason this method exists is for tidyness of the calling code, as no longer a explicit
   * cast is needed.
   *
   * @param aType the type of the parent to find, cannot be <code>null</code>;
   * @param aComponent the component to search in the hierarchy, cannot be <code>null</code>.
   * @return the requested ancestor, or <code>null</code> if not found.
   * @see SwingUtilities#getAncestorOfClass(Class, Component)
   */
  @SuppressWarnings("unchecked")
  public static <T> T getAncestorOfClass(final Class<T> aType, final Component aComponent) {
    if ((aComponent == null) || (aType == null)) {
      return null;
    }

    Container parent = aComponent.getParent();
    while ((parent != null) && !(aType.isInstance(parent))) {
      parent = parent.getParent();
    }

    return (T) parent;
  }
Exemplo n.º 2
0
  /**
   * @param aContainer
   * @param aComponentClass
   * @return
   */
  private static Component findComponent(
      final Container aContainer, final Class<? extends Component> aComponentClass) {
    Component result = null;

    final int cnt = aContainer.getComponentCount();
    for (int i = 0; (result == null) && (i < cnt); i++) {
      final Component comp = aContainer.getComponent(i);
      if (aComponentClass.equals(comp.getClass())) {
        result = comp;
      } else if (comp instanceof Container) {
        result = findComponent((Container) comp, aComponentClass);
      }
    }
    return result;
  }