use of com.github.bordertech.wcomponents.ComponentWithContext in project wcomponents by BorderTech.
the class SubordinateControlHelper method applyRegisteredControls.
/**
* Apply the registered Subordinate Controls.
*
* @param request the request being processed.
* @param useRequestValues the flag to indicate the controls should use values from the request.
*/
public static void applyRegisteredControls(final Request request, final boolean useRequestValues) {
Set<String> controls = getRegisteredSubordinateControls();
if (controls == null) {
return;
}
// Process Controls
for (String controlId : controls) {
// Find the Component for this ID
ComponentWithContext controlWithContext = WebUtilities.getComponentById(controlId, true);
if (controlWithContext == null) {
LOG.warn("Subordinate control for id " + controlId + " is no longer in the tree.");
continue;
}
if (!(controlWithContext.getComponent() instanceof WSubordinateControl)) {
LOG.warn("Component for id " + controlId + " is not a subordinate control.");
continue;
}
WSubordinateControl control = (WSubordinateControl) controlWithContext.getComponent();
UIContext uic = controlWithContext.getContext();
UIContextHolder.pushContext(uic);
try {
if (useRequestValues) {
control.applyTheControls(request);
} else {
control.applyTheControls();
}
} finally {
UIContextHolder.popContext();
}
}
}
use of com.github.bordertech.wcomponents.ComponentWithContext in project wcomponents by BorderTech.
the class TreeUtil method collateVisibles.
/**
* Obtains a list of components which are visible in the given tree. Repeated components will be returned multiple
* times, one for each row which they are visible in.
*
* @param comp the root component to search from.
* @return a list of components which are visible in the given context.
*/
public static List<ComponentWithContext> collateVisibles(final WComponent comp) {
final List<ComponentWithContext> list = new ArrayList<>();
WComponentTreeVisitor visitor = new WComponentTreeVisitor() {
@Override
public VisitorResult visit(final WComponent comp) {
// (so ignore them)
if (comp.isVisible()) {
list.add(new ComponentWithContext(comp, UIContextHolder.getCurrent()));
}
return VisitorResult.CONTINUE;
}
};
traverseVisible(comp, visitor);
return list;
}
use of com.github.bordertech.wcomponents.ComponentWithContext 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.ComponentWithContext 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()]);
}
use of com.github.bordertech.wcomponents.ComponentWithContext 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();
}
Aggregations