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.IntegerConverter;
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;
048
049/**
050 * Int widget.
051 *
052 * @author <a href="mailto:at@nuxeo.com">Anahide Tchertchian</a>
053 */
054public class IntWidgetTypeHandler extends AbstractWidgetTypeHandler {
055
056    public IntWidgetTypeHandler(TagConfig config) {
057        super(config);
058    }
059
060    @Override
061    public void apply(FaceletContext ctx, UIComponent parent, Widget widget) throws WidgetException, IOException {
062        FaceletHandlerHelper helper = new FaceletHandlerHelper(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, null, helper);
075        if (BuiltinWidgetModes.EDIT.equals(mode)) {
076            ConverterConfig convertConfig = TagConfigFactory.createConverterConfig(tagConfig, widget.getTagConfigId(),
077                    new TagAttributesImpl(new TagAttribute[0]), leaf, IntegerConverter.CONVERTER_ID);
078            ConverterHandler convert = new ConverterHandler(convertConfig);
079            FaceletHandler nextHandler = new CompositeFaceletHandler(new FaceletHandler[] { convert, leaf });
080            ComponentHandler input = helper.getHtmlComponentHandler(widgetTagConfigId, attributes, nextHandler,
081                    HtmlInputText.COMPONENT_TYPE, null);
082            String msgId = FaceletHandlerHelper.generateMessageId(ctx, widgetName);
083            ComponentHandler message = helper.getMessageComponentHandler(widgetTagConfigId, msgId, widgetId, null);
084            FaceletHandler[] handlers = { input, message };
085            FaceletHandler h = new CompositeFaceletHandler(handlers);
086            h.apply(ctx, parent);
087        } else if (BuiltinWidgetModes.CSV.equals(mode)) {
088            // default on text without any converter to ease format
089            // configuration
090            ComponentHandler output = helper.getHtmlComponentHandler(widgetTagConfigId, attributes, leaf,
091                    HtmlOutputText.COMPONENT_TYPE, null);
092            output.apply(ctx, parent);
093        } else {
094            // default on text with int converter for other modes
095            ConverterConfig convertConfig = TagConfigFactory.createConverterConfig(tagConfig, widget.getTagConfigId(),
096                    new TagAttributesImpl(new TagAttribute[0]), leaf, IntegerConverter.CONVERTER_ID);
097            ConverterHandler convert = new ConverterHandler(convertConfig);
098            ComponentHandler output = helper.getHtmlComponentHandler(widgetTagConfigId, attributes, convert,
099                    HtmlOutputText.COMPONENT_TYPE, null);
100            if (BuiltinWidgetModes.PDF.equals(mode)) {
101                // add a surrounding p:html tag handler
102                FaceletHandler h = helper.getHtmlComponentHandler(widgetTagConfigId,
103                        new TagAttributesImpl(new TagAttribute[0]), output, UIHtmlText.class.getName(), null);
104                h.apply(ctx, parent);
105            } else {
106                output.apply(ctx, parent);
107            }
108        }
109    }
110}