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.myfaces.tobago.config.ThemeConfig; 021 import org.apache.commons.logging.Log; 022 import org.apache.commons.logging.LogFactory; 023 024 import javax.faces.render.Renderer; 025 import javax.faces.context.FacesContext; 026 import javax.faces.component.UIComponent; 027 import javax.faces.component.UIInput; 028 import javax.faces.component.ValueHolder; 029 import javax.faces.convert.Converter; 030 import javax.faces.convert.ConverterException; 031 import javax.faces.el.ValueBinding; 032 import javax.faces.FacesException; 033 import java.util.Locale; 034 035 /** 036 * Date: Apr 21, 2007 037 * Time: 8:04:25 PM 038 */ 039 public class RendererBase extends Renderer { 040 protected static final Log LOG = LogFactory.getLog(LayoutableRendererBase.class); 041 042 public void decode(FacesContext facesContext, UIComponent component) { 043 // nothing to do 044 045 // FIXME later: 046 if (component instanceof UIInput) { 047 LOG.warn("decode() should be overwritten! Renderer: " 048 + this.getClass().getName()); 049 } 050 } 051 052 public String getRendererName(String rendererType) { 053 String name; 054 if (LOG.isDebugEnabled()) { 055 LOG.debug("rendererType = '" + rendererType + "'"); 056 } 057 /* if ("javax.faces.Text".equals(rendererType)) { // TODO: find a better way 058 name = RENDERER_TYPE_OUT; 059 } else {*/ 060 name = rendererType; 061 /*} 062 if (name.startsWith("javax.faces.")) { // FIXME: this is a hotfix from jsf1.0beta to jsf1.0fr 063 LOG.warn("patching renderer from " + name); 064 name = name.substring("javax.faces.".length()); 065 LOG.warn("patching renderer to " + name); 066 }*/ 067 name = name.substring(0, 1).toLowerCase(Locale.ENGLISH) + name.substring(1); 068 return name; 069 } 070 071 public int getConfiguredValue(FacesContext facesContext, 072 UIComponent component, String key) { 073 try { 074 return ThemeConfig.getValue(facesContext, component, key); 075 } catch (Exception e) { 076 LOG.error("Can't take '" + key + "' for " + getClass().getName() 077 + " from config-file: " + e.getMessage(), e); 078 } 079 return 0; 080 } 081 082 protected String getCurrentValue( 083 FacesContext facesContext, UIComponent component) { 084 085 if (component instanceof UIInput) { 086 Object submittedValue = ((UIInput) component).getSubmittedValue(); 087 if (submittedValue != null) { 088 return (String) submittedValue; 089 } 090 } 091 String currentValue = null; 092 Object currentObj = getValue(component); 093 if (currentObj != null) { 094 currentValue = RenderUtil.getFormattedValue(facesContext, component, currentObj); 095 } 096 return currentValue; 097 } 098 099 protected Object getValue(UIComponent component) { 100 if (component instanceof ValueHolder) { 101 Object value = ((ValueHolder) component).getValue(); 102 if (LOG.isDebugEnabled()) { 103 LOG.debug("component.getValue() returned " + value); 104 } 105 return value; 106 } else { 107 return null; 108 } 109 } 110 111 public Converter getConverter(FacesContext context, UIComponent component) { 112 Converter converter = null; 113 if (component instanceof ValueHolder) { 114 converter = ((ValueHolder) component).getConverter(); 115 } 116 if (converter == null) { 117 ValueBinding valueBinding = component.getValueBinding("value"); 118 if (valueBinding != null) { 119 Class converterType = valueBinding.getType(context); 120 if (converterType == null || converterType == String.class 121 || converterType == Object.class) { 122 return null; 123 } 124 try { 125 converter = context.getApplication().createConverter(converterType); 126 } catch (FacesException e) { 127 LOG.error("No Converter found for type " + converterType); 128 } 129 } 130 } 131 return converter; 132 } 133 134 public Object getConvertedValue(FacesContext context, 135 UIComponent component, Object submittedValue) 136 throws ConverterException { 137 if (!(submittedValue instanceof String)) { 138 return submittedValue; 139 } 140 Converter converter = getConverter(context, component); 141 if (converter != null) { 142 return converter.getAsObject(context, component, (String) submittedValue); 143 } else { 144 return submittedValue; 145 } 146 } 147 }