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