1 64 65 package com.jcorporate.expresso.services.html; 66 67 import java.io.PrintWriter ; 68 import java.util.Enumeration ; 69 import java.util.Vector ; 70 71 72 78 public class Radio 79 extends HtmlElement { 80 private String fieldName = null; 81 private String currentValue = (""); 82 private Vector values = new Vector (3); 83 private Vector labels = new Vector (3); 84 private String thisClass = (this.getClass().getName() + "."); 85 private boolean isVertical = false; 86 87 93 public Radio(String newFieldName) 94 throws HtmlException { 95 super(newFieldName); 96 97 String myName = (thisClass + "Radio(String)"); 98 99 if (newFieldName == null) { 100 throw new HtmlException(myName + 101 ":Field name must not be null for " + 102 getName()); 103 } 104 105 fieldName = newFieldName; 106 } 107 108 115 public Radio(String newFieldName, String newCurrentValue) 116 throws HtmlException { 117 super(newFieldName); 118 fieldName = newFieldName; 119 currentValue = newCurrentValue; 120 } 121 122 129 public synchronized void addOption(String fieldValue, String fieldLabel) 130 throws HtmlException { 131 values.addElement(fieldValue); 132 labels.addElement(fieldLabel); 133 } 134 135 136 143 protected synchronized void display(PrintWriter out, int depth) 144 throws HtmlException { 145 String myName = (thisClass + "display(PrintWriter)"); 146 147 if (values.size() == 0) { 148 throw new HtmlException(myName + ":Radio " + getName() + 149 " has no choices"); 150 } 151 152 String oneValue = null; 153 String oneLabel = null; 154 Enumeration e2 = labels.elements(); 155 156 for (Enumeration e = values.elements(); e.hasMoreElements();) { 157 oneValue = (String ) e.nextElement(); 158 oneLabel = (String ) e2.nextElement(); 159 160 if (isVertical) { 161 this.padWithTabs(out, depth); 162 out.print("<br />"); 163 } 164 if (currentValue.equals(oneValue)) { 165 this.padWithTabs(out, depth); 166 out.println("<input type=radio name=\"" + fieldName + 167 "\" checked value=\"" + oneValue + "\">" + 168 oneLabel); 169 } else { 170 this.padWithTabs(out, depth); 171 out.println("<input type=radio NAME=\"" + fieldName + 172 "\" value=\"" + oneValue + "\">" + oneLabel); 173 } 174 } 175 176 this.padWithTabs(out, depth); 177 out.println("</input>"); 178 setDisplayed(); 179 } 180 181 182 188 public void setCurrentValue(String newCurrentValue) 189 throws HtmlException { 190 currentValue = newCurrentValue; 191 } 192 193 194 197 public void setVertical(boolean state) { 198 isVertical = state; 199 } 200 201 } 202 203 | Popular Tags |