001    package org.apache.myfaces.tobago.util;
002    
003    /*
004     * Licensed to the Apache Software Foundation (ASF) under one or more
005     * contributor license agreements.  See the NOTICE file distributed with
006     * this work for additional information regarding copyright ownership.
007     * The ASF licenses this file to You under the Apache License, Version 2.0
008     * (the "License"); you may not use this file except in compliance with
009     * the License.  You may obtain a copy of the License at
010     *
011     *      http://www.apache.org/licenses/LICENSE-2.0
012     *
013     * Unless required by applicable law or agreed to in writing, software
014     * distributed under the License is distributed on an "AS IS" BASIS,
015     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016     * See the License for the specific language governing permissions and
017     * limitations under the License.
018     */
019    
020    import org.apache.myfaces.tobago.component.Attributes;
021    import org.apache.myfaces.tobago.component.ComponentTypes;
022    import org.apache.myfaces.tobago.component.Facets;
023    import org.apache.myfaces.tobago.component.OnComponentCreated;
024    import org.apache.myfaces.tobago.component.OnComponentPopulated;
025    import org.apache.myfaces.tobago.component.RendererTypes;
026    import org.apache.myfaces.tobago.component.UIMenuSelectOne;
027    import org.apache.myfaces.tobago.internal.component.AbstractUIColumn;
028    import org.apache.myfaces.tobago.internal.component.AbstractUIOut;
029    import org.apache.myfaces.tobago.internal.component.AbstractUISelectBooleanCheckbox;
030    import org.apache.myfaces.tobago.internal.util.ComponentAttributeUtils;
031    import org.apache.myfaces.tobago.layout.Display;
032    import org.apache.myfaces.tobago.layout.LayoutManager;
033    
034    import javax.faces.component.UICommand;
035    import javax.faces.component.UIComponent;
036    import javax.faces.context.FacesContext;
037    import javax.faces.el.ValueBinding;
038    
039    public class CreateComponentUtils {
040    
041      @Deprecated
042      public static UIComponent createComponent(String componentType, String rendererType) {
043        return createComponent(componentType, rendererType, null);
044      }
045    
046      public static UIComponent createComponent(String componentType, String rendererType, String clientId) {
047        final FacesContext facesContext = FacesContext.getCurrentInstance();
048        return createComponent(facesContext, componentType, rendererType, clientId);
049      }
050    
051      @Deprecated
052      public static UIComponent createComponent(FacesContext facesContext, String componentType, String rendererType) {
053        return createComponent(facesContext, componentType, rendererType, null);
054      }
055    
056      public static UIComponent createComponent(
057          FacesContext facesContext, String componentType, String rendererType, String clientId) {
058        UIComponent component  = facesContext.getApplication().createComponent(componentType);
059        component.setRendererType(rendererType);
060        component.setId(clientId);
061        return component;
062      }
063    
064      @Deprecated
065      public static AbstractUIColumn createTextColumn(String label, String sortable, String align, String value) {
066        return createTextColumn(label, sortable, align, value, null);
067      }
068    
069      public static AbstractUIColumn createTextColumn(
070          String label, String sortable, String align, String value, String clientId) {
071        AbstractUIOut text = (AbstractUIOut) createComponent(ComponentTypes.OUT, RendererTypes.OUT, clientId + "_t");
072        ComponentAttributeUtils.setStringProperty(text, Attributes.VALUE, value);
073        ComponentAttributeUtils.setBooleanProperty(text, Attributes.CREATE_SPAN, "false");
074        ComponentAttributeUtils.setBooleanProperty(text, Attributes.ESCAPE, "false");
075        text.setDisplay(Display.INLINE);
076        return createColumn(label, sortable, align, text, clientId);
077      }
078    
079      @Deprecated
080      public static AbstractUIColumn createColumn(String label, String sortable, String align, UIComponent child) {
081        return createColumn(label, sortable, align, child, null);
082      }
083    
084      public static AbstractUIColumn createColumn(
085          String label, String sortable, String align, UIComponent child, String clientId) {
086        AbstractUIColumn column = createColumn(label, sortable, align, clientId);
087        //noinspection unchecked
088        column.getChildren().add(child);
089        return column;
090      }
091    
092      @Deprecated
093      public static AbstractUIColumn createColumn(String label, String sortable, String align) {
094        return createColumn(label, sortable, align, (String) null);
095      }
096    
097      public static AbstractUIColumn createColumn(String label, String sortable, String align, String clientId) {
098        AbstractUIColumn column = (AbstractUIColumn) createComponent(ComponentTypes.COLUMN, null, clientId);
099        ComponentAttributeUtils.setStringProperty(column, Attributes.LABEL, label);
100        ComponentAttributeUtils.setBooleanProperty(column, Attributes.SORTABLE, sortable);
101        ComponentAttributeUtils.setStringProperty(column, Attributes.ALIGN, align);
102        return column;
103      }
104    
105      @Deprecated
106      public static UIMenuSelectOne createUIMenuSelectOneFacet(FacesContext facesContext,
107          javax.faces.component.UICommand command) {
108        return createUIMenuSelectOneFacet(facesContext, command, null);
109      }
110    
111      public static UIMenuSelectOne createUIMenuSelectOneFacet(
112          FacesContext facesContext, javax.faces.component.UICommand command, String clientId) {
113    
114        UIMenuSelectOne radio = (UIMenuSelectOne) createComponent(
115            facesContext, UIMenuSelectOne.COMPONENT_TYPE, RendererTypes.SELECT_ONE_RADIO, clientId);
116        //noinspection unchecked
117        command.getFacets().put(Facets.RADIO, radio);
118        final ValueBinding valueBinding = command.getValueBinding(Attributes.VALUE);
119        if (valueBinding != null) {
120          radio.setValueBinding(Attributes.VALUE, valueBinding);
121        } else {
122          radio.setValue(command.getValue());
123        }
124        return radio;
125      }
126    
127      @Deprecated
128      public static UIComponent createUISelectBooleanFacet(FacesContext facesContext, UICommand command) {
129        return createUISelectBooleanFacet(facesContext, command, null);
130      }
131    
132      public static AbstractUISelectBooleanCheckbox createUISelectBooleanFacetWithId(FacesContext facesContext,
133          UICommand command) {
134        return createUISelectBooleanFacet(facesContext, command, facesContext.getViewRoot().createUniqueId());
135      }
136    
137      public static AbstractUISelectBooleanCheckbox createUISelectBooleanFacet(FacesContext facesContext, UICommand command,
138          String clientId) {
139        AbstractUISelectBooleanCheckbox checkbox = (AbstractUISelectBooleanCheckbox) createComponent(
140            facesContext, ComponentTypes.SELECT_BOOLEAN_CHECKBOX, RendererTypes.SELECT_BOOLEAN_CHECKBOX, clientId);
141        //noinspection unchecked
142        command.getFacets().put(Facets.CHECKBOX, checkbox);
143        ValueBinding valueBinding = command.getValueBinding(Attributes.VALUE);
144        if (valueBinding != null) {
145          checkbox.setValueBinding(Attributes.VALUE, valueBinding);
146        } else {
147          //noinspection unchecked
148          checkbox.setValue(command.getValue());
149        }
150        return checkbox;
151      }
152    
153      public static LayoutManager createAndInitLayout(
154          FacesContext facesContext, String componentType, String rendererType, UIComponent parent) {
155    
156        LayoutManager layoutManager = (LayoutManager) CreateComponentUtils.createComponent(
157            facesContext, componentType, rendererType, facesContext.getViewRoot().createUniqueId());
158        if (layoutManager instanceof OnComponentCreated) {
159          ((OnComponentCreated) layoutManager).onComponentCreated(facesContext, parent);
160        }
161        if (layoutManager instanceof OnComponentPopulated) {
162          ((OnComponentPopulated) layoutManager).onComponentPopulated(facesContext, parent);
163        }
164        return layoutManager;
165      }
166    }