use of org.eclipse.scout.rt.platform.nls.NlsLocale in project scout.rt by eclipse.
the class JsonDateColumn method toJson.
@Override
public JSONObject toJson() {
JSONObject json = super.toJson();
json.put("hasDate", getColumn().isHasDate());
json.put("hasTime", getColumn().isHasTime());
json.put(IDateColumn.PROP_GROUP_FORMAT, getColumn().getGroupFormat());
// TODO [7.0] CGU: update IDateColumnInterface
// getDateFormat uses NlsLocale. IMHO getDateFormat should not perform any logic because it just a getter-> refactor. same on AbstractDateField
// Alternative would be to use a clientJob or set localethreadlocal in ui thread as well, as done in rap
Locale oldLocale = NlsLocale.getOrElse(null);
try {
NlsLocale.set(getUiSession().getClientSession().getLocale());
Method method = AbstractDateColumn.class.getDeclaredMethod("getDateFormat");
method.setAccessible(true);
SimpleDateFormat dateFormat = (SimpleDateFormat) method.invoke(getColumn());
// Don't use toLocalizedPattern, it translates the chars ('d' to 't' for german).
json.put("format", dateFormat.toPattern());
} catch (ReflectiveOperationException e) {
throw new UiException("Failed to create JSON from 'date column'", BEANS.get(DefaultExceptionTranslator.class).unwrap(e));
} finally {
NlsLocale.set(oldLocale);
}
return json;
}
use of org.eclipse.scout.rt.platform.nls.NlsLocale in project scout.rt by eclipse.
the class FormattingUtility method formatObject.
/**
* Formats the given object respecting the current thread's {@link NlsLocale} and converts it to a String. Supported
* types are:
* <ul>
* <li>{@link String}</li>
* <li>{@link java.util.Date} with empty time part is formatted using {@link java.text.DateFormat#MEDIUM}
* <li>{@link java.util.Date} with non-empty time part is formatted {@link java.text.DateFormat#SHORT} for date and
* time part, respectively</li>
* <li>{@link Float}, {@link Double} and {@link BigDecimal} are formatted using {@link java.text.NumberFormat} with
* exactly 2 fraction digits</li>
* <li>{@link Number} is formatted using {@link java.text.NumberFormat}</li>
* <li>{@link Boolean} is formatted as "X" for <code>true</code>, "" for <code>false</code></li>
* </ul>
*
* @param o
* object to format
* @return Returns formatted string representation, never <code>null</code>.
*/
public static String formatObject(Object o) {
Locale loc = NlsLocale.get();
String ret = null;
if (o instanceof String) {
ret = (String) o;
} else if (o instanceof Date) {
ret = BEANS.get(DateFormatProvider.class).getDateInstance(DateFormat.MEDIUM, loc).format(o);
// get time hours, minutes, seconds
Calendar cal = Calendar.getInstance();
cal.setTime((Date) o);
int hour = cal.get(Calendar.HOUR);
int minute = cal.get(Calendar.MINUTE);
int second = cal.get(Calendar.SECOND);
// format with time
if (hour != 0 || minute != 0 || second != 0) {
ret = BEANS.get(DateFormatProvider.class).getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT, loc).format(o);
}
} else if (o instanceof Float || o instanceof Double || o instanceof BigDecimal) {
NumberFormat f = BEANS.get(NumberFormatProvider.class).getNumberInstance(loc);
f.setMinimumFractionDigits(2);
f.setMaximumFractionDigits(2);
ret = f.format(o);
} else if (o instanceof Number) {
ret = BEANS.get(NumberFormatProvider.class).getNumberInstance(loc).format(o);
} else if (o instanceof Boolean) {
ret = ((Boolean) o) ? "X" : "";
}
return ret == null ? "" : ret;
}
Aggregations