Search in sources :

Example 1 with FindComponentsByClassVisitor

use of com.github.bordertech.wcomponents.util.visitor.FindComponentsByClassVisitor in project wcomponents by BorderTech.

the class TreeUtil method findWComponents.

/**
 * Retrieves WComponents by their path in the WComponent tree.
 * <p>
 * Paths are specified using class names, starting from the furthest ancestor. To reduce the path lengths, class
 * names do not need to be fully-qualified. The path does not need to explicitly state intermediate components
 * between components, and may include an index suffix to select a particular instance of a component in e.g. a
 * repeater or a set of fields. Some example paths are shown below.
 * </p>
 * Example paths.
 * <dl>
 * <dt><code>{ "MyComponent" }</code></dt>
 * <dd>Matches the first instance of MyComponent.</dd>
 * <dt><code>{ "MyComponent[0]" }</code></dt>
 * <dd>Also matches the first instance of MyComponent.</dd>
 * <dt><code>{ "MyComponent[1]" }</code></dt>
 * <dd>Matches the second instance of MyComponent.</dd>
 * <dt><code>{ "MyPanel", "MyComponent" }</code></dt>
 * <dd>Matches the first instance of MyComponent which is nested anywhere under a MyPanel.</dd>
 * <dt><code>{ "MyApp", "MyPanel", "MyComponent" }</code></dt>
 * <dd>Matches the first instance of MyComponent, nested within a MyPanel, which is in turn nested somewhere within
 * a MyApp.</dd>
 * </dl>
 *
 * @param component the component to search from.
 * @param path the path to the WComponent.
 * @param visibleOnly visible only
 * @return the component matching the given path, or null if not found.
 */
public static ComponentWithContext[] findWComponents(final WComponent component, final String[] path, final boolean visibleOnly) {
    UIContext uic = UIContextHolder.getCurrent();
    if (uic == null) {
        throw new IllegalStateException("No user context available.");
    }
    List<ComponentWithContext> matchAtLevel = new ArrayList<>();
    matchAtLevel.add(new ComponentWithContext(component, uic));
    for (int i = 0; i < path.length; i++) {
        List<ComponentWithContext> matchAtLastLevel = matchAtLevel;
        matchAtLevel = new ArrayList<>();
        for (ComponentWithContext comp : matchAtLastLevel) {
            String[] parts = path[i].trim().split("[\\[\\]]");
            String className = parts[0].trim();
            int index = parts.length == 2 ? Integer.parseInt(parts[1]) : -1;
            FindComponentsByClassVisitor visitor = new FindComponentsByClassVisitor(comp.getComponent(), className, i == 0);
            UIContextHolder.pushContext(comp.getContext());
            try {
                if (visibleOnly) {
                    TreeUtil.traverseVisible(comp.getComponent(), visitor);
                } else {
                    TreeUtil.traverse(comp.getComponent(), visitor);
                }
            } finally {
                UIContextHolder.popContext();
            }
            if (index >= 0) {
                if (index < visitor.getResult().size()) {
                    matchAtLevel.add(visitor.getResult().get(index));
                }
            } else {
                matchAtLevel.addAll(visitor.getResult());
            }
        }
    }
    return matchAtLevel.toArray(new ComponentWithContext[matchAtLevel.size()]);
}
Also used : FindComponentsByClassVisitor(com.github.bordertech.wcomponents.util.visitor.FindComponentsByClassVisitor) UIContext(com.github.bordertech.wcomponents.UIContext) ArrayList(java.util.ArrayList) ComponentWithContext(com.github.bordertech.wcomponents.ComponentWithContext)

Example 2 with FindComponentsByClassVisitor

use of com.github.bordertech.wcomponents.util.visitor.FindComponentsByClassVisitor in project wcomponents by BorderTech.

the class TreeUtil method findComponentsByClass.

/**
 * Search for components implementing a particular class name.
 *
 * @param root the root component to search from
 * @param className the class name to search for
 * @param includeRoot check the root component as well
 * @param visibleOnly true if process visible only
 *
 * @return the list of components implementing the class name
 */
public static List<ComponentWithContext> findComponentsByClass(final WComponent root, final String className, final boolean includeRoot, final boolean visibleOnly) {
    FindComponentsByClassVisitor visitor = new FindComponentsByClassVisitor(root, className, includeRoot);
    doTraverse(root, visibleOnly, visitor);
    return visitor.getResult() == null ? Collections.EMPTY_LIST : visitor.getResult();
}
Also used : FindComponentsByClassVisitor(com.github.bordertech.wcomponents.util.visitor.FindComponentsByClassVisitor)

Aggregations

FindComponentsByClassVisitor (com.github.bordertech.wcomponents.util.visitor.FindComponentsByClassVisitor)2 ComponentWithContext (com.github.bordertech.wcomponents.ComponentWithContext)1 UIContext (com.github.bordertech.wcomponents.UIContext)1 ArrayList (java.util.ArrayList)1