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 *     Nuxeo - initial API and implementation
016 *
017 * $Id: ChainSelectOutputRenderer.java 23042 2007-07-27 13:18:01Z glefter $
018 */
019
020package org.nuxeo.ecm.platform.ui.web.directory;
021
022import java.io.IOException;
023
024import javax.faces.component.UIComponent;
025import javax.faces.context.FacesContext;
026import javax.faces.context.ResponseWriter;
027import javax.faces.render.Renderer;
028
029import org.apache.commons.lang.StringUtils;
030
031/**
032 * Renderer for directory entry.
033 *
034 * @author <a href="mailto:glefter@nuxeo.com">George Lefter</a>
035 */
036public class ChainSelectOutputRenderer extends Renderer {
037
038    @Override
039    public void encodeBegin(FacesContext context, UIComponent component) throws IOException {
040        ChainSelectOutputComponent comp = (ChainSelectOutputComponent) component;
041        Object rawValues = comp.getValue();
042        if (rawValues == null) {
043            return;
044        }
045
046        String[] values;
047        if (rawValues instanceof String[]) {
048            if (comp.getHandleMultipleValues()) {
049                // treat rawValues as separate entries (potentially holding
050                // several keys)
051                values = (String[]) rawValues;
052            } else {
053                // treat rawValues as labels to be concatenated on the same
054                // entry
055                String concat = StringUtils.join(((String[]) rawValues), comp.getKeySeparator());
056                values = new String[] { concat };
057            }
058        } else {
059            values = new String[] { rawValues.toString() };
060        }
061        String entrySeparator = comp.getEntrySeparator();
062        String cssStyle = comp.getCssStyle();
063        String cssStyleClass = comp.getCssStyleClass();
064
065        ResponseWriter writer = context.getResponseWriter();
066
067        for (String value : values) {
068            writer.startElement("div", comp);
069            if (cssStyle != null) {
070                writer.writeAttribute("style", cssStyle, "style");
071            }
072            if (cssStyleClass != null) {
073                writer.writeAttribute("class", cssStyleClass, "class");
074            }
075
076            Selection sel = comp.createSelection(value);
077            String[] labels = sel.getLabels();
078            String label = StringUtils.join(labels, comp.getKeySeparator());
079            writer.writeText(label, null);
080            writer.writeText(entrySeparator, null);
081
082            writer.endElement("div");
083        }
084        writer.flush();
085    }
086
087}