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