/** * Retrieves the components for the given term. * * @param term the term of the children to be retrieved. * @return the child components for the given term, may be empty. */ private List<WComponent> getComponentsForTerm(final String term) { List<WComponent> childList = content.getComponentModel().getChildren(); List<WComponent> result = new ArrayList<>(); if (childList != null) { for (int i = 0; i < childList.size(); i++) { WComponent child = childList.get(i); if (term.equals(child.getTag())) { result.add(child); } } } return result; }
/** * Groups a definition list's child components by their term for rendering. * * @return a list of this definition list's children grouped by their terms. */ public List<Duplet<String, ArrayList<WComponent>>> getTerms() { Map<String, Duplet<String, ArrayList<WComponent>>> componentsByTerm = new HashMap<>(); List<Duplet<String, ArrayList<WComponent>>> result = new ArrayList<>(); List<WComponent> childList = content.getComponentModel().getChildren(); if (childList != null) { for (int i = 0; i < childList.size(); i++) { WComponent child = childList.get(i); String term = child.getTag(); Duplet<String, ArrayList<WComponent>> termComponents = componentsByTerm.get(term); if (termComponents == null) { termComponents = new Duplet<>(term, new ArrayList<WComponent>()); componentsByTerm.put(term, termComponents); result.add(termComponents); } termComponents.getSecond().add(child); } } return result; }
String tag = child.getTag();
@Test public void testAddTerm() { WDefinitionList list = new WDefinitionList(); String term1 = "WDefinitionList_Test.testAddTerm.term1"; String term2 = "WDefinitionList_Test.testAddTerm.term2"; WComponent term2data1 = new DefaultWComponent(); WComponent term2data2 = new DefaultWComponent(); WComponent term2data3 = new DefaultWComponent(); // Test addition of term with no component list.addTerm(term1); Container termContainer = (Container) list.getChildAt(0); Assert.assertEquals("Term1 should have been added", 1, termContainer.getChildCount()); Assert.assertEquals("Incorrect value for Term1", term1, termContainer.getChildAt(0).getTag()); // Test addition of term with multiple components list.addTerm(term2, term2data1, term2data2); list.addTerm(term2, term2data3); Assert.assertEquals("Incorrect term for component 1", term2, term2data1.getTag()); Assert.assertEquals("Incorrect term for component 2", term2, term2data2.getTag()); Assert.assertEquals("Incorrect term for component 3", term2, term2data3.getTag()); }
@Test public void testRemoveTerm() { WDefinitionList list = new WDefinitionList(); String term1 = "WDefinitionList_Test.testRemoveTerm.term1"; String term2 = "WDefinitionList_Test.testRemoveTerm.term2"; WComponent term2data1 = new DefaultWComponent(); WComponent term2data2 = new DefaultWComponent(); list.addTerm(term1); list.addTerm(term2, term2data1, term2data2); Container termContainer = (Container) list.getChildAt(0); Assert.assertEquals("Incorrect term data", 3, termContainer.getChildren().size()); list.removeTerm(term2); Assert.assertEquals("Incorrect term data", 1, termContainer.getChildCount()); Assert.assertEquals("Incorrect value for Term1", term1, termContainer.getChildAt(0).getTag()); } }