|
|
@@ -1,6 +1,15 @@
|
|
|
package org.leumasjaffe.recipe.view;
|
|
|
|
|
|
+import java.awt.Component;
|
|
|
+import java.awt.Container;
|
|
|
import java.lang.reflect.InvocationTargetException;
|
|
|
+import java.util.Arrays;
|
|
|
+import java.util.Collection;
|
|
|
+import java.util.NoSuchElementException;
|
|
|
+import java.util.Optional;
|
|
|
+import java.util.function.Predicate;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+import java.util.stream.Stream;
|
|
|
|
|
|
import javax.swing.JFrame;
|
|
|
import javax.swing.SwingUtilities;
|
|
|
@@ -26,15 +35,52 @@ public class SwingTestCase {
|
|
|
}
|
|
|
|
|
|
public void waitForSwing() {
|
|
|
- if (!SwingUtilities.isEventDispatchThread( )) {
|
|
|
- try {
|
|
|
- SwingUtilities.invokeAndWait(new Runnable( ) {
|
|
|
- public void run( ) {
|
|
|
- }
|
|
|
- });
|
|
|
- } catch (InterruptedException e) {
|
|
|
- } catch (InvocationTargetException e) {
|
|
|
- }
|
|
|
+ if (SwingUtilities.isEventDispatchThread()) {
|
|
|
+ return;
|
|
|
}
|
|
|
+ try {
|
|
|
+ SwingUtilities.invokeAndWait(() -> {});
|
|
|
+ } catch (InterruptedException | InvocationTargetException e) {
|
|
|
+ System.err.println("Test Error in waitForSwing(): " + e.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public <T> T firstComponentMatching(final Class<T> clazz, final Container inContainer) {
|
|
|
+ return firstComponentMatching(clazz, Optional.empty(),
|
|
|
+ Arrays.asList(inContainer.getComponents()));
|
|
|
+ }
|
|
|
+
|
|
|
+ public <T> T firstComponentMatching(final Class<T> clazz, final String named, final Container inContainer) {
|
|
|
+ return firstComponentMatching(clazz, Optional.of(named),
|
|
|
+ Arrays.asList(inContainer.getComponents()));
|
|
|
+ }
|
|
|
+
|
|
|
+ private <T> T firstComponentMatching(final Class<T> clazz,
|
|
|
+ final Optional<String> name,
|
|
|
+ final Collection<Component> horizon) {
|
|
|
+ if (horizon.isEmpty()) {
|
|
|
+ throw new NoSuchElementException("An object of type " + clazz + " was not found in this context");
|
|
|
+ }
|
|
|
+ final Stream<T> matches = horizon.stream()
|
|
|
+ .filter(nameMatcher(name))
|
|
|
+ .filter(clazz::isInstance).map(clazz::cast);
|
|
|
+
|
|
|
+ final Optional<T> found = matches.findFirst();
|
|
|
+ if (found.isPresent()) {
|
|
|
+ return found.get();
|
|
|
+ }
|
|
|
+
|
|
|
+ return firstComponentMatching(clazz, name, horizon.stream()
|
|
|
+ .filter(Container.class::isInstance).map(Container.class::cast)
|
|
|
+ .map(Container::getComponents).flatMap(Stream::of)
|
|
|
+ .collect(Collectors.toList()));
|
|
|
}
|
|
|
+
|
|
|
+ private Predicate<? super Component> nameMatcher(Optional<String> name) {
|
|
|
+ if (name.isPresent()) {
|
|
|
+ final String nm = name.get();
|
|
|
+ return comp -> comp.getName().equals(nm);
|
|
|
+ }
|
|
|
+ return comp -> true;
|
|
|
+ }
|
|
|
}
|