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