001/*
002 * (C) Copyright 2014 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:grenard@nuxeo.com">Guillaume</a>
018 */
019package org.nuxeo.ecm.platform.ui.web.renderer;
020
021import java.io.IOException;
022import java.util.Iterator;
023
024import javax.faces.component.UIComponent;
025import javax.faces.context.FacesContext;
026import javax.faces.context.ResponseWriter;
027import javax.faces.model.SelectItem;
028
029import org.apache.commons.lang3.StringUtils;
030import org.nuxeo.ecm.platform.ui.web.util.ComponentUtils;
031
032import com.sun.faces.renderkit.RenderKitUtils;
033import com.sun.faces.renderkit.html_basic.SelectManyCheckboxListRenderer;
034
035/**
036 * @since 6.0
037 */
038public class NxSelectManyCheckboxListRenderer extends SelectManyCheckboxListRenderer {
039
040    final String MORE_LESS_LIMIT_PROPERTY = "moreLessLimit";
041
042    final String EMPTY_CHOICE_PROPERTY = "emptyChoiceMessage";
043
044    public static final String RENDERER_TYPE = "org.nuxeo.NxSelectManyCheckboxList";
045
046    @Override
047    public void encodeEnd(FacesContext context, UIComponent component) throws IOException {
048
049        super.encodeEnd(context, component);
050
051        final String moreLessLimit = (String) component.getAttributes().get(MORE_LESS_LIMIT_PROPERTY);
052        if (moreLessLimit != null) {
053            ResponseWriter writer = context.getResponseWriter();
054            final int moreLessLimitInt = Integer.parseInt(moreLessLimit);
055            writer.startElement("a", component);
056            writer.writeAttribute("href", "#", null);
057            writer.writeAttribute("onclick", "nuxeo.utils.moreLessTableRows('" + component.getClientId() + "', true, "
058                    + moreLessLimitInt + ");return false;", null);
059            writer.writeAttribute("class", "nx-less-more-ctrl nx-more", null);
060            writer.write(ComponentUtils.translate(context, "label.vocabulary.more"));
061            writer.endElement("a");
062
063            writer.startElement("a", component);
064            writer.writeAttribute("href", "#", null);
065            writer.writeAttribute("onclick", "nuxeo.utils.moreLessTableRows('" + component.getClientId() + "', false, "
066                    + moreLessLimitInt + ");return false;", null);
067            writer.writeAttribute("class", "nx-less-more-ctrl nx-less", null);
068            writer.write(ComponentUtils.translate(context, "label.vocabulary.less"));
069            writer.endElement("a");
070
071            writer.startElement("script", component);
072            writer.write("jQuery(document).ready(function(){nuxeo.utils.moreLessTableRows('" + component.getClientId()
073                    + "', false, " + moreLessLimitInt + ");});");
074            writer.endElement("script");
075        }
076
077        Iterator<SelectItem> items = RenderKitUtils.getSelectItems(context, component);
078        if (!items.hasNext()) {
079            final String emptyChoiceMessage = (String) component.getAttributes().get(EMPTY_CHOICE_PROPERTY);
080            if (StringUtils.isNotBlank(emptyChoiceMessage)) {
081                ResponseWriter writer = context.getResponseWriter();
082                writer.startElement("div", component);
083                writer.writeAttribute("class", "emptyResult", null);
084                writer.write(ComponentUtils.translate(context, emptyChoiceMessage));
085                writer.endElement("div");
086            }
087        }
088    }
089
090}