📄 markupcontainer.java
字号:
final Component component = get(id); if (component != null) { remove(component); } else { throw new WicketRuntimeException("Unable to find a component with id '" + id + "' to remove"); } } /** * Removes all children from this container. * <p> * Note: implementation does not call {@link MarkupContainer#remove(Component) } for each * component. */ public final void removeAll() { if (children != null) { addStateChange(new Change() { private static final long serialVersionUID = 1L; final Object removedChildren = children; public String toString() { return "RemoveAllChange[component: " + getPath() + ", removed Children: " + removedChildren + "]"; } public void undo() { children = removedChildren; int size = children_size(); for (int i = 0; i < size; i++) { // Get next child final Component child = children_get(i); child.setParent(MarkupContainer.this); } } }); // Loop through child components int size = children_size(); for (int i = 0; i < size; i++) { // Get next child final Component child = children_get(i); // Do not call remove() because the state change would than be // recorded twice. child.detachModel(); child.setParent(null); } children = null; } } /** * Renders the entire associated markup stream for a container such as a Border or Panel. Any * leading or trailing raw markup in the associated markup is skipped. * * @param openTagName * the tag to render the associated markup for * @param exceptionMessage * message that will be used for exceptions */ public final void renderAssociatedMarkup(final String openTagName, final String exceptionMessage) { // Get markup associated with Border or Panel component final MarkupStream originalMarkupStream = getMarkupStream(); final MarkupStream associatedMarkupStream = getAssociatedMarkupStream(true); // skip until the targetted tag is found associatedMarkupStream.skipUntil(openTagName); setMarkupStream(associatedMarkupStream); // Get open tag in associated markup of border component final ComponentTag associatedMarkupOpenTag = associatedMarkupStream.getTag(); // Check for required open tag name if (!((associatedMarkupOpenTag != null) && associatedMarkupOpenTag.isOpen() && (associatedMarkupOpenTag instanceof WicketTag))) { associatedMarkupStream.throwMarkupException(exceptionMessage); } try { setIgnoreAttributeModifier(true); renderComponentTag(associatedMarkupOpenTag); associatedMarkupStream.next(); if (getApplication().getDebugSettings().isOutputMarkupContainerClassName()) { getResponse().write("<!-- MARKUP FOR "); getResponse().write(getClass().getName()); getResponse().write(" BEGIN -->"); } renderComponentTagBody(associatedMarkupStream, associatedMarkupOpenTag); if (getApplication().getDebugSettings().isOutputMarkupContainerClassName()) { getResponse().write("<!-- MARKUP FOR "); getResponse().write(getClass().getName()); getResponse().write(" END -->"); } renderClosingComponentTag(associatedMarkupStream, associatedMarkupOpenTag, false); setMarkupStream(originalMarkupStream); } finally { setIgnoreAttributeModifier(false); } } /** * Replaces a child component of this container with another * * @param child * The child * @throws IllegalArgumentException * Thrown if there was no child with the same id. * @return This */ public final MarkupContainer replace(final Component child) { checkHierarchyChange(child); if (child == null) { throw new IllegalArgumentException("argument child must be not null"); } if (log.isDebugEnabled()) { log.debug("Replacing " + child.getId() + " in " + this); } if (child.getParent() != this) { // Add to map final Component replaced = put(child); // Look up to make sure it was already in the map if (replaced == null) { throw new WicketRuntimeException( exceptionMessage("Cannot replace a component which has not been added: id='" + child.getId() + "', component=" + child)); } // first remove the component. removedComponent(replaced); // then add the other one. addedComponent(child); // The position of the associated markup remains the same child.markupIndex = replaced.markupIndex; // The generated markup id remains the same child.setMarkupIdImpl(replaced.getMarkupIdImpl()); } return this; } /** * @see org.apache.wicket.Component#setModel(org.apache.wicket.model.IModel) */ public Component setModel(final IModel model) { final IModel previous = getModelImpl(); super.setModel(model); if (previous instanceof IComponentInheritedModel) { visitChildren(new IVisitor() { public Object component(Component component) { IModel compModel = component.getModel(); if (compModel instanceof IWrapModel) { compModel = ((IWrapModel)compModel).getWrappedModel(); } if (compModel == previous) { component.setModel(null); } else if (compModel == model) { component.modelChanged(); } return IVisitor.CONTINUE_TRAVERSAL; } }); } return this; } /** * Get the number of children in this container. * * @return Number of children in this container */ public final int size() { return children_size(); } /** * @see org.apache.wicket.Component#toString() */ public String toString() { return toString(false); } /** * @param detailed * True if a detailed string is desired * @return String representation of this container */ public String toString(final boolean detailed) { final StringBuffer buffer = new StringBuffer(); buffer.append("[MarkupContainer "); buffer.append(super.toString(true)); if (detailed) { if (getMarkupStream() != null) { buffer.append(", markupStream = " + getMarkupStream()); } if (children_size() != 0) { buffer.append(", children = "); // Loop through child components final int size = children_size(); for (int i = 0; i < size; i++) { // Get next child final Component child = children_get(i); if (i != 0) { buffer.append(' '); } buffer.append(child.toString()); } } } buffer.append(']'); return buffer.toString(); } /** * Traverses all child components of the given class in this container, calling the visitor's * visit method at each one. * * @param clazz * The class of child to visit, or null to visit all children * @param visitor * The visitor to call back to * @return The return value from a visitor which halted the traversal, or null if the entire * traversal occurred */ public final Object visitChildren(final Class clazz, final IVisitor visitor) { if (visitor == null) { throw new IllegalArgumentException("argument visitor may not be null"); } // Iterate through children of this container for (int i = 0; i < children_size(); i++) { // Get next child component final Component child = children_get(i); Object value = null; // Is the child of the correct class (or was no class specified)? if (clazz == null || clazz.isInstance(child)) { // Call visitor value = visitor.component(child); // If visitor returns a non-null value, it halts the traversal if ((value != IVisitor.CONTINUE_TRAVERSAL) && (value != IVisitor.CONTINUE_TRAVERSAL_BUT_DONT_GO_DEEPER)) { return value; } } // If child is a container if ((child instanceof MarkupContainer) && (value != IVisitor.CONTINUE_TRAVERSAL_BUT_DONT_GO_DEEPER)) { // visit the children in the container value = ((MarkupContainer)child).visitChildren(clazz, visitor); // If visitor returns a non-null value, it halts the traversal if ((value != IVisitor.CONTINUE_TRAVERSAL) && (value != IVisitor.CONTINUE_TRAVERSAL_BUT_DONT_GO_DEEPER)) { return value; } } } return null; } /** * Traverses all child components in this container, calling the visitor's visit method at each * one. * * @param visitor * The visitor to call back to * @return The return value from a visitor which halted the traversal, or null if the entire * traversal occurred */ public final Object visitChildren(final IVisitor visitor) { return visitChildren(null, visitor); } /** * @param component * Component being added */ private final void addedComponent(final Component component) { // Check for degenerate case if (component == this) { throw new IllegalArgumentException("Component can't be added to itself"); } MarkupContainer parent = component.getParent(); if (parent != null) { parent.remove(component); } // Set child's parent component.setParent(this); final Page page = findPage(); final IDebugSettings debugSettings = Application.get().getDebugSettings(); if (debugSettings.isLinePreciseReportingOnAddComponentEnabled()) { component.setMetaData(ADDED_AT_KEY, Strings.toString(component, new MarkupException( "added"))); } if (page != null) { page.componentAdded(component); } // if the PREPARED_FOR_RENDER flag is set, we have already called // beforeRender on this // component's children. So we need to initialize the newly added one if (isPreparedForRender()) { component.beforeRender(); } } /** * @param child * Child to add */ private final void children_add(final Component child) { if (children == null) { children = child; } else { // Get current list size final int size = children_size(); // Create array that holds size + 1 elements final Object[] children = new Object[size + 1]; // Loop through existing children copying them for (int i = 0; i < size; i++) { children[i] = children_get(i, false); } // Add new child to the end children[size] = child; // Save new children this.children = children; } } private final Component children_get(int index) { return (Component)children_get(index, true); } /** * If the given object is a {@link ComponentSourceEntry} instance and <code>reconstruct</code> * is true, it reconstructs the component and returns it. Otherwise it just returns the object * passed as parameter * * @param object * @param reconstruct * @param parent * @param index * @return */ private final Object postprocess(Object object, boolean reconstruct, MarkupContainer parent, int index) { if (reconstruct && object instanceof ComponentSourceEntry) { object = ((ComponentSourceEntry)object).reconstruct(parent, index); } return object; } private final Object children_get(int index, boolean reconstruct) { Object component = null; if (index == 0 && children != null && children instanceof Object[] == false) { component = postprocess(children, reconstruct, this, 0); if (children != component) { children = component; } } else { // we have a list Object[] children = (Object[])this.children; component = postprocess(children[index], reconstruct, this, index); if (children[index] != component) { children[index] = component; } } return component; } /** * Returns the wicket:id of the given object, that can be either a {@link Component} or a * {@link ComponentSourceEntry} * * @param object * @return */ private final String getId(Object object) { if (object instanceof Component) { return ((Component)object).getId(); } else if (object instanceof ComponentSourceEntry) { return ((ComponentSourceEntry)object).id; } else { throw new IllegalArgumentException("Unknown type of object " + object); } } private final Component children_get(final String id) { Component component = null; if (children != null && children instanceof Object[] == false && getId(children).equals(id)) { component = (Component)postprocess(children, true, this, 0); if (children != component) { children = component; } } else if (children instanceof Object[]) { final Object[] children = (Object[])this.children; { for (int i = 0; i < children.length; i++) { if (getId(children[i]).equals(id)) { component = (Component)postprocess(children[i], true, this, i); if (children[i] != component) { children[i] = component; } break; } } } } return component; } private final int children_indexOf(Component child) { if (children != null && children instanceof Object[] == false && getId(children).equals(child.getId())) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -