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.el.ValueExpression;
33  import javax.faces.FacesException;
34  import javax.faces.component.EditableValueHolder;
35  import javax.faces.component.UIComponent;
36  import javax.faces.component.UIInput;
37  import javax.faces.component.ValueHolder;
38  import javax.faces.context.FacesContext;
39  import javax.faces.convert.Converter;
40  import javax.faces.convert.ConverterException;
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(final FacesContext facesContext, final UIComponent component) throws IOException {
55  
56      if (component instanceof SupportsMarkup) {
57        final SupportsMarkup supportsMarkup = (SupportsMarkup) component;
58        final 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(final FacesContext context, final UIComponent component) throws IOException {
68    }
69  
70    /**
71     * @deprecated todo: should be done in the StyleClasses class.
72     */
73    @Deprecated
74    protected String getRendererName(final 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(final FacesContext facesContext, final UIComponent component, final String key) {
83      return getResourceManager().getThemeMeasure(facesContext, (Configurable) component, key).getPixel();
84    }
85  
86    protected Object getCurrentValueAsObject(final UIInput input) {
87      final Object submittedValue = input.getSubmittedValue();
88      if (submittedValue != null) {
89        return submittedValue;
90      }
91      return getValue(input);
92    }
93  
94    protected String getCurrentValue(final FacesContext facesContext, final UIComponent component) {
95  
96      if (component instanceof EditableValueHolder) {
97        final EditableValueHolder editableValueHolder = (EditableValueHolder) component;
98        final Object submittedValue = editableValueHolder.getSubmittedValue();
99        if (submittedValue != null || !editableValueHolder.isValid()) {
100         return (String) submittedValue;
101       }
102     }
103     String currentValue = null;
104     final Object currentObj = getValue(component);
105     if (currentObj != null) {
106       currentValue = getFormattedValue(facesContext, component, currentObj);
107     }
108     return currentValue;
109   }
110 
111   protected String getFormattedValue(final FacesContext context, final UIComponent component, final 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       final 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(final UIComponent component) {
143     if (component instanceof ValueHolder) {
144       return ((ValueHolder) component).getValue();
145     } else {
146       return null;
147     }
148   }
149 
150   public Converter getConverter(final FacesContext context, final UIComponent component) {
151     Converter converter = null;
152     if (component instanceof ValueHolder) {
153       converter = ((ValueHolder) component).getConverter();
154     }
155     if (converter == null) {
156       final ValueExpression valueExpression = component.getValueExpression("value");
157       if (valueExpression != null) {
158         final Class converterType = valueExpression.getType(context.getELContext());
159         if (converterType == null || converterType == String.class
160             || converterType == Object.class) {
161           return null;
162         }
163         try {
164           converter = context.getApplication().createConverter(converterType);
165         } catch (final FacesException e) {
166           LOG.error("No Converter found for type " + converterType);
167         }
168       }
169     }
170     return converter;
171   }
172 
173   @Override
174   public Object getConvertedValue(final FacesContext context, final UIComponent component, final Object submittedValue)
175       throws ConverterException {
176     if (!(submittedValue instanceof String)) {
177       return submittedValue;
178     }
179     final Converter converter = getConverter(context, component);
180     if (converter != null) {
181       return converter.getAsObject(context, component, (String) submittedValue);
182     } else {
183       return submittedValue;
184     }
185   }
186 
187   public void onComponentCreated(
188       final FacesContext facesContext, final UIComponent component, final UIComponent parent) {
189   }
190   
191   protected synchronized ResourceManager getResourceManager() {
192     if (resourceManager == null) {
193       resourceManager = ResourceManagerFactory.getResourceManager(FacesContext.getCurrentInstance());
194     }
195     return resourceManager;
196   }
197 }