/
TimeAgoRenderer.java
99 lines (81 loc) · 3.41 KB
/
TimeAgoRenderer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/**
* Copyright 2011-2019 PrimeFaces Extensions
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.primefaces.extensions.component.timeago;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.context.ResponseWriter;
import org.primefaces.renderkit.CoreRenderer;
import org.primefaces.util.WidgetBuilder;
/**
* <code>TimeAgo</code> component renderer.
*
* @author Jasper de Vries <jepsar@gmail.com>
* @since 7.0.1
*/
public class TimeAgoRenderer extends CoreRenderer {
private static final String DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:ssZ";
@Override
public void encodeEnd(FacesContext context, UIComponent component) throws IOException {
final TimeAgo timeAgo = (TimeAgo) component;
final Object value = timeAgo.getValue();
if (value == null) {
return;
}
encodeMarkup(context, timeAgo, format(value));
encodeScript(context, timeAgo);
}
protected void encodeMarkup(FacesContext context, TimeAgo timeAgo, String valueToRender) throws IOException {
final ResponseWriter writer = context.getResponseWriter();
final String clientId = timeAgo.getClientId(context);
String styleClass = timeAgo.getStyleClass();
styleClass = styleClass == null ? TimeAgo.STYLE_CLASS : TimeAgo.STYLE_CLASS + " " + styleClass;
writer.startElement("span", timeAgo);
writer.writeAttribute("id", clientId, null);
writer.writeAttribute("class", styleClass, "styleClass");
if (timeAgo.getStyle() != null) {
writer.writeAttribute("style", timeAgo.getStyle(), "style");
}
encodeTime(context, timeAgo, clientId, valueToRender);
writer.endElement("span");
}
protected void encodeTime(FacesContext context, TimeAgo timeAgo, String clientId, String valueToRender)
throws IOException {
final ResponseWriter writer = context.getResponseWriter();
writer.startElement("time", null);
writer.writeAttribute("datetime", valueToRender, null);
writer.writeText(valueToRender, null);
writer.endElement("time");
}
protected void encodeScript(FacesContext context, TimeAgo timeAgo) throws IOException {
final String clientId = timeAgo.getClientId(context);
final WidgetBuilder wb = getWidgetBuilder(context);
wb.init("ExtTimeAgo", timeAgo.resolveWidgetVar(), clientId);
wb.finish();
}
// TODO When PF bumps to Java 8, support more types
protected String format(final Object value) {
return format((Date) value);
}
protected String format(final Date value) {
final SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
return sdf.format(value);
}
}