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.internal.component.AbstractUIRange;
23  import org.apache.myfaces.tobago.internal.util.AccessKeyLogger;
24  import org.apache.myfaces.tobago.internal.util.HtmlRendererUtils;
25  import org.apache.myfaces.tobago.renderkit.css.BootstrapClass;
26  import org.apache.myfaces.tobago.renderkit.css.CssItem;
27  import org.apache.myfaces.tobago.renderkit.css.TobagoClass;
28  import org.apache.myfaces.tobago.renderkit.html.HtmlAttributes;
29  import org.apache.myfaces.tobago.renderkit.html.HtmlElements;
30  import org.apache.myfaces.tobago.renderkit.html.HtmlInputTypes;
31  import org.apache.myfaces.tobago.util.ComponentUtils;
32  import org.apache.myfaces.tobago.webapp.TobagoResponseWriter;
33  
34  import javax.faces.context.FacesContext;
35  import java.io.IOException;
36  
37  public class RangeRenderer<T extends AbstractUIRange> extends MessageLayoutRendererBase<T> {
38  
39    @Override
40    protected boolean isOutputOnly(T component) {
41      return component.isDisabled() || component.isReadonly();
42    }
43  
44    @Override
45    public HtmlElements getComponentTag() {
46      return HtmlElements.TOBAGO_RANGE;
47    }
48  
49    @Override
50    protected void encodeBeginField(final FacesContext facesContext, final T component)
51        throws IOException {
52      final String title = HtmlRendererUtils.getTitleFromTipAndMessages(facesContext, component);
53      final String currentValue = getCurrentValue(facesContext, component);
54      final String clientId = component.getClientId(facesContext);
55      final String fieldId = component.getFieldId(facesContext);
56      final boolean readonly = component.isReadonly();
57      final boolean disabled = component.isDisabled();
58      final int min = component.getMin();
59      final int max = component.getMax();
60      final int step = component.getStep();
61  
62      final TobagoResponseWriter writer = getResponseWriter(facesContext);
63  
64      writer.startElement(HtmlElements.INPUT);
65  
66      if (component.getAccessKey() != null) {
67        writer.writeAttribute(HtmlAttributes.ACCESSKEY, Character.toString(component.getAccessKey()), false);
68        AccessKeyLogger.addAccessKey(facesContext, component.getAccessKey(), clientId);
69      }
70  
71      writer.writeAttribute(HtmlAttributes.TYPE, HtmlInputTypes.RANGE);
72      writer.writeNameAttribute(clientId);
73      writer.writeIdAttribute(fieldId);
74      HtmlRendererUtils.writeDataAttributes(facesContext, writer, component);
75      if (currentValue != null) {
76        writer.writeAttribute(HtmlAttributes.VALUE, currentValue, true);
77      }
78      if (title != null) {
79        writer.writeAttribute(HtmlAttributes.TITLE, title, true);
80      }
81      writer.writeAttribute(HtmlAttributes.READONLY, readonly);
82      writer.writeAttribute(HtmlAttributes.DISABLED, disabled);
83      writer.writeAttribute(HtmlAttributes.TABINDEX, component.getTabIndex());
84      writer.writeAttribute(HtmlAttributes.MIN, min);
85      writer.writeAttribute(HtmlAttributes.MAX, max);
86      writer.writeAttribute(HtmlAttributes.STEP, step);
87  
88      final CssItem rendererCssClass = getRendererCssClass();
89      writer.writeClassAttribute(
90          rendererCssClass,
91          BootstrapClass.borderColor(ComponentUtils.getMaximumSeverity(component)),
92          BootstrapClass.FORM_CONTROL,
93          component.getCustomClass());
94  
95      renderFocus(clientId, component.isFocus(), component.isError(), facesContext, writer);
96  
97      writer.endElement(HtmlElements.INPUT);
98  
99      encodeTooltip(writer, currentValue);
100 
101     encodeBehavior(writer, facesContext, component);
102   }
103 
104   private void encodeTooltip(final TobagoResponseWriter writer, final String content) throws IOException {
105     writer.startElement(HtmlElements.DIV);
106 //    writer.writeClassAttribute(TobagoClass.POPOVER__BOX, BootstrapClass.POPOVER);
107     // todo: check to used TOOLTIP instead of POPOVER
108     writer.writeClassAttribute(BootstrapClass.POPOVER, BootstrapClass.D_NONE);
109 //    writer.writeNameAttribute(popoverId);
110     writer.startElement(HtmlElements.DIV);
111     writer.writeClassAttribute(BootstrapClass.POPOVER_ARROW);
112     writer.endElement(HtmlElements.DIV);
113     writer.startElement(HtmlElements.DIV);
114     writer.writeClassAttribute(BootstrapClass.POPOVER_BODY);
115     if (content != null) {
116       writer.writeText(content);
117     }
118   }
119 
120   @Override
121   protected void encodeEndField(final FacesContext facesContext, final T component) throws IOException {
122   }
123 
124   protected CssItem getRendererCssClass() {
125     return TobagoClass.IN;
126   }
127 
128   @Override
129   protected String getFieldId(final FacesContext facesContext, final T component) {
130     return component.getFieldId(facesContext);
131   }
132 }