1 16 package org.mortbay.html; 17 import java.util.Enumeration ; 18 19 import org.apache.commons.logging.Log; 20 import org.mortbay.log.LogFactory; 21 22 23 26 public class Select extends Block 27 { 28 private static Log log = LogFactory.getLog(Select.class); 29 30 31 35 public Select(String name,boolean multiple) 36 { 37 super("select"); 38 attribute("name",name); 39 40 if (multiple) 41 attribute("multiple"); 42 } 43 44 45 49 public Select(String name,boolean multiple, String [] options) 50 { 51 this(name,multiple); 52 53 for (int i=0; i<options.length; i++) 54 add(options[i]); 55 } 56 57 58 59 public Select setSize(int size) 60 { 61 size(size); 62 return this; 63 } 64 65 66 public Select add(Enumeration e) 67 { 68 while (e.hasMoreElements()) 69 add(e.nextElement().toString()); 70 return this; 71 } 72 73 74 76 public Composite add(Object o) 77 { 78 if (o instanceof Enumeration ) 79 this.add((Enumeration )o); 80 else 81 { 82 super.add("<option>"); 83 super.add(o); 84 } 85 return this; 86 } 87 88 89 91 public Select add(Object o, boolean selected) 92 { 93 if (selected) 94 super.add("<option selected>"); 95 else 96 super.add("<option>"); 97 super.add(o); 98 return this; 99 } 100 101 102 107 public Select add(Object o, boolean selected, String value) 108 { 109 if (selected) 110 super.add("<option selected value=\""+value+"\">"); 111 else 112 super.add("<option value=\""+value+"\">"); 113 114 super.add(o); 115 116 return this; 117 } 118 119 120 126 public Select add(String arr[], int selected) 127 { 128 for (int i = 0; i < arr.length; i++){ 129 this.add(arr[i], i == selected, Integer.toString(i)); 130 } 131 return this; 132 } 133 134 135 141 public Select add(String arr[], String selected) 142 { 143 for (int i = 0; i < arr.length; i++){ 144 this.add(arr[i], arr[i].equals(selected)); 145 } 146 return this; 147 } 148 149 150 155 public static int bitsetFormResult(String result) 156 { 157 int i; 158 int from = 0; 159 int res = 0; 160 if(log.isDebugEnabled())log.debug("Result:"+result); 161 String sres = null; 162 while ((i = result.indexOf(' ', from)) != -1){ 163 sres = result.substring(from, i); 164 res = res | Integer.parseInt(sres); 165 if(log.isDebugEnabled())log.debug("Match:"+sres+"+ res="+res); 166 from = i+1; 167 } 168 sres = result.substring(from); 169 res = res | Integer.parseInt(sres); 170 if(log.isDebugEnabled())log.debug("Match:"+sres+", res="+res); 171 return res; 172 } 173 } 174 175 176 177 178 | Popular Tags |