use of com.github.bordertech.wcomponents.util.WComponentTreeVisitor.VisitorResult in project wcomponents by BorderTech.
the class TreeUtil method getClosestContextForId.
/**
* Retrieves the closest context for the component with the given Id.
*
* @param root the root component to search from.
* @param id the id to search for.
* @param visibleOnly true if process visible only
* @return the closest context for the component with the given id, or null if not found.
*/
public static UIContext getClosestContextForId(final WComponent root, final String id, final boolean visibleOnly) {
FindComponentByIdVisitor visitor = new FindComponentByIdVisitor(id) {
@Override
public VisitorResult visit(final WComponent comp) {
VisitorResult result = super.visit(comp);
if (result == VisitorResult.CONTINUE) {
// Save closest UIC as processing tree
setResult(new ComponentWithContext(comp, UIContextHolder.getCurrent()));
}
return result;
}
};
doTraverse(root, visibleOnly, visitor);
return visitor.getResult() == null ? null : visitor.getResult().getContext();
}
use of com.github.bordertech.wcomponents.util.WComponentTreeVisitor.VisitorResult in project wcomponents by BorderTech.
the class TreeUtil method isIdFocusable.
/**
* Check if this ID is focusable.
* <p>
* Considered focusable if the component and all its ancestors are visible and not hidden.
* </p>
*
* @param root the root component to search from.
* @param id the id to search for.
* @return the component with context if it is focusable, otherwise null
*/
public static boolean isIdFocusable(final WComponent root, final String id) {
FindComponentByIdVisitor visitor = new FindComponentByIdVisitor(id) {
@Override
public VisitorResult visit(final WComponent comp) {
VisitorResult result = super.visit(comp);
// If hidden then abort branch
if (result == VisitorResult.CONTINUE && comp.isHidden()) {
return VisitorResult.ABORT_BRANCH;
}
return result;
}
};
// Only traverse visible
doTraverse(root, true, visitor);
// Check if matching component is hidden
ComponentWithContext result = visitor.getResult();
return result == null ? false : !result.getComponent().isHidden();
}
use of com.github.bordertech.wcomponents.util.WComponentTreeVisitor.VisitorResult in project wcomponents by BorderTech.
the class TreeUtil method doTraverse.
/**
* Internal implementation of tree traversal method.
*
* @param node the node to traverse.
* @param visibleOnly if true, only visit visible components.
* @param visitor the visitor to notify as the tree is traversed.
* @return how the traversal should continue.
*/
private static VisitorResult doTraverse(final WComponent node, final boolean visibleOnly, final WComponentTreeVisitor visitor) {
if (visibleOnly) {
// Certain components have their visibility altered to implement custom processing.
if (node instanceof WInvisibleContainer) {
WComponent parent = node.getParent();
// If inside a CardManager, skip the InvisibleContainer and process the visible card.
if (parent instanceof WCardManager) {
WComponent visible = ((WCardManager) node.getParent()).getVisible();
if (visible == null) {
return VisitorResult.ABORT_BRANCH;
}
return doTraverse(visible, visibleOnly, visitor);
} else if (parent instanceof WWindow) {
// Abort branch if WWindow is not in ACTIVE state
if (((WWindow) parent).getState() != WWindow.ACTIVE_STATE) {
return VisitorResult.ABORT_BRANCH;
}
}
} else if (node instanceof WRepeatRoot) {
// Let be processed.
} else if (!node.isVisible()) {
// For most components, we just need to see if they're marked as visible
return VisitorResult.ABORT_BRANCH;
}
}
VisitorResult result = visitor.visit(node);
switch(result) {
case ABORT_BRANCH:
// Continue processing, but not down this branch
return VisitorResult.CONTINUE;
case CONTINUE:
// Process repeater rows
if (node instanceof WRepeater) {
// Get parent repeater
WRepeater repeater = (WRepeater) node;
// Get row contexts
List<UIContext> rowContextList = repeater.getRowContexts();
WRepeatRoot repeatRoot = (WRepeatRoot) repeater.getRepeatedComponent().getParent();
for (UIContext rowContext : rowContextList) {
UIContextHolder.pushContext(rowContext);
try {
result = doTraverse(repeatRoot, visibleOnly, visitor);
} finally {
UIContextHolder.popContext();
}
if (VisitorResult.ABORT.equals(result)) {
return VisitorResult.ABORT;
}
}
} else if (node instanceof Container) {
Container container = (Container) node;
for (int i = 0; i < container.getChildCount(); i++) {
result = doTraverse(container.getChildAt(i), visibleOnly, visitor);
if (VisitorResult.ABORT.equals(result)) {
return VisitorResult.ABORT;
}
}
}
return VisitorResult.CONTINUE;
default:
// Abort entire traversal
return VisitorResult.ABORT;
}
}
Aggregations