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 java.io.Serializable;
020import java.util.Arrays;
021
022import javax.faces.view.facelets.FaceletContext;
023import javax.faces.view.facelets.FaceletHandler;
024import javax.faces.view.facelets.TagAttribute;
025import javax.faces.view.facelets.TagAttributes;
026import javax.faces.view.facelets.TagConfig;
027
028import org.apache.commons.lang.StringUtils;
029import org.nuxeo.ecm.platform.forms.layout.api.BuiltinWidgetModes;
030import org.nuxeo.ecm.platform.forms.layout.api.Widget;
031import org.nuxeo.ecm.platform.forms.layout.api.exceptions.WidgetException;
032import org.nuxeo.ecm.platform.forms.layout.facelets.FaceletHandlerHelper;
033import org.nuxeo.ecm.platform.forms.layout.facelets.LayoutTagHandler;
034import org.nuxeo.ecm.platform.ui.web.component.seam.UIHtmlText;
035import org.nuxeo.ecm.platform.ui.web.tag.handler.TagConfigFactory;
036
037import com.sun.faces.facelets.tag.TagAttributesImpl;
038
039/**
040 * Widget rendering a layout
041 *
042 * @author Anahide Tchertchian
043 * @Since 5.4
044 */
045public class LayoutWidgetTypeHandler extends AbstractWidgetTypeHandler {
046
047    private static final long serialVersionUID = 1L;
048
049    @Override
050    public FaceletHandler getFaceletHandler(FaceletContext ctx, TagConfig tagConfig, Widget widget,
051            FaceletHandler[] subHandlers) throws WidgetException {
052        FaceletHandlerHelper helper = new FaceletHandlerHelper(ctx, tagConfig);
053        String widgetId = widget.getId();
054        String widgetMode = widget.getMode();
055
056        TagAttributes attributes = helper.getTagAttributes(widget, Arrays.asList(new String[] { "mode" }), true, true);
057        attributes = FaceletHandlerHelper.addTagAttribute(attributes, helper.createAttribute("id", widgetId));
058
059        // add mode attribute
060        String modeValue;
061        Serializable modeFromProps = widget.getProperty("mode");
062        if ((modeFromProps instanceof String) && !StringUtils.isBlank((String) modeFromProps)) {
063            modeValue = (String) modeFromProps;
064        } else {
065            modeValue = widgetMode;
066        }
067        attributes = FaceletHandlerHelper.addTagAttribute(attributes, helper.createAttribute("mode", modeValue));
068
069        FaceletHandler leaf = getNextHandler(ctx, tagConfig, widget, subHandlers, helper, false, false);
070        String widgetTagConfigId = widget.getTagConfigId();
071        TagConfig layoutTagConfig = TagConfigFactory.createTagConfig(tagConfig, widgetTagConfigId, attributes, leaf);
072        FaceletHandler res = new LayoutTagHandler(layoutTagConfig);
073        if (BuiltinWidgetModes.PDF.equals(widgetMode)) {
074            // add a surrounding p:html tag handler
075            return helper.getHtmlComponentHandler(widgetTagConfigId, new TagAttributesImpl(new TagAttribute[0]), res,
076                    UIHtmlText.class.getName(), null);
077        } else {
078            return res;
079        }
080
081    }
082
083}