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: SecretWidgetTypeHandler.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.component.html.HtmlInputSecret;
028import javax.faces.view.facelets.ComponentHandler;
029import javax.faces.view.facelets.CompositeFaceletHandler;
030import javax.faces.view.facelets.FaceletContext;
031import javax.faces.view.facelets.FaceletHandler;
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.Widget;
037import org.nuxeo.ecm.platform.forms.layout.api.exceptions.WidgetException;
038import org.nuxeo.ecm.platform.forms.layout.facelets.FaceletHandlerHelper;
039
040/**
041 * Secret widget
042 *
043 * @author <a href="mailto:at@nuxeo.com">Anahide Tchertchian</a>
044 */
045public class SecretWidgetTypeHandler extends AbstractWidgetTypeHandler {
046
047    public SecretWidgetTypeHandler(TagConfig config) {
048        super(config);
049    }
050
051    @Override
052    public void apply(FaceletContext ctx, UIComponent parent, Widget widget) throws WidgetException, IOException {
053        FaceletHandlerHelper helper = new FaceletHandlerHelper(tagConfig);
054        String mode = widget.getMode();
055        String widgetId = widget.getId();
056        String widgetName = widget.getName();
057        String widgetTagConfigId = widget.getTagConfigId();
058        TagAttributes attributes = helper.getTagAttributes(widgetId, widget);
059        FaceletHandler leaf = getNextHandler(ctx, tagConfig, widget, null, helper);
060        if (BuiltinWidgetModes.EDIT.equals(mode)) {
061            ComponentHandler input = helper.getHtmlComponentHandler(widgetTagConfigId, attributes, leaf,
062                    HtmlInputSecret.COMPONENT_TYPE, null);
063            String msgId = FaceletHandlerHelper.generateMessageId(ctx, widgetName);
064            ComponentHandler message = helper.getMessageComponentHandler(widgetTagConfigId, msgId, widgetId, null);
065            FaceletHandler[] handlers = { input, message };
066            FaceletHandler h = new CompositeFaceletHandler(handlers);
067            h.apply(ctx, parent);
068        }
069        // do not do anything for other modes
070    }
071}