001/*
002 * (C) Copyright 2013 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-2.1.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.dev;
018
019import java.io.IOException;
020
021import javax.el.ELException;
022import javax.faces.FacesException;
023import javax.faces.component.UIComponent;
024import javax.faces.view.facelets.ComponentHandler;
025import javax.faces.view.facelets.CompositeFaceletHandler;
026import javax.faces.view.facelets.FaceletContext;
027import javax.faces.view.facelets.FaceletException;
028import javax.faces.view.facelets.FaceletHandler;
029import javax.faces.view.facelets.TagAttributes;
030import javax.faces.view.facelets.TagConfig;
031import javax.faces.view.facelets.TagHandler;
032
033import org.nuxeo.ecm.platform.forms.layout.facelets.FaceletHandlerHelper;
034
035/**
036 * Dev tag container, displaying a div and decorating the dev handler for dev mode rendering, and displaying the
037 * original handler (layout or widget handler) after that.
038 *
039 * @since 6.0
040 */
041public class DevTagHandler extends TagHandler {
042
043    protected final TagConfig config;
044
045    protected final String refId;
046
047    protected final FaceletHandler originalHandler;
048
049    protected final FaceletHandler devHandler;
050
051    protected static final String PANEL_COMPONENT_TYPE = "org.richfaces.OutputPanel";
052
053    public DevTagHandler(TagConfig config, String refId, FaceletHandler originalHandler, FaceletHandler devHandler) {
054        super(config);
055        this.refId = refId;
056        this.config = config;
057        this.originalHandler = originalHandler;
058        this.devHandler = devHandler;
059    }
060
061    @Override
062    public void apply(FaceletContext ctx, UIComponent parent) throws IOException, FacesException, FaceletException,
063            ELException {
064        FaceletHandlerHelper helper = new FaceletHandlerHelper(ctx, config);
065        TagAttributes devAttrs = FaceletHandlerHelper.getTagAttributes(
066                helper.createAttribute("id", helper.generateDevContainerId(refId)),
067                helper.createAttribute("styleClass", "displayN nxlDevContainer"),
068                helper.createAttribute("layout", "block"));
069        ComponentHandler dComp = helper.getHtmlComponentHandler(config.getTagId(), devAttrs, devHandler,
070                PANEL_COMPONENT_TYPE, null);
071        FaceletHandler nextHandler = new CompositeFaceletHandler(new FaceletHandler[] {
072                helper.getDisableDevModeTagHandler(config.getTagId(), dComp), originalHandler });
073        TagAttributes cAttrs = FaceletHandlerHelper.getTagAttributes(
074                helper.createAttribute("id", helper.generateDevRegionId(refId)),
075                helper.createAttribute("styleClass", "nxlDevRegion"), helper.createAttribute("layout", "block"));
076        ComponentHandler cComp = helper.getHtmlComponentHandler(config.getTagId(), cAttrs, nextHandler,
077                PANEL_COMPONENT_TYPE, null);
078        cComp.apply(ctx, parent);
079    }
080
081}