01: /*
02: * Copyright 2004 The Apache Software Foundation.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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: mbr $)
25: * @version $Revision: 511124 $ $Date: 2007-02-23 22:56:42 +0100 (Fr, 23 Feb 2007) $
26: */
27: class _ComponentChildrenList extends AbstractList implements
28: Serializable {
29: private static final long serialVersionUID = -6775078929331154224L;
30: private UIComponent _component;
31: private List<Object> _list = new ArrayList<Object>();
32:
33: _ComponentChildrenList(UIComponent component) {
34: _component = component;
35: }
36:
37: public Object get(int index) {
38: return _list.get(index);
39: }
40:
41: public int size() {
42: return _list.size();
43: }
44:
45: public Object set(int index, Object value) {
46: checkValue(value);
47: setNewParent((UIComponent) value);
48: UIComponent child = (UIComponent) _list.set(index, value);
49: if (child != null)
50: child.setParent(null);
51: return child;
52: }
53:
54: public boolean add(Object value) {
55: checkValue(value);
56: setNewParent((UIComponent) value);
57: return _list.add(value);
58: }
59:
60: public void add(int index, Object value) {
61: checkValue(value);
62: setNewParent((UIComponent) value);
63: _list.add(index, value);
64: }
65:
66: public Object remove(int index) {
67: UIComponent child = (UIComponent) _list.remove(index);
68: if (child != null)
69: child.setParent(null);
70: return child;
71: }
72:
73: private void setNewParent(UIComponent child) {
74: UIComponent oldParent = child.getParent();
75: if (oldParent != null) {
76: oldParent.getChildren().remove(child);
77: }
78: child.setParent(_component);
79: }
80:
81: private void checkValue(Object value) {
82: if (value == null)
83: throw new NullPointerException("value");
84: if (!(value instanceof UIComponent))
85: throw new ClassCastException("value is not a UIComponent");
86: }
87:
88: }
|