| /** |
| * |
| * 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 v1.0 |
| * which accompanies this distribution, and is available at |
| * http://www.eclipse.org/legal/epl-v10.html |
| * |
| * 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); |
| } |
| } |