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