1 /*
2 * Copyright 2004 The Apache Software Foundation.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package javax.faces.component;
17
18 import java.io.Serializable;
19 import java.util.AbstractList;
20 import java.util.ArrayList;
21 import java.util.List;
22
23 /**
24 * @author Manfred Geiler (latest modification by $Author: bdudney $)
25 * @version $Revision: 225333 $ $Date: 2005-07-26 11:49:19 -0400 (Tue, 26 Jul 2005) $
26 */
27 class _ComponentChildrenList
28 extends AbstractList
29 implements Serializable
30 {
31 private static final long serialVersionUID = -6775078929331154224L;
32 private UIComponent _component;
33 private List _list = new ArrayList();
34
35 _ComponentChildrenList(UIComponent component)
36 {
37 _component = component;
38 }
39
40 public Object get(int index)
41 {
42 return _list.get(index);
43 }
44
45 public int size()
46 {
47 return _list.size();
48 }
49
50 public Object set(int index, Object value)
51 {
52 checkValue(value);
53 setNewParent((UIComponent)value);
54 UIComponent child = (UIComponent) _list.set(index, value);
55 if (child != null) child.setParent(null);
56 return child;
57 }
58
59 public boolean add(Object value)
60 {
61 checkValue(value);
62 setNewParent((UIComponent)value);
63 return _list.add(value);
64 }
65
66 public void add(int index, Object value)
67 {
68 checkValue(value);
69 setNewParent((UIComponent)value);
70 _list.add(index, value);
71 }
72
73 public Object remove(int index)
74 {
75 UIComponent child = (UIComponent) _list.remove(index);
76 if (child != null) child.setParent(null);
77 return child;
78 }
79
80
81 private void setNewParent(UIComponent child)
82 {
83 UIComponent oldParent = child.getParent();
84 if (oldParent != null)
85 {
86 oldParent.getChildren().remove(child);
87 }
88 child.setParent(_component);
89 }
90
91 private void checkValue(Object value)
92 {
93 if (value == null) throw new NullPointerException("value");
94 if (!(value instanceof UIComponent)) throw new ClassCastException("value is not a UIComponent");
95 }
96
97 }