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