1 18 package net.sf.uitags.tag.panel; 19 20 import java.util.ArrayList ; 21 import java.util.List ; 22 23 import javax.servlet.jsp.JspException ; 24 import javax.servlet.jsp.PageContext ; 25 26 import net.sf.uitags.tag.AbstractUiTag; 27 import net.sf.uitags.tagutil.AttributeSupport; 28 import net.sf.uitags.tagutil.AttributeSupportHelper; 29 import net.sf.uitags.tagutil.ScopedIdGenerator; 30 import net.sf.uitags.tagutil.TaglibProperties; 31 import net.sf.uitags.util.Template; 32 33 40 public class PanelTag extends AbstractUiTag implements AttributeSupport { 41 42 46 49 private static final long serialVersionUID = 100L; 50 51 54 static final String JS_PREFIX = "uiPanel_"; 55 56 60 private static final String TAG_INSTANCE_ID_KEY = 61 PanelTag.class.getName() + "instanceId"; 62 63 64 68 71 private static final String PROP_CSS = "panel.class"; 72 73 76 private static final String PROP_LISTENER = "panel.listener"; 77 78 79 83 86 private String cssClass; 87 88 91 private String listener; 92 93 96 private Position positioningStrategy = null; 97 98 101 private AttributeSupportHelper attributeHelper; 102 103 107 private List childJsCodeList; 108 109 110 114 119 public void setId(String val) { 120 super.setId(val); 121 } 122 123 124 public String getId() { 125 if (super.getId() == null) { 126 long instanceId = ScopedIdGenerator.nextId( 127 PageContext.REQUEST_SCOPE, TAG_INSTANCE_ID_KEY, this.pageContext) - 1; 128 setId(PanelTag.JS_PREFIX + getTagName() + instanceId); 129 } 130 return super.getId(); 131 } 132 133 138 public void setClass(String val) { 139 this.cssClass = val; 140 } 141 142 147 public void setAnchorTo(String val) { 148 this.positioningStrategy = Position.getFromAnchorId(val); 149 } 150 151 156 public void setAnchorToName(String val) { 157 this.positioningStrategy = Position.getFromAnchorName(val); 158 } 159 160 165 public void setListener(String val) { 166 this.listener = val; 167 } 168 169 170 174 181 public int doStartTag() throws JspException { 182 this.attributeHelper = new AttributeSupportHelper(); 183 this.childJsCodeList = new ArrayList (); 184 185 makeVisibleToChildren(); 186 return EVAL_BODY_BUFFERED; 187 } 188 189 private String getContentAsString() { 190 if (this.bodyContent == null) { 195 return ""; 196 } 197 return this.bodyContent.getString(); 198 } 199 200 208 public int doEndTag() throws JspException { 209 if (this.positioningStrategy != null) { 210 addChildJsCode(this.positioningStrategy.getJsCode()); 211 } 212 213 TaglibProperties props = TaglibProperties.getInstance(); 214 props.setRuntimeProperty(PROP_CSS, this.cssClass); 215 props.setRuntimeProperty(PROP_LISTENER, this.listener); 216 217 Template tpl = Template.forName(Template.PANEL); 218 tpl.map("id", getId()); 219 tpl.map("class", props.get(PROP_CSS)); 220 tpl.map("listener", props.get(PROP_LISTENER)); 221 tpl.map("content", getContentAsString()); 222 tpl.map("jsCodeList", this.childJsCodeList); 223 tpl.map("otherAttributes", this.attributeHelper.eval()); 224 println(tpl.evalToString()); 225 226 setId(null); 228 229 makeInvisibleFromChildren(); 230 return EVAL_PAGE; 231 } 232 233 234 public void addAttribute(String attrName, String attrValue) { 235 this.attributeHelper.addAttribute(attrName, attrValue); 236 } 237 238 public void addChildJsCode(String code) { 239 this.childJsCodeList.add(code); 240 } 241 242 243 247 252 private static class Position { 253 255 final String key; 257 final String value; 258 259 261 264 private Position(String key, String value) { 265 this.key = key; 266 this.value = value; 267 } 268 269 271 String getJsCode() { 272 Template tpl = Template.forName(Template.PANEL_ANCHOR); 273 tpl.map(this.key, this.value); 274 return tpl.evalToString(); 275 } 276 277 static Position getFromAnchorId(String anchorId) { 278 return new Position("anchorId", anchorId); 279 } 280 281 static Position getFromAnchorName(String anchorName) { 282 return new Position("anchorName", anchorName); 283 } 284 } 285 } 286 | Popular Tags |