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.internal.webapp.TobagoResponseWriterWrapper;
23  import org.apache.myfaces.tobago.util.ComponentUtils;
24  import org.apache.myfaces.tobago.webapp.TobagoResponseWriter;
25  
26  import javax.faces.component.EditableValueHolder;
27  import javax.faces.component.UIComponent;
28  import javax.faces.component.ValueHolder;
29  import javax.faces.context.FacesContext;
30  import javax.faces.context.ResponseWriter;
31  import javax.faces.convert.Converter;
32  import javax.faces.convert.ConverterException;
33  import javax.faces.render.Renderer;
34  
35  public class RendererBase extends Renderer {
36  
37    protected String getCurrentValue(final FacesContext facesContext, final UIComponent component) {
38  
39      if (component instanceof ValueHolder) {
40        final ValueHolder valueHolder = (ValueHolder) component;
41        if (valueHolder instanceof EditableValueHolder) {
42          final EditableValueHolder editableValueHolder = (EditableValueHolder) component;
43          final Object submittedValue = editableValueHolder.getSubmittedValue();
44          if (submittedValue != null || !editableValueHolder.isValid()) {
45            return (String) submittedValue;
46          }
47        }
48        String currentValue = null;
49        final Object result = ((ValueHolder) component).getValue();
50        if (result != null) {
51          currentValue = ComponentUtils.getFormattedValue(facesContext, component, result);
52        }
53        return currentValue;
54      } else {
55        return null;
56      }
57    }
58  
59    @Override
60    public Object getConvertedValue(final FacesContext context, final UIComponent component, final Object submittedValue)
61        throws ConverterException {
62      if (!(submittedValue instanceof String)) {
63        return submittedValue;
64      }
65      final Converter converter = ComponentUtils.getConverter(context, component, submittedValue);
66      if (converter != null) {
67        return converter.getAsObject(context, component, (String) submittedValue);
68      } else {
69        return submittedValue;
70      }
71    }
72  
73    public void onComponentCreated(
74        final FacesContext facesContext, final UIComponent component, final UIComponent parent) {
75    }
76  
77    protected TobagoResponseWriter getResponseWriter(final FacesContext facesContext) {
78      final ResponseWriter writer = facesContext.getResponseWriter();
79      if (writer instanceof TobagoResponseWriter) {
80        return (TobagoResponseWriter) writer;
81      } else {
82        return new TobagoResponseWriterWrapper(writer);
83      }
84    }
85  }