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 import org.apache.myfaces.tobago.config.ThemeConfig; 025 import org.apache.myfaces.tobago.context.ResourceManagerUtil; 026 027 import javax.faces.component.UIComponent; 028 import javax.faces.component.ValueHolder; 029 import javax.faces.context.FacesContext; 030 import javax.faces.convert.ConverterException; 031 import javax.faces.convert.Converter; 032 import java.io.IOException; 033 034 public class RenderUtil { 035 036 private static final Log LOG = LogFactory.getLog(RenderUtil.class); 037 038 public static final String COMPONENT_IN_REQUEST = "org.apache.myfaces.tobago.component"; 039 040 public static boolean contains(Object[] list, Object value) { 041 if (list == null) { 042 return false; 043 } 044 for (Object aList : list) { 045 if (aList != null && aList.equals(value)) { 046 return true; 047 } 048 } 049 return false; 050 } 051 052 public static void encodeChildren(FacesContext facesContext, 053 UIComponent panel) 054 throws IOException { 055 // UIComponent layout = panel.getFacet("layout"); 056 UILayout layout = UILayout.getLayout(panel); 057 if (layout != null) { 058 layout.encodeChildrenOfComponent(facesContext, panel); 059 } else { 060 for (Object o : panel.getChildren()) { 061 UIComponent child = (UIComponent) o; 062 encode(facesContext, child); 063 } 064 } 065 } 066 067 public static void encode(FacesContext facesContext, UIComponent component) throws IOException { 068 if (component.isRendered()) { 069 if (LOG.isDebugEnabled()) { 070 LOG.debug("rendering " + component.getRendererType() + " " + component); 071 } 072 073 LayoutRenderer layoutRenderer = (LayoutRenderer) 074 ComponentUtil.getRenderer(facesContext, UILayout.getLayout(component)); 075 layoutRenderer.prepareRender(facesContext, component); 076 077 component.encodeBegin(facesContext); 078 if (component.getRendersChildren()) { 079 component.encodeChildren(facesContext); 080 } else { 081 for (Object o : component.getChildren()) { 082 UIComponent kid = (UIComponent) o; 083 encode(facesContext, kid); 084 } 085 } 086 component.encodeEnd(facesContext); 087 } 088 } 089 090 091 092 public static String addMenuCheckToggle(String clientId, String onClick) { 093 if (onClick != null) { 094 onClick = " ; " + onClick; 095 } else { 096 onClick = ""; 097 } 098 099 onClick = "menuCheckToggle('" + clientId + "')" + onClick; 100 101 return onClick; 102 } 103 104 public static String getFormattedValue( 105 FacesContext facesContext, UIComponent component){ 106 Object value = null; 107 if (component instanceof ValueHolder) { 108 value = ((ValueHolder) component).getLocalValue(); 109 if (value == null) { 110 value = ((ValueHolder) component).getValue(); 111 } 112 } 113 return getFormattedValue(facesContext, component, value); 114 } 115 116 public static String getFormattedValue( 117 FacesContext context, UIComponent component, Object currentValue) 118 throws ConverterException { 119 120 if (currentValue == null) { 121 return ""; 122 } 123 124 if (!(component instanceof ValueHolder)) { 125 return currentValue.toString(); 126 } 127 128 Converter converter = ((ValueHolder) component).getConverter(); 129 130 if (converter == null) { 131 if (currentValue instanceof String) { 132 return (String) currentValue; 133 } 134 Class converterType = currentValue.getClass(); 135 converter = context.getApplication().createConverter(converterType); 136 } 137 138 if (converter == null) { 139 return currentValue.toString(); 140 } else { 141 return converter.getAsString(context, component, currentValue); 142 } 143 } 144 145 public static int calculateStringWidth(FacesContext facesContext, UIComponent component, String text) { 146 int width = 0; 147 int defaultCharWidth = 0; 148 try { 149 defaultCharWidth = ThemeConfig.getValue(facesContext, component, "fontWidth"); 150 } catch (NullPointerException e) { 151 LOG.warn("no value for \"fontWidth\" found in theme-config"); 152 } 153 154 String fontWidths = ResourceManagerUtil.getProperty(facesContext, "tobago", "tobago.font.widths"); 155 156 for (char c : text.toCharArray()) { 157 int charWidth; 158 if (c >= 32 && c < 128) { 159 int begin = (c - 32) * 2; 160 charWidth = Integer.parseInt(fontWidths.substring(begin, begin + 2), 16); 161 } else { 162 charWidth = defaultCharWidth; 163 } 164 width += charWidth; 165 } 166 167 return width; 168 } 169 }