001/*
002 * (C) Copyright 2006-2010 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 *     <a href="mailto:tm@nuxeo.com">Thierry Martins</a>
017 *
018 * $Id: TextareaWidgetTypeHandler.java 30416 2008-02-21 19:10:37Z atchertchian $
019 */
020
021package org.nuxeo.ecm.platform.forms.layout.facelets.plugins;
022
023import javax.faces.component.html.HtmlInputTextarea;
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.nuxeo.ecm.platform.forms.layout.api.BuiltinWidgetModes;
034import org.nuxeo.ecm.platform.forms.layout.api.Widget;
035import org.nuxeo.ecm.platform.forms.layout.api.exceptions.WidgetException;
036import org.nuxeo.ecm.platform.forms.layout.facelets.FaceletHandlerHelper;
037import org.nuxeo.ecm.platform.ui.web.component.seam.UIHtmlText;
038
039import com.sun.faces.facelets.tag.TagAttributesImpl;
040
041/**
042 * Textarea widget.
043 *
044 * @author <a href="mailto:at@nuxeo.com">Anahide Tchertchian</a>
045 * @author <a href="mailto:tm@nuxeo.com">Thierry Martins</a>
046 */
047public class TextareaWidgetTypeHandler extends AbstractWidgetTypeHandler {
048
049    private static final long serialVersionUID = -771626672018944435L;
050
051    // .wrapword{
052    // white-space: -moz-pre-wrap !important; /* Mozilla, since 1999 */
053    // white-space: -pre-wrap; /* Opera 4-6 */
054    // white-space: -o-pre-wrap; /* Opera 7 */
055    // white-space: pre-wrap; /* css-3 */
056    // word-wrap: break-word; /* Internet Explorer 5.5+ */
057    // }
058    public static String WRAP_WORD_STYLE = "white-space: -moz-pre-wrap !important; white-space: -pre-wrap; white-space: -o-pre-wrap; white-space: pre-wrap; word-wrap: break-word;";
059
060    @Override
061    public FaceletHandler getFaceletHandler(FaceletContext ctx, TagConfig tagConfig, Widget widget,
062            FaceletHandler[] subHandlers) throws WidgetException {
063        FaceletHandlerHelper helper = new FaceletHandlerHelper(ctx, tagConfig);
064        String mode = widget.getMode();
065        String widgetId = widget.getId();
066        String widgetName = widget.getName();
067        String widgetTagConfigId = widget.getTagConfigId();
068        TagAttributes attributes;
069        if (BuiltinWidgetModes.isLikePlainMode(mode)) {
070            // use attributes without id
071            attributes = helper.getTagAttributes(widget);
072        } else {
073            attributes = helper.getTagAttributes(widgetId, widget);
074            // Make text fields automatically switch to right-to-left if
075            // not told otherwise
076            if (widget.getProperty(FaceletHandlerHelper.DIR_PROPERTY) == null) {
077                TagAttribute dir = helper.createAttribute(FaceletHandlerHelper.DIR_PROPERTY,
078                        FaceletHandlerHelper.DIR_AUTO);
079                attributes = FaceletHandlerHelper.addTagAttribute(attributes, dir);
080            }
081        }
082        FaceletHandler leaf = getNextHandler(ctx, tagConfig, widget, subHandlers, helper);
083        if (BuiltinWidgetModes.EDIT.equals(mode)) {
084            ComponentHandler input = helper.getHtmlComponentHandler(widgetTagConfigId, attributes, leaf,
085                    HtmlInputTextarea.COMPONENT_TYPE, null);
086            String msgId = helper.generateMessageId(widgetName);
087            ComponentHandler message = helper.getMessageComponentHandler(widgetTagConfigId, msgId, widgetId, null);
088            FaceletHandler[] handlers = { input, message };
089            return new CompositeFaceletHandler(handlers);
090        } else {
091            // add styling for end of line characters to be displayed
092            if (!BuiltinWidgetModes.EDIT.equals(mode) && !BuiltinWidgetModes.isLikePlainMode(mode)
093                    && widget.getProperty("style") == null) {
094                TagAttribute escape = helper.createAttribute("style", WRAP_WORD_STYLE);
095                attributes = FaceletHandlerHelper.addTagAttribute(attributes, escape);
096            }
097            ComponentHandler output = helper.getHtmlComponentHandler(widgetTagConfigId, attributes, leaf,
098                    HtmlOutputText.COMPONENT_TYPE, null);
099            if (BuiltinWidgetModes.PDF.equals(mode)) {
100                // add a surrounding p:html tag handler
101                return helper.getHtmlComponentHandler(widgetTagConfigId, new TagAttributesImpl(new TagAttribute[0]),
102                        output, UIHtmlText.class.getName(), null);
103            } else {
104                return output;
105            }
106        }
107    }
108}