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 *     George Lefter
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;
027
028import javax.faces.context.FacesContext;
029import javax.faces.context.ResponseWriter;
030
031import org.apache.commons.lang.StringUtils;
032
033/**
034 * @author <a href="mailto:glefter@nuxeo.com">George Lefter</a>
035 */
036public class ChainSelectOne extends ChainSelectBase {
037
038    @Override
039    public void decode(FacesContext context) {
040        if (getDisplayValueOnly()) {
041            return;
042        }
043        decodeSelection(context);
044
045        setValid(true);
046        setSubmittedValue(getValueAsString(getSelection()));
047    }
048
049    @Override
050    public String[] getSelection() {
051        String clientId = getClientId(FacesContext.getCurrentInstance());
052        String[] selection = selectionMap.get(clientId);
053        if (selection == null) {
054            selection = getValueAsArray((String) getValue());
055            selectionMap.put(clientId, selection);
056        }
057        return selection;
058    }
059
060    @Override
061    public void encodeBegin(FacesContext context) throws IOException {
062        getChildren().clear();
063
064        if (getDisplayValueOnly()) {
065            encodeReadOnly(context);
066        } else {
067            encodeReadWrite(context);
068        }
069    }
070
071    public void encodeReadOnly(FacesContext context) throws IOException {
072        ResponseWriter writer = context.getResponseWriter();
073        String value = (String) getSubmittedValue();
074        if (value == null) {
075            value = (String) getValue();
076        }
077        if (value != null) {
078            String[] keys = StringUtils.split(value, getKeySeparator());
079            List<DirectoryEntry> nodes = resolveKeys(keys);
080            List<String> labels = new ArrayList<String>();
081            for (DirectoryEntry node : nodes) {
082                String itemValue = node.getId();
083                String itemLabel = node.getLabel();
084                itemLabel = computeItemLabel(context, itemValue, itemLabel);
085                labels.add(itemLabel);
086            }
087            String concatenatedLabel = StringUtils.join(labels.iterator(), getKeySeparator());
088
089            writer.startElement("div", this);
090            writer.write(concatenatedLabel);
091            writer.endElement("div");
092        }
093    }
094
095    public void encodeReadWrite(FacesContext context) throws IOException {
096        ResponseWriter writer = context.getResponseWriter();
097        writer.startElement("div", this);
098        writer.writeAttribute("id", getClientId(context), "id");
099        String style = getStyle();
100        if (style != null) {
101            writer.writeAttribute("style", style, "style");
102        }
103        String styleClass = getStyleClass();
104        if (styleClass != null) {
105            writer.writeAttribute("class", styleClass, "class");
106        }
107
108        String[] selectedKeys = getSelection();
109        for (int level = 0; level < getDepth(); level++) {
110            encodeListbox(context, level, selectedKeys);
111        }
112        writer.endElement("div");
113    }
114
115    @Override
116    public void validateValue(FacesContext context, Object newValue) {
117        super.validateValue(context, newValue);
118        if (newValue == null || !isValid()) {
119            return;
120        }
121        String[] keys = getValueAsArray((String) newValue);
122        validateEntry(context, keys);
123    }
124
125    @Override
126    public String getFamily() {
127        return "nxdirectory.ChainSelectOne";
128    }
129
130    @Override
131    public String getRendererType() {
132        return null;
133    }
134
135    @Override
136    public Object saveState(FacesContext context) {
137        Object[] values = new Object[1];
138        values[0] = super.saveState(context);
139        return values;
140    }
141
142    @Override
143    public void restoreState(FacesContext context, Object state) {
144        Object[] values = (Object[]) state;
145        super.restoreState(context, values[0]);
146    }
147
148}