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.Iterator;
25  import java.util.List;
26  import java.util.Map;
27  import java.util.RandomAccess;
28  
29  class _ComponentChildrenList extends AbstractList<UIComponent> implements Serializable, RandomAccess
30  {
31      private static final long serialVersionUID = -6775078929331154224L;
32      private UIComponent _component;
33      private List<UIComponent> _list = new ArrayList<UIComponent>(4);
34  
35      _ComponentChildrenList(UIComponent component)
36      {
37          _component = component;
38      }
39  
40      @Override
41      public UIComponent get(int index)
42      {
43          return _list.get(index);
44      }
45  
46      @Override
47      public int size()
48      {
49          return _list.size();
50      }
51  
52      @Override
53      public UIComponent set(int index, UIComponent value)
54      {
55          checkValue(value);
56          removeChildrenFromParent(value);
57          UIComponent child = _list.set(index, value);
58          if (child != value)
59          {
60              updateParent(value);
61              if (child != null)
62              {
63                  childRemoved(child);
64              }
65          }
66          
67          return child;
68      }
69  
70      @Override
71      public boolean add(UIComponent value)
72      {
73          checkValue(value);
74  
75          removeChildrenFromParent(value);
76          boolean res = _list.add(value);
77          
78          updateParent(value);
79          
80          return res;
81      }
82  
83      @Override
84      public void add(int index, UIComponent value)
85      {
86          checkValue(value);
87          
88          removeChildrenFromParent(value);
89          
90          _list.add(index, value);
91          
92          updateParent(value);
93      }
94  
95      @Override
96      public UIComponent remove(int index)
97      {
98          UIComponent child = _list.remove(index);
99          if (child != null)
100         {
101             childRemoved(child);
102         }
103         
104         return child;
105     }
106 
107     private void checkValue(Object value)
108     {
109         if (value == null)
110         {
111             throw new NullPointerException("value");
112         }
113         
114         // Not necessary anymore because  
115         //if (!(value instanceof UIComponent))
116         //{
117         //    throw new ClassCastException("value is not a UIComponent");
118         //}
119     }
120 
121     private void childRemoved(UIComponent child)
122     {
123         child.setParent(null);
124     }
125 
126     private void updateParent(UIComponent child)
127     {
128         child.setParent(_component);
129     }
130     
131     private void removeChildrenFromParent(UIComponent child)
132     {
133         UIComponent oldParent = child.getParent();
134         if (oldParent != null)
135         {
136             if (!oldParent.getChildren().remove(child))
137             {
138                 // Check if the component is inside a facet and remove from there
139                 if (oldParent.getFacetCount() > 0)
140                 {
141                     for (Iterator< Map.Entry<String, UIComponent > > it = 
142                         oldParent.getFacets().entrySet().iterator() ; it.hasNext() ; )
143                     {
144                         Map.Entry<String, UIComponent > entry = it.next();
145                         
146                         if (entry.getValue().equals(child))
147                         {
148                             it.remove();
149                             break;
150                         }
151                     }
152                 }
153             }
154         }
155     }
156 
157     @Override
158     public boolean remove(Object value)
159     {
160         if (!(value instanceof UIComponent))
161         {
162             throw new ClassCastException("value is not a UIComponent");
163         }
164         
165         checkValue(value);
166 
167         if (_list.remove(value))
168         {
169             childRemoved((UIComponent)value);
170             return true;
171         }
172         return false;
173     }
174 }