1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.myfaces.custom.schedule;
21
22 import org.apache.myfaces.custom.schedule.model.ScheduleEntry;
23 import org.apache.myfaces.custom.schedule.util.ScheduleEntryComparator;
24 import org.apache.myfaces.custom.schedule.util.ScheduleUtil;
25 import org.apache.myfaces.renderkit.html.util.AddResource;
26 import org.apache.myfaces.renderkit.html.util.AddResourceFactory;
27 import org.apache.myfaces.shared_tomahawk.renderkit.html.HTML;
28
29 import javax.faces.component.UIComponent;
30 import javax.faces.context.FacesContext;
31 import javax.faces.context.ResponseWriter;
32 import javax.faces.el.ValueBinding;
33 import javax.faces.event.ActionEvent;
34 import javax.faces.render.Renderer;
35 import java.io.IOException;
36 import java.io.Serializable;
37 import java.text.DateFormat;
38 import java.text.SimpleDateFormat;
39 import java.util.Calendar;
40 import java.util.Date;
41 import java.util.Locale;
42 import java.util.Map;
43
44
45
46
47
48
49
50
51
52
53 public abstract class AbstractScheduleRenderer extends Renderer implements
54 Serializable
55 {
56
57 protected static final ScheduleEntryComparator comparator = new ScheduleEntryComparator();
58 protected static final String LAST_CLICKED_DATE = "_last_clicked_date";
59 protected static final String LAST_CLICKED_Y = "_last_clicked_y";
60 private static final String CSS_RESOURCE = "css/schedule.css";
61 public static final String DEFAULT_THEME = "default";
62 public static final String OUTLOOK_THEME = "outlookxp";
63 public static final String EVOLUTION_THEME = "evolution";
64
65
66
67
68
69
70
71 public void decode(FacesContext context, UIComponent component)
72 {
73 HtmlSchedule schedule = (HtmlSchedule) component;
74 boolean queueAction = false;
75 if (ScheduleUtil.canModifyValue(component))
76 {
77 Map parameters = context.getExternalContext()
78 .getRequestParameterMap();
79 String selectedEntryId = (String) parameters.get((String) schedule
80 .getClientId(context));
81 String lastClickedDateId = (String) parameters
82 .get((String) schedule.getClientId(context)
83 + LAST_CLICKED_DATE);
84 String lastClickedY = (String) parameters.get((String) schedule
85 .getClientId(context)
86 + LAST_CLICKED_Y);
87
88 ScheduleMouseEvent mouseEvent = null;
89
90 if ((selectedEntryId != null) && (selectedEntryId.length() > 0))
91 {
92 ScheduleEntry entry = schedule.findEntry(selectedEntryId);
93 schedule.setSubmittedEntry(entry);
94 mouseEvent = new ScheduleMouseEvent(schedule,
95 ScheduleMouseEvent.SCHEDULE_ENTRY_CLICKED);
96 queueAction = true;
97 }
98
99 if (schedule.isSubmitOnClick())
100 {
101 schedule.resetMouseEvents();
102 if ((lastClickedY != null) && (lastClickedY.length() > 0))
103 {
104
105 schedule
106 .setLastClickedDateAndTime(determineLastClickedDate(
107 schedule, lastClickedDateId, lastClickedY));
108 mouseEvent = new ScheduleMouseEvent(schedule,
109 ScheduleMouseEvent.SCHEDULE_BODY_CLICKED);
110 queueAction = true;
111 }
112 else if ((lastClickedDateId != null)
113 && (lastClickedDateId.length() > 0))
114 {
115
116 schedule
117 .setLastClickedDateAndTime(determineLastClickedDate(
118 schedule, lastClickedDateId, "0"));
119 mouseEvent = new ScheduleMouseEvent(schedule,
120 ScheduleMouseEvent.SCHEDULE_HEADER_CLICKED);
121 queueAction = true;
122 }
123 else if (mouseEvent == null)
124 {
125
126 mouseEvent = new ScheduleMouseEvent(schedule,
127 ScheduleMouseEvent.SCHEDULE_NOTHING_CLICKED);
128 }
129 }
130
131 if (mouseEvent != null)
132 schedule.queueEvent(mouseEvent);
133 }
134 if (queueAction)
135 {
136 schedule.queueEvent(new ActionEvent(schedule));
137 }
138 }
139
140
141
142
143
144 public void encodeBegin(FacesContext context, UIComponent component)
145 throws IOException
146 {
147 if (!component.isRendered())
148 {
149 return;
150 }
151
152 HtmlSchedule schedule = (HtmlSchedule) component;
153 ResponseWriter writer = context.getResponseWriter();
154
155
156
157 AddResource addResource = AddResourceFactory.getInstance(context);
158 String theme = schedule.getTheme();
159
160
161 if (DEFAULT_THEME.equals(theme) || OUTLOOK_THEME.equals(theme)
162 || EVOLUTION_THEME.equals(theme))
163 {
164 addResource.addStyleSheet(context, AddResource.HEADER_BEGIN,
165 HtmlSchedule.class, CSS_RESOURCE);
166 }
167 addResource.addJavaScriptAtPosition(context, AddResource.HEADER_BEGIN,
168 HtmlSchedule.class, "javascript/schedule.js");
169
170 if (schedule.isTooltip())
171 {
172 addResource.addJavaScriptAtPosition(context, AddResource.HEADER_BEGIN,
173 HtmlSchedule.class, "javascript/alphaAPI.js");
174 addResource.addJavaScriptAtPosition(context, AddResource.HEADER_BEGIN,
175 HtmlSchedule.class, "javascript/domLib.js");
176 addResource.addJavaScriptAtPosition(context, AddResource.HEADER_BEGIN,
177 HtmlSchedule.class, "javascript/domTT.js");
178 addResource.addJavaScriptAtPosition(context, AddResource.HEADER_BEGIN,
179 HtmlSchedule.class, "javascript/fadomatic.js");
180 }
181
182
183 writer.startElement(HTML.INPUT_ELEM, schedule);
184 writer.writeAttribute(HTML.TYPE_ATTR, "hidden", null);
185 writer.writeAttribute(HTML.NAME_ATTR, schedule.getClientId(context),
186 "clientId");
187 writer.endElement(HTML.INPUT_ELEM);
188
189 writer.startElement(HTML.INPUT_ELEM, schedule);
190 writer.writeAttribute(HTML.TYPE_ATTR, "hidden", null);
191 writer.writeAttribute(HTML.NAME_ATTR, schedule.getClientId(context)
192 + "_last_clicked_date", "clicked_date");
193 writer.endElement(HTML.INPUT_ELEM);
194
195
196
197 writer.startElement(HTML.INPUT_ELEM, schedule);
198 writer.writeAttribute(HTML.TYPE_ATTR, "hidden", null);
199 writer.writeAttribute(HTML.NAME_ATTR, schedule.getClientId(context)
200 + "_last_clicked_y", "clicked_y");
201 writer.endElement(HTML.INPUT_ELEM);
202 }
203
204
205
206
207
208
209
210
211
212
213
214
215
216 protected String getDateString(FacesContext context, UIScheduleBase schedule,
217 Date date)
218 {
219
220 return getDateFormat(context, schedule, schedule.getHeaderDateFormat(), date).format(date);
221 }
222
223 protected static DateFormat getDateFormat(FacesContext context, UIScheduleBase schedule, String pattern)
224 {
225 Locale viewLocale = context.getViewRoot().getLocale();
226 DateFormat format = (pattern != null && pattern.length() > 0) ?
227 new SimpleDateFormat(pattern, viewLocale) :
228 DateFormat.getDateInstance(DateFormat.MEDIUM, viewLocale);
229
230 format.setTimeZone(schedule.getModel().getTimeZone());
231
232 return format;
233 }
234
235 protected static DateFormat getDateFormat(FacesContext context, UIScheduleBase schedule, String pattern, Date date)
236 {
237 Locale viewLocale = context.getViewRoot().getLocale();
238
239 if (pattern != null && pattern.indexOf("d'th'") >= 0)
240 {
241 pattern = pattern.replaceAll("d'th'", "d'" + daySuffix(schedule, date, viewLocale) + "'");
242 }
243
244 return getDateFormat(context, schedule, pattern);
245 }
246
247 private static String daySuffix(UIScheduleBase schedule, Date date, Locale locale) {
248 String language = locale.getLanguage();
249 Calendar calendar = ScheduleUtil.getCalendarInstance(date, schedule.getModel().getTimeZone());
250
251 int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
252
253 if (Locale.ENGLISH.getLanguage().equals(language))
254 {
255 switch(dayOfMonth) {
256 case 1:
257 case 21:
258 case 31:
259 return "st";
260 case 2:
261 case 22:
262 return "nd";
263 case 3:
264 case 23:
265 return "rd";
266 default:
267 return "th";
268 }
269 }
270 else if (Locale.GERMAN.getLanguage().equals(language))
271 {
272 return ".";
273 }
274 else
275 {
276 return "";
277 }
278 }
279
280
281
282
283
284
285
286
287
288
289 protected String getStyleClass(UIComponent component, String className)
290 {
291
292 ValueBinding binding = component.getValueBinding(className);
293 if (binding != null)
294 {
295 String value = (String) binding.getValue(FacesContext
296 .getCurrentInstance());
297
298 if (value != null)
299 {
300 return value;
301 }
302 }
303
304
305 Map attributes = component.getAttributes();
306 String returnValue = (String) attributes.get(className + "Class");
307 return returnValue == null ? className : returnValue;
308 }
309
310
311
312
313
314
315
316
317
318
319 protected ScheduleEntryRenderer getEntryRenderer(HtmlSchedule schedule)
320 {
321 Object entryRenderer = schedule.getEntryRenderer();
322 if (entryRenderer instanceof ScheduleEntryRenderer)
323 {
324 return (ScheduleEntryRenderer) entryRenderer;
325 } else {
326 return new DefaultScheduleEntryRenderer();
327 }
328 }
329
330
331
332
333 protected abstract int getDefaultRowHeight();
334
335
336
337
338
339
340
341 protected abstract int getRowHeight(UIScheduleBase schedule);
342
343
344
345
346
347
348
349
350 protected abstract Date determineLastClickedDate(HtmlSchedule schedule,
351 String dateId, String yPos);
352
353
354
355
356 public boolean getRendersChildren()
357 {
358 return true;
359 }
360
361 protected Calendar getCalendarInstance(UIScheduleBase schedule, Date date)
362 {
363 return ScheduleUtil.getCalendarInstance(date, schedule.getModel().getTimeZone());
364 }
365 }
366