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: TextWidgetTypeHandler.java 30416 2008-02-21 19:10:37Z atchertchian $
018 */
019
020package org.nuxeo.ecm.platform.forms.layout.facelets.plugins;
021
022import java.io.Serializable;
023import java.util.ArrayList;
024import java.util.HashMap;
025import java.util.List;
026import java.util.Map;
027
028import javax.faces.component.html.HtmlInputText;
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.FieldDefinition;
040import org.nuxeo.ecm.platform.forms.layout.api.Widget;
041import org.nuxeo.ecm.platform.forms.layout.api.exceptions.WidgetException;
042import org.nuxeo.ecm.platform.forms.layout.facelets.FaceletHandlerHelper;
043import org.nuxeo.ecm.platform.forms.layout.facelets.ValueExpressionHelper;
044import org.nuxeo.ecm.platform.ui.web.component.seam.UIHtmlText;
045import org.nuxeo.ecm.platform.ui.web.util.ComponentTagUtils;
046
047import com.sun.faces.facelets.tag.TagAttributesImpl;
048
049/**
050 * Text widget.
051 *
052 * @author <a href="mailto:at@nuxeo.com">Anahide Tchertchian</a>
053 */
054public class TextWidgetTypeHandler extends AbstractWidgetTypeHandler {
055
056    private static final long serialVersionUID = 1495841177711755669L;
057
058    @Override
059    public FaceletHandler getFaceletHandler(FaceletContext ctx, TagConfig tagConfig, Widget widget,
060            FaceletHandler[] subHandlers) throws WidgetException {
061        FaceletHandlerHelper helper = new FaceletHandlerHelper(ctx, tagConfig);
062        String mode = widget.getMode();
063        String widgetId = widget.getId();
064        String widgetName = widget.getName();
065        String widgetTagConfigId = widget.getTagConfigId();
066        FaceletHandler leaf = getNextHandler(ctx, tagConfig, widget, subHandlers, helper);
067        if (BuiltinWidgetModes.EDIT.equals(mode)) {
068            TagAttributes attributes = helper.getTagAttributes(widgetId, widget);
069            // Make text fields automatically switch to right-to-left if
070            // not told otherwise
071            if (widget.getProperty(FaceletHandlerHelper.DIR_PROPERTY) == null) {
072                TagAttribute dir = helper.createAttribute(FaceletHandlerHelper.DIR_PROPERTY,
073                        FaceletHandlerHelper.DIR_AUTO);
074                attributes = FaceletHandlerHelper.addTagAttribute(attributes, dir);
075            }
076            ComponentHandler input = helper.getHtmlComponentHandler(widgetTagConfigId, attributes, leaf,
077                    HtmlInputText.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            TagAttributes attributes = getViewTagAttributes(ctx, helper, widgetId, widget,
084                    !BuiltinWidgetModes.isLikePlainMode(mode));
085            // default on text for other modes
086            ComponentHandler output = helper.getHtmlComponentHandler(widgetTagConfigId, attributes, leaf,
087                    HtmlOutputText.COMPONENT_TYPE, null);
088            if (BuiltinWidgetModes.PDF.equals(mode)) {
089                // add a surrounding p:html tag handler
090                return helper.getHtmlComponentHandler(widgetTagConfigId, new TagAttributesImpl(new TagAttribute[0]),
091                        output, UIHtmlText.class.getName(), null);
092            } else {
093                return output;
094            }
095        }
096    }
097
098    /**
099     * Return tag attributes after having replaced the usual value expression for the 'value' field by a specific
100     * expression to display the translated value if set as is in the widget properties.
101     */
102    protected TagAttributes getViewTagAttributes(FaceletContext ctx, FaceletHandlerHelper helper, String id,
103            Widget widget, boolean addId) {
104        List<TagAttribute> attrs = new ArrayList<TagAttribute>();
105        FieldDefinition[] fields = widget.getFieldDefinitions();
106        if (fields != null && fields.length > 0) {
107            FieldDefinition field = fields[0];
108            String pname = field != null ? field.getPropertyName() : null;
109            if (ComponentTagUtils.isValueReference(pname)) {
110                // do not override value for localization in this case, see NXP-13456
111                TagAttribute valueAttr = helper.createAttribute("value",
112                        ValueExpressionHelper.createExpressionString(widget.getValueName(), field));
113                attrs.add(valueAttr);
114            } else {
115                String bareExpression = ValueExpressionHelper.createBareExpressionString(widget.getValueName(), field);
116                String bundleName = ctx.getFacesContext().getApplication().getMessageBundle();
117                String localizedExpression = String.format("%s[%s]", bundleName, bareExpression);
118                String expression = String.format("#{%s ? %s : %s}", "widget.properties.localize", localizedExpression,
119                        bareExpression);
120                TagAttribute valueAttr = helper.createAttribute("value", expression);
121                attrs.add(valueAttr);
122            }
123        }
124
125        // fill with widget properties
126        Map<String, Serializable> widgetPropsClone = new HashMap<String, Serializable>();
127        Map<String, Serializable> widgetProps = widget.getProperties();
128        if (widgetProps != null) {
129            widgetPropsClone.putAll(widgetProps);
130            // remove localize property
131            widgetPropsClone.remove("localize");
132        }
133        List<TagAttribute> propertyAttrs = helper.getTagAttributes(widgetPropsClone, null, true, widget.getType(),
134                widget.getTypeCategory(), widget.getMode());
135        if (propertyAttrs != null) {
136            attrs.addAll(propertyAttrs);
137        }
138        TagAttributes widgetAttrs = FaceletHandlerHelper.getTagAttributes(attrs);
139        // handle id
140        if (!addId) {
141            return widgetAttrs;
142        } else {
143            TagAttributes res = FaceletHandlerHelper.addTagAttribute(widgetAttrs, helper.createAttribute("id", id));
144            // Make text fields automatically switch to right-to-left if
145            // not told otherwise
146            if (widget.getProperty(FaceletHandlerHelper.DIR_PROPERTY) == null) {
147                TagAttribute dir = helper.createAttribute(FaceletHandlerHelper.DIR_PROPERTY,
148                        FaceletHandlerHelper.DIR_AUTO);
149                res = FaceletHandlerHelper.addTagAttribute(res, dir);
150            }
151            return res;
152        }
153
154    }
155
156}