001/*
002 * (C) Copyright 2006-2007 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 *     <a href="mailto:at@nuxeo.com">Anahide Tchertchian</a>
018 *
019 * $Id: TextWidgetTypeHandler.java 30416 2008-02-21 19:10:37Z atchertchian $
020 */
021
022package org.nuxeo.ecm.platform.forms.layout.facelets.plugins;
023
024import java.io.IOException;
025
026import javax.faces.component.UIComponent;
027import javax.faces.component.html.HtmlInputHidden;
028import javax.faces.view.facelets.ComponentHandler;
029import javax.faces.view.facelets.CompositeFaceletHandler;
030import javax.faces.view.facelets.FaceletContext;
031import javax.faces.view.facelets.FaceletHandler;
032import javax.faces.view.facelets.TagAttribute;
033import javax.faces.view.facelets.TagAttributes;
034import javax.faces.view.facelets.TagConfig;
035
036import org.nuxeo.ecm.platform.forms.layout.api.BuiltinWidgetModes;
037import org.nuxeo.ecm.platform.forms.layout.api.Widget;
038import org.nuxeo.ecm.platform.forms.layout.api.exceptions.WidgetException;
039import org.nuxeo.ecm.platform.forms.layout.facelets.FaceletHandlerHelper;
040import org.nuxeo.ecm.platform.ui.web.component.seam.UIHtmlText;
041
042import com.sun.faces.facelets.tag.TagAttributesImpl;
043
044/**
045 * Hidden widget.
046 *
047 * @author <a href="mailto:cpriceputu@nuxeo.com">Cristian Priceputu</a>
048 */
049public class HiddenWidgetTypeHandler extends AbstractWidgetTypeHandler {
050
051    public HiddenWidgetTypeHandler(TagConfig config) {
052        super(config);
053    }
054
055    @Override
056    public void apply(FaceletContext ctx, UIComponent parent, Widget widget) throws WidgetException, IOException {
057        FaceletHandlerHelper helper = new FaceletHandlerHelper(tagConfig);
058        String mode = widget.getMode();
059        String widgetId = widget.getId();
060        String widgetName = widget.getName();
061        String widgetTagConfigId = widget.getTagConfigId();
062        TagAttributes attributes;
063        if (BuiltinWidgetModes.isLikePlainMode(mode)) {
064            // use attributes without id
065            attributes = helper.getTagAttributes(widget);
066        } else {
067            attributes = helper.getTagAttributes(widgetId, widget);
068        }
069        FaceletHandler leaf = getNextHandler(ctx, tagConfig, widget, null, helper, false, false);
070        if (BuiltinWidgetModes.EDIT.equals(mode)) {
071            ComponentHandler input = helper.getHtmlComponentHandler(widgetTagConfigId, attributes, leaf,
072                    HtmlInputHidden.COMPONENT_TYPE, null);
073            String msgId = FaceletHandlerHelper.generateMessageId(ctx, widgetName);
074            ComponentHandler message = helper.getMessageComponentHandler(widgetTagConfigId, msgId, widgetId, null);
075            FaceletHandler[] handlers = { input, message };
076            FaceletHandler h = new CompositeFaceletHandler(handlers);
077            h.apply(ctx, parent);
078        } else {
079            // default on text for other modes
080            ComponentHandler output = helper.getHtmlComponentHandler(widgetTagConfigId, attributes, leaf,
081                    HtmlInputHidden.COMPONENT_TYPE, null);
082            if (BuiltinWidgetModes.PDF.equals(mode)) {
083                // add a surrounding p:html tag handler
084                FaceletHandler h = helper.getHtmlComponentHandler(widgetTagConfigId, new TagAttributesImpl(
085                        new TagAttribute[0]), output, UIHtmlText.class.getName(), null);
086                h.apply(ctx, parent);
087            } else {
088                output.apply(ctx, parent);
089            }
090        }
091    }
092}