1 23 24 package org.dbforms.taglib; 25 26 import org.dbforms.config.*; 27 28 import org.dbforms.util.*; 29 30 import java.util.List ; 31 32 import javax.servlet.http.*; 33 import javax.servlet.jsp.*; 34 35 36 37 52 public class DbSearchComboTag extends DbSearchTag implements DataContainer, 53 javax.servlet.jsp.tagext.TryCatchFinally { 54 private List embeddedData = null; 55 private String compareWith = "key"; 56 private String customEntry; 57 private String selectedIndex; 58 private String size = "1"; 59 60 63 public DbSearchComboTag() { 64 setSearchAlgo("sharp"); 65 } 66 67 72 public void setCompareWith(String string) { 73 compareWith = string; 74 } 75 76 77 82 public String getCompareWith() { 83 return compareWith; 84 } 85 86 87 92 public void setCustomEntry(String customEntry) { 93 this.customEntry = customEntry; 94 } 95 96 97 102 public String getCustomEntry() { 103 return customEntry; 104 } 105 106 107 115 public void setEmbeddedData(List embeddedData) { 116 this.embeddedData = embeddedData; 117 } 118 119 120 125 public void setSelectedIndex(String selectedIndex) { 126 this.selectedIndex = selectedIndex; 127 } 128 129 130 135 public String getSelectedIndex() { 136 return selectedIndex; 137 } 138 139 140 145 public void setSize(String string) { 146 size = string; 147 } 148 149 150 152 157 public String getSize() { 158 return size; 159 } 160 161 162 165 public void doCatch(Throwable t) throws Throwable { 166 throw t; 167 } 168 169 170 178 public int doEndTag() throws javax.servlet.jsp.JspException { 179 HttpServletRequest request = (HttpServletRequest) this.pageContext 180 .getRequest(); 181 182 Field field = getField(); 183 184 StringBuffer tagBuf = new StringBuffer (); 185 186 String oldValue = ParseUtil.getParameter(request, 187 field 188 .getSearchFieldName()); 189 190 if (!Util.isNull(oldValue)) { 191 selectedIndex = oldValue; 192 } 193 194 boolean isSelected = false; 195 196 if (embeddedData != null) { 198 if (!Util.isNull(customEntry)) { 199 String aKey = org.dbforms.util.StringUtil 200 .getEmbeddedStringWithoutDots(customEntry, 0, ','); 201 String aValue = org.dbforms.util.StringUtil 202 .getEmbeddedStringWithoutDots(customEntry, 1, ','); 203 204 if (Util.isNull(selectedIndex)) { 205 isSelected = Util.getTrue(org.dbforms.util.StringUtil.getEmbeddedStringWithoutDots(customEntry, 206 2, 207 ',').trim()); 208 } 209 210 tagBuf.append(generateTagString(aKey, aValue, isSelected)); 211 } 212 213 int embeddedDataSize = embeddedData.size(); 214 int selectedIndexList = 0; 215 216 try { 217 selectedIndexList = Integer.parseInt(selectedIndex); 218 } catch (Exception e) { 219 selectedIndexList = 0; 220 } 221 222 for (int i = 0; i < embeddedDataSize; i++) { 223 KeyValuePair aKeyValuePair = (KeyValuePair) embeddedData.get(i); 224 String aKey = aKeyValuePair.getKey(); 225 String aValue = aKeyValuePair.getValue(); 226 227 if (Util.isNull(selectedIndex) && !isSelected) { 229 isSelected = i == 0; 230 } else { 231 if ("value".equalsIgnoreCase(getCompareWith())) { 232 isSelected = aValue.equals(selectedIndex); 233 } else if ("list".equalsIgnoreCase(getCompareWith())) { 234 isSelected = selectedIndexList == i; 235 } else { 236 isSelected = aKey.equals(selectedIndex); 237 } 238 } 239 240 tagBuf.append(generateTagString(aKey, aValue, isSelected)); 241 } 242 } 243 244 tagBuf.append("</select>"); 245 246 try { 247 pageContext.getOut() 248 .write(renderPatternHtmlInputField()); 249 pageContext.getOut() 250 .write(RenderHiddenFields(field)); 251 pageContext.getOut() 252 .write(generateSelectHeader(field)); 253 pageContext.getOut() 254 .write(tagBuf.toString()); 255 } catch (java.io.IOException ioe) { 256 throw new JspException("IO Error: " + ioe.getMessage()); 257 } 258 259 return EVAL_PAGE; 260 } 261 262 263 266 public void doFinally() { 267 embeddedData = null; 268 selectedIndex = null; 269 customEntry = null; 270 size = "1"; 271 compareWith = "key"; 272 super.doFinally(); 273 } 274 275 276 283 public int doStartTag() throws javax.servlet.jsp.JspException { 284 return EVAL_BODY_INCLUDE; 285 } 286 287 288 private String generateSelectHeader(Field f) 289 throws javax.servlet.jsp.JspException { 290 StringBuffer tagBuf = new StringBuffer (); 292 tagBuf.append("<select name=\""); 293 tagBuf.append(f.getSearchFieldName()); 294 tagBuf.append("\""); 295 296 if (size != null) { 297 tagBuf.append(" size=\""); 298 tagBuf.append(size); 299 tagBuf.append("\""); 300 } 301 302 tagBuf.append(prepareStyles()); 303 tagBuf.append(prepareEventHandlers()); 304 tagBuf.append(">"); 305 306 return tagBuf.toString(); 307 } 308 309 310 private String generateTagString(String value, 311 String description, 312 boolean selected) { 313 StringBuffer tagBuf = new StringBuffer (); 314 tagBuf.append("<option value=\""); 315 tagBuf.append(value); 316 tagBuf.append("\""); 317 318 if (selected) { 319 tagBuf.append(" selected=\"selected\""); 320 } 321 322 tagBuf.append(">"); 323 tagBuf.append(description.trim()); 324 tagBuf.append("</option>"); 325 326 return tagBuf.toString(); 327 } 328 } 329 | Popular Tags |