1 15 package org.apache.tapestry.form; 16 17 import org.apache.hivemind.ApplicationRuntimeException; 18 import org.apache.tapestry.IMarkupWriter; 19 import org.apache.tapestry.IRequestCycle; 20 import org.apache.tapestry.Tapestry; 21 import org.apache.tapestry.valid.ValidationStrings; 22 import org.apache.tapestry.valid.ValidatorException; 23 24 35 public abstract class RadioGroup extends AbstractRequirableField 36 { 37 private Object _selection; 39 40 private int _selectedOption; 43 44 private boolean _rewinding; 45 46 private boolean _rendering; 47 48 private int _nextOptionId; 49 50 54 55 private static final String ATTRIBUTE_NAME = "org.apache.tapestry.active.RadioGroup"; 56 57 public static RadioGroup get(IRequestCycle cycle) 58 { 59 return (RadioGroup) cycle.getAttribute(ATTRIBUTE_NAME); 60 } 61 62 public int getNextOptionId() 63 { 64 if (!_rendering) 65 throw Tapestry.createRenderOnlyPropertyException(this, "nextOptionId"); 66 67 return _nextOptionId++; 68 } 69 70 public boolean isRewinding() 71 { 72 if (!_rendering) 73 throw Tapestry.createRenderOnlyPropertyException(this, "rewinding"); 74 75 return _rewinding; 76 } 77 78 82 83 public boolean isSelection(Object value) 84 { 85 if (!_rendering) 86 throw Tapestry.createRenderOnlyPropertyException(this, "selection"); 87 88 if (_selection == value) 89 return true; 90 91 if (_selection == null || value == null) 92 return false; 93 94 return _selection.equals(value); 95 } 96 97 101 102 public void updateSelection(Object value) 103 { 104 getBinding("selected").setObject(value); 105 } 106 107 110 111 public boolean isSelected(int option) 112 { 113 return _selectedOption == option; 114 } 115 116 119 protected void prepareForRender(IRequestCycle cycle) 120 { 121 if (cycle.getAttribute(ATTRIBUTE_NAME) != null) 122 throw new ApplicationRuntimeException(Tapestry.getMessage("RadioGroup.may-not-nest"), 123 this, null, null); 124 125 cycle.setAttribute(ATTRIBUTE_NAME, this); 126 127 _rendering = true; 128 _nextOptionId = 0; 129 } 130 131 134 protected void cleanupAfterRender(IRequestCycle cycle) 135 { 136 _rendering = false; 137 _selection = null; 138 139 cycle.removeAttribute(ATTRIBUTE_NAME); 140 } 141 142 145 protected void finishLoad() 146 { 147 setRequiredMessage(ValidationStrings.getMessagePattern(ValidationStrings.REQUIRED_SELECT_FIELD, getPage().getLocale())); 148 } 149 150 153 public void bind(IMarkupWriter writer, IRequestCycle cycle) throws ValidatorException 154 { 155 String value = getSubmittedValue(cycle); 156 157 if (value == null) 158 _selectedOption = -1; 159 else 160 _selectedOption = Integer.parseInt(value); 161 162 _rewinding = true; 163 164 renderBody(writer, cycle); 165 } 166 167 170 protected void renderFormComponent(IMarkupWriter writer, IRequestCycle cycle) 171 { 172 super.renderFormComponent(writer, cycle); 173 174 _rewinding = false; 175 176 _selection = getBinding("selected").getObject(); 179 180 renderBody(writer, cycle); 181 } 182 } | Popular Tags |