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