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