Skip to content

Commit

Permalink
#654 optimization and several bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
stephanrauh committed Mar 25, 2017
1 parent 566f7a8 commit 821dfca
Show file tree
Hide file tree
Showing 5 changed files with 245 additions and 195 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,9 @@ public void setValueExpression(String name, ValueExpression binding) {
super.setValueExpression(name, binding);
}

/**
* Converts the date from the moment.js format to a java.util.Date.
*/
@Override
public Object getConvertedValue(FacesContext context, Object submittedValue)
throws ConverterException {
Expand All @@ -130,7 +133,6 @@ public Object getConvertedValue(FacesContext context, Object submittedValue)
if (val.trim().length() == 0) {
return null;
}
//System.out.println("CV: " + val);

Converter converter = this.getConverter();

Expand All @@ -140,12 +142,11 @@ public Object getConvertedValue(FacesContext context, Object submittedValue)
}
// Else we use our own converter
Locale sloc = BsfUtils.selectLocale(context.getViewRoot().getLocale(), this.getLocale(), this);
String componentFormat = BsfUtils.selectDateTimeFormat(sloc, this.getFormat(), this.isShowDate(), this.isShowTime());
String sdf = BsfUtils.selectDateFormat(sloc, componentFormat);
sdf = LocaleUtils.momentToJavaFormat(sdf);
String momentJSFormat = BsfUtils.selectMomentJSDateTimeFormat(sloc, this.getFormat(), this.isShowDate(), this.isShowTime());
String javaFormat = LocaleUtils.momentToJavaFormat(momentJSFormat);

Calendar cal = Calendar.getInstance(sloc);
SimpleDateFormat format = new SimpleDateFormat(sdf, sloc);
SimpleDateFormat format = new SimpleDateFormat(javaFormat, sloc);
format.setTimeZone(cal.getTimeZone());

try {
Expand All @@ -162,7 +163,7 @@ public Object getConvertedValue(FacesContext context, Object submittedValue)
e.printStackTrace();
this.setValid(false);
throw new ConverterException(
BsfUtils.getMessage("javax.faces.converter.DateTimeConverter.DATE", val, sdf, BsfUtils.getLabel(context, this)));
BsfUtils.getMessage("javax.faces.converter.DateTimeConverter.DATE", val, javaFormat, BsfUtils.getLabel(context, this)));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package net.bootsfaces.component.dateTimePicker;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

Expand All @@ -35,7 +36,6 @@
import net.bootsfaces.render.Responsive;
import net.bootsfaces.render.Tooltip;
import net.bootsfaces.utils.BsfUtils;
import net.bootsfaces.utils.FacesMessages;
import net.bootsfaces.utils.LocaleUtils;
import net.bootsfaces.utils.TestSingleton;

Expand Down Expand Up @@ -64,11 +64,11 @@ public void decode(FacesContext context, UIComponent component) {
fieldId = clientId + "_Input";
}
new AJAXRenderer().decode(context, dtp, fieldId);
new AJAXRenderer().decode(context, dtp, clientId);
// new AJAXRenderer().decode(context, dtp, clientId);
}

/**
* Get value displayable
* Yields the value which is displayed in the input field of the date picker.
* @param ctx
* @param dtp
* @return
Expand All @@ -79,11 +79,9 @@ public static String getValueAsString(Object value, FacesContext ctx, DateTimePi
return null;
}
Locale sloc = BsfUtils.selectLocale(ctx.getViewRoot().getLocale(), dtp.getLocale(), dtp);
String sdf = BsfUtils.selectDateFormat(sloc, dtp.getFormat());
// assume that the format is always specified as moment.js format
sdf = LocaleUtils.momentToJavaFormat(sdf);
String javaFormatString = BsfUtils.selectJavaDateTimeFormatFromMomentJSFormatOrDefault(sloc, dtp.getFormat(), dtp.isShowDate(), dtp.isShowTime());

return getDateAsString(ctx, dtp, value, sdf, sloc);
return getDateAsString(ctx, dtp, value, javaFormatString, sloc);
}

/**
Expand All @@ -94,33 +92,31 @@ public static String getValueAsString(Object value, FacesContext ctx, DateTimePi
* @return
*/

