1 16 package org.ajaxtags.tags; 17 18 import java.io.IOException ; 19 20 import javax.servlet.jsp.JspException ; 21 import javax.servlet.jsp.JspWriter ; 22 import javax.servlet.jsp.tagext.TagSupport ; 23 24 import org.apache.commons.lang.BooleanUtils; 25 import org.apache.commons.lang.StringUtils; 26 import org.apache.taglibs.standard.lang.support.ExpressionEvaluatorManager; 27 28 34 public class AjaxToggleTag extends TagSupport { 35 36 private String var; 37 38 private String attachTo; 39 40 private String baseUrl; 41 42 private String parameters; 43 44 private String source; 45 46 private String ratings; 47 48 private String defaultRating; 49 50 private String state; 51 52 private String onOff; 53 54 private String containerClass; 55 56 private String messageClass; 57 58 private String selectedClass; 59 60 private String selectedOverClass; 61 62 private String selectedLessClass; 63 64 private String overClass; 65 66 private String preFunction; 67 68 private String postFunction; 69 70 private String errorFunction; 71 72 private String parser; 73 74 private String updateFunction; 75 public String getUpdateFunction() { 76 return updateFunction; 77 } 78 public void setUpdateFunction(String updateFunction) { 79 this.updateFunction = updateFunction; 80 } 81 82 public String getBaseUrl() { 83 return baseUrl; 84 } 85 86 public void setBaseUrl(String baseUrl) { 87 this.baseUrl = baseUrl; 88 } 89 90 public String getContainerClass() { 91 return containerClass; 92 } 93 94 public void setContainerClass(String containerClass) { 95 this.containerClass = containerClass; 96 } 97 98 public String getDefaultRating() { 99 return defaultRating; 100 } 101 102 public void setDefaultRating(String defaultRating) { 103 this.defaultRating = defaultRating; 104 } 105 106 public String getErrorFunction() { 107 return errorFunction; 108 } 109 110 public void setErrorFunction(String errorFunction) { 111 this.errorFunction = errorFunction; 112 } 113 114 public String getSource() { 115 return source; 116 } 117 118 public void setSource(String source) { 119 this.source = source; 120 } 121 122 public String getMessageClass() { 123 return messageClass; 124 } 125 126 public void setMessageClass(String messageClass) { 127 this.messageClass = messageClass; 128 } 129 130 public String getOnOff() { 131 return onOff; 132 } 133 134 public void setOnOff(String onOff) { 135 this.onOff = onOff; 136 } 137 138 public String getOverClass() { 139 return overClass; 140 } 141 142 public void setOverClass(String overClass) { 143 this.overClass = overClass; 144 } 145 146 public String getParameters() { 147 return parameters; 148 } 149 150 public void setParameters(String parameters) { 151 this.parameters = parameters; 152 } 153 154 public String getParser() { 155 return parser; 156 } 157 158 public void setParser(String parser) { 159 this.parser = parser; 160 } 161 162 public String getPostFunction() { 163 return postFunction; 164 } 165 166 public void setPostFunction(String postFunction) { 167 this.postFunction = postFunction; 168 } 169 170 public String getPreFunction() { 171 return preFunction; 172 } 173 174 public void setPreFunction(String preFunction) { 175 this.preFunction = preFunction; 176 } 177 178 public String getRatings() { 179 return ratings; 180 } 181 182 public void setRatings(String ratings) { 183 this.ratings = ratings; 184 } 185 186 public String getSelectedClass() { 187 return selectedClass; 188 } 189 190 public void setSelectedClass(String selectedClass) { 191 this.selectedClass = selectedClass; 192 } 193 194 public String getSelectedLessClass() { 195 return selectedLessClass; 196 } 197 198 public void setSelectedLessClass(String selectedLessClass) { 199 this.selectedLessClass = selectedLessClass; 200 } 201 202 public String getSelectedOverClass() { 203 return selectedOverClass; 204 } 205 206 public void setSelectedOverClass(String selectedOverClass) { 207 this.selectedOverClass = selectedOverClass; 208 } 209 210 public String getState() { 211 return state; 212 } 213 214 public void setState(String state) { 215 this.state = state; 216 } 217 218 public String getVar() { 219 return var; 220 } 221 222 public void setVar(String var) { 223 this.var = var; 224 } 225 226 public String getAttachTo() { 227 return attachTo; 228 } 229 230 public void setAttachTo(String attachTo) { 231 this.attachTo = attachTo; 232 } 233 234 @Override 235 public int doStartTag() throws JspException { 236 this.baseUrl = (String ) ExpressionEvaluatorManager.evaluate("baseUrl", 238 this.baseUrl, String .class, this, super.pageContext); 239 this.source = (String ) ExpressionEvaluatorManager.evaluate("source", 240 this.source, String .class, this, super.pageContext); 241 this.ratings = (String ) ExpressionEvaluatorManager.evaluate("ratings", 242 this.ratings, String .class, this, super.pageContext); 243 this.containerClass = (String ) ExpressionEvaluatorManager.evaluate( 244 "containerClass", this.containerClass, String .class, this, 245 super.pageContext); 246 this.selectedClass = (String ) ExpressionEvaluatorManager.evaluate( 247 "selectedClass", this.selectedClass, String .class, this, 248 super.pageContext); 249 this.selectedOverClass = (String ) ExpressionEvaluatorManager.evaluate( 250 "selectedOverClass", this.selectedOverClass, String .class, 251 this, super.pageContext); 252 this.selectedLessClass = (String ) ExpressionEvaluatorManager.evaluate( 253 "selectedLessClass", this.selectedLessClass, String .class, 254 this, super.pageContext); 255 this.overClass = (String ) ExpressionEvaluatorManager.evaluate( 256 "overClass", this.overClass, String .class, this, 257 super.pageContext); 258 259 if (this.var != null) { 261 this.var = (String ) ExpressionEvaluatorManager.evaluate("var", 262 this.var, String .class, this, super.pageContext); 263 } 264 if (this.attachTo != null) { 265 this.attachTo = (String ) ExpressionEvaluatorManager.evaluate( 266 "attachTo", this.attachTo, String .class, this, 267 super.pageContext); 268 } 269 if (this.messageClass != null) { 270 this.messageClass = (String ) ExpressionEvaluatorManager.evaluate( 271 "messageClass", this.messageClass, String .class, this, 272 super.pageContext); 273 } 274 if (this.parameters != null) { 275 this.parameters = (String ) ExpressionEvaluatorManager.evaluate( 276 "parameters", this.parameters, String .class, this, 277 super.pageContext); 278 } 279 if (this.state != null) { 280 this.state = (String ) ExpressionEvaluatorManager.evaluate("state", 281 this.state, String .class, this, super.pageContext); 282 } 283 if (this.onOff != null) { 284 this.onOff = (String ) ExpressionEvaluatorManager.evaluate("onOff", 285 this.onOff, String .class, this, super.pageContext); 286 } 287 if (this.defaultRating != null) { 288 this.defaultRating = (String ) ExpressionEvaluatorManager.evaluate( 289 "defaultRating", this.defaultRating, String .class, this, 290 super.pageContext); 291 } 292 if (this.preFunction != null) { 293 this.preFunction = (String ) ExpressionEvaluatorManager.evaluate( 294 "preFunction", this.preFunction, String .class, this, 295 super.pageContext); 296 } 297 if (this.postFunction != null) { 298 this.postFunction = (String ) ExpressionEvaluatorManager.evaluate( 299 "postFunction", this.postFunction, String .class, this, 300 super.pageContext); 301 } 302 if (this.errorFunction != null) { 303 this.errorFunction = (String ) ExpressionEvaluatorManager.evaluate( 304 "errorFunction", this.errorFunction, String .class, this, 305 super.pageContext); 306 } 307 308 return SKIP_BODY; 309 } 310 311 @Override 312 public int doEndTag() throws JspException { 313 OptionsBuilder options = new OptionsBuilder(); 314 options.add("source", this.source, true).add("ratings", this.ratings, 315 true).add("containerClass", this.containerClass, true).add( 316 "selectedClass", this.selectedClass, true).add( 317 "selectedOverClass", this.selectedOverClass, true).add( 318 "selectedLessClass", this.selectedLessClass, true).add( 319 "overClass", this.overClass, true); 320 321 if (this.messageClass != null) 322 options.add("messageClass", this.messageClass, true); 323 if (this.parameters != null) 324 options.add("parameters", this.parameters, true); 325 if (this.state != null) 326 options.add("state", this.state, true); 327 if (this.onOff != null) 328 options.add("onOff", this.onOff, true); 329 if (this.defaultRating != null) 330 options.add("defaultRating", this.defaultRating, true); 331 if (this.preFunction != null) 332 options.add("preFunction", this.preFunction, false); 333 if (this.postFunction != null) 334 options.add("postFunction", this.postFunction, false); 335 if (this.errorFunction != null) 336 options.add("errorFunction", this.errorFunction, false); 337 if (this.parser != null) 338 options.add("parser", this.parser, false); 339 if (this.updateFunction != null) 340 options.add("updateFunction", this.updateFunction, false); 341 342 StringBuffer script = new StringBuffer (); 343 script.append("<div id=\"").append(this.source).append("\" class=\"") 345 .append(this.containerClass); 346 if (this.onOff != null && "true".equalsIgnoreCase(this.onOff)) { 347 script.append(" onoff"); 348 } 349 script.append("\">"); 350 if (BooleanUtils.toBoolean(this.onOff)) { 352 if (StringUtils.isNotBlank(this.defaultRating) 353 && StringUtils.isNotBlank(this.ratings) 354 && this.defaultRating.equalsIgnoreCase(this.ratings 355 .split(",")[0])) { 356 script.append("<a HREF=\"javascript:void(0)\" title=\"") 357 .append(this.ratings.split(",")[0]).append( 358 "\" class=\"").append(this.selectedClass) 359 .append("\"></a>"); 360 } else { 361 script.append("<a HREF=\"javascript:void(0)\" title=\"") 362 .append(this.ratings.split(",")[1]).append("\"></a>"); 363 } 364 } else { 365 boolean ratingMatch = false; 366 String [] ratingValues = this.ratings.split(","); 367 for (int i = 0; i < ratingValues.length; i++) { 368 String val = ratingValues[i]; 369 if (StringUtils.isBlank(this.defaultRating) 370 || ratingMatch == true) { 371 script.append("<a HREF=\"javascript:void(0)\" title=\"") 372 .append(val).append("\"></a>"); 373 } else if (ratingMatch == false 374 || this.defaultRating.equalsIgnoreCase(val)) { 375 script.append("<a HREF=\"javascript:void(0)\" title=\"") 376 .append(val).append("\" class=\"").append( 377 this.selectedClass).append("\"></a>"); 378 if (this.defaultRating.equalsIgnoreCase(val)) 379 ratingMatch = true; 380 } 381 } 382 } 383 script.append("</div>\n"); 384 385 script.append("<script type=\"text/javascript\">\n"); 387 388 if (StringUtils.isNotEmpty(this.attachTo)) { 389 script.append(this.attachTo).append("."); 390 if (StringUtils.isNotEmpty(this.var)) { 391 script.append(this.var); 392 } else { 393 script.append("aj_").append(this.source); 394 } 395 } else if (StringUtils.isNotEmpty(this.var)) { 396 script.append("var ").append(this.var); 397 } else { 398 script.append("var aj_").append(this.source); 399 } 400 401 script.append(" = new AjaxJspTag.Toggle(\n").append("\"").append( 402 this.baseUrl).append("\", {\n").append(options.toString()) 403 .append("});\n").append("</script>\n\n"); 404 405 JspWriter writer = pageContext.getOut(); 406 try { 407 writer.println(script); 408 } catch (IOException e) { 409 throw new JspException (e.getMessage()); 410 } 411 return EVAL_PAGE; 412 } 413 414 @Override 415 public void release() { 416 this.var = null; 417 this.attachTo = null; 418 this.baseUrl = null; 419 this.parameters = null; 420 this.source = null; 421 this.ratings = null; 422 this.defaultRating = null; 423 this.state = null; 424 this.onOff = null; 425 this.containerClass = null; 426 this.messageClass = null; 427 this.selectedClass = null; 428 this.selectedOverClass = null; 429 this.selectedLessClass = null; 430 this.overClass = null; 431 this.preFunction = null; 432 this.postFunction = null; 433 this.errorFunction = null; 434 this.parser = null; 435 super.release(); 436 } 437 } 438
| Popular Tags
|