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