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