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.Converter; 031 import javax.faces.convert.ConverterException; 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 public static String addMenuCheckToggle(String clientId, String onClick) { 092 if (onClick != null) { 093 onClick = " ; " + onClick; 094 } else { 095 onClick = ""; 096 } 097 098 onClick = "menuCheckToggle('" + clientId + "')" + onClick; 099 100 return onClick; 101 } 102 103 public static String getFormattedValue( 104 FacesContext facesContext, UIComponent component) { 105 Object value = null; 106 if (component instanceof ValueHolder) { 107 value = ((ValueHolder) component).getLocalValue(); 108 if (value == null) { 109 value = ((ValueHolder) component).getValue(); 110 } 111 } 112 return getFormattedValue(facesContext, component, value); 113 } 114 115 public static String getFormattedValue( 116 FacesContext context, UIComponent component, Object currentValue) 117 throws ConverterException { 118 119 if (currentValue == null) { 120 return ""; 121 } 122 123 if (!(component instanceof ValueHolder)) { 124 return currentValue.toString(); 125 } 126 127 Converter converter = ((ValueHolder) component).getConverter(); 128 129 if (converter == null) { 130 if (currentValue instanceof String) { 131 return (String) currentValue; 132 } 133 Class converterType = currentValue.getClass(); 134 converter = context.getApplication().createConverter(converterType); 135 } 136 137 if (converter == null) { 138 return currentValue.toString(); 139 } else { 140 return converter.getAsString(context, component, currentValue); 141 } 142 } 143 144 public static int calculateStringWidth2(FacesContext facesContext, UIComponent component, String text) { 145 int width = 0; 146 int defaultCharWidth = 0; 147 try { 148 defaultCharWidth = ThemeConfig.getValue(facesContext, component, "fontWidth"); 149 } catch (NullPointerException e) { 150 LOG.warn("no value for \"fontWidth\" found in theme-config"); 151 } 152 153 String fontWidths = ResourceManagerUtil.getProperty(facesContext, "tobago", "tobago.font2.widths"); 154 155 for (char c : text.toCharArray()) { 156 int charWidth; 157 if (c >= 32 && c < 128) { 158 int begin = (c - 32) * 2; 159 charWidth = Integer.parseInt(fontWidths.substring(begin, begin + 2), 16); 160 } else { 161 charWidth = defaultCharWidth; 162 } 163 width += charWidth; 164 } 165 166 return width; 167 } 168 169 public static int calculateStringWidth(FacesContext facesContext, UIComponent component, String text) { 170 int width = 0; 171 int defaultCharWidth = 0; 172 try { 173 defaultCharWidth = ThemeConfig.getValue(facesContext, component, "fontWidth"); 174 } catch (NullPointerException e) { 175 LOG.warn("no value for \"fontWidth\" found in theme-config"); 176 } 177 178 String fontWidths = ResourceManagerUtil.getProperty(facesContext, "tobago", "tobago.font.widths"); 179 180 for (char c : text.toCharArray()) { 181 int charWidth; 182 if (c >= 32 && c < 128) { 183 int begin = (c - 32) * 2; 184 charWidth = Integer.parseInt(fontWidths.substring(begin, begin + 2), 16); 185 } else { 186 charWidth = defaultCharWidth; 187 } 188 width += charWidth; 189 } 190 191 return width; 192 } 193 }