View Javadoc

1   /*
2    Wotonomy: OpenStep design patterns for pure Java applications.
3    Copyright (C) 2000 Blacksmith, Inc.
4   
5    This library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Lesser General Public
7    License as published by the Free Software Foundation; either
8    version 2.1 of the License, or (at your option) any later version.
9   
10   This library is distributed in the hope that it will be useful,
11   but WITHOUT ANY WARRANTY; without even the implied warranty of
12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13   Lesser General Public License for more details.
14  
15   You should have received a copy of the GNU Lesser General Public
16   License along with this library; if not, see http://www.gnu.org
17   */
18  
19  package net.wotonomy.web;
20  
21  import net.wotonomy.foundation.NSDictionary;
22  
23  /***
24  * WOConditional renders whatever is inside its opening and closing tags
25   * only if a condition is met.
26   * Bindings are:
27   * <ul>
28   * <li>condition: a boolean property that indicates whether the contents of the element
29   * should be displayed, invoked, or passed the request form values.</li>
30   * <li>negate: if this is true, then the behavior of the element is reversed, showing its
31   * contents only if the condition is NOT met.</li>
32   * </ul>
33   *
34   * @author ezamudio@nasoft.com
35   * @author $Author: cgruber $
36   * @version $Revision: 893 $
37   */
38  public class WOConditional extends WODynamicElement {
39  
40      public boolean condition;
41      public boolean negate;
42  
43      protected WOConditional() {
44          super();
45      }
46  
47      public WOConditional(String aName, NSDictionary aMap, WOElement template) {
48          super(aName, aMap, template);
49      }
50  
51      public void setCondition(boolean value) {
52          condition = value;
53      }
54      public boolean condition() {
55          return condition;
56      }
57  
58      public void setNegate(boolean value) {
59          negate = value;
60      }
61      public boolean negate() {
62          return negate;
63      }
64  
65      protected void pullValuesFromParent(WOComponent c) {
66          condition = booleanForProperty("condition", c);
67          negate    = booleanForProperty("negate", c);
68      }
69  
70      public void takeValuesFromRequest(WORequest aRequest, WOContext aContext) {
71          if (rootElement == null)
72              return;
73          pullValuesFromParent(aContext.component());
74          if ((condition && !negate) || (!condition && negate)) {
75              rootElement.takeValuesFromRequest(aRequest, aContext);
76          }
77      }
78  
79      public WOActionResults invokeAction(WORequest aRequest, WOContext aContext) {
80          if (rootElement == null)
81              return null;
82          pullValuesFromParent(aContext.component());
83          WOActionResults el = null;
84          if ((condition && !negate) || (!condition && negate)) {
85              el = rootElement.invokeAction(aRequest, aContext);
86          }
87          return el;
88      }
89  
90      public void appendToResponse(WOResponse aResponse, WOContext aContext) {
91          if (rootElement == null)
92              return;
93          pullValuesFromParent(aContext.component());
94          if ((condition && !negate) || (!condition && negate)) {
95              rootElement.appendToResponse(aResponse, aContext);
96          }
97      }
98  
99  }