001/*
002 * (C) Copyright 2006-2007 Nuxeo SAS (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.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 *     <a href="mailto:at@nuxeo.com">Anahide Tchertchian</a>
016 *
017 * $Id: HtmlTextWidgetTypeHandler.java 30416 2008-02-21 19:10:37Z atchertchian $
018 */
019
020package org.nuxeo.ecm.platform.forms.layout.facelets.plugins;
021
022import java.util.Arrays;
023
024import javax.faces.component.html.HtmlOutputText;
025import javax.faces.view.facelets.ComponentHandler;
026import javax.faces.view.facelets.CompositeFaceletHandler;
027import javax.faces.view.facelets.FaceletContext;
028import javax.faces.view.facelets.FaceletHandler;
029import javax.faces.view.facelets.TagAttribute;
030import javax.faces.view.facelets.TagAttributes;
031import javax.faces.view.facelets.TagConfig;
032
033import org.apache.commons.lang.StringUtils;
034import org.nuxeo.ecm.platform.forms.layout.api.BuiltinWidgetModes;
035import org.nuxeo.ecm.platform.forms.layout.api.Widget;
036import org.nuxeo.ecm.platform.forms.layout.api.exceptions.WidgetException;
037import org.nuxeo.ecm.platform.forms.layout.facelets.FaceletHandlerHelper;
038import org.nuxeo.ecm.platform.ui.web.component.editor.UIHtmlEditor;
039import org.nuxeo.ecm.platform.ui.web.component.seam.UIHtmlText;
040
041import com.sun.faces.facelets.tag.TagAttributesImpl;
042
043/**
044 * Html text widget.
045 *
046 * @author <a href="mailto:at@nuxeo.com">Anahide Tchertchian</a>
047 */
048public class HtmlTextWidgetTypeHandler extends AbstractWidgetTypeHandler {
049
050    private static final long serialVersionUID = 1495841177711755669L;
051
052    @Override
053    public FaceletHandler getFaceletHandler(FaceletContext ctx, TagConfig tagConfig, Widget widget,
054            FaceletHandler[] subHandlers) throws WidgetException {
055        FaceletHandlerHelper helper = new FaceletHandlerHelper(ctx, tagConfig);
056        String mode = widget.getMode();
057        String widgetId = widget.getId();
058        String widgetName = widget.getName();
059        String widgetTagConfigId = widget.getTagConfigId();
060        TagAttributes attributes;
061        if (BuiltinWidgetModes.isLikePlainMode(mode)) {
062            // use attributes without id
063            attributes = helper.getTagAttributes(widget);
064        } else {
065            if (BuiltinWidgetModes.EDIT.equals(mode)) {
066                // exclude styleClass, it needs a specific css class also, see
067                // below
068                attributes = helper.getTagAttributes(widget, Arrays.asList("styleClass"), true);
069                attributes = FaceletHandlerHelper.addTagAttribute(attributes, helper.createAttribute("id", widgetId));
070            } else {
071                attributes = helper.getTagAttributes(widgetId, widget);
072            }
073        }
074        FaceletHandler leaf = getNextHandler(ctx, tagConfig, widget, subHandlers, helper);
075        if (BuiltinWidgetModes.EDIT.equals(mode)) {
076            ComponentHandler input = helper.getHtmlComponentHandler(widgetTagConfigId, attributes, leaf,
077                    UIHtmlEditor.COMPONENT_TYPE, null);
078            String msgId = helper.generateMessageId(widgetName);
079            ComponentHandler message = helper.getMessageComponentHandler(widgetTagConfigId, msgId, widgetId, null);
080            FaceletHandler[] handlers = { input, message };
081            return new CompositeFaceletHandler(handlers);
082        } else {
083            // default on text for other modes, do not escape
084            TagAttribute escape = helper.createAttribute("escape", "false");
085            attributes = FaceletHandlerHelper.addTagAttribute(attributes, escape);
086            String styleClass = (String) widget.getProperty("styleClass");
087            TagAttribute styleClassAttr = helper.createAttribute("styleClass",
088                    StringUtils.isBlank(styleClass) ? "textBlock" : "textBlock " + styleClass);
089            attributes = FaceletHandlerHelper.addTagAttribute(attributes, styleClassAttr);
090            ComponentHandler output = helper.getHtmlComponentHandler(widgetTagConfigId, attributes, leaf,
091                    HtmlOutputText.COMPONENT_TYPE, null);
092            if (BuiltinWidgetModes.PDF.equals(mode)) {
093                // add a surrounding p:html tag handler
094                return helper.getHtmlComponentHandler(widgetTagConfigId, new TagAttributesImpl(new TagAttribute[0]),
095                        output, UIHtmlText.class.getName(), null);
096            } else {
097                return output;
098            }
099        }
100    }
101}