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.UITime;
24  import org.apache.myfaces.tobago.context.Markup;
25  import org.apache.myfaces.tobago.context.ResourceManagerUtils;
26  import org.apache.myfaces.tobago.internal.util.DateFormatUtils;
27  import org.apache.myfaces.tobago.renderkit.InputRendererBase;
28  import org.apache.myfaces.tobago.renderkit.css.Classes;
29  import org.apache.myfaces.tobago.renderkit.css.Style;
30  import org.apache.myfaces.tobago.renderkit.html.DataAttributes;
31  import org.apache.myfaces.tobago.renderkit.html.HtmlAttributes;
32  import org.apache.myfaces.tobago.renderkit.html.HtmlElements;
33  import org.apache.myfaces.tobago.renderkit.html.HtmlInputTypes;
34  import org.apache.myfaces.tobago.renderkit.html.util.HtmlRendererUtils;
35  import org.apache.myfaces.tobago.util.ComponentUtils;
36  import org.apache.myfaces.tobago.webapp.TobagoResponseWriter;
37  import org.slf4j.Logger;
38  import org.slf4j.LoggerFactory;
39  
40  import javax.faces.component.UIComponent;
41  import javax.faces.context.FacesContext;
42  import javax.faces.convert.Converter;
43  import javax.faces.convert.DateTimeConverter;
44  import java.io.IOException;
45  import java.text.SimpleDateFormat;
46  import java.util.Calendar;
47  import java.util.Date;
48  
49  public class TimeRenderer extends InputRendererBase {
50  
51    private static final Logger LOG = LoggerFactory.getLogger(TimeRenderer.class);
52  
53    public void encodeEnd(FacesContext facesContext, UIComponent component) throws IOException {
54  
55      UITime time = (UITime) component;
56  
57      String title = HtmlRendererUtils.getTitleFromTipAndMessages(facesContext, time);
58      String currentValue = getCurrentValue(facesContext, time);
59      if (LOG.isDebugEnabled()) {
60        LOG.debug("currentValue = '" + currentValue + "'");
61      }
62  
63      String pattern = "HH:mm";
64      if (time.getConverter() != null) {
65        Converter converter = time.getConverter();
66        if (converter instanceof DateTimeConverter) {
67          String string = DateFormatUtils.findPattern((DateTimeConverter) converter);
68          if (string != null && string.indexOf('s') > -1) {
69            pattern += ":ss";
70          }
71        }
72      }
73  
74      boolean hasSeconds = pattern.indexOf('s') > -1;
75  
76      Object value = time.getValue();
77      Date date;
78      if (value instanceof Date) {
79        date = (Date) value;
80      } else if (value instanceof Calendar) {
81        date = ((Calendar) value).getTime();
82      } else {
83        date = new Date();
84      }
85  
86      String hour = new SimpleDateFormat("HH").format(date);
87      String minute = new SimpleDateFormat("mm").format(date);
88      String second = new SimpleDateFormat("ss").format(date);
89  
90      String id = time.getClientId(facesContext);
91      final String idPrefix = id + ComponentUtils.SUB_SEPARATOR;
92  
93      final TobagoResponseWriter writer = HtmlRendererUtils.getTobagoResponseWriter(facesContext);
94  
95      writer.startElement(HtmlElements.DIV, time);
96      writer.writeIdAttribute(id);
97      writer.writeClassAttribute(Classes.create(time));
98      HtmlRendererUtils.writeDataAttributes(facesContext, writer, time);
99      Style style = new Style(facesContext, time);
100     writer.writeStyleAttribute(style);
101     String dateInputId = (String) time.getAttributes().get(Attributes.DATE_INPUT_ID);
102     if (dateInputId != null) {
103       writer.writeAttribute(DataAttributes.DATE_INPUT_ID, dateInputId, false);
104     }
105     writer.writeAttribute(DataAttributes.PATTERN, pattern, false);
106 
107     writer.startElement(HtmlElements.DIV, time);
108     writer.writeIdAttribute(idPrefix + "borderDiv");
109     Markup markup = time.getCurrentMarkup();
110     if (hasSeconds) {
111       markup = markup.add(Markup.SECONDS);
112     }
113     writer.writeClassAttribute(Classes.create(time, "borderDiv", markup));
114 
115     writeInput(writer, time, idPrefix, "hour", hour, title, 24);
116     writeInputSeparator(writer, time, ":");
117     writeInput(writer, time, idPrefix, "minute", minute, title, 60);
118     if (hasSeconds) {
119       writeInputSeparator(writer, time, ":");
120       writeInput(writer, time, idPrefix, "second", second, title, 60);
121     }
122 
123     writer.endElement(HtmlElements.DIV);
124 
125     writer.startElement(HtmlElements.IMG, null);
126     writer.writeIdAttribute(idPrefix + "inc");
127     writer.writeClassAttribute(Classes.create(time, "incImage", markup));
128     writer.writeAttribute(HtmlAttributes.SRC,
129         ResourceManagerUtils.getImageWithPath(facesContext, "image/timeIncrement.gif"), true);
130     writer.writeAttribute(HtmlAttributes.ALT, "", false); // TODO: tip
131     writer.writeAttribute(HtmlAttributes.READONLY, time.isReadonly());
132     writer.writeAttribute(HtmlAttributes.DISABLED, time.isDisabled());
133     writer.endElement(HtmlElements.IMG);
134 
135     writer.startElement(HtmlElements.IMG, null);
136     writer.writeIdAttribute(idPrefix + "dec");
137     writer.writeClassAttribute(Classes.create(time, "decImage", markup));
138     writer.writeAttribute(HtmlAttributes.SRC,
139         ResourceManagerUtils.getImageWithPath(facesContext, "image/timeDecrement.gif"), true);
140     writer.writeAttribute(HtmlAttributes.ALT, "", false); // TODO: tip
141     writer.writeAttribute(HtmlAttributes.READONLY, time.isReadonly());
142     writer.writeAttribute(HtmlAttributes.DISABLED, time.isDisabled());
143     writer.endElement(HtmlElements.IMG);
144 
145     writer.startElement(HtmlElements.INPUT, time);
146     writer.writeAttribute(HtmlAttributes.TYPE, HtmlInputTypes.HIDDEN, false);
147     writer.writeIdAttribute(idPrefix + "field");
148     writer.writeNameAttribute(id);
149     writer.writeAttribute(HtmlAttributes.VALUE, hour + ":" + minute + ":" + second, false);
150     writer.endElement(HtmlElements.INPUT);
151 
152     writer.endElement(HtmlElements.DIV);
153   }
154 
155   private void writeInputSeparator(TobagoResponseWriter writer, UITime time, String sep) throws IOException {
156     writer.startElement(HtmlElements.SPAN, null);
157     writer.writeClassAttribute(Classes.create(time, "sep"));
158     writer.writeText(sep);
159     writer.endElement(HtmlElements.SPAN);
160   }
161 
162   private void writeInput(
163       TobagoResponseWriter writer, UITime input, String idPrefix, String unit, String value, String title, int max)
164       throws IOException {
165     Integer tabIndex = input.getTabIndex();
166     writer.startElement(HtmlElements.INPUT, null);
167     writer.writeAttribute(HtmlAttributes.TYPE, HtmlInputTypes.TEXT, false);
168     writer.writeIdAttribute(idPrefix + unit);
169     if (tabIndex != null) {
170       writer.writeAttribute(HtmlAttributes.TABINDEX, tabIndex);
171     }
172     writer.writeAttribute(HtmlAttributes.TITLE, title, true);
173 
174     writer.writeClassAttribute(Classes.create(input, "input"));
175     writer.writeAttribute(HtmlAttributes.READONLY, input.isReadonly());
176     writer.writeAttribute(HtmlAttributes.DISABLED, input.isDisabled());
177     writer.writeAttribute(HtmlAttributes.VALUE, value, true);
178     writer.writeAttribute(DataAttributes.MAX, Integer.toString(max), true);
179     writer.writeAttribute(DataAttributes.UNIT, unit, true);
180     writer.endElement(HtmlElements.INPUT);
181   }
182 }