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.AbstractUIDate;
23  import org.apache.myfaces.tobago.internal.component.AbstractUIInput;
24  import org.apache.myfaces.tobago.internal.context.DateTimeI18n;
25  import org.apache.myfaces.tobago.internal.util.JsonUtils;
26  import org.apache.myfaces.tobago.internal.util.StringUtils;
27  import org.apache.myfaces.tobago.renderkit.css.BootstrapClass;
28  import org.apache.myfaces.tobago.renderkit.css.CssItem;
29  import org.apache.myfaces.tobago.renderkit.css.Icons;
30  import org.apache.myfaces.tobago.renderkit.css.TobagoClass;
31  import org.apache.myfaces.tobago.renderkit.css.VaadinClass;
32  import org.apache.myfaces.tobago.renderkit.html.DataAttributes;
33  import org.apache.myfaces.tobago.renderkit.html.HtmlAttributes;
34  import org.apache.myfaces.tobago.renderkit.html.HtmlButtonTypes;
35  import org.apache.myfaces.tobago.renderkit.html.HtmlElements;
36  import org.apache.myfaces.tobago.util.ResourceUtils;
37  import org.apache.myfaces.tobago.webapp.TobagoResponseWriter;
38  import org.slf4j.Logger;
39  import org.slf4j.LoggerFactory;
40  
41  import javax.faces.context.FacesContext;
42  import java.io.IOException;
43  import java.lang.invoke.MethodHandles;
44  import java.text.SimpleDateFormat;
45  import java.util.Date;
46  
47  public class DateRenderer<T extends AbstractUIDate> extends InRenderer<T> {
48  
49    private static final Logger LOG = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
50  
51    @Override
52    public HtmlElements getComponentTag() {
53      return HtmlElements.TOBAGO_DATE;
54    }
55  
56    @Override
57    protected void writeAdditionalAttributes(
58        final FacesContext facesContext, final TobagoResponseWriter writer, final AbstractUIInput input)
59        throws IOException {
60  
61      final AbstractUIDate date = (AbstractUIDate) input;
62  
63      super.writeAdditionalAttributes(facesContext, writer, date);
64      writer.writeAttribute(DataAttributes.PATTERN, date.getPattern(), true);
65      final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
66      writer.writeAttribute(DataAttributes.TODAY, sdf.format(new Date()), true);
67      final DateTimeI18n dateTimeI18n = DateTimeI18n.valueOf(facesContext.getViewRoot().getLocale());
68      writer.writeAttribute(DataAttributes.DATE_TIME_I18N, JsonUtils.encode(dateTimeI18n), true);
69      writer.writeAttribute(DataAttributes.TODAY_BUTTON, date.isTodayButton());
70    }
71  
72    @Override
73    protected void encodeBeginField(final FacesContext facesContext, final T component) throws IOException {
74      final TobagoResponseWriter writer = getResponseWriter(facesContext);
75  
76      writer.startElement(HtmlElements.DIV);
77      writer.writeClassAttribute(TobagoClass.INPUT__GROUP__OUTER);
78  
79      writer.startElement(HtmlElements.DIV);
80      writer.writeClassAttribute(BootstrapClass.INPUT_GROUP);
81  
82      super.encodeBeginField(facesContext, component);
83    }
84  
85    @Override
86    public void encodeEndField(final FacesContext facesContext, final T component) throws IOException {
87  
88      super.encodeEndField(facesContext, component);
89  
90      final String pattern = component.getPattern();
91      final TobagoResponseWriter writer = getResponseWriter(facesContext);
92  
93      writer.startElement(HtmlElements.SPAN);
94      writer.writeClassAttribute(BootstrapClass.INPUT_GROUP_APPEND);
95      writer.startElement(HtmlElements.BUTTON);
96      writer.writeClassAttribute(
97          BootstrapClass.BTN,
98          BootstrapClass.BTN_SECONDARY,
99          TobagoClass.DATE__PICKER);
100     writer.writeAttribute(HtmlAttributes.TYPE, HtmlButtonTypes.BUTTON);
101     writer.writeAttribute(HtmlAttributes.TITLE,
102         ResourceUtils.getString(facesContext, "date.title"), true);
103     writer.writeAttribute(HtmlAttributes.DISABLED, component.isDisabled() || component.isReadonly());
104     writer.writeAttribute(HtmlAttributes.TABINDEX, component.getTabIndex());
105 
106     final boolean hasDate = StringUtils.containsAny(pattern, "yYMDdE");
107     final boolean hasTime = StringUtils.containsAny(pattern, "Hhms");
108 
109     if (hasDate || !hasTime) { //  || !hasTime is, to have at least one icon
110       writer.startElement(HtmlElements.I);
111       writer.writeClassAttribute(Icons.FA, Icons.CALENDAR);
112       writer.endElement(HtmlElements.I);
113     }
114     if (hasTime) {
115       writer.startElement(HtmlElements.I);
116       writer.writeClassAttribute(Icons.FA, Icons.CLOCK_O);
117       writer.endElement(HtmlElements.I);
118     }
119 
120     if (StringUtils.containsAny(pattern, "GWFKzX")) {
121       LOG.warn("Pattern chars 'G', 'W', 'F', 'K', 'z' and 'X' are not supported: " + pattern);
122     }
123 
124     writer.endElement(HtmlElements.BUTTON);
125     writer.endElement(HtmlElements.SPAN);
126 
127     writer.endElement(HtmlElements.DIV);
128 
129     writer.endElement(HtmlElements.DIV);
130   }
131 
132   @Override
133   protected CssItem getRendererCssClass() {
134     return VaadinClass.INPUT;
135   }
136 }