001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com)
006 *
007 * This library is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU Lesser General Public
009 * License as published by the Free Software Foundation; either
010 * version 2.1 of the License, or (at your option) any later version.
011 *
012 * This library is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015 * Lesser General Public License for more details.
016 *
017 * For further information about Alkacon Software GmbH & Co. KG, please see the
018 * company website: http://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: http://www.opencms.org
022 *
023 * You should have received a copy of the GNU Lesser General Public
024 * License along with this library; if not, write to the Free Software
025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
026 */
027
028package org.opencms.workplace.list;
029
030import org.opencms.i18n.CmsMessageContainer;
031
032import java.util.Date;
033import java.util.Locale;
034
035/**
036 * Formatter for dates.<p>
037 *
038 * The 'never' message will be displayed if the date is null or <code>{@link Date#getTime()}==0</code>.<p>
039 *
040 * @since 6.0.0
041 */
042public class CmsListDateMacroFormatter extends CmsListMacroFormatter {
043
044    /** Constant for never message. */
045    private final CmsMessageContainer m_never;
046
047    /** Constant for never time. */
048    private final long m_neverTime;
049
050    /**
051     * Default constructor that sets the mask to use.<p>
052     *
053     * @param mask pattern for <code>{@link java.text.MessageFormat}</code>
054     * @param never message (without args) for the 'never' message
055     */
056    public CmsListDateMacroFormatter(CmsMessageContainer mask, CmsMessageContainer never) {
057
058        this(mask, never, 0);
059    }
060
061    /**
062     * Default constructor that sets the mask to use.<p>
063     *
064     * @param mask pattern for <code>{@link java.text.MessageFormat}</code>
065     * @param never message (without args) for the 'never' message
066     * @param neverTime the time considered as 'never', default is <code>0</code>
067     */
068    public CmsListDateMacroFormatter(CmsMessageContainer mask, CmsMessageContainer never, long neverTime) {
069
070        super(mask);
071        m_never = never;
072        m_neverTime = neverTime;
073    }
074
075    /**
076     * Returns a default date formatter object.<p>
077     *
078     * @return a default date formatter object
079     */
080    public static I_CmsListFormatter getDefaultDateFormatter() {
081
082        return new CmsListDateMacroFormatter(
083            Messages.get().container(Messages.GUI_LIST_DATE_FORMAT_1),
084            Messages.get().container(Messages.GUI_LIST_DATE_FORMAT_NEVER_0));
085    }
086
087    /**
088     * Returns a default date formatter object.<p>
089     *
090     * @param never time considered as never
091     *
092     * @return a default date formatter object
093     */
094    public static I_CmsListFormatter getDefaultDateFormatter(long never) {
095
096        return new CmsListDateMacroFormatter(
097            Messages.get().container(Messages.GUI_LIST_DATE_FORMAT_1),
098            Messages.get().container(Messages.GUI_LIST_DATE_FORMAT_NEVER_0),
099            never);
100    }
101
102    /**
103     * @see org.opencms.workplace.list.CmsListMacroFormatter#format(java.lang.Object, java.util.Locale)
104     */
105    @Override
106    public String format(Object data, Locale locale) {
107
108        if (data == null) {
109            return m_never.key(locale);
110        }
111        if (data instanceof Date) {
112            if (((Date)data).getTime() == m_neverTime) {
113                return m_never.key(locale);
114            }
115        }
116        return super.format(data, locale);
117    }
118}