1 7 package fr.improve.struts.taglib.layout.datagrid; 8 9 import java.util.Iterator ; 10 11 import javax.servlet.jsp.JspException ; 12 13 import org.apache.struts.util.ResponseUtils; 14 15 import fr.improve.struts.taglib.layout.collection.SimpleItemContext; 16 import fr.improve.struts.taglib.layout.field.AbstractFieldTag; 17 import fr.improve.struts.taglib.layout.field.Choice; 18 import fr.improve.struts.taglib.layout.field.ChoiceTag; 19 20 24 public class DatagridSelectTag extends AbstractDatagridColumnTag implements ChoiceTag { 25 28 static class DatagridChoice { 29 private DatagridChoice(Choice in_choice) { 30 label = in_choice.getChoiceLabel(); 31 value= in_choice.getChoiceValue(); 32 } 33 String label; 34 String value; 35 } 36 37 41 protected SimpleItemContext createItemContext() { 42 DatagridItemContext lc_context = new DatagridItemContext(); 43 lc_context.setColumnType(ColumnType.select()); 44 return lc_context; 45 } 46 47 51 public void addChoice(StringBuffer sb, String value, String label) throws JspException { 52 54 } 55 56 59 public void addChoice(StringBuffer in_buffer, Choice in_choice) throws JspException { 60 ((DatagridItemContext)context).getColumnType().getValues().add(new DatagridChoice(in_choice)); 61 } 62 63 66 protected String doAfterValue() throws JspException { 67 StringBuffer buffer = new StringBuffer (); 69 70 Iterator it = ((DatagridItemContext)context).getColumnType().getValues().iterator(); 72 Object fieldValue = buildInputFieldValue(parentCollectionTag, false); 73 while (it.hasNext()) { 74 DatagridChoice choice = (DatagridChoice) it.next(); 75 if (fieldDisplayMode!=AbstractFieldTag.MODE_INSPECT) { 76 appendEditableOption(buffer, fieldValue, choice); 78 } else { 79 appendInspectOption(buffer, fieldValue, choice); 81 } 82 } 83 84 if (fieldDisplayMode!=AbstractFieldTag.MODE_INSPECT) { 86 buffer.append("</select>\n"); 87 } 88 return buffer.toString(); 89 } 90 91 94 protected void appendEditableOption(StringBuffer in_buffer, Object fieldValue, DatagridChoice choice) { 95 in_buffer.append("<option value=\""); 96 in_buffer.append(choice.value); 97 in_buffer.append("\""); 98 if (fieldValue!=null && fieldValue.toString().equals(choice.value)) { 99 in_buffer.append(" selected"); 100 } 101 in_buffer.append(">"); 102 in_buffer.append(ResponseUtils.filter(choice.label)); 103 in_buffer.append("</option>\n"); 104 } 105 106 109 protected void appendInspectOption(StringBuffer in_buffer, Object fieldValue, DatagridChoice choice) { 110 if (fieldValue!=null && fieldValue.toString().equals(choice.value)) { 111 in_buffer.append(ResponseUtils.filter(choice.label)); 112 } 113 } 114 115 118 protected void appendFieldStart(StringBuffer in_buffer) { 119 in_buffer.append("<select"); 120 } 121 122 125 protected void appendFieldValue(StringBuffer in_buffer, Object in_value) { 126 } 128 129 134 protected void appendInspectFieldValue(StringBuffer in_buffer, Object in_value) { 135 } 137 138 protected int evaluateFirstBody() { 139 return EVAL_BODY_INCLUDE; 140 } 141 142 protected int evaluateNextBody() { 143 return EVAL_BODY_INCLUDE; 144 } 145 146 protected void reset() { 147 ((DatagridItemContext)context).getColumnType().getValues().clear(); 148 super.reset(); 149 } 150 } 151 | Popular Tags |