View Javadoc

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