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$
018 */
019
020package org.nuxeo.ecm.platform.ui.web.directory;
021
022import java.io.IOException;
023import java.util.ArrayList;
024import java.util.List;
025import java.util.Locale;
026import java.util.Map;
027
028import javax.faces.component.UIComponent;
029import javax.faces.context.FacesContext;
030import javax.faces.context.ResponseWriter;
031import javax.faces.render.Renderer;
032
033import org.apache.commons.logging.Log;
034import org.apache.commons.logging.LogFactory;
035import org.nuxeo.common.utils.i18n.I18NUtils;
036import org.nuxeo.ecm.platform.ui.web.util.BaseURL;
037
038/**
039 * Renders many listboxes for the MultiListbox component
040 *
041 * @deprecated : renderer is useless (not declared correctly in deployment-fragment.xml and bugg) should be refactored
042 *             instead
043 */
044@Deprecated
045public class ChainSelectMultiListboxRenderer extends Renderer {
046
047    @SuppressWarnings("unused")
048    private static final Log log = LogFactory.getLog(ChainSelectMultiListboxRenderer.class);
049
050    @Override
051    public void decode(FacesContext facesContext, UIComponent component) {
052    }
053
054    @Override
055    public void encodeBegin(FacesContext context, UIComponent component) throws IOException {
056        ChainSelectMultiListboxComponent comp = (ChainSelectMultiListboxComponent) component;
057        ResponseWriter writer = context.getResponseWriter();
058        Boolean displayValueOnly = comp.getChain().getBooleanProperty("displayValueOnly", false);
059        if (displayValueOnly) {
060            return;
061        }
062        encodeInput(context, writer, comp);
063    }
064
065    private void encodeInput(FacesContext context, ResponseWriter writer, ChainSelectMultiListboxComponent mcomp)
066            throws IOException {
067
068        ChainSelectListboxComponent[] sComps = mcomp.createSingleComponents();
069        for (ChainSelectListboxComponent component : sComps) {
070            encodeInput(context, writer, component);
071
072            // mcomp.getChildren()
073            // mcomp.encodeChildren(context);
074        }
075    }
076
077    private static void encodeInput(FacesContext context, ResponseWriter writer, ChainSelectListboxComponent comp)
078            throws IOException {
079        String id = comp.getClientId(context);
080        ChainSelect chain = comp.getChain();
081        String cssStyleClass = comp.getStringProperty("cssStyleClass", null);
082        String cssStyle = comp.getStringProperty("cssStyle", null);
083        String onchange = comp.getStringProperty("onchange", null);
084
085        String id1 = comp.getClientId(context).split(":")[0];
086        String id2 = comp.getClientId(context);
087        onchange = "A4J.AJAX.Submit('_viewRoot','" + id1 + "',event,{'parameters':{'" + id2 + "':'" + id2
088                + "'},'actionUrl':'" + BaseURL.getContextPath() + "/documents/tabs/document_externe_edit.faces'})";
089
090        boolean multiSelect = comp.getBooleanProperty("multiSelect", false);
091        String size = comp.getStringProperty("size", null);
092        boolean displayIdAndLabel = comp.getBooleanProperty("displayIdAndLabel", false);
093        String displayIdAndLabelSeparator = comp.getStringProperty("displayIdAndLabelSeparator", " ");
094        boolean localize = comp.getBooleanProperty("localize", false);
095        String display = comp.getStringProperty("display", "");
096
097        if (chain.getBooleanProperty("displayValueOnly", false)) {
098            return;
099        }
100
101        writer.startElement("select", comp);
102        writer.writeAttribute("id", id, "id");
103        writer.writeAttribute("name", id, "name");
104        if (onchange != null) {
105            writer.writeAttribute("onchange", onchange, "onchange");
106        }
107        if (cssStyleClass != null) {
108            writer.writeAttribute("class", cssStyleClass, "class");
109        }
110        if (cssStyle != null) {
111            writer.writeAttribute("style", cssStyle, "style");
112        }
113        if (multiSelect) {
114            writer.writeAttribute("multiple", "true", "multiple");
115        }
116        if (size != null && Integer.valueOf(size) > 0) {
117            writer.writeAttribute("size", size, "size");
118        }
119
120        // <select id="j_id202:theme7" name="j_id202:theme7"
121        // onchange="A4J.AJAX.Submit('_viewRoot','j_id202',event,{'parameters':{'j_id202:j_id286':'j_id202:j_id286'},'actionUrl':'/nuxeo/documents/tabs/document_externe_edit.faces'})"
122        // multiple="true" size="4"><option value="">Select a value</option>
123        // writer.writeAttribute("onchange","onchange");
124
125        List<String> valueList = new ArrayList<String>();
126
127        int index = comp.getIndex();
128        Selection[] selections = chain.getSelections();
129        if (selections != null) {
130            for (Selection selection : selections) {
131                valueList.add(selection.getColumnValue(index));
132            }
133        }
134
135        String optionsLabel = translate(context, "label.vocabulary.selectValue");
136
137        writer.startElement("option", comp);
138        writer.writeAttribute("value", "", "value");
139        writer.writeText(optionsLabel, null);
140        writer.endElement("option");
141        writer.write("\n");
142
143        Map<String, DirectorySelectItem> options = comp.getOptions();
144        if (options != null) {
145            for (DirectorySelectItem item : options.values()) {
146                String optionId = (String) item.getValue();
147                String optionLabel = item.getLabel();
148
149                writer.startElement("option", comp);
150                writer.writeAttribute("value", optionId, "value");
151                if (valueList.contains(optionId)) {
152                    writer.writeAttribute("selected", "true", "selected");
153                }
154                if (localize) {
155                    optionLabel = translate(context, optionLabel);
156                }
157                writer.writeText(DirectoryHelper.getOptionValue(optionId, optionLabel, display, displayIdAndLabel,
158                        displayIdAndLabelSeparator), null);
159                writer.endElement("option");
160            }
161        }
162        writer.endElement("select");
163        writer.write("\n");
164    }
165
166    protected static String translate(FacesContext context, String label) {
167        String bundleName = context.getApplication().getMessageBundle();
168        Locale locale = context.getViewRoot().getLocale();
169        label = I18NUtils.getMessageString(bundleName, label, null, locale);
170        return label;
171    }
172}