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