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.FaceletContext;
025import javax.faces.view.facelets.FaceletException;
026import javax.faces.view.facelets.TagAttribute;
027import javax.faces.view.facelets.TagAttributes;
028import javax.faces.view.facelets.TagConfig;
029import javax.faces.view.facelets.TagHandler;
030
031import org.apache.commons.lang.StringUtils;
032import org.nuxeo.ecm.platform.forms.layout.api.Widget;
033import org.nuxeo.ecm.platform.forms.layout.facelets.FaceletHandlerHelper;
034import org.nuxeo.ecm.platform.forms.layout.facelets.plugins.AbstractWidgetTypeHandler;
035import org.nuxeo.ecm.platform.ui.web.tag.handler.TagConfigFactory;
036
037import com.sun.faces.facelets.tag.ui.DecorateHandler;
038
039/**
040 * Dev tag handler for widgets, retrieving the template to use on the widget properties (preferably on the widget type
041 * properties) using key {@link AbstractWidgetTypeHandler#DEV_TEMPLATE_PROPERTY_NAME}.
042 * <p>
043 * When the property {@link AbstractWidgetTypeHandler#DISABLE_DEV_PROPERTY_NAME} is not defined, a default template is
044 * used for the widget dev mode. If this property (resolved on the widget or the widget type properties) resolves to
045 * false, this handler is skipped.
046 *
047 * @since 6.0
048 */
049public class WidgetTypeDevTagHandler extends TagHandler {
050
051    protected final String DEFAULT_TEMPLATE = "/widgets/dev/widget_dev_template.xhtml";
052
053    protected final TagConfig config;
054
055    protected final TagAttribute widget;
056
057    protected final TagAttribute template;
058
059    public WidgetTypeDevTagHandler(TagConfig config) {
060        super(config);
061        this.config = config;
062        this.widget = getRequiredAttribute("widget");
063        this.template = getAttribute("template");
064    }
065
066    @Override
067    public void apply(FaceletContext ctx, UIComponent parent) throws IOException, FacesException, FaceletException,
068            ELException {
069        Widget widgetInstance = (Widget) widget.getObject(ctx, Widget.class);
070        FaceletHandlerHelper helper = new FaceletHandlerHelper(ctx, config);
071        String templateValue = template != null ? template.getValue(ctx) : null;
072        if (StringUtils.isBlank(templateValue)) {
073            templateValue = DEFAULT_TEMPLATE;
074        }
075        TagAttribute templateAttr = helper.createAttribute("template", templateValue);
076        TagAttributes attributes = FaceletHandlerHelper.getTagAttributes(templateAttr);
077        String widgetTagConfigId = widgetInstance.getTagConfigId();
078        TagConfig config = TagConfigFactory.createTagConfig(this.config, widgetTagConfigId, attributes, nextHandler);
079        DecorateHandler includeHandler = new DecorateHandler(config);
080        includeHandler.apply(ctx, parent);
081    }
082}