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