/* Wotonomy: OpenStep design patterns for pure Java applications. Copyright (C) 2000 Blacksmith, Inc. This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, see http://www.gnu.org */ package net.wotonomy.web; import java.util.Iterator; import java.util.List; import net.wotonomy.foundation.NSMutableArray; /** * This class represents a parent node in an element tree. * It has no content in itself, and exists only to forward * messages to each of its children, in turn. * Package access only, as it is not in the specification. * * @author michael@mpowers.net * @author $Author: cgruber $ * @version $Revision: 905 $ */ class WOParentElement extends WOElement { NSMutableArray children; /** * Default constructor. */ public WOParentElement() { children = new NSMutableArray(); } /** * Returns an element with the specified children. */ public WOParentElement( List childElements ) { this(); children.addAll( childElements ); } /** * Package access only. Called to initialize the component with * the proper context before the start of the request-response cycle. * If the context has a current component, that component becomes * this component's parent. */ void ensureAwakeInContext (WOContext aContext) { WOElement element; Iterator it = children.iterator(); while ( it.hasNext() ) { element = (WOElement) it.next(); aContext.pushElement( element ); element.ensureAwakeInContext( aContext ); aContext.popElement(); } } /** * Forwards this message to all child elements. */ public void takeValuesFromRequest ( WORequest aRequest, WOContext aContext) { WOElement element; // aContext.incrementLastElementIDComponent(); aContext.appendZeroElementIDComponent(); Iterator it = children.iterator(); while ( it.hasNext() ) { element = (WOElement) it.next(); aContext.pushElement( element ); aContext.incrementLastElementIDComponent(); element.takeValuesFromRequest( aRequest, aContext ); aContext.popElement(); } aContext.deleteLastElementIDComponent(); } /** * Forwards this message to all child elements, * returning the first non-null result. */ public WOActionResults invokeAction ( WORequest aRequest, WOContext aContext) { WOElement element; // aContext.incrementLastElementIDComponent(); aContext.appendZeroElementIDComponent(); WOActionResults result = null; Iterator it = children.iterator(); while ( it.hasNext() ) { element = (WOElement) it.next(); aContext.pushElement( element ); aContext.incrementLastElementIDComponent(); result = element.invokeAction( aRequest, aContext ); aContext.popElement(); if ( result != null ) break; } aContext.deleteLastElementIDComponent(); return result; } /** * Forwards this message to all child elements. */ public void appendToResponse (WOResponse aResponse, WOContext aContext) { WOElement element; // aContext.incrementLastElementIDComponent(); aContext.appendZeroElementIDComponent(); // for each child element Iterator it = children.iterator(); while ( it.hasNext() ) { element = (WOElement) it.next(); aContext.pushElement( element ); aContext.incrementLastElementIDComponent(); // forward the message element.appendToResponse( aResponse, aContext ); aContext.popElement(); } aContext.deleteLastElementIDComponent(); } public WOResponse generateResponse() { WOResponse r = new WOResponse(); return r; } public String toString() { StringBuffer result = new StringBuffer(); result.append( "[WOParentElement: " ); // for each child element Iterator it = children.iterator(); while ( it.hasNext() ) { result.append( "[ " ); result.append( it.next().toString() ); result.append( " ]" ); } result.append( " ]" ); return result.toString(); } }