001    package org.apache.myfaces.tobago.renderkit;
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.commons.logging.Log;
021    import org.apache.commons.logging.LogFactory;
022    import org.apache.myfaces.tobago.component.ComponentUtil;
023    import org.apache.myfaces.tobago.component.UILayout;
024    
025    import javax.faces.component.UIComponent;
026    import javax.faces.component.ValueHolder;
027    import javax.faces.context.FacesContext;
028    import javax.faces.convert.ConverterException;
029    import javax.faces.convert.Converter;
030    import java.io.IOException;
031    
032    public class RenderUtil {
033    
034      private static final Log LOG = LogFactory.getLog(RenderUtil.class);
035    
036      public static final String COMPONENT_IN_REQUEST = "org.apache.myfaces.tobago.component";
037    
038      public static boolean contains(Object[] list, Object value) {
039        if (list == null) {
040          return false;
041        }
042        for (Object aList : list) {
043          if (aList != null && aList.equals(value)) {
044            return true;
045          }
046        }
047        return false;
048      }
049    
050      public static void encodeChildren(FacesContext facesContext,
051          UIComponent panel)
052          throws IOException {
053    //    UIComponent layout = panel.getFacet("layout");
054        UILayout layout = UILayout.getLayout(panel);
055        if (layout != null) {
056          layout.encodeChildrenOfComponent(facesContext, panel);
057        } else {
058          for (Object o : panel.getChildren()) {
059            UIComponent child = (UIComponent) o;
060            encode(facesContext, child);
061          }
062        }
063      }
064    
065      public static void encode(FacesContext facesContext, UIComponent component) throws IOException {
066        if (component.isRendered()) {
067          if (LOG.isDebugEnabled()) {
068            LOG.debug("rendering " + component.getRendererType() + " " + component);
069          }
070    
071          LayoutRenderer layoutRenderer = (LayoutRenderer)
072              ComponentUtil.getRenderer(facesContext, UILayout.getLayout(component));
073          layoutRenderer.prepareRender(facesContext, component);
074    
075          component.encodeBegin(facesContext);
076          if (component.getRendersChildren()) {
077            component.encodeChildren(facesContext);
078          } else {
079            for (Object o : component.getChildren()) {
080              UIComponent kid = (UIComponent) o;
081              encode(facesContext, kid);
082            }
083          }
084          component.encodeEnd(facesContext);
085        }
086      }
087    
088    
089    
090      public static String addMenuCheckToggle(String clientId, String onClick) {
091        if (onClick != null) {
092          onClick = " ; " + onClick;
093        } else {
094          onClick = "";
095        }
096    
097        onClick = "menuCheckToggle('" + clientId + "')" + onClick;
098    
099        return onClick;
100      }
101    
102      public static String getFormattedValue(
103          FacesContext facesContext, UIComponent component){
104        Object value = null;
105        if (component instanceof ValueHolder) {
106          value = ((ValueHolder) component).getLocalValue();
107          if (value == null) {
108            value =  ((ValueHolder) component).getValue();
109          }
110        }
111        return getFormattedValue(facesContext, component, value);
112      }
113    
114      public static String getFormattedValue(
115          FacesContext context, UIComponent component, Object currentValue)
116          throws ConverterException {
117    
118        if (currentValue == null) {
119          return "";
120        }
121    
122        if (!(component instanceof ValueHolder)) {
123          return currentValue.toString();
124        }
125    
126        Converter converter = ((ValueHolder) component).getConverter();
127    
128        if (converter == null) {
129          if (currentValue instanceof String) {
130            return (String) currentValue;
131          }
132          Class converterType = currentValue.getClass();
133          converter = context.getApplication().createConverter(converterType);
134        }
135    
136        if (converter == null) {
137          return currentValue.toString();
138        } else {
139          return converter.getAsString(context, component, currentValue);
140        }
141      }
142    }