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.*;
23  
24  /**
25   * @author Manfred Geiler (latest modification by $Author: skitching $)
26   * @version $Revision: 676278 $ $Date: 2008-07-13 03:35:04 -0500 (Sun, 13 Jul 2008) $
27   */
28  class _ComponentChildrenList
29          extends AbstractList
30          implements Serializable
31  {
32      private static final long serialVersionUID = -6775078929331154224L;
33      private UIComponent _component;
34      private List _list = new ArrayList();
35      private Map _idIndexedMap = new HashMap();
36  
37      _ComponentChildrenList(UIComponent component)
38      {
39          _component = component;
40      }
41      
42      public UIComponent get(String id) {
43         return (UIComponent) _idIndexedMap.get(id);
44      }
45  
46      public Object get(int index)
47      {
48          return _list.get(index);
49      }
50  
51      public int size()
52      {
53          return _list.size();
54      }
55  
56      public Object set(int index, Object value)
57      {
58          checkValue(value);
59          setNewParent((UIComponent)value);
60          UIComponent child = (UIComponent) _list.set(index, value);
61          resetParent(child);
62          return child;
63      }
64  
65      public boolean add(Object value)
66      {
67          checkValue(value);
68          setNewParent((UIComponent)value);
69          return _list.add(value);
70      }
71  
72      public void add(int index, Object value)
73      {
74          checkValue(value);
75          setNewParent((UIComponent)value);
76          _list.add(index, value);
77      }
78  
79      public Object remove(int index)
80      {
81          UIComponent child = (UIComponent) _list.remove(index);
82          resetParent(child);
83          return child;
84      }
85  
86  
87      private void setNewParent(UIComponent child)
88      {
89          UIComponent oldParent = child.getParent();
90          if (oldParent != null)
91          {
92              oldParent.getChildren().remove(child);
93          }
94          child.setParent(_component);
95          _idIndexedMap.put(child.getId(),child);
96      }
97  
98      private void resetParent(UIComponent child) {
99          if (child != null)
100                 child.setParent(null);
101         _idIndexedMap.remove(child.getId());
102     }
103 
104     private void checkValue(Object value)
105     {
106         if (value == null) throw new NullPointerException("value");
107         if (!(value instanceof UIComponent)) throw new ClassCastException("value is not a UIComponent");
108     }
109 
110     public void updateId(String oldId, UIComponent component) {
111         _idIndexedMap.remove(oldId);
112         _idIndexedMap.put(component.getId(), component);
113     }
114 }