KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > util > HTMLFormGenerator


1 /*
2
3 This software is OSI Certified Open Source Software.
4 OSI Certified is a certification mark of the Open Source Initiative.
5
6 The license (Mozilla version 1.0) can be read at the MMBase site.
7 See http://www.MMBase.org/license
8
9 */

10 package org.mmbase.util;
11
12 import java.util.Enumeration JavaDoc;
13 import java.util.Vector JavaDoc;
14
15 import org.mmbase.module.ProcessorInterface;
16 import org.mmbase.util.logging.Logger;
17 import org.mmbase.util.logging.Logging;
18
19 /**
20  * Generates from the MACRO Strings a HTML FORM.
21  *<br />
22  * <strong>input</strong> Macro Vector of Strings, Proc Processor.<br />
23  * <strong>output</strong> HTML FORM Element(String).<br />
24  * <p>
25  * How should a macro look like in your HTML file?<br />
26  * &lt;MACRO ELEMENT NAME VALUE [TAG(S)]&gt;<br />
27  * The "MACRO" word will be stripped in de servscan Servlet and does not enters this object.<br />
28  * ELEMENT: (if you are fimiliar with HTML FORMs this should be known stuff <br />
29  * TEXTAREA, SELECT, PASSWORD, RADIO, CHECKBOX <br />
30  * NAME:<br />
31  * The name of this HTML Element <br />
32  * VALUE:<br />
33  * This can do 2 things: <br />
34  * 1. if the PROC TAG is present, this value will be sent to the processor <br />
35  * 2. else ( no PROC TAG) this will be the VALUE of the HTML Element <br />
36  * TAG(S) <br />
37  * there can be more TAGS (some TYPES does support them and some don't ...)<br />
38  * some need an argument some don't (some can handle both :-)<br />
39  * <pre>
40  * TAG WHAT DOES IT? SUPPORTED BY WHICH ELEMENT?
41  * <hr />
42  * ROWS= The number of ROWS TEXTAREA
43  * COLS= The number of COLS TEXTAREA
44  * SIZE= The SIZE of this ELEMENT SELECT TEXT PASSWORD
45  * MULTIPLE We want MULTIPLE selection SELECT
46  * CHECKED(=)/SELECTED(=) The SELECTED element (if there is no PROC tag
47  * you don't need an argument) SELECT RADIO CHECKBOX
48  * EXCLUDE= The EXCLUDED element (only use with PROC) SELECT RADIO CHECKBOX
49  * VERTICAL Add a &lt;BR&gt; after a item SELECT RADIO CHECKBOX
50  * HORIZONTAL Don't add a &lt;BR&gt; after a item SELECT RADIO CHECKBOX
51  * EMPTY Add an EMPTY element SELECT
52  * PROC Use the processor to get a list (Vector) of values (Strings) TEXTAREA TEXT and PASSWORD (only the first of the Vector will be used) SELECT RADIO CHECKBOX
53  * DOUBLE Tell the processor to get a paired list of values SELECT RADIO CHECKBOX
54  * </pre>
55  *
56  * <strong>example</strong>: SELECT name ProcessorTag SELECTED="selected_item" MULTIPLE SIZE=10 PROC VERTICAL.<br />
57  * This generates a SELECT HTML FORM with NAME=name the OPTIONs are filled with<br />
58  * the list which is received from the Processor.getList("ProcessorTag") call<br />
59  * If the "selected_item" is presend in the list it is "&lt;OPTION SELECTED&gt;" item" <br />
60  * The SIZE is the number of displayed items and MULTIPLE tells that multiple <br />
61  * selections are posible. <br />
62  * VERTICAL tells that you want a &lt;BR&gt; after every &lt;OPTION&gt;<br />
63  * <br /> <strong> Generated</strong> from <strong>example</strong>:<br />
64  * &lt;SELECT NAME=name SIZE=10 MULTIPLE&gt; <br />
65  * &lt;OPTION&gt; item1 &lt;BR&gt;<br />
66  * &lt;OPTION SELECTED&gt; sleceted_item &lt;BR&gt;<br />
67  * &lt;OPTION&gt; item3 &lt;BR&gt;<br />
68  * &lt;/SELECT&gt; <br />
69  *<P>
70  *
71  *
72  * @application SCAN
73  * @author Jan van Oosterom
74  * @version $Id: HTMLFormGenerator.java,v 1.9 2004/09/29 14:29:25 pierre Exp $
75  */

