001/*
002 * (C) Copyright 2011 Nuxeo SA (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Anahide Tchertchian
016 */
017package org.nuxeo.ecm.platform.ui.web.tag.handler;
018
019import java.text.ParseException;
020import java.text.SimpleDateFormat;
021import java.util.Date;
022
023import javax.faces.view.facelets.ComponentConfig;
024import javax.faces.view.facelets.FaceletContext;
025import javax.faces.view.facelets.MetaRuleset;
026import javax.faces.view.facelets.Metadata;
027import javax.faces.view.facelets.TagAttribute;
028import javax.faces.view.facelets.TagAttributes;
029
030import org.apache.commons.logging.Log;
031import org.apache.commons.logging.LogFactory;
032import org.jboss.seam.international.LocaleSelector;
033import org.jboss.seam.international.TimeZoneSelector;
034import org.richfaces.component.UICalendar;
035
036/**
037 * @since 5.4.2
038 */
039public class InputDateTimeTagHandler extends GenericHtmlComponentHandler {
040
041    private static final Log log = LogFactory.getLog(InputDateTimeTagHandler.class);
042
043    protected final String defaultTime;
044
045    /**
046     * @since 5.7.2
047     */
048    protected TagAttributes attributes;
049
050    public InputDateTimeTagHandler(ComponentConfig config) {
051        super(config);
052        attributes = config.getTag().getAttributes();
053        defaultTime = getValue(attributes, "defaultTime", "12:00");
054    }
055
056    protected String getValue(TagAttributes attrs, String name, String defaultValue) {
057        TagAttribute attr = attrs.get(name);
058        if (attr == null) {
059            return defaultValue;
060        }
061        return attr.getValue();
062    }
063
064    @Override
065    @SuppressWarnings("rawtypes")
066    protected MetaRuleset createMetaRuleset(Class type) {
067        MetaRuleset m = super.createMetaRuleset(type);
068
069        // aliases for the old date time component compatibility
070        m.alias("format", "datePattern");
071        // showsTime is not configurable anymore
072        m.ignore("showsTime");
073        // locale ok
074        // timeZone ok
075        // do not bind styleClass to inputClass anymore: styleClass can be
076        // taken into account but the datetime widget itself, see NXP-14963.
077        // m.alias("styleClass", "inputClass");
078        m.alias("triggerLabel", "buttonLabel");
079        m.alias("triggerImg", "buttonIcon");
080        m.alias("triggerStyleClass", "buttonClass");
081
082        m.alias("onclick", "oninputclick");
083
084        // setup some default properties
085        m.add(new TagMetaData());
086
087        // onchange and onselect not working anymore, but keep them in case
088        // this is solved one day
089        // m.ignore("onchange");
090        // m.ignore("onselect");
091
092        return m;
093
094    }
095
096    class TagMetaData extends Metadata {
097
098        public TagMetaData() {
099            super();
100        }
101
102        @Override
103        public void applyMetadata(FaceletContext ctx, Object instance) {
104            if (!(instance instanceof UICalendar)) {
105                log.error("Cannot apply date time component metadata, " + "not a HtmlCalendar instance: " + instance);
106                return;
107            }
108            UICalendar c = (UICalendar) instance;
109            c.setTimeZone(TimeZoneSelector.instance().getTimeZone());
110            c.setLocale(LocaleSelector.instance().getLocale());
111        }
112    }
113
114    @Override
115    public void setAttributes(FaceletContext ctx, Object instance) {
116        super.setAttributes(ctx, instance);
117        // set default time in timezone
118        UICalendar c = (UICalendar) instance;
119        c.setPopup(Boolean.parseBoolean(getValue(attributes, "popup", "true")));
120        c.setEnableManualInput(Boolean.parseBoolean(getValue(attributes, "enableManualInput", "true")));
121        c.setShowApplyButton(Boolean.parseBoolean(getValue(attributes, "showApplyButton", "false")));
122        c.setZindex(Integer.parseInt(getValue(attributes, "zindex", "1500")));
123        setDefaultTime(c);
124    }
125
126    protected void setDefaultTime(UICalendar instance) {
127        UICalendar c = instance;
128        SimpleDateFormat format = new SimpleDateFormat("HH:mm");
129        format.setTimeZone(instance.getTimeZone());
130        Date date;
131        try {
132            date = format.parse(defaultTime);
133        } catch (ParseException e) {
134            return;
135        }
136        c.setDefaultTime(date);
137    }
138}