1 package fr.improve.struts.taglib.layout.field; 2 3 import java.util.List ; 4 5 import javax.servlet.jsp.JspException ; 6 7 import org.apache.struts.taglib.html.Constants; 8 import org.apache.struts.taglib.html.FormTag; 9 10 import fr.improve.struts.taglib.layout.el.Expression; 11 import fr.improve.struts.taglib.layout.event.EndLayoutEvent; 12 import fr.improve.struts.taglib.layout.event.ErrorEvent; 13 import fr.improve.struts.taglib.layout.event.LayoutEventListener; 14 import fr.improve.struts.taglib.layout.event.StartLayoutEvent; 15 import fr.improve.struts.taglib.layout.util.LayoutUtils; 16 import fr.improve.struts.taglib.layout.util.TagUtils; 17 26 public abstract class AbstractLayoutFieldTag extends AbstractModeFieldTag implements LayoutEventListener { 27 30 private String help = null; 31 32 35 private String helpStyleClass = "HELP"; 36 37 40 private String isRequired = "false"; 41 42 45 private String jspIsRequired; 46 47 50 private String errorStyleClass = "ERROR"; 51 52 55 private String hint = null; 56 57 60 private boolean layout = true; 61 64 private String tooltip = null; 65 66 69 private String model = null; 70 71 75 public static final String FIRST_ERROR_FIELD_FOCUS = "fr.improve.struts.taglib.layout.field.AbstractLayoutFieldTag.FIRST_ERROR_FIELD_FOCUS"; 76 77 80 protected void beginFieldError(List in_errors) throws JspException { 81 if (in_errors.size()!=0 && getSkin().getDisplayErrorMessage()) { 82 StringBuffer lc_buffer = new StringBuffer (); 83 getSkin().getFieldInterface(model).doStartErrors(lc_buffer, this, in_errors); 84 TagUtils.write(pageContext, lc_buffer.toString()); 85 } 86 } 87 90 protected final void beginFieldLayout() throws JspException { 91 StringBuffer buffer = new StringBuffer (); 92 93 if (Boolean.FALSE.equals(new StartLayoutEvent(this, null).send())) { 94 buffer.append("<tr>"); 96 } 97 98 getSkin().getFieldInterface(model).doStartField(buffer, this, getLabel(), getFieldValue()); 100 101 TagUtils.write(pageContext, buffer.toString()); 103 } 104 105 protected abstract int doEndEditField() throws JspException ; 106 protected int doEndEditMode() throws JspException { 107 int lc_result = doEndEditField(); 108 if (layout) { 109 endFieldLayout(); 110 } 111 return lc_result; 112 } 113 protected abstract int doEndInspectField() throws JspException ; 114 protected final int doEndInspectMode() throws JspException { 115 int lc_result = doEndInspectField(); 116 if (layout) { 117 endFieldLayout(); 118 } 119 return lc_result; 120 } 121 protected abstract int doStartEditField() throws JspException ; 122 protected int doStartEditMode() throws JspException { 123 List lc_errors = computeErrors(); 124 if (layout) { 125 beginFieldLayout(); 126 beginFieldError(lc_errors); 127 } 128 int lc_result = doStartEditField(); 129 if (layout) { 130 endFieldError(lc_errors); 131 } 132 return lc_result; 133 } 134 135 139 protected List computeErrors() throws JspException { 140 List lc_errors = retrieveErrors(); 142 143 if (lc_errors.size()>0) { 144 new ErrorEvent(this, lc_errors).send(); 145 if (getSkin().getFocusFirstErrorField()) { 146 if (pageContext.getRequest().getAttribute(FIRST_ERROR_FIELD_FOCUS)==null) { 147 FormTag lc_formTag = (FormTag) pageContext.findAttribute(Constants.FORM_KEY); 148 lc_formTag.setFocus(getProperty()); 149 pageContext.getRequest().setAttribute(FIRST_ERROR_FIELD_FOCUS, getProperty()); 150 } 151 } 152 } 153 154 return lc_errors; 156 } 157 158 159 protected abstract int doStartInspectField() throws JspException ; 160 protected final int doStartInspectMode() throws JspException { 161 if (layout) { 162 beginFieldLayout(); 163 } 164 if (MODE_INSPECT==getFieldDisplayMode()) { 165 printHiddenValue(getFieldValue()); 166 } 167 return doStartInspectField(); 168 } 169 170 protected void endFieldError(List in_errors) throws JspException { 171 if (in_errors.size()!=0) { 172 if (getSkin().getDisplayErrorMessage()) { 173 StringBuffer lc_buffer = new StringBuffer (); 174 getSkin().getFieldInterface(model).doEndErrors(lc_buffer, this, in_errors); 175 TagUtils.write(pageContext, lc_buffer.toString()); 176 } 177 } 178 } 179 182 protected final void endFieldLayout() throws JspException { 183 StringBuffer buffer = new StringBuffer (); 184 185 getSkin().getFieldInterface(model).doEndField(buffer, this, getFieldValue()); 186 187 TagUtils.write(pageContext, buffer.toString()); 189 190 if (Boolean.FALSE.equals(new EndLayoutEvent(this, null).send())) { 192 TagUtils.write(pageContext, "</tr>"); 194 } 195 196 197 if (help!=null) { 198 buffer.setLength(0); 199 if (Boolean.FALSE.equals(new StartLayoutEvent(this, null).send())) { 201 buffer.append("<tr>"); 202 } 203 buffer.append("<td colspan=\"").append(getSkin().getFieldInterface(model).getColumnNumber()).append("\" class=\""); 204 buffer.append(helpStyleClass); 205 buffer.append("\">"); 206 buffer.append(LayoutUtils.getLabel(pageContext, help, null)); 207 buffer.append("</td>"); 208 TagUtils.write(pageContext, buffer.toString()); 209 if (Boolean.FALSE.equals(new EndLayoutEvent(this, null).send())) { 210 TagUtils.write(pageContext, "</tr>"); 211 } 212 } 213 } 214 215 220 public java.lang.String getErrorStyleClass() { 221 return errorStyleClass; 222 } 223 public String getHelp() { 224 return help; 225 } 226 231 public java.lang.String getHelpStyleClass() { 232 return helpStyleClass; 233 } 234 239 240 public void release() { 241 super.release(); 242 help = null; 243 hint = null; 244 tooltip = null; 245 helpStyleClass = "HELP"; 246 isRequired = "false"; 247 errorStyleClass = "ERROR"; 248 layout = true; 249 model = null; 250 } 251 252 protected void reset() { 253 super.reset(); 254 isRequired = jspIsRequired; 255 jspIsRequired = null; 256 } 257 258 public Object processStartLayoutEvent(StartLayoutEvent in_event) throws JspException { 259 return Boolean.FALSE; 260 } 261 public Object processEndLayoutEvent(EndLayoutEvent in_event) { 262 return Boolean.FALSE; 263 } 264 267 private List retrieveErrors() throws JspException { 268 return LayoutUtils.retrieveErrors(pageContext, bundle, property); 269 } 270 275 public void setErrorStyleClass(java.lang.String newErrorStyleClass) { 276 errorStyleClass = newErrorStyleClass; 277 } 278 279 protected void initDynamicValues() { 280 super.initDynamicValues(); 281 jspIsRequired = isRequired; 282 isRequired = Expression.evaluate(jspIsRequired, pageContext); 283 } 284 285 288 public void setHelp(String help) { 289 this.help = help; 290 } 291 294 public void setTooltip(String tooltip) { 295 this.tooltip = tooltip; 296 } 297 300 public String getTooltip() { 301 return tooltip; 302 } 303 308 public void setHelpStyleClass(java.lang.String newHelpStyleClass) { 309 helpStyleClass = newHelpStyleClass; 310 } 311 315 public void setIsRequired(String newIsRequired) { 316 isRequired = newIsRequired; 317 } 318 319 public String getIsRequired() { 320 return isRequired; 321 } 322 323 public boolean isRequired() { 324 return "true".equalsIgnoreCase(isRequired); 325 } 326 327 328 public void setHint(String in_hint) { 329 hint = in_hint; 330 } 331 public String getHint() { 332 return hint; 333 } 334 335 339 public boolean isLayout() { 340 return layout; 341 } 342 343 347 public void setLayout(boolean layout) { 348 this.layout = layout; 349 } 350 351 public String getModel() { 352 return model; 353 } 354 public void setModel(String in_string) { 355 model = in_string; 356 } 357 358 361 protected String getLabel() throws JspException { 362 if (isLayout()) { 363 return super.getLabel(); 364 } else { 365 return ""; 366 } 367 } 368 369 } 370 | Popular Tags |