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