01: /* If.java
02:
03: {{IS_NOTE
04: Purpose:
05:
06: Description:
07:
08: History:
09: Tue Sep 6 15:33:06 2005, Created by tomyeh
10: }}IS_NOTE
11:
12: Copyright (C) 2005 Potix Corporation. All Rights Reserved.
13:
14: {{IS_RIGHT
15: This program is distributed under GPL Version 2.0 in the hope that
16: it will be useful, but WITHOUT ANY WARRANTY.
17: }}IS_RIGHT
18: */
19: package org.zkoss.web.servlet.dsp.action;
20:
21: import java.io.IOException;
22: import java.io.StringWriter;
23:
24: import org.zkoss.web.mesg.MWeb;
25: import org.zkoss.web.servlet.ServletException;
26:
27: /**
28: * Tests whether an condition is true and render the child only
29: * if the condition is true.
30: *
31: * @author tomyeh
32: */
33: public class If extends AbstractAction {
34: private boolean _cond, _trim = true;
35:
36: /** Returns the test result. */
37: public boolean getTest() {
38: return _cond;
39: }
40:
41: /** Sets the test result. */
42: public void setTest(boolean cond) {
43: _cond = cond;
44: }
45:
46: /** Returns whether to trim the result.
47: * <p>Default: true.
48: */
49: public boolean isTrim() {
50: return _trim;
51: }
52:
53: /** Sets whether to trim the result. */
54: public void setTrim(boolean trim) {
55: _trim = trim;
56: }
57:
58: //-- Action --//
59: public void render(ActionContext ac, boolean nested)
60: throws javax.servlet.ServletException, IOException {
61: if (nested && _cond && isEffective()) {
62: if (_trim) {
63: final StringWriter sw = new StringWriter();
64: ac.renderFragment(sw);
65: ac.getOut().write(sw.toString().trim());
66: } else {
67: ac.renderFragment(null);
68: }
69: }
70: }
71:
72: //-- Object --//
73: public String toString() {
74: return "if";
75: }
76: }
|