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 package info.magnolia.ui.workbench.column;
35
36 import info.magnolia.cms.security.MgnlUserManager;
37 import info.magnolia.context.Context;
38 import info.magnolia.objectfactory.Components;
39 import info.magnolia.ui.workbench.column.definition.AbstractColumnDefinition;
40 import info.magnolia.ui.workbench.column.definition.MetaDataColumnDefinition;
41
42 import java.util.Date;
43 import java.util.Locale;
44 import java.util.SimpleTimeZone;
45 import java.util.TimeZone;
46
47 import javax.inject.Inject;
48
49 import org.apache.commons.lang3.StringUtils;
50 import org.apache.commons.lang3.time.FastDateFormat;
51
52 import com.vaadin.server.Page;
53 import com.vaadin.v7.data.Item;
54 import com.vaadin.v7.data.Property;
55 import com.vaadin.v7.ui.Table;
56
57
58
59
60 public class DateColumnFormatter extends AbstractColumnFormatter<AbstractColumnDefinition> {
61
62 private static final String BROWSER_TIMEZONE = "browser";
63
64 private final FastDateFormat dateFormatter;
65 private final FastDateFormat timeFormatter;
66 private final TimeZone timeZone;
67
68 private final Context context;
69
70 @Inject
71 public DateColumnFormatter(AbstractColumnDefinition definition, Context context) {
72 super(definition);
73 this.context = context;
74
75 final Locale locale = context.getLocale();
76 final String timeZoneId = context.getUser().getProperty(MgnlUserManager.PROPERTY_TIMEZONE);
77 if (timeZoneId == null || BROWSER_TIMEZONE.equals(timeZoneId)) {
78 if (Page.getCurrent() != null) {
79 int offset = Page.getCurrent().getWebBrowser().getTimezoneOffset();
80 timeZone = new SimpleTimeZone(offset, BROWSER_TIMEZONE);
81 } else {
82 timeZone = null;
83 }
84 } else {
85 timeZone = TimeZone.getTimeZone(timeZoneId);
86 }
87 if (definition instanceof MetaDataColumnDefinition) {
88 MetaDataColumnDefinition metaDataColumnDefinition = (MetaDataColumnDefinition) definition;
89
90
91 if (StringUtils.isEmpty(metaDataColumnDefinition.getDateFormat())) {
92 dateFormatter = null;
93 } else {
94 dateFormatter = FastDateFormat.getInstance(metaDataColumnDefinition.getDateFormat(), timeZone, locale);
95 }
96 if (StringUtils.isEmpty(metaDataColumnDefinition.getTimeFormat())) {
97 timeFormatter = null;
98 } else {
99 timeFormatter = FastDateFormat.getInstance(metaDataColumnDefinition.getTimeFormat(), timeZone, locale);
100 }
101 } else {
102 dateFormatter = FastDateFormat.getDateInstance(FastDateFormat.MEDIUM, timeZone, locale);
103 timeFormatter = FastDateFormat.getTimeInstance(FastDateFormat.SHORT, timeZone, locale);
104 }
105 }
106
107
108
109
110 @Deprecated
111 public DateColumnFormatter(AbstractColumnDefinition definition) {
112 this(definition, Components.getComponent(Context.class));
113 }
114
115 @Override
116 public Object generateCell(Table source, Object itemId, Object columnId) {
117 Item item = source.getItem(itemId);
118 Property prop = (item == null) ? null : item.getItemProperty(columnId);
119
120
121 if (prop != null && prop.getValue() != null && Date.class.equals(prop.getType())) {
122 String date = dateFormatter == null ? StringUtils.EMPTY : dateFormatter.format(prop.getValue());
123 String time = timeFormatter == null ? StringUtils.EMPTY : timeFormatter.format(prop.getValue());
124 String dateHtml = StringUtils.isEmpty(date) ? StringUtils.EMPTY : String.format("<span class=\"datefield\">%s</span>", date);
125 String timeHtml = StringUtils.isEmpty(time) ? StringUtils.EMPTY : String.format("<span class=\"timefield\">%s</span>", time);
126 String tooltip = timeZone == null ? StringUtils.EMPTY : "title=\"" + date + " " + time + " (" + timeZone.getDisplayName(context.getLocale()) + ")" + "\"";
127 return String.format("<span class=\"datetimefield\" %s>%s%s</span>", tooltip, dateHtml, timeHtml);
128 }
129 return null;
130 }
131 }