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  
20  package org.apache.myfaces.tobago.util;
21  
22  import org.apache.myfaces.tobago.component.Attributes;
23  import org.apache.myfaces.tobago.component.Facets;
24  import org.apache.myfaces.tobago.component.RendererTypes;
25  import org.apache.myfaces.tobago.component.UIMenuSelectOne;
26  import org.apache.myfaces.tobago.component.UISelectBooleanCheckbox;
27  import org.apache.myfaces.tobago.internal.component.AbstractUISelectBooleanCheckbox;
28  
29  import javax.faces.component.UICommand;
30  import javax.faces.component.UIComponent;
31  import javax.faces.context.FacesContext;
32  import javax.faces.el.ValueBinding;
33  
34  public final class CreateComponentUtils {
35  
36    private CreateComponentUtils() {
37    }
38  
39    public static UIComponent createComponent(
40        final String componentType, final RendererTypes rendererType, final String clientId) {
41      final FacesContext facesContext = FacesContext.getCurrentInstance();
42      return createComponent(facesContext, componentType, rendererType, clientId);
43    }
44  
45    public static UIComponent createComponent(
46        final FacesContext facesContext, final String componentType, final RendererTypes rendererType,
47        final String clientId) {
48      final UIComponent component  = facesContext.getApplication().createComponent(componentType);
49      if (rendererType != null) {
50        component.setRendererType(rendererType.name());
51      }
52      component.setId(clientId);
53      return component;
54    }
55  
56    public static UIMenuSelectOne createUIMenuSelectOneFacet(
57        final FacesContext facesContext, final UICommand command, final String clientId) {
58  
59      final UIMenuSelectOne radio = (UIMenuSelectOne) createComponent(
60          facesContext, UIMenuSelectOne.COMPONENT_TYPE, RendererTypes.SelectOneRadio, clientId);
61      //noinspection unchecked
62      ComponentUtils.setFacet(command, Facets.radio, radio);
63      final ValueBinding valueBinding = command.getValueBinding(Attributes.value.getName());
64      if (valueBinding != null) {
65        radio.setValueBinding(Attributes.value.getName(), valueBinding);
66      } else {
67        radio.setValue(command.getValue());
68      }
69      return radio;
70    }
71  
72    public static AbstractUISelectBooleanCheckbox createUISelectBooleanFacetWithId(
73        final FacesContext facesContext, final UICommand command) {
74      return createUISelectBooleanFacet(facesContext, command, facesContext.getViewRoot().createUniqueId());
75    }
76  
77    public static AbstractUISelectBooleanCheckbox createUISelectBooleanFacet(
78        final FacesContext facesContext, final UICommand command, final String clientId) {
79      final AbstractUISelectBooleanCheckbox checkbox = (AbstractUISelectBooleanCheckbox) createComponent(
80          facesContext, UISelectBooleanCheckbox.COMPONENT_TYPE, RendererTypes.SelectBooleanCheckbox, clientId);
81      //noinspection unchecked
82      ComponentUtils.setFacet(command, Facets.checkbox, checkbox);
83      final ValueBinding valueBinding = command.getValueBinding(Attributes.value.getName());
84      if (valueBinding != null) {
85        checkbox.setValueBinding(Attributes.value.getName(), valueBinding);
86      } else {
87        //noinspection unchecked
88        checkbox.setValue(command.getValue());
89      }
90      return checkbox;
91    }
92  }