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 DropDown 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 86 92 public DropDown(String newFieldName) 93 throws HtmlException { 94 super(newFieldName); 95 96 String myName = (thisClass + "DropDown(String)"); 97 98 if (newFieldName == null) { 99 throw new HtmlException(myName + 100 ":Field name must not be null for " + 101 getName()); 102 } 103 104 fieldName = newFieldName; 105 } 106 107 114 public DropDown(String newFieldName, String newCurrentValue) 115 throws HtmlException { 116 super(newFieldName); 117 fieldName = newFieldName; 118 currentValue = newCurrentValue; 119 } 120 121 128 public synchronized void addOption(String fieldValue, String fieldLabel) 129 throws HtmlException { 130 values.addElement(fieldValue); 131 labels.addElement(fieldLabel); 132 } 133 134 135 142 protected synchronized void display(PrintWriter out, int depth) 143 throws HtmlException { 144 String myName = (thisClass + "display(PrintWriter)"); 145 146 if (values.size() == 0) { 147 throw new HtmlException(myName + ":DropDown " + getName() + 148 " has no choices"); 149 } 150 151 this.padWithTabs(out, depth); 152 out.print("<select"); 153 154 if (cSSClass != null) { 155 out.print(" class=\"" + cSSClass + "\""); 156 } 157 if (cSSID != null) { 158 out.print(" id=\"" + cSSID + "\""); 159 } 160 161 out.println(" name=" + fieldName + ">"); 162 163 String oneValue = null; 164 String oneLabel = null; 165 Enumeration e2 = labels.elements(); 166 167 for (Enumeration e = values.elements(); e.hasMoreElements();) { 168 oneValue = (String ) e.nextElement(); 169 oneLabel = (String ) e2.nextElement(); 170 171 if (currentValue.equals(oneValue)) { 172 this.padWithTabs(out, depth + 1); 173 out.println("<option selected value=\"" + oneValue + "\">" + 174 oneLabel); 175 } else { 176 this.padWithTabs(out, depth + 1); 177 out.println("<option value=\"" + oneValue + "\">" + oneLabel); 178 } 179 } 180 181 this.padWithTabs(out, depth); 182 out.println("</select>"); 183 setDisplayed(); 184 } 185 186 187 193 public void setCurrentValue(String newCurrentValue) 194 throws HtmlException { 195 currentValue = newCurrentValue; 196 } 197 198 199 } 200 201 | Popular Tags |