76 public class HTMLFormGenerator {
77     // logger
78
private static Logger log = Logging.getLoggerInstance(HTMLFormGenerator.class.getName());
79
80     /**
81     * TEXTAREA Element
82     */

83     protected HTMLElementTextArea textArea;
84
85     /**
86     * RADIO Element
87     */

88     protected HTMLElementRadio radio;
89     /**
90     * SELECT Element
91     */

92     protected HTMLElementSelect select;
93     /**
94      * CHECKBOX Element
95     */

96     protected HTMLElementCheckBox checkBox;
97     /**
98     * TEXT
99     */

100     protected HTMLElementText text;
101     /**
102     * PASSWORD
103     */

104     protected HTMLElementPassword password;
105
106     /**
107     * Contructs the HTMLElements
108     */

109     public HTMLFormGenerator() {
110         textArea = new HTMLElementTextArea();
111         radio = new HTMLElementRadio();
112         select = new HTMLElementSelect();
113         checkBox = new HTMLElementCheckBox();
114         text = new HTMLElementText();
115         password = new HTMLElementPassword();
116     }
117
118     /**
119     * Gets the first element of the Vector and selects the correspondending HTMLElement
120     * to handle this element, and passes the tail elements to that Element.
121     * <br />
122     * Output String: HTML FORM Element (TEXTAREA, RADIO, SELECT, CHECKBOX, TEXT or PASSWORD).
123     * @param proc The Processor to handle the getList (2nd Element from the Vector marco)
124     * @param macro The Vector with Strings
125     */

126     public String JavaDoc getHTMLElement (scanpage sp,ProcessorInterface proc, Vector JavaDoc macro) {
127         String JavaDoc type = getFirstElement(macro);
128         Vector JavaDoc params = getTailElements(macro);
129
130         if (type.equalsIgnoreCase("TEXTAREA")) {
131             // we want a TEXTAREA .....
132
return textArea.generateHTML(sp,proc,params);
133         }
134
135         if (type.equalsIgnoreCase("RADIO")) {
136             //We want a RADIO ......
137
String JavaDoc radioHTML = radio.generateHTML(sp,proc,params) ;
138             return radioHTML;
139         }
140
141         if (type.equalsIgnoreCase("SELECT")) {
142             //We want a SELECT ......
143
String JavaDoc selectHTML = select.generateHTML(sp,proc,params) ;
144             return selectHTML;
145         }
146
147         if (type.equalsIgnoreCase("CHECKBOX")) {
148             //We want a CHECKBOX......
149
String JavaDoc checkBoxHTML = checkBox.generateHTML(sp,proc,params) ;
150             return checkBoxHTML;
151         }
152
153         if (type.equalsIgnoreCase("TEXT")) {
154             //We want a TEXT input......
155
String JavaDoc textHTML = text.generateHTML(sp,proc,params) ;
156             return textHTML;
157         }
158
159         if (type.equalsIgnoreCase("PASSWORD")) {
160             //We want a PASSWORD input......
161
String JavaDoc passwordHTML = password.generateHTML(sp,proc,params) ;
162             return passwordHTML;
163         }
164         log.error("HTMLFormGenerator: Unknown HTML type re quested: " + type);
165         return null;
166     }
167
168     /*
169     * Returns the tail elements from the vector. (All but the first).
170     */

171     protected Vector JavaDoc getTailElements(Vector JavaDoc vector) {
172         Enumeration JavaDoc e = vector.elements();
173         if (e.hasMoreElements()) {
174             //We don't want the first one ....
175
// Object dummy =
176
e.nextElement();
177
178             Vector JavaDoc tailToReturn = new Vector JavaDoc();
179             //We only want the tail
180

181             while(e.hasMoreElements()) {
182                 tailToReturn.addElement(e.nextElement());
183             }
184             return tailToReturn;
185         } else {
186             log.error("Empty Vector in HTMLFormGenerator");
187             return null;
188         }
189
190     }
191     /*
192     * Returns the first element of the vector
193     */

194     protected String JavaDoc getFirstElement(Vector JavaDoc vector) {
195         return (String JavaDoc) vector.elementAt(0);
196     }
197 }
198
Popular Tags