blob: 068a14dd01167a533f2c53a7d3b2a1242140f981 [file] [log] [blame]
/**
*
* Copyright (c) 2011, 2016 - Loetz GmbH&Co.KG (69115 Heidelberg, Germany)
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Christophe Loetz (Loetz GmbH&Co.KG) - initial implementation
*
*/
package org.eclipse.osbp.utils.vaadin;
import java.text.DateFormat;
import java.util.Locale;
import org.apache.commons.lang.StringEscapeUtils;
import com.vaadin.data.util.converter.StringToDateConverter;
public class StringToFormattedDateConverter extends StringToDateConverter {
private static final long serialVersionUID = 721600785465571493L;
private PropertyLookup lookup = null;
public void setLookup(PropertyLookup lookup) {
this.lookup = lookup;
}
protected DateFormat getDefaultFormat(Locale locale) {
if (locale == null) {
locale = Locale.getDefault();
}
DateFormat f = DateFormat.getDateInstance(DateFormat.MEDIUM, locale);
f.setLenient(false);
return f;
}
@Override
protected DateFormat getFormat(Locale locale) {
if (locale == null) {
if (lookup == null) {
locale = Locale.getDefault();
return getDefaultFormat(locale);
} else {
locale = lookup.getLocale();
}
}
if (lookup == null || lookup.getFormat() == null) {
return getDefaultFormat(locale);
}
return SimpleDateFormatter.getFormat(StringEscapeUtils.unescapeHtml(lookup.getFormat()), locale);
}
}