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