br.com.jarch.faces.ui.InputDateUI Maven / Gradle / Ivy
package br.com.jarch.faces.ui;
import br.com.jarch.util.LogUtils;
import javax.faces.component.FacesComponent;
import javax.faces.component.NamingContainer;
import javax.faces.component.UIInput;
import javax.faces.component.UINamingContainer;
import javax.faces.context.FacesContext;
import javax.faces.convert.ConverterException;
import javax.faces.event.AjaxBehaviorEvent;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
@FacesComponent("br.com.jarch.faces.ui.InputDateUI")
public class InputDateUI extends UIInput implements NamingContainer {
public static final int ONE_HUNDRED = 100;
// Fields -------------------------------------------------------------------------------------
private UIInput day;
private UIInput month;
private UIInput year;
private static Integer[] createIntegerArray(int begin, int end) {
int direction = Integer.compare(end, begin);
int size = Math.abs(end - begin) + 1;
Integer[] array = new Integer[size];
for (int i = 0; i < size; i++) {
array[i] = begin + (i * direction);
}
return array;
}
@Override
public String getFamily() {
return UINamingContainer.COMPONENT_FAMILY;
}
@Override
public void encodeBegin(FacesContext context) throws IOException {
Calendar calendar = Calendar.getInstance();
int maxYear = getAttributeValue("yearMax", calendar.get(Calendar.YEAR));
int minYear = getAttributeValue("yearMin", maxYear - ONE_HUNDRED);
Date date = (Date) getValue();
if (date != null) {
calendar.setTime(date);
int yearLocal = calendar.get(Calendar.YEAR);
if (yearLocal > maxYear || minYear > yearLocal) {
throw new IllegalArgumentException(String.format("Year %d out of min/max range %d/%d.", yearLocal, minYear, maxYear));
}
}
day.setValue(calendar.get(Calendar.DATE));
month.setValue(calendar.get(Calendar.MONTH) + 1);
year.setValue(calendar.get(Calendar.YEAR));
setDays(createIntegerArray(1, calendar.getActualMaximum(Calendar.DATE)));
setMonths(createIntegerArray(1, calendar.getActualMaximum(Calendar.MONTH) + 1));
setYears(createIntegerArray(maxYear, minYear));
super.encodeBegin(context);
}
@Override
public Object getSubmittedValue() {
return day.getSubmittedValue()
+ "-" + month.getSubmittedValue()
+ "-" + year.getSubmittedValue();
}
@Override
protected Object getConvertedValue(FacesContext context, Object submittedValue) {
try {
return new SimpleDateFormat("dd-MM-yyyy").parse((String) submittedValue);
} catch (ParseException e) {
throw new ConverterException(e); // This is not to be expected in normal circumstances.
}
}
public void updateDaysIfNecessary(AjaxBehaviorEvent event) {
LogUtils.generate("event = " + event);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.DATE, 1);
calendar.set(Calendar.MONTH, (Integer) month.getValue() - 1);
calendar.set(Calendar.YEAR, (Integer) year.getValue());
int maxDay = calendar.getActualMaximum(Calendar.DATE);
if (getDays().length != maxDay) {
setDays(createIntegerArray(1, maxDay));
if ((Integer) day.getValue() > maxDay) {
day.setValue(maxDay); // Fix the selected value if it exceeds new max value.
}
FacesContext context = FacesContext.getCurrentInstance(); // Update day field.
context.getPartialViewContext().getRenderIds().add(day.getClientId(context));
}
}
private T getAttributeValue(String key, T defaultValue) {
T value = (T) getAttributes().get(key);
return (value != null) ? value : defaultValue;
}
public UIInput getDay() {
return day;
}
public void setDay(UIInput day) {
this.day = day;
}
public UIInput getMonth() {
return month;
}
public void setMonth(UIInput month) {
this.month = month;
}
public UIInput getYear() {
return year;
}
public void setYear(UIInput year) {
this.year = year;
}
public Integer[] getDays() {
return (Integer[]) getStateHelper().get("days");
}
public void setDays(Integer[] days) {
getStateHelper().put("days", days);
}
public Integer[] getMonths() {
return (Integer[]) getStateHelper().get("months");
}
public void setMonths(Integer[] months) {
getStateHelper().put("months", months);
}
public Integer[] getYears() {
return (Integer[]) getStateHelper().get("years");
}
public void setYears(Integer[] years) {
getStateHelper().put("years", years);
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy