summaryrefslogtreecommitdiff
path: root/projects/net.wotonomy.web/src/main/java/net/wotonomy/web/WOParentElement.java
blob: cf84b17d30d0da9ca02886fef67283024f906722 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*
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();
	}

}