1 package com.opensymphony.webwork.components; 2 3 import com.opensymphony.webwork.components.template.Template; 4 import com.opensymphony.webwork.components.template.TemplateEngine; 5 import com.opensymphony.webwork.components.template.TemplateEngineManager; 6 import com.opensymphony.webwork.components.template.TemplateRenderingContext; 7 import com.opensymphony.webwork.config.Configuration; 8 import com.opensymphony.xwork.config.ConfigurationException; 9 import com.opensymphony.xwork.util.OgnlValueStack; 10 import org.apache.commons.logging.Log; 11 import org.apache.commons.logging.LogFactory; 12 13 import javax.servlet.http.HttpServletRequest ; 14 import javax.servlet.http.HttpServletResponse ; 15 import java.io.Writer ; 16 17 22 public abstract class UIBean extends Component { 23 private static final Log LOG = LogFactory.getLog(UIBean.class); 24 25 protected HttpServletRequest request; 26 protected HttpServletResponse response; 27 28 public UIBean(OgnlValueStack stack, HttpServletRequest request, HttpServletResponse response) { 29 super(stack); 30 this.request = request; 31 this.response = response; 32 } 33 34 protected String template; 36 37 protected String templateDir; 39 protected String theme; 40 41 protected String cssClass; 42 protected String cssStyle; 43 protected String disabled; 44 protected String label; 45 protected String labelPosition; 46 protected String name; 47 protected String required; 48 protected String tabindex; 49 protected String value; 50 51 protected String onclick; 53 protected String ondblclick; 54 protected String onmousedown; 55 protected String onmouseup; 56 protected String onmouseover; 57 protected String onmousemove; 58 protected String onmouseout; 59 protected String onfocus; 60 protected String onblur; 61 protected String onkeypress; 62 protected String onkeydown; 63 protected String onkeyup; 64 protected String onselect; 65 protected String onchange; 66 67 public void end(Writer writer) { 68 evaluateParams(); 69 try { 70 mergeTemplate(writer, buildTemplateName(template, getDefaultTemplate())); 71 } catch (Exception e) { 72 e.printStackTrace(); 73 } 74 75 super.end(writer); 76 } 77 78 86 protected abstract String getDefaultTemplate(); 87 88 protected Template buildTemplateName(String myTemplate, String myDefaultTemplate) { 89 String template = myDefaultTemplate; 90 91 if (myTemplate != null) { 92 template = findString(myTemplate); 93 } 94 95 String templateDir = getTemplateDir(); 96 String theme = getTheme(); 97 98 return new Template(templateDir, theme, template); 99 100 } 101 102 protected void mergeTemplate(Writer writer, Template template) throws Exception { 103 final TemplateEngine engine = TemplateEngineManager.getTemplateEngine(template); 104 if (engine == null) { 105 throw new ConfigurationException("Unable to find a TemplateEngine for template " + template); 106 } 107 108 if (LOG.isDebugEnabled()) { 109 LOG.debug("Rendering template " + template); 110 } 111 112 final TemplateRenderingContext context = new TemplateRenderingContext(template, writer, getStack(), getParameters(), this); 113 engine.renderTemplate(context); 114 } 115 116 public String getTemplateDir() { 117 String templateDir = null; 118 119 if (this.templateDir != null) { 120 templateDir = findString(this.templateDir); 121 } 122 123 if ((templateDir == null) || (templateDir.equals(""))) { 126 templateDir = (String ) stack.findValue("#attr.templateDir"); 127 } 128 129 if ((templateDir == null) || (templateDir.equals(""))) { 131 templateDir = Configuration.getString("webwork.ui.templateDir"); 132 } 133 134 if ((templateDir == null) || (templateDir.equals(""))) { 136 templateDir = "template"; 137 } 138 139 return templateDir; 140 } 141 142 public String getTheme() { 143 String theme = null; 144 145 if (this.theme != null) { 146 theme = findString(this.theme); 147 } 148 149 if ((theme == null) || (theme.equals(""))) { 152 theme = (String ) stack.findValue("#attr.theme"); 153 } 154 155 if ((theme == null) || (theme.equals(""))) { 157 theme = Configuration.getString("webwork.ui.theme"); 158 } 159 160 return theme; 161 } 162 163 public void evaluateParams() { 164 addParameter("templateDir", getTemplateDir()); 165 addParameter("theme", getTheme()); 166 167 Object name = null; 168 169 if (this.name != null) { 170 name = findString(this.name); 171 addParameter("name", name); 172 } 173 174 if (label != null) { 175 addParameter("label", findString(label)); 176 } 177 178 if (labelPosition != null) { 179 addParameter("labelposition", findString(labelPosition)); 180 } 181 182 if (required != null) { 183 addParameter("required", findValue(required, Boolean .class)); 184 } 185 186 if (disabled != null) { 187 addParameter("disabled", findValue(disabled, Boolean .class)); 188 } 189 190 if (tabindex != null) { 191 addParameter("tabindex", findString(tabindex)); 192 } 193 194 if (onclick != null) { 195 addParameter("onclick", findString(onclick)); 196 } 197 198 if (ondblclick != null) { 199 addParameter("ondblclick", findString(ondblclick)); 200 } 201 202 if (onmousedown != null) { 203 addParameter("onmousedown", findString(onmousedown)); 204 } 205 206 if (onmouseup != null) { 207 addParameter("onmouseup", findString(onmouseup)); 208 } 209 210 if (onmouseover != null) { 211 addParameter("onmouseover", findString(onmouseover)); 212 } 213 214 if (onmousemove != null) { 215 addParameter("onmousemove", findString(onmousemove)); 216 } 217 218 if (onmouseout != null) { 219 addParameter("onmouseout", findString(onmouseout)); 220 } 221 222 if (onfocus != null) { 223 addParameter("onfocus", findString(onfocus)); 224 } 225 226 if (onblur != null) { 227 addParameter("onblur", findString(onblur)); 228 } 229 230 if (onkeypress != null) { 231 addParameter("onkeypress", findString(onkeypress)); 232 } 233 234 if (onkeydown != null) { 235 addParameter("onkeydown", findString(onkeydown)); 236 } 237 238 if (onkeyup != null) { 239 addParameter("onkeyup", findString(onkeyup)); 240 } 241 242 if (onselect != null) { 243 addParameter("onselect", findString(onselect)); 244 } 245 246 if (onchange != null) { 247 addParameter("onchange", findString(onchange)); 248 } 249 250 if (cssClass != null) { 251 addParameter("cssClass", findString(cssClass)); 252 } 253 254 if (cssStyle != null) { 255 addParameter("cssStyle", findString(cssStyle)); 256 } 257 258 if (evaluateNameValue()) { 259 final Class valueClazz = getValueClassType(); 260 261 if (valueClazz != null) { 262 if (value != null) { 263 addParameter("nameValue", findValue(value, valueClazz)); 264 } else if (name != null) { 265 String expr = name.toString(); 266 if (ALT_SYNTAX) { 267 expr = "%{" + expr + "}"; 268 } 269 270 addParameter("nameValue", findValue(expr, valueClazz)); 271 } 272 } else { 273 if (value != null) { 274 addParameter("nameValue", findValue(value)); 275 } else if (name != null) { 276 addParameter("nameValue", findValue(name.toString())); 277 } 278 } 279 } 280 281 final Form form = (Form) findAncestor(Form.class); 282 283 if (id != null) { 284 addParameter("id", id); 285 } else if (form != null) { 286 addParameter("id", form.getParameters().get("id") + "_" + name); 287 } 288 289 if (form != null) { 290 addParameter("form", form.getParameters()); 291 } 292 293 evaluateExtraParams(); 294 } 295 296 protected void evaluateExtraParams() { 297 } 298 299 protected boolean evaluateNameValue() { 300 return true; 301 } 302 303 protected Class getValueClassType() { 304 return String .class; 305 } 306 307 public void setTheme(String theme) { 308 this.theme = theme; 309 } 310 311 public String getTemplate() { 312 return template; 313 } 314 315 public void setTemplate(String template) { 316 this.template = template; 317 } 318 319 public void setCssClass(String cssClass) { 320 this.cssClass = cssClass; 321 } 322 323 public void setCssStyle(String cssStyle) { 324 this.cssStyle = cssStyle; 325 } 326 327 public void setDisabled(String disabled) { 328 this.disabled = disabled; 329 } 330 331 public void setLabel(String label) { 332 this.label = label; 333 } 334 335 public void setLabelPosition(String labelPosition) { 336 this.labelPosition = labelPosition; 337 } 338 339 public void setName(String name) { 340 this.name = name; 341 } 342 343 public void setRequired(String required) { 344 this.required = required; 345 } 346 347 public void setTabindex(String tabindex) { 348 this.tabindex = tabindex; 349 } 350 351 public void setValue(String value) { 352 this.value = value; 353 } 354 355 public void setOnclick(String onclick) { 356 this.onclick = onclick; 357 } 358 359 public void setOndblclick(String ondblclick) { 360 this.ondblclick = ondblclick; 361 } 362 363 public void setOnmousedown(String onmousedown) { 364 this.onmousedown = onmousedown; 365 } 366 367 public void setOnmouseup(String onmouseup) { 368 this.onmouseup = onmouseup; 369 } 370 371 public void setOnmouseover(String onmouseover) { 372 this.onmouseover = onmouseover; 373 } 374 375 public void setOnmousemove(String onmousemove) { 376 this.onmousemove = onmousemove; 377 } 378 379 public void setOnmouseout(String onmouseout) { 380 this.onmouseout = onmouseout; 381 } 382 383 public void setOnfocus(String onfocus) { 384 this.onfocus = onfocus; 385 } 386 387 public void setOnblur(String onblur) { 388 this.onblur = onblur; 389 } 390 391 public void setOnkeypress(String onkeypress) { 392 this.onkeypress = onkeypress; 393 } 394 395 public void setOnkeydown(String onkeydown) { 396 this.onkeydown = onkeydown; 397 } 398 399 public void setOnkeyup(String onkeyup) { 400 this.onkeyup = onkeyup; 401 } 402 403 public void setOnselect(String onselect) { 404 this.onselect = onselect; 405 } 406 407 public void setOnchange(String onchange) { 408 this.onchange = onchange; 409 } 410 } 411 | Popular Tags |