001/*
002 * (C) Copyright 2006-2007 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 *     <a href="mailto:at@nuxeo.com">Anahide Tchertchian</a>
018 *
019 * $Id: DateTimeWidgetTypeHandler.java 30416 2008-02-21 19:10:37Z atchertchian $
020 */
021
022package org.nuxeo.ecm.platform.forms.layout.facelets.plugins;
023
024import java.io.IOException;
025
026import javax.faces.component.UIComponent;
027import javax.faces.component.html.HtmlOutputText;
028import javax.faces.component.html.HtmlPanelGroup;
029import javax.faces.view.facelets.ComponentConfig;
030import javax.faces.view.facelets.ComponentHandler;
031import javax.faces.view.facelets.CompositeFaceletHandler;
032import javax.faces.view.facelets.ConverterConfig;
033import javax.faces.view.facelets.ConverterHandler;
034import javax.faces.view.facelets.FaceletContext;
035import javax.faces.view.facelets.FaceletHandler;
036import javax.faces.view.facelets.TagAttribute;
037import javax.faces.view.facelets.TagAttributes;
038import javax.faces.view.facelets.TagConfig;
039import javax.faces.view.facelets.ValidatorConfig;
040import javax.faces.view.facelets.ValidatorHandler;
041
042import org.nuxeo.ecm.platform.forms.layout.api.BuiltinWidgetModes;
043import org.nuxeo.ecm.platform.forms.layout.api.Widget;
044import org.nuxeo.ecm.platform.forms.layout.api.exceptions.WidgetException;
045import org.nuxeo.ecm.platform.forms.layout.facelets.FaceletHandlerHelper;
046import org.nuxeo.ecm.platform.ui.web.component.seam.UIHtmlText;
047import org.nuxeo.ecm.platform.ui.web.tag.handler.InputDateTimeTagHandler;
048import org.nuxeo.ecm.platform.ui.web.tag.handler.LeafFaceletHandler;
049import org.nuxeo.ecm.platform.ui.web.tag.handler.TagConfigFactory;
050import org.richfaces.component.UICalendar;
051
052import com.sun.faces.facelets.tag.TagAttributesImpl;
053import com.sun.faces.facelets.tag.jsf.core.ConvertDateTimeHandler;
054
055/**
056 * Date time widget
057 *
058 * @author <a href="mailto:at@nuxeo.com">Anahide Tchertchian</a>
059 */
060public class DateTimeWidgetTypeHandler extends AbstractWidgetTypeHandler {
061
062    public DateTimeWidgetTypeHandler(TagConfig config) {
063        super(config);
064    }
065
066    @Override
067    public void apply(FaceletContext ctx, UIComponent parent, Widget widget) throws WidgetException, IOException {
068        FaceletHandlerHelper helper = new FaceletHandlerHelper(tagConfig);
069        String mode = widget.getMode();
070        String widgetId = widget.getId();
071        String widgetName = widget.getName();
072        String widgetTagConfigId = widget.getTagConfigId();
073        TagAttributes attributes;
074        if (BuiltinWidgetModes.isLikePlainMode(mode)) {
075            // use attributes without id
076            attributes = helper.getTagAttributes(widget);
077        } else {
078            attributes = helper.getTagAttributes(widgetId, widget);
079        }
080        FaceletHandler leaf = getNextHandler(ctx, tagConfig, widget, null, helper);
081        if (BuiltinWidgetModes.EDIT.equals(mode)) {
082            ValidatorConfig validatorConfig = TagConfigFactory.createValidatorConfig(tagConfig,
083                    widget.getTagConfigId(), FaceletHandlerHelper.getTagAttributes(), new LeafFaceletHandler(),
084                    org.nuxeo.ecm.platform.ui.web.component.date.DateTimeValidator.VALIDATOR_ID);
085            ValidatorHandler validateHandler = new ValidatorHandler(validatorConfig);
086            FaceletHandler nextHandler = new CompositeFaceletHandler(new FaceletHandler[] { validateHandler, leaf });
087            ComponentConfig config = TagConfigFactory.createComponentConfig(tagConfig, widget.getTagConfigId(),
088                    attributes, nextHandler, UICalendar.COMPONENT_TYPE, "org.richfaces.CalendarRenderer");
089            ComponentHandler input = new InputDateTimeTagHandler(config);
090            String styleClass = "inputDate";
091            if (widget.getProperty("styleClass") != null) {
092                styleClass += " " + (String) widget.getProperty("styleClass");
093            }
094            // always add a surrounding span tag with associated style class,
095            // see NXP-14963
096            input = helper.getHtmlComponentHandler(widgetTagConfigId,
097                    FaceletHandlerHelper.getTagAttributes(helper.createAttribute("styleClass", styleClass)), input,
098                    HtmlPanelGroup.COMPONENT_TYPE, null);
099            String msgId = FaceletHandlerHelper.generateMessageId(ctx, widgetName);
100            ComponentHandler message = helper.getMessageComponentHandler(widgetTagConfigId, msgId, widgetId, null);
101            FaceletHandler[] handlers = { input, message };
102            FaceletHandler h = new CompositeFaceletHandler(handlers);
103            h.apply(ctx, parent);
104        } else {
105            ConverterConfig convertConfig = TagConfigFactory.createConverterConfig(tagConfig, widget.getTagConfigId(),
106                    attributes, new LeafFaceletHandler(), javax.faces.convert.DateTimeConverter.CONVERTER_ID);
107            ConverterHandler convert = new ConvertDateTimeHandler(convertConfig);
108            FaceletHandler nextHandler = new CompositeFaceletHandler(new FaceletHandler[] { convert, leaf });
109            ComponentHandler output = helper.getHtmlComponentHandler(widgetTagConfigId, attributes, nextHandler,
110                    HtmlOutputText.COMPONENT_TYPE, null);
111            if (BuiltinWidgetModes.PDF.equals(mode)) {
112                // add a surrounding p:html tag handler
113                FaceletHandler h = helper.getHtmlComponentHandler(widgetTagConfigId, new TagAttributesImpl(
114                        new TagAttribute[0]), output, UIHtmlText.class.getName(), null);
115                h.apply(ctx, parent);
116            } else {
117                output.apply(ctx, parent);
118            }
119        }
120    }
121
122}