001 /* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 */ 019 020 package org.apache.myfaces.tobago.renderkit; 021 022 import org.apache.commons.logging.Log; 023 import org.apache.commons.logging.LogFactory; 024 import org.apache.myfaces.tobago.component.ComponentUtil; 025 import org.apache.myfaces.tobago.component.UILayout; 026 import org.apache.myfaces.tobago.config.ThemeConfig; 027 import org.apache.myfaces.tobago.context.ResourceManagerUtil; 028 029 import javax.faces.component.UIComponent; 030 import javax.faces.component.ValueHolder; 031 import javax.faces.context.FacesContext; 032 import javax.faces.convert.Converter; 033 import javax.faces.convert.ConverterException; 034 import java.io.IOException; 035 036 public class RenderUtil { 037 038 private static final Log LOG = LogFactory.getLog(RenderUtil.class); 039 040 public static final String COMPONENT_IN_REQUEST = "org.apache.myfaces.tobago.component"; 041 042 public static boolean contains(Object[] list, Object value) { 043 if (list == null) { 044 return false; 045 } 046 for (Object aList : list) { 047 if (aList != null && aList.equals(value)) { 048 return true; 049 } 050 } 051 return false; 052 } 053 054 public static void encodeChildren(FacesContext facesContext, 055 UIComponent panel) 056 throws IOException { 057 // UIComponent layout = panel.getFacet("layout"); 058 UILayout layout = UILayout.getLayout(panel); 059 if (layout != null) { 060 layout.encodeChildrenOfComponent(facesContext, panel); 061 } else { 062 for (Object o : panel.getChildren()) { 063 UIComponent child = (UIComponent) o; 064 encode(facesContext, child); 065 } 066 } 067 } 068 069 public static void encode(FacesContext facesContext, UIComponent component) throws IOException { 070 if (component.isRendered()) { 071 if (LOG.isDebugEnabled()) { 072 LOG.debug("rendering " + component.getRendererType() + " " + component); 073 } 074 075 LayoutRenderer layoutRenderer = (LayoutRenderer) 076 ComponentUtil.getRenderer(facesContext, UILayout.getLayout(component)); 077 layoutRenderer.prepareRender(facesContext, component); 078 079 component.encodeBegin(facesContext); 080 if (component.getRendersChildren()) { 081 component.encodeChildren(facesContext); 082 } else { 083 for (Object o : component.getChildren()) { 084 UIComponent kid = (UIComponent) o; 085 encode(facesContext, kid); 086 } 087 } 088 component.encodeEnd(facesContext); 089 } 090 } 091 092 093 public static String addMenuCheckToggle(String clientId, String onClick) { 094 if (onClick != null) { 095 onClick = " ; " + onClick; 096 } else { 097 onClick = ""; 098 } 099 100 onClick = "menuCheckToggle('" + clientId + "')" + onClick; 101 102 return onClick; 103 } 104 105 public static String getFormattedValue( 106 FacesContext facesContext, UIComponent component) { 107 Object value = null; 108 if (component instanceof ValueHolder) { 109 value = ((ValueHolder) component).getLocalValue(); 110 if (value == null) { 111 value = ((ValueHolder) component).getValue(); 112 } 113 } 114 return getFormattedValue(facesContext, component, value); 115 } 116 117 public static String getFormattedValue( 118 FacesContext context, UIComponent component, Object currentValue) 119 throws ConverterException { 120 121 if (currentValue == null) { 122 return ""; 123 } 124 125 if (!(component instanceof ValueHolder)) { 126 return currentValue.toString(); 127 } 128 129 Converter converter = ((ValueHolder) component).getConverter(); 130 131 if (converter == null) { 132 if (currentValue instanceof String) { 133 return (String) currentValue; 134 } 135 Class converterType = currentValue.getClass(); 136 converter = context.getApplication().createConverter(converterType); 137 } 138 139 if (converter == null) { 140 return currentValue.toString(); 141 } else { 142 return converter.getAsString(context, component, currentValue); 143 } 144 } 145 146 public static int calculateStringWidth2(FacesContext facesContext, UIComponent component, String text) { 147 int width = 0; 148 int defaultCharWidth = 0; 149 try { 150 defaultCharWidth = ThemeConfig.getValue(facesContext, component, "fontWidth"); 151 } catch (NullPointerException e) { 152 if (LOG.isDebugEnabled()) { 153 LOG.debug("no value for \"fontWidth\" found in theme-config"); 154 } 155 } 156 157 String fontWidths = ResourceManagerUtil.getProperty(facesContext, "tobago", "tobago.font2.widths"); 158 159 for (char c : text.toCharArray()) { 160 int charWidth; 161 if (c >= 32 && c < 128) { 162 int begin = (c - 32) * 2; 163 charWidth = Integer.parseInt(fontWidths.substring(begin, begin + 2), 16); 164 } else { 165 charWidth = defaultCharWidth; 166 } 167 width += charWidth; 168 } 169 170 return width; 171 } 172 173 public static int calculateStringWidth(FacesContext facesContext, UIComponent component, String text) { 174 int width = 0; 175 int defaultCharWidth = 0; 176 try { 177 defaultCharWidth = ThemeConfig.getValue(facesContext, component, "fontWidth"); 178 } catch (NullPointerException e) { 179 if (LOG.isDebugEnabled()) { 180 LOG.debug("no value for \"fontWidth\" found in theme-config"); 181 } 182 } 183 184 String fontWidths = ResourceManagerUtil.getProperty(facesContext, "tobago", "tobago.font.widths"); 185 186 for (char c : text.toCharArray()) { 187 int charWidth; 188 if (c >= 32 && c < 128) { 189 int begin = (c - 32) * 2; 190 charWidth = Integer.parseInt(fontWidths.substring(begin, begin + 2), 16); 191 } else { 192 charWidth = defaultCharWidth; 193 } 194 width += charWidth; 195 } 196 197 return width; 198 } 199 }