1 package fr.improve.struts.taglib.layout; 2 3 import java.util.Iterator ; 4 5 import javax.servlet.jsp.JspException ; 6 import javax.servlet.jsp.PageContext ; 7 8 import org.apache.struts.Globals; 9 import org.apache.struts.action.ActionErrors; 10 import org.apache.struts.action.ActionMessage; 11 import org.apache.struts.taglib.html.Constants; 12 13 import fr.improve.struts.taglib.layout.event.EndLayoutEvent; 14 import fr.improve.struts.taglib.layout.event.LayoutEventListener; 15 import fr.improve.struts.taglib.layout.event.StartLayoutEvent; 16 import fr.improve.struts.taglib.layout.util.LayoutUtils; 17 import fr.improve.struts.taglib.layout.util.TagUtils; 18 19 34 public class FieldTag extends BaseFieldTag implements LayoutEventListener { 35 36 protected String accept; 37 protected String access = READWRITE; 38 public static final String CHECKBOX = "CHECKBOX"; 39 protected String cols = null; 41 public static final String DATE = "DATE"; 42 public static final String EMAIL ="EMAIL"; 43 protected String error = null; 45 protected org.apache.struts.taglib.html.BaseHandlerTag fieldTag = null; 46 protected boolean isRequired = false; 47 protected String maxlength = null; 48 public static final String NUMBER = "NUMBER"; 49 public static final String PASSWORD = "PASSWORD"; 50 public static final String READONLY = "READONLY"; 51 public static final String READSEND ="READSEND"; 52 public static final String READWRITE = "READWRITE"; 55 protected boolean redisplay = true; 56 protected String rows = null; 57 public static final String TEXT = "TEXT"; 61 public static final String TEXTAREA = "TEXTAREA"; 62 protected String type = TEXT; 63 protected String value = null; 64 65 public FieldTag() { 66 name = Constants.BEAN_KEY; 67 } 68 public int doEndLayoutTag() throws JspException { 69 70 StringBuffer buffer = new StringBuffer (); 71 72 endField(buffer); 73 74 TagUtils.write(pageContext, buffer.toString()); 75 76 return EVAL_PAGE; 77 } 78 public int doStartLayoutTag() throws JspException { 79 80 StringBuffer buffer = new StringBuffer (); 81 82 beginField(buffer); 84 85 error = retrieveError(); 87 if (error!=null && access.equals(READWRITE)) 88 buffer.append("<table><tr><td class=ERROR>"); 89 90 91 TagUtils.write(pageContext,buffer.toString()); 92 93 if (access.equals(READSEND)) type = "hidden"; 96 if (type.equals(CHECKBOX)) fieldTag = new org.apache.struts.taglib.html.CheckboxTag(); 97 if (type.equals(PASSWORD)) fieldTag = new org.apache.struts.taglib.html.PasswordTag(); 98 if (type.equals(TEXT)) fieldTag = new org.apache.struts.taglib.html.TextTag(); 99 if (type.equals(TEXTAREA)) fieldTag = new org.apache.struts.taglib.html.TextareaTag(); 100 if (type.equals(DATE)) fieldTag = new org.apache.struts.taglib.html.TextTag(); 101 if (type.equals(NUMBER)) fieldTag = new org.apache.struts.taglib.html.TextTag(); 102 if (type.equals(EMAIL)) fieldTag = new org.apache.struts.taglib.html.TextTag(); 103 if (type.equals("hidden")) fieldTag = new org.apache.struts.taglib.html.HiddenTag(); 104 LayoutUtils.copyProperties(fieldTag,this); 106 107 boolean check = isRequired; 109 fieldTag.setOnchange("checkValue(this, '"+ property + "','"+ type +"'," + check +");"); 110 111 if (!access.equals(READONLY)) { 113 fieldTag.doStartTag(); 114 fieldTag.doEndTag(); 115 } 116 117 buffer.setLength(0); 118 Object objvalue = LayoutUtils.getBeanFromPageContext(pageContext, name, property); 119 120 if (!access.equals(READWRITE) && objvalue!=null) { 122 buffer.append("<span class=\""); 123 buffer.append(styleClass); 124 buffer.append("\">"); 125 buffer.append(objvalue.toString()); 126 buffer.append("</span>"); 127 } 128 129 130 if (error!=null && access.equals(READWRITE)) { 132 buffer.append(error); 133 buffer.append("</td></tr></table>"); 134 } 135 136 137 if (objvalue==null) objvalue=""; 139 if (isRequired && objvalue.equals("")) { 140 buffer.append("<img name=\"" + property + "required\" SRC=\""); 141 buffer.append(getSkin().getImageDirectory(pageContext.getRequest())); 142 buffer.append("/ast.gif\">"); 143 } else { 144 buffer.append("<img name=\"" + property + "required\" SRC=\""); 145 buffer.append(getSkin().getImageDirectory(pageContext.getRequest())); 146 buffer.append("/clearpixel.gif\">"); 147 } 148 149 TagUtils.write(pageContext, buffer.toString()); 150 151 return EVAL_BODY_INCLUDE; 152 } 153 public String getAccept() { 154 return accept; 155 } 156 public String getCols() { 157 return cols; 158 } 159 public String getMaxlength() { 160 return maxlength; 161 } 162 public boolean getRedisplay() { 163 return redisplay; 164 } 165 public String getRows() { 166 return rows; 167 } 168 public String getSize() { 169 return cols; 170 } 171 public String getType() { 172 return type; 173 } 174 public String getValue() { 175 return value; 176 } 177 public void release() { 178 super.release(); 179 access = READWRITE; 180 cols = null; 181 isRequired = false; 182 rows = null; 183 type = TEXT; 184 name = Constants.BEAN_KEY; 185 186 if (fieldTag!=null) { 187 fieldTag.release(); 188 fieldTag = null; 189 } 190 191 error = null; 192 } 193 196 protected String retrieveError() throws JspException { 197 ActionErrors errors = (ActionErrors) pageContext.getAttribute(Globals.ERROR_KEY, PageContext.REQUEST_SCOPE); 198 String error = null; 199 if (errors!=null && !errors.isEmpty()) { 200 Iterator it = errors.get(property); 201 if (it.hasNext()) { 202 ActionMessage report = (ActionMessage) it.next(); 203 error = LayoutUtils.getLabel(pageContext, report.getKey(), report.getValues()); 204 } 205 } 206 return error; 207 208 } 209 public void setAccept(String accept) { 210 this.accept = accept; 211 } 212 219 public void setAccess(String newAccess) { 220 access = READWRITE; 221 if (newAccess.equalsIgnoreCase(READONLY)) access = READONLY; 222 if (newAccess.equalsIgnoreCase(READSEND)) access = READSEND; 223 } 224 225 230 public void setCols(String newCols) { 231 cols = newCols; 232 } 233 237 public void setIsRequired(String newIsRequired) { 238 isRequired = newIsRequired.equalsIgnoreCase("true"); 239 } 240 public void setMaxlength(String maxlength) { 241 this.maxlength = maxlength; 242 } 243 public void setRedisplay(boolean redisplay) { 244 this.redisplay = redisplay; 245 } 246 251 public void setRows(String newRows) { 252 rows = newRows; 253 } 254 259 public void setSize(String newSize) { 260 cols = newSize; 261 } 262 272 public void setType(String newType) { 273 if (newType.equalsIgnoreCase(TEXT)) type = TEXT; 274 if (newType.equalsIgnoreCase(TEXTAREA)) type = TEXTAREA; 275 if (newType.equalsIgnoreCase(PASSWORD)) type = PASSWORD; 276 if (newType.equalsIgnoreCase(CHECKBOX)) type = CHECKBOX; 277 if (newType.equalsIgnoreCase(NUMBER)) type = NUMBER; 278 if (newType.equalsIgnoreCase(DATE)) type = DATE; 279 if (newType.equalsIgnoreCase(EMAIL)) type = EMAIL; 280 281 } 282 public void setValue(String value) { 283 this.value = value; 284 } 285 289 public String getAccess() { 290 return access; 291 } 292 293 297 public String getIsRequired() { 298 return isRequired?"true":"false"; 299 } 300 public Object processStartLayoutEvent(StartLayoutEvent in_event) { 301 return Boolean.FALSE; 302 } 303 public Object processEndLayoutEvent(EndLayoutEvent in_event) { 304 return Boolean.FALSE; 305 } 306 307 } 308 | Popular Tags |