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: FileWidgetTypeHandler.java 30416 2008-02-21 19:10:37Z atchertchian $
018 */
019
020package org.nuxeo.ecm.platform.forms.layout.facelets.plugins;
021
022import javax.faces.view.facelets.ComponentHandler;
023import javax.faces.view.facelets.CompositeFaceletHandler;
024import javax.faces.view.facelets.FaceletContext;
025import javax.faces.view.facelets.FaceletHandler;
026import javax.faces.view.facelets.TagAttribute;
027import javax.faces.view.facelets.TagAttributes;
028import javax.faces.view.facelets.TagConfig;
029
030import org.nuxeo.ecm.platform.forms.layout.api.BuiltinWidgetModes;
031import org.nuxeo.ecm.platform.forms.layout.api.FieldDefinition;
032import org.nuxeo.ecm.platform.forms.layout.api.Widget;
033import org.nuxeo.ecm.platform.forms.layout.api.exceptions.WidgetException;
034import org.nuxeo.ecm.platform.forms.layout.facelets.FaceletHandlerHelper;
035import org.nuxeo.ecm.platform.forms.layout.facelets.ValueExpressionHelper;
036import org.nuxeo.ecm.platform.ui.web.component.file.UIInputFile;
037import org.nuxeo.ecm.platform.ui.web.component.file.UIOutputFile;
038import org.nuxeo.ecm.platform.ui.web.component.seam.UIHtmlText;
039
040import com.sun.faces.facelets.tag.TagAttributesImpl;
041
042/**
043 * File widget.
044 *
045 * @author <a href="mailto:at@nuxeo.com">Anahide Tchertchian</a>
046 */
047public class FileWidgetTypeHandler extends AbstractWidgetTypeHandler {
048
049    private static final long serialVersionUID = 1495841177711755669L;
050
051    @Override
052    public FaceletHandler getFaceletHandler(FaceletContext ctx, TagConfig tagConfig, Widget widget,
053            FaceletHandler[] subHandlers) throws WidgetException {
054        FaceletHandlerHelper helper = new FaceletHandlerHelper(ctx, tagConfig);
055        String mode = widget.getMode();
056        String widgetId = widget.getId();
057        String widgetName = widget.getName();
058        String widgetTagConfigId = widget.getTagConfigId();
059        TagAttributes attributes;
060        if (BuiltinWidgetModes.isLikePlainMode(mode)) {
061            // use attributes without id
062            attributes = helper.getTagAttributes(widget);
063        } else {
064            attributes = helper.getTagAttributes(widgetId, widget);
065        }
066        // add filename from field definition
067        FieldDefinition[] fields = widget.getFieldDefinitions();
068        if (fields != null && fields.length > 1) {
069            FieldDefinition filenameField = fields[1];
070            TagAttribute filenameAttr = helper.createAttribute("filename",
071                    ValueExpressionHelper.createExpressionString(widget.getValueName(), filenameField));
072            attributes = FaceletHandlerHelper.addTagAttribute(attributes, filenameAttr);
073        }
074        // file components do not support client behaviors => do not add input
075        // slot
076        boolean isEdit = BuiltinWidgetModes.EDIT.equals(mode);
077        FaceletHandler leaf = getNextHandler(ctx, tagConfig, widget, subHandlers, helper, false, isEdit);
078        if (isEdit) {
079            ComponentHandler input = helper.getHtmlComponentHandler(widgetTagConfigId, attributes, leaf,
080                    UIInputFile.COMPONENT_TYPE, null);
081            String msgId = helper.generateMessageId(widgetName);
082            ComponentHandler message = helper.getMessageComponentHandler(widgetTagConfigId, msgId, widgetId, null);
083            FaceletHandler[] handlers = { input, message };
084            return new CompositeFaceletHandler(handlers);
085        } else {
086            // TODO: handle PLAIN and PDF mode better?
087            ComponentHandler output = helper.getHtmlComponentHandler(widgetTagConfigId, attributes, leaf,
088                    UIOutputFile.COMPONENT_TYPE, null);
089            if (BuiltinWidgetModes.PDF.equals(mode)) {
090                // add a surrounding p:html tag handler
091                return helper.getHtmlComponentHandler(widgetTagConfigId, new TagAttributesImpl(new TagAttribute[0]),
092                        output, UIHtmlText.class.getName(), null);
093            } else {
094                return output;
095            }
096        }
097    }
098}