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 * <a href="mailto:at@nuxeo.com">Anahide Tchertchian</a> 018 * 019 * $Id: DirectorySelectManyWidgetTypeHandler.java 30416 2008-02-21 19:10:37Z atchertchian $ 020 */ 021 022package org.nuxeo.ecm.platform.forms.layout.facelets.plugins; 023 024import java.io.IOException; 025import java.io.Serializable; 026import java.util.ArrayList; 027import java.util.List; 028import java.util.Map; 029 030import javax.faces.component.UIComponent; 031import javax.faces.component.html.HtmlColumn; 032import javax.faces.component.html.HtmlDataTable; 033import javax.faces.component.html.HtmlOutputText; 034import javax.faces.component.html.HtmlSelectManyListbox; 035import javax.faces.view.facelets.ComponentHandler; 036import javax.faces.view.facelets.CompositeFaceletHandler; 037import javax.faces.view.facelets.FaceletContext; 038import javax.faces.view.facelets.FaceletHandler; 039import javax.faces.view.facelets.TagAttribute; 040import javax.faces.view.facelets.TagAttributes; 041import javax.faces.view.facelets.TagConfig; 042 043import org.nuxeo.ecm.platform.forms.layout.api.BuiltinWidgetModes; 044import org.nuxeo.ecm.platform.forms.layout.api.FieldDefinition; 045import org.nuxeo.ecm.platform.forms.layout.api.Widget; 046import org.nuxeo.ecm.platform.forms.layout.api.exceptions.WidgetException; 047import org.nuxeo.ecm.platform.forms.layout.facelets.FaceletHandlerHelper; 048import org.nuxeo.ecm.platform.forms.layout.facelets.ValueExpressionHelper; 049import org.nuxeo.ecm.platform.ui.web.component.list.UIEditableList; 050import org.nuxeo.ecm.platform.ui.web.component.seam.UIHtmlText; 051import org.nuxeo.ecm.platform.ui.web.directory.DirectoryEntryOutputComponent; 052 053import com.sun.faces.facelets.tag.TagAttributesImpl; 054 055/** 056 * Select many directory widget 057 * 058 * @author <a href="mailto:at@nuxeo.com">Anahide Tchertchian</a> 059 */ 060public class DirectorySelectManyWidgetTypeHandler extends AbstractDirectorySelectWidgetTypeHandler { 061 062 public DirectorySelectManyWidgetTypeHandler(TagConfig config) { 063 super(config); 064 } 065 066 protected String getEditComponentType() { 067 return HtmlSelectManyListbox.COMPONENT_TYPE; 068 } 069 070 @Override 071 public void apply(FaceletContext ctx, UIComponent parent, Widget widget) throws WidgetException, IOException { 072 String mode = widget.getMode(); 073 if (BuiltinWidgetModes.EDIT.equals(mode)) { 074 super.apply(ctx, parent, widget, getEditComponentType()); 075 return; 076 } 077 078 FaceletHandlerHelper helper = new FaceletHandlerHelper(tagConfig); 079 FaceletHandler leaf = getNextHandler(ctx, tagConfig, widget, null, helper); 080 String widgetName = widget.getName(); 081 String widgetTagConfigId = widget.getTagConfigId(); 082 083 // build value attribute for iteration component 084 String valueAttributeName = "value"; 085 Map<String, Serializable> properties = widget.getProperties(); 086 TagAttribute valueAttr = null; 087 if (properties.containsKey("value")) { 088 valueAttr = helper.createAttribute(valueAttributeName, (String) properties.get("value")); 089 } 090 FieldDefinition[] fields = widget.getFieldDefinitions(); 091 if (fields != null && fields.length > 0) { 092 FieldDefinition field = fields[0]; 093 valueAttr = helper.createAttribute(valueAttributeName, 094 ValueExpressionHelper.createExpressionString(widget.getValueName(), field)); 095 } 096 if (valueAttr == null) { 097 // don't bother 098 return; 099 } 100 101 // build directory item attributes, using widget properties 102 List<TagAttribute> attrs = new ArrayList<TagAttribute>(); 103 for (Map.Entry<String, Serializable> property : properties.entrySet()) { 104 if (!"value".equals(property.getKey())) { 105 Serializable value = property.getValue(); 106 TagAttribute attr = null; 107 if (value instanceof String) { 108 attr = helper.createAttribute(property.getKey(), (String) value); 109 } else if (value != null) { 110 attr = helper.createAttribute(property.getKey(), value.toString()); 111 } 112 if (attr != null) { 113 attrs.add(attr); 114 } 115 } 116 } 117 if (BuiltinWidgetModes.isLikePlainMode(mode)) { 118 attrs.add(helper.createAttribute("value", "#{model.rowData}")); 119 } else { 120 attrs.add(helper.createAttribute("value", "#{item}")); 121 } 122 TagAttributes dirEntryAttrs = FaceletHandlerHelper.getTagAttributes(attrs); 123 ComponentHandler dirEntry = helper.getHtmlComponentHandler(widgetTagConfigId, dirEntryAttrs, leaf, 124 DirectoryEntryOutputComponent.COMPONENT_TYPE, null); 125 126 if (BuiltinWidgetModes.isLikePlainMode(mode)) { 127 // use an iteration and a comma to separate items instead of an 128 // HTML table component 129 TagAttributes commaAttributes = FaceletHandlerHelper.getTagAttributes( 130 helper.createAttribute("value", ", "), 131 helper.createAttribute("rendered", "#{model.rowIndex < model.rowCount}")); 132 ComponentHandler commaHandler = helper.getHtmlComponentHandler(widgetTagConfigId, commaAttributes, leaf, 133 HtmlOutputText.COMPONENT_TYPE, null); 134 135 CompositeFaceletHandler childHandler = new CompositeFaceletHandler(new FaceletHandler[] { dirEntry, 136 commaHandler }); 137 138 TagAttributes itAttributes = FaceletHandlerHelper.getTagAttributes(valueAttr, 139 helper.createAttribute("model", "model")); 140 ComponentHandler itHandler = helper.getHtmlComponentHandler(widgetTagConfigId, itAttributes, childHandler, 141 UIEditableList.COMPONENT_TYPE, null); 142 itHandler.apply(ctx, parent); 143 } else { 144 // build a standard table 145 ComponentHandler columnEntry = helper.getHtmlComponentHandler(widgetTagConfigId, 146 FaceletHandlerHelper.getTagAttributes(), dirEntry, HtmlColumn.COMPONENT_TYPE, null); 147 148 TagAttributes iterationAttributes = FaceletHandlerHelper.getTagAttributes( 149 helper.createIdAttribute(ctx, widgetName), valueAttr, helper.createAttribute("var", "item")); 150 151 ComponentHandler table = helper.getHtmlComponentHandler(widgetTagConfigId, iterationAttributes, 152 columnEntry, HtmlDataTable.COMPONENT_TYPE, null); 153 154 if (BuiltinWidgetModes.PDF.equals(mode)) { 155 // add a surrounding p:html tag handler 156 FaceletHandler h = helper.getHtmlComponentHandler(widgetTagConfigId, new TagAttributesImpl( 157 new TagAttribute[0]), table, UIHtmlText.class.getName(), null); 158 h.apply(ctx, parent); 159 } else { 160 table.apply(ctx, parent); 161 } 162 } 163 } 164}