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.NSArray;
22  import net.wotonomy.foundation.NSDictionary;
23  
24  /*** 
25  * Implements a FORM element with dynamic bindings.
26  * @author michael@mpowers.net
27  * @author $Author: cgruber $
28  * @version $Revision: 905 $
29  */
30  public class WOForm extends WODynamicElement {
31  
32      public WOForm() {
33          super();
34      }
35  
36      public WOForm(String n, NSDictionary m, WOElement t) {
37          super(n, m, t);
38      }
39  
40      public String href(WOContext c) {
41          return (String)valueForProperty("href", c.component());
42      }
43  
44      public String directActionName(WOContext c) {
45          return (String)valueForProperty("directAction", c.component());
46      }
47  
48      public String directActionClass(WOContext c) {
49          return (String)valueForProperty("actionClass", c.component());
50      }
51  
52      public boolean multipleSubmit(WOContext c) {
53          return booleanForProperty("multipleSubmit", c.component());
54      }
55  
56      public void takeValuesFromRequest (
57      	WORequest aRequest, WOContext aContext)
58      {
59          if ( rootElement != null )
60          {
61              rootElement.takeValuesFromRequest( aRequest, aContext );
62          }
63      }
64          
65      public void appendToResponse(WOResponse r, WOContext c) {
66          //Append the opening tag
67          r.appendContentString("<FORM");
68          String link = href(c);
69          //Append the href, if present
70          if (link != null) {
71              r.appendContentString(" HREF=\"");
72              r.appendContentString(link);
73              r.appendContentString("\"");
74              link = null;
75          } else {
76              link = directActionName(c);
77          }
78  
79          //otherwise, append Direct Action
80          if (link != null) {
81              r.appendContentString(" HREF=\"");
82              if (directActionClass(c) != null)
83                  link = directActionClass(c) + "/" + link;
84              r.appendContentString(c.directActionURLForActionNamed(link, urlFields(c.component())));
85              r.appendContentString("\"");
86              link = null;
87  //        } else if (associations.objectForKey("action") != null) {
88          } else {
89              //finally, append action
90              r.appendContentString(" action=\"");
91              r.appendContentString(c.componentActionURL());
92              r.appendContentString("\"");
93          } //else
94  // now defaulting to action if not specified(?): WOBuilder does generate WOForms without any bindings(!)        
95  //            throw new IllegalArgumentException("You must use one of directActionName, action, or href.");
96  
97          //Append any additional properties
98          link = additionalHTMLProperties(c.component(), new NSArray(new Object[]{
99              "href", "action", "directActionName", "actionClass", "multipleSubmit" }));
100         if (link.length() > 0)
101             r.appendContentString(link);
102         r.appendContentString(">");
103         //Notify that we're inside a form now
104         c.setInForm(true);
105         //Append the inner template
106         rootElement.appendToResponse(r, c);
107         //Close the tag
108         r.appendContentString("</FORM>");
109 //        c.deleteLastElementIDComponent();
110         c.setInForm(false);
111     }
112 
113     public WOActionResults invokeAction(WORequest r, WOContext c) {
114         //We only process the request if it's not a multipleSubmit (otherwise we leave it to the buttons)
115         if (!multipleSubmit(c) 
116             && c.elementID().equals(c.senderID()) 
117             && associations.objectForKey("action") != null ) {
118             return (WOActionResults)valueForProperty("action", c.component());
119         }
120         WOActionResults res = rootElement.invokeAction(r, c);
121         return res;
122     }
123 
124 }