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