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