1 7 package fr.improve.struts.taglib.layout.collection; 8 9 import java.util.HashMap ; 10 import java.util.Map ; 11 12 import javax.servlet.jsp.JspException ; 13 import javax.servlet.jsp.tagext.TagSupport ; 14 15 import fr.improve.struts.taglib.layout.util.LayoutUtils; 16 import fr.improve.struts.taglib.layout.util.TagUtils; 17 18 24 public class CollectionDetailTag extends TagSupport { 25 protected String name; 26 protected String property; 27 protected boolean skip = false; 28 protected BaseCollectionTag collectionTag; 29 30 protected static final String DETAIL_KEY = "fr.improve.struts.taglib.layout.collection.CollectionDetailTag.DETAIL_KEY"; 31 32 public int doStartTag() throws JspException { 33 try { 34 collectionTag = 35 (BaseCollectionTag) findAncestorWithClass(this, BaseCollectionTag.class); 36 if (collectionTag.getId()==null) { 37 throw new JspException ("Invalid use of collectionDetail tag: parent collection tag attribute 'id' should be set"); 38 } 39 if (collectionTag.getIndexId()==null) { 40 throw new JspException ("Invalid use of collectionDetail tag: parent collection tag attribute 'indexId' should be set"); 41 } 42 if (collectionTag.isFirst()) { 43 return SKIP_BODY; 44 } 45 if (name!=null) { 46 if (!collectionTag.getSpans().containsKey(name)) { 47 skip = true; 48 return SKIP_BODY; 49 } else { 50 pageContext.setAttribute(CollectionItemTag.SPAN_KEY, collectionTag.getSpans().get(name)); 51 } 52 } 53 54 } catch (ClassCastException e) { 55 throw new JspException ("Invalid use of collectionDetail tag"); 56 } catch (NullPointerException e) { 57 throw new JspException ("Invalid use of collectionDetail tag"); 58 } 59 return SKIP_BODY; 60 } 61 62 public int doEndTag() throws JspException { 63 if (collectionTag.isFirst()) { 64 Map lc_map = getDetailInfo(); 65 if (!lc_map.containsKey(collectionTag.getId())) { 66 lc_map.put(collectionTag.getId(), new Integer (-1)); 68 69 renderInit(collectionTag.getId()); 71 72 String onover = collectionTag.getOnRowMouseOver(); 74 String onout = collectionTag.getOnRowMouseOut(); 75 collectionTag.setOnRowMouseOver("showDetail(" + collectionTag.getId() + ",${" + collectionTag.getIndexId() + "});" + onover); 76 collectionTag.setOnRowMouseOut("clearDetail(" + collectionTag.getId() + ");" + onout); 77 } 78 return EVAL_PAGE; 79 } 80 if (skip) { 81 skip = false; 82 collectionTag.incrementColumn(); 83 return EVAL_PAGE; 84 } 85 86 Map lc_map = getDetailInfo(); 87 Integer lc_int = (Integer ) lc_map.get(collectionTag.getId()); 88 if (lc_int.intValue()< collectionTag.getIndex()) { 89 lc_map.put(collectionTag.getId(), new Integer (collectionTag.getIndex())); 90 renderNext(collectionTag.getId(), collectionTag.getIndex()); 91 } 92 93 Object lc_value = buildContent(); 94 renderDetail(collectionTag.getId(), property, collectionTag.getIndex(), lc_value==null ? "" : lc_value.toString()); 95 96 reset(); 97 return EVAL_PAGE; 98 } 99 100 private Map getDetailInfo() { 101 Map lc_map = (Map ) pageContext.getAttribute(DETAIL_KEY); 102 if (lc_map==null) { 103 lc_map = new HashMap (); 104 pageContext.setAttribute(DETAIL_KEY, lc_map); 105 } 106 return lc_map; 107 } 108 109 112 protected Object buildContent() throws JspException { 113 Object lc_cell = null; 114 if (name == null) { 115 lc_cell = collectionTag.getBean(); 117 } else { 118 lc_cell = pageContext.findAttribute(name); 120 } 121 if (lc_cell != null && property != null) { 122 lc_cell = getPropertyValue(lc_cell, property); 124 } 125 return lc_cell; 126 } 127 128 132 protected Object getPropertyValue(Object in_bean, String in_property) throws JspException { 133 return LayoutUtils.getProperty(in_bean, in_property); 134 } 135 136 142 protected void renderDetail(String in_name, String in_property, int in_index, String in_value) throws JspException { 143 TagUtils.write(pageContext, "<script>"); 144 TagUtils.write(pageContext, in_name); 145 TagUtils.write(pageContext, "["); 146 TagUtils.write(pageContext, String.valueOf(in_index)); 147 TagUtils.write(pageContext, "]['"); 148 TagUtils.write(pageContext, in_property); 149 TagUtils.write(pageContext, "'] = \""); 150 TagUtils.write(pageContext, filterDoubleQuotes(in_value)); 151 TagUtils.write(pageContext, "\"; "); 152 TagUtils.write(pageContext, "</script>\n"); 153 } 154 155 158 protected String filterDoubleQuotes(String in_value) { 159 StringBuffer sb = new StringBuffer (in_value.length()); 160 int i =0; 161 char c; 162 for (i = 0; i < in_value.length(); i++) { 163 c = in_value.charAt(i); 164 if (c=='"') { 165 sb.append('\"'); 166 } else { 167 sb.append(c); 168 } 169 } 170 return sb.toString(); 171 } 172 173 174 177 public void renderInit(String in_name) throws JspException { 178 TagUtils.write(pageContext, "<script>var "); 179 TagUtils.write(pageContext, in_name); 180 TagUtils.write(pageContext, " = new Array();"); 181 TagUtils.write(pageContext, "</script>\n"); 182 } 183 184 public void renderNext(String in_name, int in_index) throws JspException { 185 TagUtils.write(pageContext, "<script>"); 186 TagUtils.write(pageContext, in_name); 187 TagUtils.write(pageContext, "["); 188 TagUtils.write(pageContext, String.valueOf(in_index)); 189 TagUtils.write(pageContext, "] = new Object();"); 190 TagUtils.write(pageContext, "</script>\n"); 191 } 192 193 protected void reset() { 194 collectionTag = null; 195 skip = false; 196 } 197 198 public void release() { 199 property = null; 200 } 201 204 public String getName() { 205 return name; 206 } 207 210 public void setName(String name) { 211 this.name = name; 212 } 213 216 public String getProperty() { 217 return property; 218 } 219 222 public void setProperty(String property) { 223 this.property = property; 224 } 225 } 226 | Popular Tags |