com.objogate.wl.swing.probe.RecursiveComponentFinder Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of windowlicker-swing Show documentation
Show all versions of windowlicker-swing Show documentation
This is the Windowlicker Swing library.
The newest version!
package com.objogate.wl.swing.probe;
import javax.swing.MenuElement;
import java.awt.Component;
import java.awt.Container;
import java.awt.Window;
import java.util.ArrayList;
import static java.util.Arrays.asList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import org.hamcrest.Description;
import org.hamcrest.Matcher;
import com.objogate.wl.swing.ComponentFinder;
public class RecursiveComponentFinder implements ComponentFinder {
private final ComponentFinder extends Component> parentOrOwnerFinder;
private final Class componentType;
private final Matcher super T> criteria;
private Set found = new LinkedHashSet();
public RecursiveComponentFinder(Class componentType, Matcher super T> criteria, ComponentFinder extends Component> parentOrOwnerFinder) {
this.parentOrOwnerFinder = parentOrOwnerFinder;
this.componentType = componentType;
this.criteria = criteria;
}
public boolean isSatisfied() {
return parentOrOwnerFinder.isSatisfied();
}
public List components() {
return new ArrayList(found);
}
public void probe() {
parentOrOwnerFinder.probe();
found.clear();
searchWithin(parentOrOwnerFinder.components());
}
public void describeTo(Description description) {
describeBrieflyTo(description);
description.appendText("\n in ")
.appendDescriptionOf(parentOrOwnerFinder);
}
public void describeFailureTo(Description description) {
parentOrOwnerFinder.describeFailureTo(description);
if (parentOrOwnerFinder.isSatisfied()) {
description.appendText("\n contained ")
.appendText(String.valueOf(found.size()))
.appendText(" ");
describeBrieflyTo(description);
}
}
private void searchWithin(Iterable extends Component> components) {
for (Component component : components) {
searchWithin(component);
}
}
private void searchWithin(Component component) {
if (componentType.isInstance(component) && criteria.matches(component)) {
found.add(componentType.cast(component));
} else {
if (component instanceof Container) {
searchWithin(componentsInside((Container) component));
}
if (component instanceof Window) {
searchWithin(windowsOwnedBy((Window) component));
}
if (component instanceof MenuElement) {
searchWithin(componentsInMenu((MenuElement) component));
}
}
}
private void describeBrieflyTo(Description description) {
description.appendText(componentType.getSimpleName())
.appendText(" ")
.appendDescriptionOf(criteria);
}
private List componentsInMenu(MenuElement menuElement) {
List list = new ArrayList();
for (MenuElement element : menuElement.getSubElements()) {
list.add(element.getComponent());
}
return list;
}
private List windowsOwnedBy(Window window) {
return asList(window.getOwnedWindows());
}
private List componentsInside(Container container) {
return asList(container.getComponents());
}
}