001/*
002 * (C) Copyright 2010 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.forms.layout.facelets.plugins;
018
019import javax.faces.component.html.HtmlInputText;
020import javax.faces.component.html.HtmlOutputText;
021import javax.faces.convert.DoubleConverter;
022import javax.faces.view.facelets.ComponentHandler;
023import javax.faces.view.facelets.CompositeFaceletHandler;
024import javax.faces.view.facelets.ConverterConfig;
025import javax.faces.view.facelets.ConverterHandler;
026import javax.faces.view.facelets.FaceletContext;
027import javax.faces.view.facelets.FaceletHandler;
028import javax.faces.view.facelets.TagAttribute;
029import javax.faces.view.facelets.TagAttributes;
030import javax.faces.view.facelets.TagConfig;
031
032import org.nuxeo.ecm.platform.forms.layout.api.BuiltinWidgetModes;
033import org.nuxeo.ecm.platform.forms.layout.api.Widget;
034import org.nuxeo.ecm.platform.forms.layout.api.exceptions.WidgetException;
035import org.nuxeo.ecm.platform.forms.layout.facelets.FaceletHandlerHelper;
036import org.nuxeo.ecm.platform.ui.web.component.seam.UIHtmlText;
037import org.nuxeo.ecm.platform.ui.web.tag.handler.TagConfigFactory;
038
039import com.sun.faces.facelets.tag.TagAttributesImpl;
040
041/**
042 * Double widget.
043 *
044 * @author Anahide Tchertchian
045 * @since 5.4.2
046 */
047public class DoubleWidgetTypeHandler extends AbstractWidgetTypeHandler {
048
049    private static final long serialVersionUID = 1L;
050
051    @Override
052    public FaceletHandler getFaceletHandler(FaceletContext ctx, TagConfig tagConfig, Widget widget,
053            FaceletHandler[] subHandlers) throws WidgetException {
054        FaceletHandlerHelper helper = new FaceletHandlerHelper(ctx, tagConfig);
055        String mode = widget.getMode();
056        String widgetId = widget.getId();
057        String widgetName = widget.getName();
058        String widgetTagConfigId = widget.getTagConfigId();
059        TagAttributes attributes;
060        if (BuiltinWidgetModes.isLikePlainMode(mode)) {
061            // use attributes without id
062            attributes = helper.getTagAttributes(widget);
063        } else {
064            attributes = helper.getTagAttributes(widgetId, widget);
065        }
066        FaceletHandler leaf = getNextHandler(ctx, tagConfig, widget, subHandlers, helper);
067        if (BuiltinWidgetModes.EDIT.equals(mode)) {
068            ConverterConfig convertConfig = TagConfigFactory.createConverterConfig(tagConfig, widget.getTagConfigId(),
069                    new TagAttributesImpl(new TagAttribute[0]), leaf, DoubleConverter.CONVERTER_ID);
070            ConverterHandler convert = new ConverterHandler(convertConfig);
071            ComponentHandler input = helper.getHtmlComponentHandler(widgetTagConfigId, attributes, convert,
072                    HtmlInputText.COMPONENT_TYPE, null);
073            String msgId = helper.generateMessageId(widgetName);
074            ComponentHandler message = helper.getMessageComponentHandler(widgetTagConfigId, msgId, widgetId, null);
075            FaceletHandler[] handlers = { input, message };
076            return new CompositeFaceletHandler(handlers);
077        } else if (BuiltinWidgetModes.CSV.equals(mode)) {
078            // default on text without any converter to ease format
079            // configuration
080            ComponentHandler output = helper.getHtmlComponentHandler(widgetTagConfigId, attributes, leaf,
081                    HtmlOutputText.COMPONENT_TYPE, null);
082            return output;
083        } else {
084            // default on text with int converter for other modes
085            ConverterConfig convertConfig = TagConfigFactory.createConverterConfig(tagConfig, widget.getTagConfigId(),
086                    new TagAttributesImpl(new TagAttribute[0]), leaf, DoubleConverter.CONVERTER_ID);
087            ConverterHandler convert = new ConverterHandler(convertConfig);
088            ComponentHandler output = helper.getHtmlComponentHandler(widgetTagConfigId, attributes, convert,
089                    HtmlOutputText.COMPONENT_TYPE, null);
090            if (BuiltinWidgetModes.PDF.equals(mode)) {
091                // add a surrounding p:html tag handler
092                return helper.getHtmlComponentHandler(widgetTagConfigId, new TagAttributesImpl(new TagAttribute[0]),
093                        output, UIHtmlText.class.getName(), null);
094            } else {
095                return output;
096            }
097        }
098    }
099
100}