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