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 org.apache.myfaces.trinidad.component;
20  
21  import javax.faces.component.UIComponent;
22  
23  import java.util.Map;
24  
25  import org.apache.myfaces.trinidad.util.ArrayMap;
26  
27  /**
28   * HashMap used for storing facets.
29   *
30   */
31  class FacetHashMap extends ArrayMap<String, UIComponent>
32  {
33    public FacetHashMap(UIComponent parent)
34    {
35      super(0, 5);
36      _parent = parent;
37    }
38  
39    @Override
40    public void clear()
41    {
42      for(UIComponent value : values())
43      {
44        value.setParent(null);
45      }
46  
47      super.clear();
48    }
49    
50    @Override
51    public UIComponent put(String key, UIComponent value)
52    {
53      if ((key == null) || (value == null))
54      {
55        throw new NullPointerException();
56      }
57  
58      UIComponent previous = super.get(key);
59      if (previous != null)
60      {
61        previous.setParent(null);
62      }
63      
64      if (value.getParent() != null)
65      {
66        ChildArrayList.__removeFromParent(value, -1);
67      }
68      
69      // calling setParent triggers an addEvent, which might have listeners, 
70      // so first put the component in the map, then set the parent
71      UIComponent comp = super.put(key, value);
72      value.setParent(_parent);
73      return comp;
74    }
75  
76    @Override
77    public void putAll(Map<? extends String, ? extends UIComponent> map)
78    {
79      if (map == null)
80      {
81        throw new NullPointerException();
82      }
83  
84      for(Map.Entry<? extends String, ? extends UIComponent> entry : map.entrySet())
85      {
86        put(entry.getKey(), entry.getValue());
87      }
88    }
89    
90    @Override
91    public UIComponent remove(Object key)
92    {
93      UIComponent previous = super.remove(key);
94      if (previous != null)
95      {
96        previous.setParent(null);
97      }
98      
99      return (previous);
100   }
101 
102   private UIComponent _parent;
103 }