1 15 package org.apache.tapestry.form; 16 17 import java.util.HashSet ; 18 import java.util.Set ; 19 20 import org.apache.hivemind.ApplicationRuntimeException; 21 import org.apache.tapestry.IMarkupWriter; 22 import org.apache.tapestry.IRequestCycle; 23 import org.apache.tapestry.Tapestry; 24 import org.apache.tapestry.valid.ValidationStrings; 25 import org.apache.tapestry.valid.ValidatorException; 26 27 39 public abstract class Select extends AbstractRequirableField 40 { 41 private boolean _rewinding; 42 43 private boolean _rendering; 44 45 private Set _selections; 46 47 private int _nextOptionId; 48 49 53 54 private final static String ATTRIBUTE_NAME = "org.apache.tapestry.active.Select"; 55 56 public static Select get(IRequestCycle cycle) 57 { 58 return (Select) cycle.getAttribute(ATTRIBUTE_NAME); 59 } 60 61 public abstract boolean isMultiple(); 62 63 public boolean isRewinding() 64 { 65 if (!_rendering) 66 throw Tapestry.createRenderOnlyPropertyException(this, "rewinding"); 67 68 return _rewinding; 69 } 70 71 public String getNextOptionId() 72 { 73 if (!_rendering) 74 throw Tapestry.createRenderOnlyPropertyException(this, "nextOptionId"); 75 76 78 return Integer.toString(_nextOptionId++); 79 } 80 81 public boolean isSelected(String value) 82 { 83 if (_selections == null) 84 return false; 85 86 return _selections.contains(value); 87 } 88 89 92 protected void prepareForRender(IRequestCycle cycle) 93 { 94 if (cycle.getAttribute(ATTRIBUTE_NAME) != null) 95 throw new ApplicationRuntimeException(Tapestry.getMessage("Select.may-not-nest"), this, 96 null, null); 97 98 cycle.setAttribute(ATTRIBUTE_NAME, this); 99 100 _rendering = true; 101 _nextOptionId = 0; 102 } 103 104 107 protected void cleanupAfterRender(IRequestCycle cycle) 108 { 109 _rendering = false; 110 _selections = null; 111 } 112 113 116 protected void finishLoad() 117 { 118 setRequiredMessage(ValidationStrings.getMessagePattern( 119 ValidationStrings.REQUIRED_SELECT_FIELD, 120 getPage().getLocale())); 121 } 122 123 127 public void bind(IMarkupWriter writer, IRequestCycle cycle) throws ValidatorException 128 { 129 _selections = null; 130 _rewinding = true; 131 132 String [] parameters = cycle.getParameters(getName()); 133 134 if (parameters != null) 135 { 136 int length = parameters.length; 137 138 _selections = new HashSet ((length > 30) ? 101 : 7); 139 140 for (int i = 0; i < length; i++) 141 _selections.add(parameters[i]); 142 } 143 144 renderBody(writer, cycle); 145 } 146 147 151 protected void renderFormComponent(IMarkupWriter writer, IRequestCycle cycle) 152 { 153 super.renderFormComponent(writer, cycle); 154 155 _rewinding = false; 156 157 renderDelegatePrefix(writer, cycle); 158 159 writer.begin("select"); 160 161 writer.attribute("name", getName()); 162 163 if (isMultiple()) 164 writer.attribute("multiple", "multiple"); 165 166 if (isDisabled()) 167 writer.attribute("disabled", "disabled"); 168 169 renderIdAttribute(writer, cycle); 170 171 renderDelegateAttributes(writer, cycle); 172 173 renderInformalParameters(writer, cycle); 174 175 renderBody(writer, cycle); 176 177 writer.end(); 178 179 renderDelegateSuffix(writer, cycle); 180 } 181 } | Popular Tags |