org.sakaiproject.jsf2.renderer.InputDateRenderer Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jsf2-widgets Show documentation
Show all versions of jsf2-widgets Show documentation
This is the Maven project for the custom JSF 2 widgets.
The widgets and the resources projects are closely tied together.
These widgets will be deployed as a jar file containing Sakai JSF 2 widgets
(components).
Web applications can include this jar in order to use the Sakai JSF 2 widgets in a JSF tool.
/**
* Copyright (c) 2003-2021 The Apereo Foundation
*
* Licensed under the Educational Community 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://opensource.org/licenses/ecl2
*
* 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.sakaiproject.jsf2.renderer;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.Map;
import javax.faces.component.EditableValueHolder;
import javax.faces.component.UIComponent;
import javax.faces.component.UIInput;
import javax.faces.component.ValueHolder;
import javax.faces.context.FacesContext;
import javax.faces.context.ResponseWriter;
import javax.faces.render.Renderer;
import lombok.Data;
import org.sakaiproject.jsf2.util.ConfigurationResource;
import org.sakaiproject.jsf2.util.LocaleUtil;
import org.sakaiproject.jsf2.util.RendererUtil;
public class InputDateRenderer
extends Renderer
{
// calendar popup configuration
private static final String HEIGHT = "16";
private static final String WIDTH = "16";
/** Bean to get resources for specific locale. */
@Data
private class ConfigurationResourceBean
{
// calendar popup configuration
private final String cursorStyle;
private final String clickAlt;
private final String calendarPath;
private final String calendarIcon;
// input date and time configuration, from global resources
private final boolean inputMonthFirst;
private final String dateFormatString;
private final SimpleDateFormat dateFormat;
private final SimpleDateFormat timeFormat;
private final SimpleDateFormat dateTimeFormat;
private final String dateHint;
private final String timeHint;
public ConfigurationResourceBean(final Locale locale)
{
ConfigurationResource cr = new ConfigurationResource(locale);
String resources = cr.get("resources");
cursorStyle = cr.get("picker_style");
calendarPath = "/" + resources + "/" + cr.get("inputDatePopup");
calendarIcon = "/" + resources + "/" + cr.get("inputDateImage");
clickAlt = cr.get("date_pick_alt");
inputMonthFirst = Boolean.valueOf(cr.get("inputMonthFirst"));
boolean inputTimeColon = Boolean.valueOf(cr.get("inputTimeColon"));
boolean inputTime24 = Boolean.valueOf(cr.get("inputTime24"));
dateHint = cr.get("inputDateHint");
timeHint = cr.get("inputTimeHint");
String timeFormatString;
if (inputMonthFirst)
{
dateFormatString = cr.get("inputMonthFirstFormat");
}
else
{
dateFormatString = cr.get("inputDayFirstFormat");
}
if (inputTimeColon)
{
if (inputTime24)
{
timeFormatString = cr.get("inputTimeColonFormat24");
}
else
{
timeFormatString = cr.get("inputTimeColonFormatAMPM");
}
}
else
{
if (inputTime24)
{
timeFormatString = cr.get("inputTimeDotFormat24");
}
else
{
timeFormatString = cr.get("inputTimeDotFormatAMPM");
}
}
dateFormat = new SimpleDateFormat(dateFormatString, locale);
timeFormat = new SimpleDateFormat(timeFormatString, locale);
dateTimeFormat =
new SimpleDateFormat(dateFormatString + " " + timeFormatString, locale);
}
}
public boolean supportsComponentType(UIComponent component)
{
return (component instanceof UIInput);
}
/**
* decode the value
* @param context
* @param component
*/
public void decode(FacesContext context, UIComponent component)
{
if (RendererUtil.isDisabledOrReadonly(context, component))
{
return;
}
// get request parameters
Map requestParameterMap = context.getExternalContext()
.getRequestParameterMap();
String clientId = component.getClientId(context);
String dateStr = (String) requestParameterMap.get(clientId + "_date");
String timeStr = (String) requestParameterMap.get(clientId + "_time");
if(dateStr==null && timeStr==null) return;
EditableValueHolder ev = (EditableValueHolder) component;
// create holder for subcomponent values
// Map dateParts = new HashMap();
// // get the subcomponent values
// // the hidden clientId maintains all the values togethr
//
// dateParts.put("clientId", clientId);
// dateParts.put("date", date);
// dateParts.put("time", time);
// // set the submitted value to the subcomponent value map
// ev.setSubmittedValue(dateParts);
ConfigurationResourceBean crb = new ConfigurationResourceBean(LocaleUtil.getLocale(context));
Date date = null;
if(dateStr==null && timeStr!=null)
{
try
{
date = crb.getTimeFormat().parse(timeStr);
}
catch (ParseException ex)
{
//leave null
}
}
else if(dateStr!=null && timeStr==null)
{
try
{
date = crb.getDateFormat().parse(dateStr);
}
catch (ParseException ex)
{
//leave null
}
}
else
{
try
{
date = crb.getDateTimeFormat().parse(dateStr + " " + timeStr);
}
catch (ParseException ex)
{
//leave null
}
}
ev.setSubmittedValue(date);
}
/**
* Faces render output method .
* @param context FacesContext
for the current request
* @param component UIComponent
being rendered
*
* @throws IOException if an input/output error occurs
*/
public void encodeEnd(FacesContext context,
UIComponent component) throws IOException
{
if (RendererUtil.isDisabledOrReadonly(context, component) || !component.isRendered())
{
return;
}
ResponseWriter writer = context.getResponseWriter();
String clientId = component.getClientId(context);
/////////////////////////////////////////////////////////
// VALUE HOLDER AND ATTRIBUTES
/////////////////////////////////////////////////////////
ConfigurationResourceBean crb = new ConfigurationResourceBean(LocaleUtil.getLocale(context));
String dateString = "";
String timeString = "";
Date date = null;
Date submittedValue =
(Date) ( (EditableValueHolder) component).getSubmittedValue();
if (submittedValue != null)
{
date = submittedValue;
}
else
{
Object value = ( (ValueHolder) component).getValue();
if (value instanceof Date)
{
date = (Date) value;
}
else if (value instanceof String)
{
try
{
date = crb.getDateTimeFormat().parse((String) value);
}
catch (ParseException ex)
{
// leave alone
}
}
}
if (date !=null)
{
dateString = crb.getDateFormat().format(date);
timeString = crb.getTimeFormat().format(date);
}
// display options attributes
boolean showDate;
boolean showTime;
showDate = "true".equals
((String) RendererUtil.getAttribute(context, component, "showDate"));
showTime = "true".equals
((String) RendererUtil.getAttribute(context, component, "showTime"));
/////////////////////////////////////////////////////////
// RENDER DATE INPUT
/////////////////////////////////////////////////////////
if (showDate)
{
writer.write(" " + crb.getDateHint() + " " );
String dateId = clientId + "_date" ;
String type ="text";
writer.write(" ");
// script creates unique javascript popup calendar object
String calRand = "cal" + ("" + Math.random()).substring(2);
String calendar;
if (crb.isInputMonthFirst())
{
calendar = "calendar2";
}
else
{
calendar = "calendar1";
}
String calScript =
"var " + calRand + " = new " + calendar+ "(" +
"document.getElementById('" + dateId + "'));" +
"" + calRand + ".year_scroll = true;" +
"" + calRand + ".time_comp = false;";
// calendar icon with onclick to script
writer.write(" \n");
}
/////////////////////////////////////////////////////////
// RENDER TIME INPUT
/////////////////////////////////////////////////////////
if (showTime)
{
writer.write(" " + crb.getTimeHint() + " " );
String timeId = clientId + "_time" ;
writer.write(" ");
}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy