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.orchestra.dynaForm.jsf.component;
20  
21  import javax.faces.component.UIComponentBase;
22  import java.util.Iterator;
23  import java.util.LinkedHashMap;
24  import java.util.Map;
25  
26  /**
27   * An instance of this type can hold any number of DynaConfig components as children.
28   * <p>
29   * It provides simple lookup methods for finding a child DynaConfig object given
30   * the name of the "for" attribute on that DynaConfig instance. 
31   */
32  public class DynaConfigs extends UIComponentBase
33  {
34      public static final String COMPONENT_TYPE = "org.apache.myfaces.orchestra.dynaForm.DynaConfigs";
35      public static final String COMPONENT_FAMILY = "org.apache.myfaces.orchestra.dynaForm.DynaForm";
36  
37      private transient Map<String, DynaConfig> configMap = null;
38  
39      @Override
40      public String getFamily()
41      {
42          return COMPONENT_FAMILY;
43      }
44  
45      public DynaConfig getConfig(String name)
46      {
47          Map<String, DynaConfig> configMap = getConfigMap();
48          return configMap.get(name);
49      }
50  
51      protected Map<String, DynaConfig> getConfigMap()
52      {
53          if (configMap != null)
54          {
55              return configMap;
56          }
57          configMap = new LinkedHashMap<String, DynaConfig>();
58  
59          for (Object child : getChildren())
60          {
61              if (child instanceof DynaConfig)
62              {
63                  DynaConfig dynaConfig = (DynaConfig) child;
64                  if (configMap.containsKey(dynaConfig.getFor()))
65                  {
66                      throw new IllegalStateException("duplicate config for property '" + dynaConfig.getFor() + "'");
67                  }
68                  configMap.put(dynaConfig.getFor(), dynaConfig);
69              }
70          }
71  
72          return configMap;
73      }
74  
75      public Iterator<DynaConfig> iterator()
76      {
77          return getConfigMap().values().iterator();
78      }
79  }