1 15 package org.apache.tapestry.form; 16 17 import org.apache.tapestry.IMarkupWriter; 18 import org.apache.tapestry.IRequestCycle; 19 import org.apache.tapestry.Tapestry; 20 import org.apache.tapestry.valid.ValidationStrings; 21 import org.apache.tapestry.valid.ValidatorException; 22 23 46 public abstract class PropertySelection extends AbstractRequirableField 47 { 48 52 public void bind(IMarkupWriter writer, IRequestCycle cycle) throws ValidatorException 53 { 54 if (isDisabled()) 55 return; 56 57 setValue(getModel().translateValue(getSubmittedValue(cycle))); 58 } 59 60 63 protected void finishLoad() 64 { 65 setRequiredMessage(ValidationStrings.getMessagePattern( 66 ValidationStrings.REQUIRED_SELECT_FIELD, 67 getPage().getLocale())); 68 } 69 70 77 protected void renderFormComponent(IMarkupWriter writer, IRequestCycle cycle) 78 { 79 super.renderFormComponent(writer, cycle); 80 81 renderDelegatePrefix(writer, cycle); 82 83 writer.begin("select"); 84 writer.attribute("name", getName()); 85 86 if (isDisabled()) 87 writer.attribute("disabled", "disabled"); 88 89 if (getSubmitOnChange()) 90 writer.attribute("onchange", "this.form.submit()"); 91 92 renderIdAttribute(writer, cycle); 93 94 renderDelegateAttributes(writer, cycle); 95 96 renderInformalParameters(writer, cycle); 98 99 writer.println(); 100 101 IPropertySelectionModel model = getModel(); 102 103 if (model == null) 104 throw Tapestry.createRequiredParameterException(this, "model"); 105 106 int count = model.getOptionCount(); 107 boolean foundSelected = false; 108 Object value = getValue(); 109 110 for (int i = 0; i < count; i++) 111 { 112 Object option = model.getOption(i); 113 114 writer.begin("option"); 115 writer.attribute("value", model.getValue(i)); 116 117 if (!foundSelected && isEqual(option, value)) 118 { 119 writer.attribute("selected", "selected"); 120 121 foundSelected = true; 122 } 123 124 writer.print(model.getLabel(i)); 125 126 writer.end(); 127 128 writer.println(); 129 } 130 131 writer.end(); 133 renderDelegateSuffix(writer, cycle); 134 } 135 136 private boolean isEqual(Object left, Object right) 137 { 138 140 if (left == right) 141 return true; 142 143 145 if (left == null || right == null) 146 return false; 147 148 150 return left.equals(right); 151 } 152 153 public abstract IPropertySelectionModel getModel(); 154 155 156 public abstract boolean getSubmitOnChange(); 157 158 159 public abstract Object getValue(); 160 161 162 public abstract void setValue(Object value); 163 } | Popular Tags |