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