浏览代码

Don't use getters for IT stuff.

Sam Jaffe 5 年之前
父节点
当前提交
5023ec511a

+ 1 - 2
src/main/lombok/org/leumasjaffe/recipe/view/RecipeCardPanel.java

@@ -10,7 +10,6 @@ import org.leumasjaffe.recipe.model.RecipeCard;
 import org.leumasjaffe.recipe.view.summary.SummaryPanel;
 
 import lombok.AccessLevel;
-import lombok.Getter;
 import lombok.experimental.FieldDefaults;
 
 import java.awt.Dimension;
@@ -25,7 +24,7 @@ public class RecipeCardPanel extends JSplitPane {
 	ForwardingObservableListener<RecipeCard> listener;
 	
 	SummaryPanel summaryPanel;
-	@Getter(AccessLevel.PACKAGE) AutoGrowPanel<ElementPanel, Element> panelElements;
+	AutoGrowPanel<ElementPanel, Element> panelElements;
 	
 	public RecipeCardPanel() {
 		setPreferredSize(new Dimension(1050, 600));

+ 1 - 1
src/main/lombok/org/leumasjaffe/recipe/view/StepPanel.java

@@ -36,7 +36,7 @@ public class StepPanel extends JPanel implements AutoGrowPanel.ChildComponent {
 
 	JLabel lblIndex;
 	@Getter(AccessLevel.PACKAGE) JTextPane txtpnInstructions;
-	@Getter(AccessLevel.PACKAGE) AutoGrowPanel<IngredientPanel, Ingredient> panelIngredients;
+	AutoGrowPanel<IngredientPanel, Ingredient> panelIngredients;
 		
 	public StepPanel() {
 		GridBagLayout gridBagLayout = new GridBagLayout();

+ 2 - 1
src/test/java/org/leumasjaffe/recipe/view/RecipeCardPanelIT.java

@@ -39,8 +39,9 @@ class RecipeCardPanelIT extends SwingTestCase {
 		clearInvocations(listener);
 	}
 	
+	@SuppressWarnings("unchecked")
 	private AutoGrowPanel<ElementPanel, Element> getPanelIngredients() {
-		return panel.getPanelElements();
+		return firstComponentMatching(AutoGrowPanel.class, panel);
 	}
 
 	@Test

+ 2 - 1
src/test/java/org/leumasjaffe/recipe/view/StepPanelIT.java

@@ -39,8 +39,9 @@ class StepPanelIT extends SwingTestCase {
 		clearInvocations(listener);
 	}
 	
+	@SuppressWarnings("unchecked")
 	private AutoGrowPanel<IngredientPanel, Ingredient> getPanelIngredients() {
-		return panel.getPanelIngredients();
+		return firstComponentMatching(AutoGrowPanel.class, panel);
 	}
 
 	@Test

+ 55 - 9
src/test/java/org/leumasjaffe/recipe/view/SwingTestCase.java

@@ -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;
+	}
 }