View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  
20  package org.apache.myfaces.tobago.renderkit;
21  
22  import org.apache.myfaces.tobago.component.SupportsMarkup;
23  import org.apache.myfaces.tobago.config.Configurable;
24  import org.apache.myfaces.tobago.context.Markup;
25  import org.apache.myfaces.tobago.context.ResourceManager;
26  import org.apache.myfaces.tobago.internal.context.ResourceManagerFactory;
27  import org.apache.myfaces.tobago.layout.Measure;
28  import org.apache.myfaces.tobago.util.ComponentUtils;
29  import org.slf4j.Logger;
30  import org.slf4j.LoggerFactory;
31  
32  import javax.faces.FacesException;
33  import javax.faces.component.EditableValueHolder;
34  import javax.faces.component.UIComponent;
35  import javax.faces.component.UIInput;
36  import javax.faces.component.ValueHolder;
37  import javax.faces.context.FacesContext;
38  import javax.faces.convert.Converter;
39  import javax.faces.convert.ConverterException;
40  import javax.faces.el.ValueBinding;
41  import javax.faces.render.Renderer;
42  import java.io.IOException;
43  import java.util.Locale;
44  
45  public class RendererBase extends Renderer {
46  
47    private static final Logger LOG = LoggerFactory.getLogger(RendererBase.class);
48  
49    private ResourceManager resourceManager;
50  
51    /**
52     * Hook to e. g. register resources, etc.
53     */
54    public void prepareRender(FacesContext facesContext, UIComponent component) throws IOException {
55  
56      if (component instanceof SupportsMarkup) {
57        final SupportsMarkup supportsMarkup = (SupportsMarkup) component;
58        Markup markup = ComponentUtils.updateMarkup(component, supportsMarkup.getMarkup());
59        supportsMarkup.setCurrentMarkup(markup);
60      }
61    }
62  
63    public boolean getPrepareRendersChildren() {
64      return false;
65    }
66  
67    public void prepareRendersChildren(FacesContext context, UIComponent component) throws IOException {
68    }
69  
70    /**
71     * @deprecated todo: should be done in the StyleClasses class.
72     */
73    @Deprecated
74    protected String getRendererName(String rendererType) {
75      return rendererType.substring(0, 1).toLowerCase(Locale.ENGLISH) + rendererType.substring(1);
76    }
77  
78    /**
79     * @deprecated since 1.5.0, please use getResourceManager().getThemeMeasure()
80     */
81    @Deprecated
82    public int getConfiguredValue(FacesContext facesContext, UIComponent component, String key) {
83      return getResourceManager().getThemeMeasure(facesContext, (Configurable) component, key).getPixel();
84    }
85  
86    protected Object getCurrentValueAsObject(UIInput input) {
87      Object submittedValue = input.getSubmittedValue();
88      if (submittedValue != null) {
89        return submittedValue;
90      }
91      return getValue(input);
92    }
93  
94    protected String getCurrentValue(FacesContext facesContext, UIComponent component) {
95  
96      if (component instanceof EditableValueHolder) {
97        EditableValueHolder editableValueHolder = (EditableValueHolder) component;
98        Object submittedValue = editableValueHolder.getSubmittedValue();
99        if (submittedValue != null || !editableValueHolder.isValid()) {
100         return (String) submittedValue;
101       }
102     }
103     String currentValue = null;
104     Object currentObj = getValue(component);
105     if (currentObj != null) {
106       currentValue = getFormattedValue(facesContext, component, currentObj);
107     }
108     return currentValue;
109   }
110 
111   protected String getFormattedValue(FacesContext context, UIComponent component, Object currentValue)
112       throws ConverterException {
113 
114     if (currentValue == null) {
115       return "";
116     }
117 
118     if (!(component instanceof ValueHolder)) {
119       return currentValue.toString();
120     }
121 
122     Converter converter = ((ValueHolder) component).getConverter();
123 
124     if (converter == null) {
125       if (currentValue instanceof String) {
126         return (String) currentValue;
127       }
128       if (currentValue instanceof Measure) {
129         return ((Measure) currentValue).serialize();
130       }
131       Class converterType = currentValue.getClass();
132       converter = context.getApplication().createConverter(converterType);
133     }
134 
135     if (converter == null) {
136       return currentValue.toString();
137     } else {
138       return converter.getAsString(context, component, currentValue);
139     }
140   }
141 
142   protected Object getValue(UIComponent component) {
143     if (component instanceof ValueHolder) {
144       Object value = ((ValueHolder) component).getValue();
145       if (LOG.isDebugEnabled()) {
146         LOG.debug("component.getValue() returned " + value);
147       }
148       return value;
149     } else {
150       return null;
151     }
152   }
153 
154   public Converter getConverter(FacesContext context, UIComponent component) {
155     Converter converter = null;
156     if (component instanceof ValueHolder) {
157       converter = ((ValueHolder) component).getConverter();
158     }
159     if (converter == null) {
160       ValueBinding valueBinding = component.getValueBinding("value");
161       if (valueBinding != null) {
162         Class converterType = valueBinding.getType(context);
163         if (converterType == null || converterType == String.class
164             || converterType == Object.class) {
165           return null;
166         }
167         try {
168           converter = context.getApplication().createConverter(converterType);
169         } catch (FacesException e) {
170           LOG.error("No Converter found for type " + converterType);
171         }
172       }
173     }
174     return converter;
175   }
176 
177   @Override
178   public Object getConvertedValue(FacesContext context, UIComponent component, Object submittedValue)
179       throws ConverterException {
180     if (!(submittedValue instanceof String)) {
181       return submittedValue;
182     }
183     Converter converter = getConverter(context, component);
184     if (converter != null) {
185       return converter.getAsObject(context, component, (String) submittedValue);
186     } else {
187       return submittedValue;
188     }
189   }
190 
191   public void onComponentCreated(FacesContext facesContext, UIComponent component, UIComponent parent) {
192   }
193   
194   protected synchronized ResourceManager getResourceManager() {
195     if (resourceManager == null) {
196       resourceManager = ResourceManagerFactory.getResourceManager(FacesContext.getCurrentInstance());
197     }
198     return resourceManager;
199   }
200 }