00001 import java.util.Map;
00002 import java.util.HashMap;
00003 import java.util.Set;
00004 import java.util.Iterator;
00005 import java.sql.SQLException;
00006
00007 public class SelectBox extends Widget
00008 {
00009 public int rows;
00010 public String text;
00011 public boolean multi;
00012
00013 Integer id;
00014 Map ids = new HashMap();
00015
00016 public SelectBox(boolean multi, int rows, String prefix, Form form)
00017 {
00018 super(prefix, form);
00019 this.rows = rows;
00020 this.multi = multi;
00021 }
00022
00023 public void loadValues()
00024 {
00025 if (multi)
00026 {
00027 String[] v = form.req.getParameterValues(prefix);
00028 if (v == null) return;
00029 int vl = v.length;
00030 for(int i = 0; i < vl; ++i)
00031 {
00032 int n = toInt(v[i],0);
00033 if (n > 0) ids.put(new Integer(n), null);
00034 }
00035 }
00036 else
00037 {
00038 String s = loadAttribute();
00039 if (s != null) id = new Integer(toInt(s,0));
00040 }
00041 }
00042
00043 public void display(boolean hidden)
00044 {
00045 display(hidden, null);
00046 }
00047
00048 public void display(boolean hidden, String query)
00049 {
00050 if (hidden)
00051 {
00052 if (multi)
00053 {
00054 Set s = ids.keySet();
00055 Iterator i = s.iterator();
00056 while(i.hasNext())
00057 {
00058 Integer n = (Integer) i.next();
00059 printAttribute(null, n.toString());
00060 }
00061 }
00062 else
00063 {
00064 if (id != null)
00065 printAttribute(null, id.toString());
00066 }
00067 return;
00068 }
00069
00070 p("<select name=" + prefix + " rows=" + rows + (multi ? " multiple" : "") + ">\n");
00071 if (query != null)
00072 {
00073 form.connectDb();
00074 Query q = new Query(form.conn, form.pw, query);
00075 try
00076 {
00077 if (q.r != null)
00078 while (q.r.next())
00079 {
00080 int rid = q.r.getInt(1);
00081 String rname = q.r.getString(2);
00082 String s = (multi && ids.containsKey(new Integer(rid)))
00083 || (!multi && id != null && id.intValue() == rid) ?
00084 " selected" : "";
00085 p(" <option value=" + rid + s + ">" + htmlencode(rname) + "</option>\n");
00086 }
00087 }
00088 catch (SQLException e)
00089 {
00090 p(e);
00091 }
00092 finally
00093 {
00094 q.close();
00095 }
00096 }
00097 p("</select>\n");
00098 }
00099 };