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.html.scarborough.standard.tag;
21  
22  import org.apache.myfaces.tobago.component.Attributes;
23  import org.apache.myfaces.tobago.component.UICommand;
24  import org.apache.myfaces.tobago.component.UIProgress;
25  import org.apache.myfaces.tobago.layout.Measure;
26  import org.apache.myfaces.tobago.renderkit.LayoutComponentRendererBase;
27  import org.apache.myfaces.tobago.renderkit.css.Classes;
28  import org.apache.myfaces.tobago.renderkit.css.Style;
29  import org.apache.myfaces.tobago.renderkit.html.HtmlAttributes;
30  import org.apache.myfaces.tobago.renderkit.html.HtmlElements;
31  import org.apache.myfaces.tobago.renderkit.html.util.HtmlRendererUtils;
32  import org.apache.myfaces.tobago.webapp.TobagoResponseWriter;
33  import org.slf4j.Logger;
34  import org.slf4j.LoggerFactory;
35  
36  import javax.faces.component.UIComponent;
37  import javax.faces.context.FacesContext;
38  import javax.swing.BoundedRangeModel;
39  import javax.swing.DefaultBoundedRangeModel;
40  import java.io.IOException;
41  
42  public class ProgressRenderer extends LayoutComponentRendererBase {
43  
44    private static final Logger LOG = LoggerFactory.getLogger(ProgressRenderer.class);
45  
46    public void encodeEnd(FacesContext facesContext, UIComponent component) throws IOException {
47  
48      UIProgress progress = (UIProgress) component;
49  
50      BoundedRangeModel model = (BoundedRangeModel) progress.getValue();
51  
52      if (model == null) {
53        LOG.warn("'null' value found! Using dummy Model instead!");
54        model = new DefaultBoundedRangeModel(0, 1, 0, 100);
55      }
56  
57      final int diff = model.getMaximum() - model.getMinimum();
58      Object title = progress.getAttributes().get(Attributes.TIP);
59      if (title == null && diff > 0) {
60        title = Integer.toString(100 * model.getValue() / diff) + " %";
61      }
62  
63      final Style style = new Style(facesContext, progress);
64      final Measure width = style.getWidth();
65      final Measure valueWidth = diff > 0 ? width.multiply(model.getValue()).divide(diff) : width;
66  
67      final Style valueStyle = new Style();
68      valueStyle.setHeight(style.getHeight());
69      valueStyle.setWidth(valueWidth);
70  
71      TobagoResponseWriter writer = HtmlRendererUtils.getTobagoResponseWriter(facesContext);
72  
73      writer.startElement(HtmlElements.DIV, progress);
74      writer.writeClassAttribute(Classes.create(progress));
75      HtmlRendererUtils.writeDataAttributes(facesContext, writer, progress);
76      writer.writeStyleAttribute(style);
77      if (title != null) {
78        writer.writeAttribute(HtmlAttributes.TITLE, String.valueOf(title), true);
79      }
80      UIComponent facet = progress.getFacet("complete");
81      if (model.getValue() == model.getMaximum() && facet instanceof UICommand) {
82        HtmlRendererUtils.renderCommandFacet(progress, facesContext, writer);
83      }
84      writer.startElement(HtmlElements.DIV, null);
85      writer.writeClassAttribute(Classes.create(progress, "value"));
86      writer.writeStyleAttribute(valueStyle);
87      writer.endElement(HtmlElements.DIV);
88  
89      writer.endElement(HtmlElements.DIV);
90  
91    }
92  }