View Javadoc

1   /*
2   Wotonomy: OpenStep design patterns for pure Java applications.
3   Copyright (C) 2000 Blacksmith, Inc.
4   
5   This library is free software; you can redistribute it and/or
6   modify it under the terms of the GNU Lesser General Public
7   License as published by the Free Software Foundation; either
8   version 2.1 of the License, or (at your option) any later version.
9   
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  Lesser General Public License for more details.
14  
15  You should have received a copy of the GNU Lesser General Public
16  License along with this library; if not, see http://www.gnu.org
17  */
18  
19  package net.wotonomy.web;
20  
21  import java.util.Iterator;
22  import java.util.List;
23  
24  import net.wotonomy.foundation.NSMutableArray;
25  
26  /***
27  * This class represents a parent node in an element tree.
28  * It has no content in itself, and exists only to forward
29  * messages to each of its children, in turn.
30  * Package access only, as it is not in the specification.
31  *
32  * @author michael@mpowers.net
33  * @author $Author: cgruber $
34  * @version $Revision: 905 $
35  */
36  class WOParentElement extends WOElement
37  {
38  	NSMutableArray children;
39  
40  	/***
41  	* Default constructor.  
42  	*/
43  	public WOParentElement()
44  	{
45  		children = new NSMutableArray();
46  	}
47  	
48  	/***
49  	* Returns an element with the specified children.
50  	*/
51  	public WOParentElement( List childElements )
52  	{
53  		this();
54  		children.addAll( childElements );
55  	}
56  	
57      /***
58      * Package access only.  Called to initialize the component with 
59      * the proper context before the start of the request-response cycle.
60      * If the context has a current component, that component becomes
61      * this component's parent.
62      */
63      void ensureAwakeInContext (WOContext aContext)
64      {
65          WOElement element;
66      	Iterator it = children.iterator();
67  	    while ( it.hasNext() )
68  	    {
69              element = (WOElement) it.next();
70              aContext.pushElement( element );
71  	    	element.ensureAwakeInContext( aContext );
72              aContext.popElement();
73  	    }
74      }
75  
76  	/***
77  	* Forwards this message to all child elements.
78  	*/
79      public void takeValuesFromRequest (
80      	WORequest aRequest, WOContext aContext)
81      {
82      	WOElement element;
83  	    
84  //        aContext.incrementLastElementIDComponent();
85          aContext.appendZeroElementIDComponent();
86          
87      	Iterator it = children.iterator();
88  	    while ( it.hasNext() )
89  	    {
90              element = (WOElement) it.next();
91              aContext.pushElement( element );
92              aContext.incrementLastElementIDComponent();
93  	    	element.takeValuesFromRequest( aRequest, aContext );
94              aContext.popElement();
95  	    }
96          
97          aContext.deleteLastElementIDComponent();
98      }
99  
100     /***
101 	* Forwards this message to all child elements,
102 	* returning the first non-null result.
103     */
104     public WOActionResults invokeAction (
105     	WORequest aRequest, WOContext aContext)
106     {
107     	WOElement element;
108 	    
109 //        aContext.incrementLastElementIDComponent();
110         aContext.appendZeroElementIDComponent();
111 
112     	WOActionResults result = null;
113     	Iterator it = children.iterator();
114 	    while ( it.hasNext() )
115 	    {
116 	    	element = (WOElement) it.next();
117             aContext.pushElement( element );
118             aContext.incrementLastElementIDComponent();
119 	    	result = element.invokeAction( aRequest, aContext );
120             aContext.popElement();
121 		    if ( result != null ) break;
122 	    }
123         
124         aContext.deleteLastElementIDComponent();
125 	    return result;
126     }
127     
128     /***
129 	* Forwards this message to all child elements.
130     */
131     public void appendToResponse (WOResponse aResponse, WOContext aContext)
132     {
133     	WOElement element;
134 	    
135 //        aContext.incrementLastElementIDComponent();
136         aContext.appendZeroElementIDComponent();
137         
138 	    // for each child element
139     	Iterator it = children.iterator();
140 	    while ( it.hasNext() )
141 	    {
142 	    	element = (WOElement) it.next();
143             aContext.pushElement( element );
144             aContext.incrementLastElementIDComponent();
145 
146 		    // forward the message
147 	    	element.appendToResponse( 
148 		    	aResponse, aContext );
149                 
150             aContext.popElement();
151 
152 	    }
153         aContext.deleteLastElementIDComponent();
154     }
155     
156     public WOResponse generateResponse() 
157     {
158         WOResponse r = new WOResponse();
159         return r;
160     }
161     
162     public String toString()
163     {
164         StringBuffer result = new StringBuffer();
165         result.append( "[WOParentElement: " );
166 	    // for each child element
167     	Iterator it = children.iterator();
168 	    while ( it.hasNext() )
169 	    {
170             result.append( "[ " );
171             result.append( it.next().toString() );
172             result.append( " ]" );
173 	    }
174         result.append( " ]" );
175         return result.toString();
176     }
177     
178 }