KickJava   Java API By Example, From Geeks To Geeks.

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


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.*;
13 import org.mmbase.util.logging.*;
14
15 /**
16  * Generates a HTML Element: SELECT.
17  * Uses these variables which are set in the
18  * super class (HTMLElement) to generate HTML:
19  * <ul>
20  * <li>boolean sel : if true it checks if the String selected equals the
21  * current value if equal the HTML tag CHECKED is added
22  * after OPTION.</li>
23  * <li>String selected : see above </li>
24  * <li>boolean ex : if true it checks if the String exclude equals the current
25  * value, if equal this value will be skipped (no HTML
26  * generated for this item)</li>
27  * <li>String exclude : see above</li>
28  * <li>boolean moreValues : if true it will make a list of items.</li>
29  * <li>boolean moredouble : if true it will make a paired list of items.
30  * (first item = VALUE second item=NAME)</li>
31  * <li>Vector valuesList : The list of items. </li>
32  * <li>String size : if not null the HTML tag SIZE=size is added </li>
33  * <li>boolean multiple : if true the HTML tag MULTIPLE is added.</li>
34  * <li>boolean empty : if true an empty option value is added.</li>
35  * </ul>
36  *
37  * @application SCAN
38  * @author Jan van Oosterom
39  * @version $Id: HTMLElementSelect.java,v 1.7 2004/09/29 14:29:24 pierre Exp $
40  */

41
42 public class HTMLElementSelect extends HTMLElement {
43
44     // logger
45
private static Logger log = Logging.getLoggerInstance(HTMLElementSelect.class.getName());
46
47     /**
48      * Creates a HTMLElementSelect.
49      */

50     public HTMLElementSelect() {
51     }
52
53     /**
54      * Generates the HTML code.
55      */

56     protected String JavaDoc generate() {
57         // log.debug("generate");
58
if (selected != null && selected.equals("null")) {
59             sel = false;
60         }
61         if (exclude != null && exclude.equals("null")) {
62             ex = false;
63         }
64         String JavaDoc html = "";
65         if (moreValues) {
66             html += "<select name=\"" + name + "\" ";
67             if (size != null) html += "size=\"" + size+"\"";
68             if (multiple) {
69                 html += " multiple";
70             }
71             html += ">";
72             if (empty) html += "<option></option>";
73
74             String JavaDoc val = null;
75             Vector list = new Vector();
76             Vector vec = valuesList;
77             // log.debug("ServScan->"+vec);
78
if (sorted!=null && (sorted.equals("ALPHA") || sorted.equals("\"ALPHA\""))) {
79                 vec=SortedVector.SortVector(vec);
80             }
81             Enumeration e = vec.elements();
82             int j=0;
83             while (e.hasMoreElements() && ((j++<max)||max==-1)) {
84                 val = (String JavaDoc) e.nextElement();
85                 if (sel && selected.equalsIgnoreCase(val)) {
86                     list.addElement("<option selected>" + val + "</option>\n");
87                 } else if (!ex || (!exclude.equalsIgnoreCase(val))) {
88                     list.addElement("<option>" + val + "</option>\n");
89                 }
90 /*
91                 if (sel) {
92                     if (selected.equalsIgnoreCase(val)) {
93                         list.addElement ("<OPTION SELECTED>" + val + "\n");
94                     } else {
95                         if (ex) {
96                             if (!exclude.equalsIgnoreCase(val)) {
97                                 list.addElement("<OPTION>" + val + "\n");
98                             }
99                         } else {
100                             list.addElement("<OPTION>" + val + "\n");
101                         }
102                     }
103                 } else {
104                     if (ex) {
105                         if (!exclude.equalsIgnoreCase(val))
106                             list.addElement("<OPTION>" + val +"\n");
107                     } else {
108                         list.addElement("<OPTION>" + val +"\n");
109                     }
110                 }
111 */

112             }
113             Enumeration le = list.elements();
114             int i=0;
115             String JavaDoc h = "";
116             while(le.hasMoreElements())
117             {
118                 while( i < 22 && le.hasMoreElements() )
119                 {
120                     h += (String JavaDoc) le.nextElement();
121                     i++;
122                 }
123                 html += h;
124                 h = "";
125                 i = 0;
126             }
127             html += "</select>" ;
128         }
129         else if (moredouble)
130         {
131             //log.debug("moredouble");
132
html += "<select name=\"" + name + "\" ";
133             if (size != null) html += "size=\"" + size+"\"";
134             if (multiple) {
135                 html += " multiple";
136             }
137             html += ">";
138             if (empty) html += "<option></option>";
139
140             String JavaDoc val = null;
141             String JavaDoc val2 = null;
142             Vector list = new Vector();
143             Enumeration e = valuesList.elements();
144             while (e.hasMoreElements()) {
145                 val = (String JavaDoc) e.nextElement();
146                 if (e.hasMoreElements()) {
147                     val2 = (String JavaDoc) e.nextElement();
148                 } else {
149                     log.error("HTMLElementSelect.generate: Expecting a double list (the DOUBLE key word was selected");
150                     return html;
151                 }
152                 if (sel && selected.equalsIgnoreCase(val)) {
153                     list.addElement("<option value=\""+val2+"\" selected>" + val + "</option>\n");
154                 } else if (!ex || (!exclude.equalsIgnoreCase(val))) {
155                     list.addElement("<option value=\""+val2+"\">" + val + "</option>\n");
156                 }
157 /**
158                 if (sel)
159                 {
160                     if (selected.equalsIgnoreCase(val2))
161                     {
162                         list.addElement ("<OPTION VALUE=\"" + val2 + "\" SELECTED>" + val + "\n");
163                     }
164                     else
165                     {
166                         if (ex)
167                         {
168                             if (!exclude.equalsIgnoreCase(val2))
169                                 list.addElement("<OPTION VALUE=\"" + val2 + "\">" + val + "\n");
170                         }
171                         else
172                         {
173                                 list.addElement("<OPTION VALUE=\"" + val2 + "\">" + val + "\n");
174                         }
175                     }
176                 }
177                 else
178                 {
179                     if (ex)
180                     {
181                         if (!exclude.equalsIgnoreCase(val2))
182                             list.addElement("<OPTION VALUE=\"" + val2 + "\">" + val +"\n");
183                     }
184                     else
185                     {
186                         list.addElement("<OPTION VALUE=\"" + val2 + "\">" + val +"\n");
187                     }
188                 }
189 */

190             }
191             //log.debug("after");
192
Enumeration le = list.elements();
193             int i=0;
194             String JavaDoc h = "";
195             while(le.hasMoreElements()) {
196                 while( i < 22 && le.hasMoreElements()) {
197                     h += (String JavaDoc) le.nextElement();
198                     i++;
199                 }
200                 html += h;
201                 h = "";
202                 i = 0;
203             }
204             html += "</select>" ;
205         } else {
206             html += "<select name=\"" + name + "\" ";
207             if (size != null) html += "size=\"" + size+"\"";
208             if (multiple) {
209                 html += " multiple";
210             }
211             html += ">";
212             html += "<option ";
213             if (sel) html += " selected";
214             html += ">" + values + "</option></select>" ;
215         }
216         return html;
217     }
218 }
219
Popular Tags