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.internal.renderkit.renderer;
21  
22  import org.apache.myfaces.tobago.component.RendererTypes;
23  import org.apache.myfaces.tobago.component.Tags;
24  import org.apache.myfaces.tobago.context.Markup;
25  import org.apache.myfaces.tobago.internal.component.AbstractUIProgress;
26  import org.apache.myfaces.tobago.internal.component.AbstractUIStyle;
27  import org.apache.myfaces.tobago.internal.util.HtmlRendererUtils;
28  import org.apache.myfaces.tobago.internal.util.StyleRenderUtils;
29  import org.apache.myfaces.tobago.layout.Measure;
30  import org.apache.myfaces.tobago.renderkit.RendererBase;
31  import org.apache.myfaces.tobago.renderkit.css.BootstrapClass;
32  import org.apache.myfaces.tobago.renderkit.css.TobagoClass;
33  import org.apache.myfaces.tobago.renderkit.html.Arias;
34  import org.apache.myfaces.tobago.renderkit.html.HtmlAttributes;
35  import org.apache.myfaces.tobago.renderkit.html.HtmlElements;
36  import org.apache.myfaces.tobago.renderkit.html.HtmlRoleValues;
37  import org.apache.myfaces.tobago.webapp.TobagoResponseWriter;
38  
39  import javax.faces.context.FacesContext;
40  import java.io.IOException;
41  
42  public class ProgressRenderer<T extends AbstractUIProgress> extends RendererBase<T> {
43  
44    @Override
45    public void encodeBeginInternal(final FacesContext facesContext, final T component) throws IOException {
46      final double value = component.getRangeValue();
47      final double max = component.getRangeMax();
48      final double percent = value / max;
49      final Markup markup = component.getMarkup();
50  
51      final TobagoResponseWriter writer = getResponseWriter(facesContext);
52  
53      writer.startElement(HtmlElements.DIV);
54      final String clientId = component.getClientId(facesContext);
55      writer.writeIdAttribute(clientId);
56      writer.writeClassAttribute(
57          TobagoClass.PROGRESS,
58          TobagoClass.PROGRESS.createMarkup(markup),
59          BootstrapClass.PROGRESS,
60          component.getCustomClass());
61      HtmlRendererUtils.writeDataAttributes(facesContext, writer, component);
62  
63      writer.startElement(HtmlElements.DIV);
64      writer.writeClassAttribute(BootstrapClass.PROGRESS_BAR);
65      writer.writeAttribute(HtmlAttributes.ROLE, HtmlRoleValues.PROGRESSBAR.toString(), false);
66      writer.writeAttribute(Arias.VALUEMIN, 0);
67      writer.writeAttribute(Arias.VALUEMAX, 100);
68      writer.writeAttribute(Arias.VALUENOW, String.valueOf((int) percent * 100), false);
69  
70      final AbstractUIStyle style = (AbstractUIStyle) facesContext.getApplication()
71          .createComponent(facesContext, Tags.style.componentType(), RendererTypes.Style.name());
72      style.setTransient(true);
73      style.setSelector(StyleRenderUtils.encodeIdSelector(clientId) + ">." + BootstrapClass.PROGRESS_BAR.getName());
74      style.setWidth(new Measure(percent * 100, Measure.Unit.PERCENT));
75      component.getChildren().add(style);
76  
77      encodeBehavior(writer, facesContext, component);
78    }
79  
80    @Override
81    public void encodeEndInternal(final FacesContext facesContext, final T component) throws IOException {
82  
83      final TobagoResponseWriter writer = getResponseWriter(facesContext);
84      writer.endElement(HtmlElements.DIV);
85      writer.endElement(HtmlElements.DIV);
86    }
87  }