1 23 package com.sun.enterprise.tools.jsfext.layout.descriptor; 24 25 import com.sun.enterprise.tools.jsfext.el.VariableResolver; 26 import com.sun.enterprise.tools.jsfext.event.AfterLoopEvent; 27 import com.sun.enterprise.tools.jsfext.event.BeforeLoopEvent; 28 29 import java.io.IOException ; 30 import java.util.Iterator ; 31 import java.util.List ; 32 import java.util.Map ; 33 34 import javax.faces.component.UIComponent; 35 import javax.faces.context.FacesContext; 36 import javax.faces.el.ValueBinding; 37 import javax.faces.webapp.UIComponentTag; 38 39 40 50 public class LayoutForEach extends LayoutElementBase implements LayoutElement { 51 52 60 public LayoutForEach(LayoutElement parent, String listBinding, String key) { 61 super(parent, null); 62 if ((listBinding == null) || listBinding.equals("")) { 63 throw new IllegalArgumentException ("'listBinding' is required!"); 64 } 65 if ((key == null) || key.equals("")) { 66 throw new IllegalArgumentException ("'key' is required!"); 67 } 68 _listBinding = listBinding; 69 _key = key; 70 } 71 72 73 85 protected boolean encodeThis(FacesContext context, UIComponent component) { 86 return true; 87 } 88 89 101 protected List getList(FacesContext context) { 102 Object value = VariableResolver.resolveVariables( 110 context, this, null , _listBinding); 111 112 if (value != null) { 114 String strVal = value.toString(); 115 if (UIComponentTag.isValueReference(strVal)) { 116 ValueBinding vb = 117 context.getApplication().createValueBinding(strVal); 118 value = vb.getValue(context); 119 } 120 } 121 122 if (value == null) { 124 throw new NullPointerException ("List not found via expression: '" 125 + _listBinding + "'."); 126 } 127 128 if (!(value instanceof List )) { 130 throw new IllegalArgumentException ("Expression '" + _listBinding 131 + "' did not resolve to a List! Found: '" + value + "'"); 132 } 133 134 return (List ) value; 136 } 137 138 153 protected void setCurrentForEachValue(FacesContext context, Object value, int index, String key) { 154 Map map = context.getExternalContext().getRequestMap(); 155 map.put(key, value); 156 map.put(key + "-index", "" + index); 157 } 158 159 167 public void encode(FacesContext context, UIComponent component) throws IOException { 168 Object result = dispatchHandlers(context, BEFORE_LOOP, 170 new BeforeLoopEvent(component)); 171 172 String key = resolveValue( 173 context, component, _key).toString(); 174 175 Iterator it = getList(context).iterator(); 178 for (int index = 1; it.hasNext(); index++) { 179 setCurrentForEachValue(context, it.next(), index, key); 180 super.encode(context, component); 181 } 182 183 result = dispatchHandlers(context, AFTER_LOOP, 185 new AfterLoopEvent(component)); 186 } 187 188 189 private String _listBinding = null; 190 private String _key = null; 191 192 193 199 public static final String AFTER_LOOP = "afterLoop"; 200 201 207 public static final String BEFORE_LOOP = "beforeLoop"; 208 } 209 | Popular Tags |