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.nuxeo.ecm.platform.forms.layout.api.BuiltinModes;
034import org.nuxeo.ecm.platform.forms.layout.api.Layout;
035import org.nuxeo.ecm.platform.forms.layout.facelets.FaceletHandlerHelper;
036import org.nuxeo.ecm.platform.ui.web.tag.handler.TagConfigFactory;
037
038import com.sun.faces.facelets.tag.ui.DecorateHandler;
039
040/**
041 * Dev tag handler for layouts, retrieving the template on the layout definition (or its type definition) using the
042 * template defined in mode {@link BuiltinModes#DEV}.
043 * <p>
044 * When no template is defined, this handler is skipped.
045 *
046 * @since 6.0
047 */
048public class LayoutDevTagHandler extends TagHandler {
049
050    protected final TagConfig config;
051
052    protected final TagAttribute layout;
053
054    public LayoutDevTagHandler(TagConfig config) {
055        super(config);
056        this.config = config;
057        this.layout = getRequiredAttribute("layout");
058    }
059
060    @Override
061    public void apply(FaceletContext ctx, UIComponent parent) throws IOException, FacesException, FaceletException,
062            ELException {
063        Layout layoutInstance = (Layout) layout.getObject(ctx, Layout.class);
064        FaceletHandlerHelper helper = new FaceletHandlerHelper(config);
065        TagAttribute templateAttr = helper.createAttribute("template", layoutInstance.getDevTemplate());
066        TagAttributes attributes = FaceletHandlerHelper.getTagAttributes(templateAttr);
067        String widgetTagConfigId = layoutInstance.getTagConfigId();
068        TagConfig config = TagConfigFactory.createTagConfig(this.config, widgetTagConfigId, attributes, nextHandler);
069        DecorateHandler includeHandler = new DecorateHandler(config);
070        includeHandler.apply(ctx, parent);
071    }
072
073}