public static String getDateAsString(FacesContext fc, DateTimePicker dtp, Object value, String format, Locale locale) {
public static String getDateAsString(FacesContext fc, DateTimePicker dtp, Object value, String javaFormatString, Locale locale) {
if (value == null) {
return null;
}

Converter converter = dtp.getConverter();
return converter == null ?
getInternalDateAsString(value, format, locale)
getInternalDateAsString(value, javaFormatString, locale)
:
converter.getAsString(fc, dtp, value);

}


public static String getInternalDateAsString(Object dt, String format, Locale locale) {
public static String getInternalDateAsString(Object dt, String javaFormatString, Locale locale) {
if (dt == null) {
return null;
}

if (dt instanceof String) {
return (String) dt;
} else if (dt instanceof Date) {
// SimpleDateFormat dtFormat = new SimpleDateFormat(format, locale);
// dtFormat.setTimeZone(java.util.TimeZone.getDefault());
//
// return dtFormat.format((Date) dt);
return TestSingleton.getInstance().formatDate((Date) dt, format);
SimpleDateFormat dtFormat = new SimpleDateFormat(javaFormatString, locale);
String result = dtFormat.format((Date) dt);
return result;
} else {
throw new IllegalArgumentException(
"Value could be either String or java.util.Date, you may want to use a custom converter.");
Expand Down Expand Up @@ -379,12 +375,10 @@ private void encodeJS(FacesContext fc, ResponseWriter rw, DateTimePicker dtp, St
}

Locale sloc = BsfUtils.selectLocale(fc.getViewRoot().getLocale(), dtp.getLocale(), dtp);
String format = BsfUtils.selectDateTimeFormat(sloc, dtp.getFormat(), dtp.isShowDate(), dtp.isShowTime());
String displayFormat = "'" + (dtp.getFormat() == null ? LocaleUtils.javaToMomentFormat(format) : format) + "'";
String inlineDisplayDate = "'" + (dtp.getFormat() == null ?
getDateAsString(fc, dtp, v, format, sloc)
:
getDateAsString(fc, dtp, v, LocaleUtils.momentToJavaFormat(format), sloc)) + "'";
String format = BsfUtils.selectMomentJSDateTimeFormat(sloc, dtp.getFormat(), dtp.isShowDate(), dtp.isShowTime());
String displayFormat = "'" + format + "'";
String inlineDisplayDate = "'" +
getValueAsString(v, fc, dtp) + "'";

String fullSelector = "#" + BsfUtils.escapeJQuerySpecialCharsInSelector(datePickerId);

Expand Down
74 changes: 70 additions & 4 deletions src/main/java/net/bootsfaces/utils/BsfUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -474,19 +474,49 @@ public static String selectDateFormat(Locale locale, String format) {

return selFormat;
}

/**
* Selects the Date Pattern to use based on the given Locale if the input
* format is null
*
* @param locale
* Locale (may be the result of a call to selectLocale)
* @param format
* optional Input format String, given as Moment.js date format
* @return Moment.js Date Pattern eg. DD/MM/YYYY
*/
public static String selectMomentJSDateFormat(Locale locale, String format) {
String selFormat;
if (format == null) {
selFormat = ((SimpleDateFormat) DateFormat.getDateInstance(DateFormat.SHORT, locale)).toPattern();
// Since DateFormat.SHORT is silly, return a smart format
if (selFormat.equals("M/d/yy")) {
return "MM/DD/YYYY";
}
if (selFormat.equals("d/M/yy")) {
return "DD/MM/YYYY";
}
return LocaleUtils.javaToMomentFormat(selFormat);
} else {
selFormat = format;
}

return selFormat;
}


/**
* Selects the Date Pattern to use based on the given Locale if the input
* format is null
*
* @param locale
* Locale (may be the result of a call to selectLocale)
* @param momentJSFormat
* Input format String
* @return Date Pattern eg. dd/MM/yyyy
*/
public static String selectDateTimeFormat(Locale locale, String format, boolean withDate, boolean withTime) {
if (format == null) {
public static String selectJavaDateTimeFormatFromMomentJSFormatOrDefault(Locale locale, String momentJSFormat, boolean withDate, boolean withTime) {
if (momentJSFormat == null) {
String dateFormat = "";
if (withDate) {
dateFormat = ((SimpleDateFormat) DateFormat.getDateInstance(DateFormat.SHORT, locale)).toPattern();
Expand All @@ -504,11 +534,47 @@ else if (dateFormat.equals("d/M/yy")) {
}
return (dateFormat + " " + timeFormat).trim();
} else {
return format;
return LocaleUtils.momentToJavaFormat(momentJSFormat);
}
}

/**
* Selects the Date Pattern to use based on the given Locale if the input
* format is null
*
* @param locale
* Locale (may be the result of a call to selectLocale)
* @param momentJSFormat
* Input format String
* @return moment.js Date Pattern eg. DD/MM/YYYY
*/
public static String selectMomentJSDateTimeFormat(Locale locale, String momentJSFormat, boolean withDate, boolean withTime) {
if (momentJSFormat == null) {
String dateFormat = "";
if (withDate) {
dateFormat = ((SimpleDateFormat) DateFormat.getDateInstance(DateFormat.SHORT, locale)).toPattern();
}
String timeFormat = "";
if (withTime) {
timeFormat = ((SimpleDateFormat)DateFormat.getTimeInstance(DateFormat.MEDIUM, locale)).toPattern();
}
// Since DateFormat.SHORT is silly, return a smart format
if (dateFormat.equals("M/d/yy")) {
dateFormat = "MM/dd/yyyy";
}
else if (dateFormat.equals("d/M/yy")) {
dateFormat = "dd/MM/yyyy";
}
String result = LocaleUtils.javaToMomentFormat((dateFormat + " " + timeFormat).trim());
System.out.println(result);
return result;
} else {
return momentJSFormat;
}
}



/**
* <p>
* Returns the <code>label</code> property from the specified component.
Expand Down
Loading

0 comments on commit 821dfca

Please sign in to comment.