1 7 package com.inversoft.verge.mvc.view.jsp.html; 8 9 10 import java.io.IOException ; 11 import java.util.Collection ; 12 import java.util.Map ; 13 14 import javax.servlet.jsp.JspException ; 15 16 import org.apache.log4j.Category; 17 18 import com.inversoft.beans.BeanException; 19 import com.inversoft.beans.NestedBeanProperty; 20 import com.inversoft.util.ObjectTools; 21 import com.inversoft.verge.mvc.view.HtmlViewToolkit; 22 import com.inversoft.verge.mvc.view.jsp.JspTools; 23 24 25 31 public class OptionsArrayTag extends HtmlBaseTag { 32 33 34 private static final Category logger = Category.getInstance(OptionsArrayTag.class); 35 36 private Object items; 37 private Object localItems; 38 private String valueProperty; 39 private String textProperty; 40 protected SelectTag selectParent; 41 42 43 48 public Object getItems() { 49 return items; 50 } 51 52 57 public void setItems(Object items) { 58 this.items = items; 59 } 60 61 66 public String getValueProperty() { 67 return valueProperty; 68 } 69 70 75 public void setValueProperty(String valueProperty) { 76 this.valueProperty = valueProperty; 77 } 78 79 84 public String getTextProperty() { 85 return textProperty; 86 } 87 88 93 public void setTextProperty(String textProperty) { 94 this.textProperty = textProperty; 95 } 96 97 100 protected void initialize() throws JspException { 101 super.initialize(); 102 103 selectParent = (SelectTag) findAncestorWithClass(this, SelectTag.class); 104 if (selectParent == null) { 105 throw new JspException ("Option tags must be imbedded in select tags"); 106 } 107 108 localItems = items; 109 if (items instanceof String ) { 110 localItems = JspTools.expand("items", (String ) items, Object .class, 111 this, pageContext); 112 } 113 } 114 115 125 public int doStartTag() throws JspException { 126 127 initialize(); 129 130 Object [] finalArray = null; 133 boolean isMap = false; 134 135 if (localItems instanceof Object []) { 136 finalArray = (Object []) localItems; 137 } else if (localItems instanceof Collection ) { 138 finalArray = ((Collection ) localItems).toArray(); 139 } else if (localItems instanceof Map ) { 140 isMap = true; 141 finalArray = ((Map ) localItems).keySet().toArray(); 142 } else if (localItems == null) { 143 return SKIP_BODY; 144 } else { 145 throw new JspException ("Array attribute must refer to an array or" + 146 " Collection or Map"); 147 } 148 149 NestedBeanProperty prop; 151 Object textObject; 152 Object valueObject; 153 Object text; 154 Object value; 155 boolean selected; 156 StringBuffer buf = new StringBuffer (); 157 158 for (int i = 0; i < finalArray.length; i++) { 159 160 if (isMap) { 161 textObject = ((Map ) localItems).get(finalArray[i]); 162 valueObject = finalArray[i]; 163 } else { 164 textObject = finalArray[i]; 165 valueObject = finalArray[i]; 166 } 167 168 try { 169 170 if (valueProperty == null) { 172 value = valueObject; 173 } else { 174 prop = new NestedBeanProperty(valueProperty, valueObject.getClass()); 175 value = prop.getPropertyValue(valueObject); 176 } 177 178 if (textProperty == null) { 180 text = textObject; 181 } else { 182 prop = new NestedBeanProperty(textProperty, textObject.getClass()); 183 text = prop.getPropertyValue(textObject); 184 } 185 } catch (BeanException be) { 186 throw new JspException (be.toString()); 187 } 188 189 selected = ObjectTools.areObjectsEqual(value, selectParent.getLocalValue()); 191 createOptionTag(buf, value.toString(), text.toString(), selected); 192 buf.append("\n"); 193 194 if (logger.isDebugEnabled()) { 195 logger.debug("The option tags: " + buf.toString()); 196 } 197 } 198 199 try { 200 pageContext.getOut().print(buf.toString()); 201 } catch (IOException ioe) { 202 throw new JspException (ioe.toString()); 203 } 204 205 return SKIP_BODY; 206 } 207 208 216 protected void createOptionTag(StringBuffer buf, String value, String text, 217 boolean selected) { 218 HtmlViewToolkit.createOptionTag(buf, id, value, text, selected, 219 attributes, singleAttrs); 220 } 221 } | Popular Tags |