| 1 package fr.improve.struts.taglib.layout.collection; 2 3 import javax.servlet.jsp.JspException ; 4 5 import org.apache.struts.taglib.html.Constants; 6 7 import fr.improve.struts.taglib.layout.util.LayoutUtils; 8 12 public class CollectionStyleTag extends javax.servlet.jsp.tagext.TagSupport { 13 protected String name = Constants.BEAN_KEY; 14 protected String property; 15 protected String value; 16 protected String valueName; 17 protected String valueProperty; 18 protected String matchStyleClass; 19 protected String nonMatchStyleClass; 20 protected String matchStyle; 21 protected String nonMatchStyle; 22 23 protected String style; 24 protected String styleClass; 25 26 protected BaseCollectionTag collectionTag; 27 28 protected boolean notNull = false; 29 protected String greaterThan; 30 protected String lessThan; 31 32 public int doEndTag() { 33 if (styleClass!=null) { 34 collectionTag.setTempStyleClass(null); 35 styleClass = null; 36 } 37 if (style!=null) { 38 collectionTag.removeTempStyle(); 39 style = null; 40 } 41 collectionTag = null; 42 return EVAL_PAGE; 43 } 44 public int doStartTag() throws JspException { 45 try { 46 collectionTag = (BaseCollectionTag) findAncestorWithClass(this, BaseCollectionTag.class); 48 if (collectionTag==null) throw new JspException ("Invalid use of collectionStyle tag"); 49 if (collectionTag.first) return EVAL_BODY_INCLUDE; 50 51 if (checkCondition()) { 53 styleClass = matchStyleClass; 54 style = matchStyle; 55 } else { 56 styleClass = nonMatchStyleClass; 57 style = nonMatchStyle; 58 } 59 60 if (styleClass!=null) { 62 collectionTag.setTempStyleClass(styleClass); 63 } 64 if (style!=null) { 65 collectionTag.addTempStyle(style); 66 } 67 68 } catch (ClassCastException e) { 69 throw new JspException ("Invalid use of collectionStyle tag"); 70 } 71 72 return EVAL_BODY_INCLUDE; 73 } 74 75 protected boolean checkCondition() throws JspException { 76 Object lc_value = pageContext.findAttribute(name); 77 if (lc_value!=null) { 78 lc_value = LayoutUtils.getProperty(lc_value, property); 79 } 80 81 if (greaterThan!=null) { 82 return checkGreaterThan(lc_value); 83 }else if (lessThan!=null) { 84 return checkLessThan(lc_value); 85 } else if (notNull) { 86 return lc_value!=null; 87 } else { 88 89 Object lc_valueToCheck = null; 90 if (valueName!=null) { 91 lc_valueToCheck = pageContext.findAttribute(valueName); 92 if (lc_valueToCheck!=null) lc_valueToCheck = LayoutUtils.getProperty(lc_valueToCheck, valueProperty); 93 } else { 94 lc_valueToCheck = value; 95 } 96 97 if (lc_valueToCheck!=null && lc_value!=null && lc_valueToCheck.toString().equals(lc_value.toString())) { 98 return true; 99 } else { 100 return false; 101 } 102 } 103 } 104 105 private boolean checkLessThan(Object in_value) { 106 return checkGreaterThan(lessThan, in_value); 107 } 108 109 private boolean checkGreaterThan(Object in_value) { 110 return checkGreaterThan(in_value, greaterThan); 111 } 112 113 private boolean checkGreaterThan(Object in_value, Object greaterThan) { 114 if (in_value==null || greaterThan==null) { 115 return false; 116 } 117 118 Number n1; 119 Number n2; 120 if (in_value instanceof Number ) { 121 n1 = (Number ) in_value; 122 } else { 123 n1 = new Float (in_value.toString()); 124 } 125 if (greaterThan instanceof Number ) { 126 n2 = (Number ) greaterThan; 127 } else { 128 n2 = new Float (greaterThan.toString()); 129 } 130 return n1.doubleValue() >= n2.doubleValue(); 131 } 132 137 public java.lang.String getMatchStyleClass() { 138 return matchStyleClass; 139 } 140 145 public java.lang.String getName() { 146 return name; 147 } 148 153 public java.lang.String getNonMatchStyleClass() { 154 return nonMatchStyleClass; 155 } 156 161 public java.lang.String getProperty() { 162 return property; 163 } 164 169 public java.lang.String getValue() { 170 return value; 171 } 172 177 public java.lang.String getValueName() { 178 return valueName; 179 } 180 185 public java.lang.String getValueProperty() { 186 return valueProperty; 187 } 188 public void release() { 189 super.release(); 190 name = Constants.BEAN_KEY; 191 property = null; 192 value = null; 193 valueName = null; 194 valueProperty = null; 195 matchStyleClass = null; 196 nonMatchStyleClass = null; 197 matchStyle = null; 198 nonMatchStyle = null; 199 collectionTag = null; 200 201 greaterThan = null; 202 lessThan = null; 203 } 204 209 public void setMatchStyleClass(java.lang.String newMatchStyleClass) { 210 matchStyleClass = newMatchStyleClass; 211 } 212 217 public void setName(java.lang.String newName) { 218 name = newName; 219 } 220 225 public void setNonMatchStyleClass(java.lang.String newNonMatchStyleClass) { 226 nonMatchStyleClass = newNonMatchStyleClass; 227 } 228 233 public void setProperty(java.lang.String newProperty) { 234 property = newProperty; 235 } 236 241 public void setValue(java.lang.String newValue) { 242 value = newValue; 243 } 244 249 public void setValueName(java.lang.String newValueName) { 250 valueName = newValueName; 251 } 252 257 public void setValueProperty(java.lang.String newValueProperty) { 258 valueProperty = newValueProperty; 259 } 260 264 public void setMatchStyle(String matchStyle) { 265 this.matchStyle = matchStyle; 266 } 267 268 272 public void setNonMatchStyle(String nonMatchStyle) { 273 this.nonMatchStyle = nonMatchStyle; 274 } 275 276 public void setPresent(boolean in_bool) { 277 notNull = in_bool; 278 } 279 280 public void setGreaterThan(String in_greater) { 281 greaterThan = in_greater; 282 } 283 284 public void setLessThan(String in_less){ 285 lessThan = in_less; 286 } 287 288 } 289 | Popular Tags |