1
|
|
/*
|
2
|
|
* Angkor Web Framework
|
3
|
|
*
|
4
|
|
* Distributable under LGPL license.
|
5
|
|
* See terms of license at gnu.org.
|
6
|
|
*/
|
7
|
|
|
8
|
|
package com.tirsen.angkor.widget;
|
9
|
|
|
10
|
|
import com.tirsen.angkor.RenderContext;
|
11
|
|
import com.tirsen.angkor.View;
|
12
|
|
import com.tirsen.angkor.Component;
|
13
|
|
|
14
|
|
import java.io.IOException;
|
15
|
|
import java.util.ArrayList;
|
16
|
|
import java.util.Collection;
|
17
|
|
import java.util.Collections;
|
18
|
|
import java.util.Iterator;
|
19
|
|
import java.util.List;
|
20
|
|
|
21
|
|
/**
|
22
|
|
* Default implementation of a component that contains other components.
|
23
|
|
*
|
24
|
|
* <!-- $Id: Container.java,v 1.4 2002/10/13 19:59:23 tirsen Exp $ -->
|
25
|
|
*
|
26
|
|
* @author $Author: tirsen $
|
27
|
|
* @version $Revision: 1.4 $
|
28
|
|
*/
|
29
|
|
public class Container extends Component
|
30
|
|
{
|
31
|
|
protected List children = new ArrayList();
|
32
|
|
private boolean childrenCreated = false;
|
33
|
|
|
34
|
11
|
public Container()
|
35
|
|
{
|
36
|
|
}
|
37
|
|
|
38
|
0
|
public Container(String id)
|
39
|
|
{
|
40
|
0
|
super(id);
|
41
|
|
}
|
42
|
|
|
43
|
|
/**
|
44
|
|
* Iterates the collection even if it is <code>null</code>.
|
45
|
|
*/
|
46
|
5
|
protected static Iterator iterateCollection(Collection coll)
|
47
|
|
{
|
48
|
5
|
Iterator it;
|
49
|
5
|
if (coll == null)
|
50
|
|
{
|
51
|
0
|
it = Collections.EMPTY_LIST.iterator();
|
52
|
|
}
|
53
|
|
else
|
54
|
|
{
|
55
|
5
|
it = coll.iterator();
|
56
|
|
}
|
57
|
5
|
return it;
|
58
|
|
}
|
59
|
|
|
60
|
|
|
61
|
5
|
public void renderChildren(RenderContext context) throws IOException
|
62
|
|
{
|
63
|
5
|
Collection elements = getChildren();
|
64
|
5
|
Iterator iterator = iterateCollection(elements);
|
65
|
5
|
while (iterator.hasNext())
|
66
|
|
{
|
67
|
9
|
View element = (View) iterator.next();
|
68
|
9
|
element.render(context);
|
69
|
|
}
|
70
|
|
}
|
71
|
|
|
72
|
30
|
public List getChildren()
|
73
|
|
{
|
74
|
30
|
maybeCreateChildren();
|
75
|
30
|
return children;
|
76
|
|
}
|
77
|
|
|
78
|
0
|
public int getNumberOfChildren()
|
79
|
|
{
|
80
|
0
|
return getChildren().size();
|
81
|
|
}
|
82
|
|
|
83
|
|
/**
|
84
|
|
* Iterates <em>all</em> elements of this container, not just the main elements.
|
85
|
|
*/
|
86
|
0
|
public Iterator iterateAllElements()
|
87
|
|
{
|
88
|
0
|
return iterateCollection(getChildren());
|
89
|
|
}
|
90
|
|
|
91
|
|
/**
|
92
|
|
* Inserts main element at specified index. All other add-methods call this method, thus subclasses
|
93
|
|
* only need to overload this method to provide additional behaviour.
|
94
|
|
*/
|
95
|
8
|
public void add(int index, View element)
|
96
|
|
{
|
97
|
8
|
element.setContainer(this);
|
98
|
8
|
getChildren().add(index, element);
|
99
|
|
}
|
100
|
|
|
101
|
|
/**
|
102
|
|
* Add a main element.
|
103
|
|
*/
|
104
|
8
|
public void add(View element)
|
105
|
|
{
|
106
|
8
|
add(getChildren().size(), element);
|
107
|
|
}
|
108
|
|
|
109
|
|
/**
|
110
|
|
* Remove a main element.
|
111
|
|
*/
|
112
|
0
|
public void remove(Component element)
|
113
|
|
{
|
114
|
0
|
getChildren().remove(element);
|
115
|
0
|
element.setContainer(null);
|
116
|
|
}
|
117
|
|
|
118
|
0
|
public void parse(RenderContext context)
|
119
|
|
{
|
120
|
0
|
super.parse(context);
|
121
|
0
|
if (isVisible())
|
122
|
|
{
|
123
|
0
|
for (Iterator iterator = iterateAllElements(); iterator.hasNext();)
|
124
|
|
{
|
125
|
0
|
View element = (View) iterator.next();
|
126
|
0
|
element.parse(context);
|
127
|
|
}
|
128
|
|
}
|
129
|
|
}
|
130
|
|
|
131
|
0
|
public void render(RenderContext context) throws IOException
|
132
|
|
{
|
133
|
0
|
renderChildren(context);
|
134
|
|
}
|
135
|
|
|
136
|
|
/**
|
137
|
|
* Overload to create any default children in subclasses. Will be executed before any children
|
138
|
|
* is actually needed by the framework.
|
139
|
|
*/
|
140
|
6
|
protected void createChildren()
|
141
|
|
{
|
142
|
|
// no default children
|
143
|
|
}
|
144
|
|
|
145
|
30
|
protected void maybeCreateChildren()
|
146
|
|
{
|
147
|
30
|
if (!isChildrenCreated())
|
148
|
|
{
|
149
|
6
|
childrenCreated = true;
|
150
|
6
|
createChildren();
|
151
|
|
}
|
152
|
|
}
|
153
|
|
|
154
|
30
|
public boolean isChildrenCreated()
|
155
|
|
{
|
156
|
30
|
return childrenCreated;
|
157
|
|
}
|
158
|
|
}
|
159
|
|
|