/** * Returns true if this component is the same as or an ancestor of the specified component. * * @param comp Component to test. * @return True if this component is the same as or an ancestor of the specified component. */ public boolean isAncestor(BaseComponent comp) { while (comp != null && comp != this) { comp = comp.getParent(); } return comp != null; }
/** * Finds the named attribute, returning its value. If the named attribute does not exist or has * a null value, the parent chain will be searched until a match is found. * * @param name The attribute name. * @return The attribute value, or null if not found. */ public Object findAttribute(String name) { Object value = null; BaseComponent cmp = this; while (cmp != null && (value = cmp.attributes.get(name)) == null) { cmp = cmp.getParent(); } return value; }
/** * Return first ancestor that is of the requested type. * * @param <T> Expected return type. * @param type The type of ancestor sought. * @param includeSelf If true, include this component in the search. * @return The ancestor component of the requested type, or null if none found. */ @SuppressWarnings("unchecked") public <T> T getAncestor(Class<T> type, boolean includeSelf) { BaseComponent cmp = includeSelf ? this : this.getParent(); while (cmp != null) { if (type.isInstance(cmp)) { break; } else { cmp = cmp.getParent(); } } return (T) cmp; }
/** * Moves this child to the specified index within its parent. * * @param index New index for this child. */ public void setIndex(int index) { getParent().addChild(this, index); }
/** * Recurses parent menu nodes to build the menu path. * * @param comp Current component in menu tree. * @param sb String builder to receive path. */ private static void getPath(BaseComponent comp, StringBuilder sb) { while (comp instanceof BaseMenuComponent) { sb.insert(0, "\\" + ((BaseMenuComponent) comp).getLabel()); comp = comp.getParent(); } }
/** * Validates that the specified component is currently a child of this component. * * @param child The component to check. * @exception ComponentException Thrown if the component fails validation. */ protected void validateIsChild(BaseComponent child) { ComponentException.assertTrue(child == null || child.getParent() == this, child, "Child does not belong to this parent"); }
/** * Send an event to all the ancestors of this component. Event propagation stops if any * recipient invokes the <code>stopPropagation</code> method on the event. * * @param event Event to send. * @param includeThis If true, include this component in the recipient chain. */ public void notifyAncestors(Event event, boolean includeThis) { BaseComponent next = includeThis ? this : getParent(); while (next != null && !event.isStopped()) { next.fireEvent(event); next = next.getParent(); } }
/** * Returns the namespace to which this component belongs. May be null. * * @return The namespace to which this component belongs. */ public BaseComponent getNamespace() { BaseComponent comp = this; while (comp != null) { if (comp.isNamespace()) { return comp; } comp = comp.getParent(); } return null; }
/** * Returns the sibling of this component at the specified offset. * * @param offset Offset from this component. For example, 2 would mean the second sibling * following this component. * @return The requested sibling, or null if none exists at the requested offset. */ private BaseComponent getRelativeSibling(int offset) { int i = getIndex(); i = i == -1 ? -1 : i + offset; return i < 0 || i >= getParent().getChildCount() ? null : getParent().getChildAt(i); }
/** * Searches for a drop renderer associated with the specified component or one of its ancestor * components. * * @param component BaseComponent whose drop renderer is sought. * @return The associated drop renderer, or null if none found. */ public static IDropRenderer getDropRenderer(BaseComponent component) { IDropRenderer dropRenderer = null; while (dropRenderer == null && component != null) { dropRenderer = (IDropRenderer) component.getAttribute(DROP_RENDERER_ATTR); component = component.getParent(); } return dropRenderer; }
/** * Locates and returns a controller of the given type by searching up the component tree * starting at the specified component. * * @param <T> The type of controller sought. * @param comp Component for start of search. * @param type The type of controller sought. * @return The controller instance, or null if not found. */ @SuppressWarnings("unchecked") public static <T> T getController(BaseComponent comp, Class<T> type) { while (comp != null) { Object controller = getController(comp); if (type.isInstance(controller)) { return (T) controller; } comp = comp.getParent(); } return null; }
/** * 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; } } }
/** * Display context-sensitive help associated with the specified component. If none is associated * with the component, its parent is examined, and so on. If no help association is found, no * action is taken. * * @param component Component whose CSH is to be displayed. */ public static void showCSH(BaseComponent component) { while (component != null) { HelpContext target = (HelpContext) component.getAttribute(CSH_TARGET); if (target != null) { HelpUtil.show(target); break; } component = component.getParent(); } }
/** * Ensures that this node is visible (i.e., all of its parent tree nodes are expanded. */ public void makeVisible() { BaseComponent node = getParent(); while (node instanceof Treenode) { ((Treenode) node).setCollapsed(false); node = node.getParent(); } scrollIntoView(); }
@EventHandler(value = "click", target = "@btnLeft") private void onClick$btnLeft() { Treenode node = tree.getSelectedNode(); doDrop(node, (Treenode) node.getParent().getParent(), false); }
/** * Adds a child to the child list immediately before the reference child. * * @param child Child to add. * @param before The reference child. */ public void addChild(BaseComponent child, BaseComponent before) { if (before == null) { children.add(-1, child); } else { ComponentException.assertTrue(before.getParent() == this, this, "Reference component does not belong to this parent"); int i = children.indexOf(before); children.add(i, child); } }
protected void remove(BaseComponent child, boolean noSync, boolean destroy) { if (child instanceof IComposite) { BaseComponent root = ((IComposite) child).getCompositeRoot(); BaseComponent parent = root == null ? null : root.getParent(); if (parent != null) { parent.children.remove(root, noSync, destroy); } child.parent = null; modCount++; } else { int index = indexOf(child); ComponentException.assertTrue(index != -1, parent, "Child does not belong to this parent"); parent.beforeRemoveChild(child); parent.nameIndex.remove(child); child.parent = null; delegate.remove(child); modCount++; if (!noSync) { parent.invokeIfAttached("removeChild", child, destroy); } child.dead |= destroy; parent.afterRemoveChild(child); } }