1 16 package com.blandware.atleap.webapp.taglib.core.html; 17 18 import com.blandware.atleap.webapp.util.core.ApplicationResources; 19 import com.blandware.atleap.webapp.taglib.core.html.FormTag; 20 import org.apache.commons.lang.StringUtils; 21 import org.apache.commons.logging.Log; 22 import org.apache.commons.logging.LogFactory; 23 import org.apache.commons.validator.Field; 24 import org.apache.commons.validator.Form; 25 import org.apache.commons.validator.ValidatorResources; 26 import org.apache.struts.Globals; 27 import org.apache.struts.action.ActionMessage; 28 import org.apache.struts.action.ActionMessages; 29 import org.apache.struts.taglib.TagUtils; 30 import org.apache.struts.taglib.html.Constants; 31 import org.apache.struts.validator.ValidatorPlugIn; 32 33 import javax.servlet.http.HttpServletRequest ; 34 import javax.servlet.jsp.JspException ; 35 import javax.servlet.jsp.JspTagException ; 36 import javax.servlet.jsp.PageContext ; 37 import javax.servlet.jsp.tagext.SimpleTagSupport ; 38 import java.util.Iterator ; 39 import java.util.Locale ; 40 41 42 105 public class LabelTag extends SimpleTagSupport { 106 protected transient final Log log = LogFactory.getLog(LabelTag.class); 107 108 111 protected String key = null; 112 113 117 protected String styleClass = null; 118 119 122 protected String style = null; 123 124 127 protected String errorClass = null; 128 129 132 protected boolean colon = true; 133 134 137 protected boolean helpTip = false; 138 139 142 protected String property = null; 143 144 147 protected String formName = null; 148 149 154 public void doTag() throws JspException { 155 156 PageContext pageContext = (PageContext ) getJspContext(); 157 HttpServletRequest request = (HttpServletRequest ) pageContext.getRequest(); 158 ApplicationResources applicationResources = ApplicationResources.getInstance(pageContext.getServletContext()); 159 160 boolean requiredField = false; 162 boolean validationError = false; 163 TagUtils tagUtils = TagUtils.getInstance(); 164 165 ValidatorResources resources = 166 (ValidatorResources) pageContext.getServletContext() 167 .getAttribute(ValidatorPlugIn.VALIDATOR_KEY); 168 169 Locale locale = 170 (Locale ) pageContext.findAttribute(Globals.LOCALE_KEY); 171 172 if ( locale == null ) { 173 locale = Locale.getDefault(); 174 } 175 176 FormTag formTag = 177 (FormTag) pageContext.getAttribute(Constants.FORM_KEY, 178 PageContext.REQUEST_SCOPE); 179 180 if ( formTag == null ) { 182 formTag = (FormTag) findAncestorWithClass(this, FormTag.class); 183 } 184 185 if ( formTag == null ) { 187 String errorMessage = "Couldn't find enclosing 'form' tag"; 188 if ( log.isErrorEnabled() ) { 189 log.error(errorMessage); 190 } 191 throw new JspTagException (errorMessage); 192 } 193 194 if ( formName == null ) { 195 formName = formTag.getBeanName(); 196 } 197 198 if ( key == null || key.length() == 0 ) { 199 StringBuffer strBuffer = new StringBuffer (formName); 200 strBuffer.append("."); 201 strBuffer.append(property); 202 key = strBuffer.toString(); 203 } 204 205 if ( resources != null ) { 206 Form form = resources.getForm(locale, formName); 207 208 if ( form != null ) { 209 Field field = form.getField(property); 210 211 if ( field != null ) { 212 if ( field.isDependency("required") ) { 213 requiredField = true; 214 } 215 } 216 } 217 } 218 219 ActionMessages errors = tagUtils.getActionMessages(pageContext, Globals.ERROR_KEY); 220 221 StringBuffer valMessage = new StringBuffer (); 222 223 if ( errors != null ) { 224 Iterator valIterator = errors.get(property); 226 227 while ( valIterator.hasNext() ) { 228 validationError = true; 229 230 ActionMessage error = (ActionMessage) valIterator.next(); 231 232 valMessage.append(applicationResources.getMessage(request, error.getKey(), error.getValues())); 234 } 235 } 236 237 String message = applicationResources.getMessage(request, key); 239 240 StringBuffer valError = new StringBuffer (); 241 242 if ( message == null ) { 243 message = "???" + key + "???"; 244 } else if ( validationError ) { 245 valError.append(valMessage); 246 } 247 248 String cssClass = null; 249 if ( styleClass != null ) { 250 cssClass = styleClass; 251 } else if ( requiredField ) { 252 cssClass = "required"; 253 } 254 255 String cssErrorClass = (errorClass != null) ? errorClass : "error"; 256 StringBuffer label = new StringBuffer (); 257 258 if ( (message == null) || "".equals(message.trim()) ) { 259 label.append(""); 260 } else { 261 label.append("<label for=\"" + property + "\""); 262 263 if ( validationError ) { 264 label.append(" class=\"" + cssErrorClass + "\""); 265 } else if ( cssClass != null ) { 266 label.append(" class=\"" + cssClass + "\""); 267 } 268 269 if ( style != null && style.length() != 0 ) { 270 label.append(" style=\"" + style + "\""); 271 } 272 273 label.append(">" + ((requiredField) ? "* " : "") + message); 274 String marker = (locale.equals(Locale.FRENCH)) ? " :" : ":"; 275 label.append(((colon) ? marker : "") + "</label>"); 276 277 if ( valError.length() > 0 ) { 278 String error = valError.toString(); 279 280 if ( helpTip ) { 281 String htmlFriendly = StringUtils.replace(error, "'", "\\\'"); 283 htmlFriendly = 284 StringUtils.replace(htmlFriendly, "\"", "\\\""); 285 label.append(" <a class=\"errorLink\" HREF=\"?\" onclick=\"showHelpTip(event, '"); 286 287 label.append(htmlFriendly + "', false); return false\" "); 288 label.append("onmouseover=\"showHelpTip(event, '"); 289 label.append(htmlFriendly + "', false); return false\" "); 290 label.append("onmouseout=\"hideHelpTip(event); return false\">"); 291 } 292 293 294 label.append("<img class=\"validationWarning\" "); 295 296 String context = 297 ((HttpServletRequest ) pageContext.getRequest()) 298 .getContextPath(); 299 300 label.append("src=\"" + context); 301 label.append(applicationResources.getMessage(request, "core.commons.icon.warning.img")); 302 label.append("\" />"); 303 if ( helpTip ) { 304 label.append("</a>"); 305 } 306 } 307 } 308 309 tagUtils.write(pageContext, label.toString()); 311 312 } 313 314 323 public void setKey(String key) { 324 this.key = key; 325 } 326 327 336 public void setColon(boolean colon) { 337 this.colon = colon; 338 } 339 340 349 public void setStyleClass(String styleClass) { 350 this.styleClass = styleClass; 351 } 352 353 362 public void setStyle(String style) { 363 this.style = style; 364 } 365 366 375 public void setErrorClass(String errorClass) { 376 this.errorClass = errorClass; 377 } 378 379 388 public void setHelpTip(boolean helpTip) { 389 this.helpTip = helpTip; 390 } 391 392 401 public void setProperty(String property) { 402 this.property = property; 403 } 404 405 414 public void setFormName(String formName) { 415 this.formName = formName; 416 } 417 418 } 419 | Popular Tags |