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