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