/** * Returns an iterable of children of the specified type. * * @param <T> The desired type. * @param type The desired type. * @return An iterable of children of the specified type. Never null. */ public final <T extends BaseComponent> Iterable<T> getChildren(Class<T> type) { return MiscUtil.iterableForType(getChildren(), type); }
public static void disableChildren(BaseComponent parent, boolean disable) { for (BaseComponent child : parent.getChildren()) { if (child instanceof IDisable) { ((IDisable) child).setDisabled(disable); } } }
/** * Returns the style sheet associated with the specified URL. * * @param url URL of style sheet. * @return The associated style sheet, or null if not found. */ private Style findStyleSheet(String url) { for (Style ss : registeredStyles.getChildren(Style.class)) { if (ss.getSrc().equals(url)) { return ss; } } return null; }
private Radiobutton getSelected(BaseComponent parent) { for (Radiobutton child : parent.getChildren(Radiobutton.class)) { if (child.isChecked()) { return child; } } for (BaseComponent child : parent.getChildren()) { Radiobutton rb = child instanceof Radiogroup ? null : getSelected(child); if (rb != null) { return rb; } } return null; }
/** * Alphabetically sorts children under the specified parent. * * @param parent Parent whose child nodes (Treenode) are to be sorted. * @param recurse If true, sorting is recursed through all children. */ public static void sort(BaseComponent parent, boolean recurse) { if (parent == null || parent.getChildren().size() < 2) { return; } int i = 1; int size = parent.getChildren().size(); while (i < size) { Treenode item1 = (Treenode) parent.getChildren().get(i - 1); Treenode item2 = (Treenode) parent.getChildren().get(i); if (compare(item1, item2) > 0) { parent.swapChildren(i - 1, i); i = i == 1 ? 2 : i - 1; } else { i++; } } if (recurse) { for (BaseComponent child : parent.getChildren()) { sort(child, recurse); } } }
private void forwardChangeEvents(BaseComponent root) { for (BaseUIComponent child : root.getChildren(BaseUIComponent.class)) { child.addEventListener("change", changeListener); forwardChangeEvents(child); } }
private void expandOrCollapse(BaseComponent parent, boolean collapse) { for (BaseComponent child : parent.getChildren()) { ((Treenode) child).setCollapsed(collapse); expandOrCollapse(child, collapse); } }
/** * Recursively remove empty menu container elements. This is done after removing menu items to * keep the menu structure lean. * * @param parent The starting menu container. */ public static void pruneMenus(BaseComponent parent) { while (parent != null && parent instanceof BaseMenuComponent) { if (parent.getChildren().isEmpty()) { BaseComponent newParent = parent.getParent(); parent.destroy(); parent = newParent; } else { break; } } }
/** * Expand / collapse tree children to specified depth. * * @param parent Tree children whose tree items are to be expanded / collapsed. * @param depth Expand tree items to this depth. Tree items below this depth are collapsed. */ private static void expandChildren(BaseComponent parent, int depth) { if (parent == null || depth <= 0) { return; } depth--; for (Object object : parent.getChildren()) { Treenode item = (Treenode) object; item.setCollapsed(depth <= 0); expandChildren(item, depth); } }
/** * Alphabetically sorts a range of menu items. * * @param parent Parent whose children are to be sorted alphabetically. * @param startIndex Index of first child to be sorted. * @param endIndex Index of last child to be sorted. */ public static void sortMenu(BaseComponent parent, int startIndex, int endIndex) { List<BaseComponent> items = parent.getChildren(); int bottom = startIndex + 1; for (int i = startIndex; i < endIndex;) { BaseComponent item1 = items.get(i++); BaseComponent item2 = items.get(i); if (item1 instanceof BaseMenuComponent && item2 instanceof BaseMenuComponent && ((BaseMenuComponent) item1) .getLabel().compareToIgnoreCase(((BaseMenuComponent) item2).getLabel()) > 0) { parent.swapChildren(i - 1, i); if (i > bottom) { i -= 2; } } } }
for (BaseComponent cmp : root.getChildren()) { toXML(cmp, child);
/** * Search the plugin's component tree for components (or their controllers) implementing the * IPluginEvent interface. Those that are found are registered as listeners. * * @param cmpt BaseComponent to search */ private void findListeners(BaseComponent cmpt) { for (BaseComponent child : cmpt.getChildren()) { tryRegisterListener(child, true); tryRegisterListener(FrameworkController.getController(child), true); findListeners(child); } }
/** * Returns the menu with the specified label, or creates one if it does not exist. * * @param parent The parent component under which to search. May be a Toolbar or a Menupopup * component. * @param label Label of menu to search. * @param insertBefore If not null, the new menu is inserted before this one. If null, the menu * is appended. * @return Menu or menu item with the specified label. */ public static BaseMenuComponent findMenu(BaseComponent parent, String label, BaseComponent insertBefore) { for (BaseMenuComponent child : parent.getChildren(BaseMenuComponent.class)) { if (label.equalsIgnoreCase(child.getLabel())) { return child; } } BaseMenuComponent cmp = createMenuOrMenuitem(parent); cmp.setLabel(label); parent.addChild(cmp, insertBefore); return cmp; }
MatchMode mode = matchMode != MatchMode.AUTO ? matchMode : index >= 0 ? MatchMode.INDEX : MatchMode.CASE_INSENSITIVE; List<BaseComponent> children = parent.getChildren(); int size = children.size();
/** * Resequence all UI elements to match that of the tree view. * * @param tc Root of subtree. * @param parent The parent UI element. */ protected void resequenceTargets(BaseComponent tc, ElementBase parent) { if (tc != null) { int index = -1; for (BaseComponent child : tc.getChildren()) { index++; Treenode node = (Treenode) child; ElementBase real = getProxy(node).getReal(); real.setParent(parent); real.setIndex(index); resequenceTargets(node, real); } } }
private void test(BaseComponent root, String eventName, int eventData, int expectedSize) { String labelText = eventName + "." + eventData; EventManager.getInstance().fireLocalEvent(eventName, labelText); getMockEnvironment().flushEvents(); assertEquals(expectedSize, root.getChildren().size()); Label label = (Label) root.getChildren().get(expectedSize - 1).getFirstChild(); assertEquals(labelText, label.getLabel()); } }
/** * Sets focus to first input element under the parent that is capable of receiving focus. * * @param parent Parent component. * @param select If true, select contents after setting focus. * @return The input element that received focus, or null if focus was not set. */ public static BaseInputboxComponent<?> focusFirst(BaseComponent parent, boolean select) { for (BaseComponent child : parent.getChildren()) { BaseInputboxComponent<?> ele; if (child instanceof BaseInputboxComponent) { ele = (BaseInputboxComponent<?>) child; if (ele.isVisible() && !ele.isDisabled() && !ele.isReadonly()) { ele.focus(); if (select) { ele.selectAll(); } return ele; } } else if ((ele = focusFirst(child, select)) != null) { return ele; } } return null; }
@Test public void test() throws Exception { CareWebShell shell = new CareWebShell(); shell.setParent(getMockEnvironment().getSession().getPage()); shell.setLayout("/StatusPanelTest.xml"); getMockEnvironment().flushEvents(); ElementPlugin plugin = shell.getActivatedPlugin("cwfStatusPanel"); BaseComponent root = plugin.getOuterComponent().getFirstChild(); StatusPanel controller = (StatusPanel) FrameworkController.getController(root); assertNotNull("Controller must not be null.", controller); assertEquals(1, root.getChildren().size()); test(root, "STATUS", 1, 1); test(root, "STATUS.TEST1", 1, 2); test(root, "STATUS.TEST1", 2, 2); test(root, "STATUS.TEST2", 1, 3); test(root, "STATUS.TEST2", 2, 3); }