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.ComponentTypes;
24  import org.apache.myfaces.tobago.component.Facets;
25  import org.apache.myfaces.tobago.component.OnComponentCreated;
26  import org.apache.myfaces.tobago.component.OnComponentPopulated;
27  import org.apache.myfaces.tobago.component.RendererTypes;
28  import org.apache.myfaces.tobago.component.UIMenuSelectOne;
29  import org.apache.myfaces.tobago.internal.component.AbstractUIColumn;
30  import org.apache.myfaces.tobago.internal.component.AbstractUIOut;
31  import org.apache.myfaces.tobago.internal.component.AbstractUISelectBooleanCheckbox;
32  import org.apache.myfaces.tobago.internal.util.ComponentAttributeUtils;
33  import org.apache.myfaces.tobago.layout.Display;
34  import org.apache.myfaces.tobago.layout.LayoutManager;
35  
36  import javax.faces.component.UICommand;
37  import javax.faces.component.UIComponent;
38  import javax.faces.context.FacesContext;
39  import javax.faces.el.ValueBinding;
40  
41  public class CreateComponentUtils {
42  
43    @Deprecated
44    public static UIComponent createComponent(String componentType, String rendererType) {
45      return createComponent(componentType, rendererType, null);
46    }
47  
48    public static UIComponent createComponent(String componentType, String rendererType, String clientId) {
49      final FacesContext facesContext = FacesContext.getCurrentInstance();
50      return createComponent(facesContext, componentType, rendererType, clientId);
51    }
52  
53    @Deprecated
54    public static UIComponent createComponent(FacesContext facesContext, String componentType, String rendererType) {
55      return createComponent(facesContext, componentType, rendererType, null);
56    }
57  
58    public static UIComponent createComponent(
59        FacesContext facesContext, String componentType, String rendererType, String clientId) {
60      UIComponent component  = facesContext.getApplication().createComponent(componentType);
61      component.setRendererType(rendererType);
62      component.setId(clientId);
63      return component;
64    }
65  
66    @Deprecated
67    public static AbstractUIColumn createTextColumn(String label, String sortable, String align, String value) {
68      return createTextColumn(label, sortable, align, value, null);
69    }
70  
71    public static AbstractUIColumn createTextColumn(
72        String label, String sortable, String align, String value, String clientId) {
73      AbstractUIOut text = (AbstractUIOut) createComponent(ComponentTypes.OUT, RendererTypes.OUT, clientId + "_t");
74      ComponentAttributeUtils.setStringProperty(text, Attributes.VALUE, value);
75      ComponentAttributeUtils.setBooleanProperty(text, Attributes.CREATE_SPAN, "false");
76      ComponentAttributeUtils.setBooleanProperty(text, Attributes.ESCAPE, "false");
77      text.setDisplay(Display.INLINE);
78      return createColumn(label, sortable, align, text, clientId);
79    }
80  
81    @Deprecated
82    public static AbstractUIColumn createColumn(String label, String sortable, String align, UIComponent child) {
83      return createColumn(label, sortable, align, child, null);
84    }
85  
86    public static AbstractUIColumn createColumn(
87        String label, String sortable, String align, UIComponent child, String clientId) {
88      AbstractUIColumn column = createColumn(label, sortable, align, clientId);
89      //noinspection unchecked
90      column.getChildren().add(child);
91      return column;
92    }
93  
94    @Deprecated
95    public static AbstractUIColumn createColumn(String label, String sortable, String align) {
96      return createColumn(label, sortable, align, (String) null);
97    }
98  
99    public static AbstractUIColumn createColumn(String label, String sortable, String align, String clientId) {
100     AbstractUIColumn column = (AbstractUIColumn) createComponent(ComponentTypes.COLUMN, null, clientId);
101     ComponentAttributeUtils.setStringProperty(column, Attributes.LABEL, label);
102     ComponentAttributeUtils.setBooleanProperty(column, Attributes.SORTABLE, sortable);
103     ComponentAttributeUtils.setStringProperty(column, Attributes.ALIGN, align);
104     return column;
105   }
106 
107   @Deprecated
108   public static UIMenuSelectOne createUIMenuSelectOneFacet(FacesContext facesContext,
109       javax.faces.component.UICommand command) {
110     return createUIMenuSelectOneFacet(facesContext, command, null);
111   }
112 
113   public static UIMenuSelectOne createUIMenuSelectOneFacet(
114       FacesContext facesContext, javax.faces.component.UICommand command, String clientId) {
115 
116     UIMenuSelectOne radio = (UIMenuSelectOne) createComponent(
117         facesContext, UIMenuSelectOne.COMPONENT_TYPE, RendererTypes.SELECT_ONE_RADIO, clientId);
118     //noinspection unchecked
119     command.getFacets().put(Facets.RADIO, radio);
120     final ValueBinding valueBinding = command.getValueBinding(Attributes.VALUE);
121     if (valueBinding != null) {
122       radio.setValueBinding(Attributes.VALUE, valueBinding);
123     } else {
124       radio.setValue(command.getValue());
125     }
126     return radio;
127   }
128 
129   @Deprecated
130   public static UIComponent createUISelectBooleanFacet(FacesContext facesContext, UICommand command) {
131     return createUISelectBooleanFacet(facesContext, command, null);
132   }
133 
134   public static AbstractUISelectBooleanCheckbox createUISelectBooleanFacetWithId(FacesContext facesContext,
135       UICommand command) {
136     return createUISelectBooleanFacet(facesContext, command, facesContext.getViewRoot().createUniqueId());
137   }
138 
139   public static AbstractUISelectBooleanCheckbox createUISelectBooleanFacet(FacesContext facesContext, UICommand command,
140       String clientId) {
141     AbstractUISelectBooleanCheckbox checkbox = (AbstractUISelectBooleanCheckbox) createComponent(
142         facesContext, ComponentTypes.SELECT_BOOLEAN_CHECKBOX, RendererTypes.SELECT_BOOLEAN_CHECKBOX, clientId);
143     //noinspection unchecked
144     command.getFacets().put(Facets.CHECKBOX, checkbox);
145     ValueBinding valueBinding = command.getValueBinding(Attributes.VALUE);
146     if (valueBinding != null) {
147       checkbox.setValueBinding(Attributes.VALUE, valueBinding);
148     } else {
149       //noinspection unchecked
150       checkbox.setValue(command.getValue());
151     }
152     return checkbox;
153   }
154 
155   public static LayoutManager createAndInitLayout(
156       FacesContext facesContext, String componentType, String rendererType, UIComponent parent) {
157 
158     LayoutManager layoutManager = (LayoutManager) CreateComponentUtils.createComponent(
159         facesContext, componentType, rendererType, facesContext.getViewRoot().createUniqueId());
160     if (layoutManager instanceof OnComponentCreated) {
161       ((OnComponentCreated) layoutManager).onComponentCreated(facesContext, parent);
162     }
163     if (layoutManager instanceof OnComponentPopulated) {
164       ((OnComponentPopulated) layoutManager).onComponentPopulated(facesContext, parent);
165     }
166     return layoutManager;
167   }
168 }