Main Page | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | File Members

TableEditor Class Reference

Inheritance diagram for TableEditor:

Widget List of all members.

Public Member Functions

 TableEditor (String prefix, Form form)
void loadValues ()
boolean saveRow ()
void deleteRow (int row)
void display (boolean hidden)

Public Attributes

int tableIdx = 0
int oid = 0
ActionButton action
TextBox[] fields
String message = ""

Static Public Attributes

static final int CHOOSE = 1
static final int EDIT = 2
static final int DELETE = 3
static final int SAVE = 4

Static Package Functions

 [static initializer]

Static Package Attributes

static Table[] tables = new Table[10]

Classes

class  Table

Detailed Description

Definition at line 5 of file TableEditor.java.


Constructor & Destructor Documentation

TableEditor::TableEditor String  prefix,
Form  form
[inline]
 

Definition at line 105 of file TableEditor.java.

References action, Widget::addChild(), and Widget::n().

00106         {
00107           super(prefix, form);
00108           action = new ActionButton(n("action"), form);
00109           addChild(action);
00110         }


Member Function Documentation

TableEditor::[static initializer]  )  [inline, static, package]
 

void TableEditor::deleteRow int  row  )  [inline]
 

Definition at line 199 of file TableEditor.java.

References Form::conn, Form::connectDb(), TableEditor::Table::delete, Form::disconnectDb(), Widget::form, message, tableIdx, and tables.

00200         {
00201           Table t = tables[tableIdx];
00202           form.connectDb();
00203           try
00204           {
00205             PreparedStatement s = form.conn.prepareStatement(t.delete);
00206             try
00207             {
00208               s.setInt(1,row);
00209               s.executeUpdate();
00210             }
00211             finally
00212             {
00213               s.close();
00214             }
00215           }
00216           catch (SQLException e)
00217           {
00218             message = "<p><b><font color=red>Unable to delete row. Postgres gives this error message:</font></b></p>\n"
00219               + "<blockquote style=\"background-color: #EEEEEE\">" + e.toString() + "</blockquote>\n";
00220             form.disconnectDb(); // XXX: see above
00221             return;
00222           }
00223           message = "<p><b><font color=blue>Row deleted successfully</font></b></p>\n";
00224         }

void TableEditor::display boolean  hidden  )  [inline]
 

Reimplemented from Widget.

Definition at line 226 of file TableEditor.java.

References action, TableEditor::Table::autoColumn, CHOOSE, Query::close(), TableEditor::Table::columns, Form::connectDb(), DELETE, TextBox::display(), ActionButton::display(), EDIT, fields, Widget::form, Widget::htmlencode(), message, Widget::modalChild, Widget::n(), Widget::name, oid, Widget::p(), Widget::printAttribute(), Form::pw, Query::query(), Query::r, SAVE, TableEditor::Table::select, tableIdx, tables, and TextBox::text.

00227         {
00228           p(message);
00229           
00230           super.display(hidden);
00231           printAttribute("oid", "" + oid);        
00232           if (hidden || modalChild != null)
00233           {
00234             printAttribute("tableIdx", "" + tableIdx);
00235             return;
00236           }
00237           
00238           p("<select name=" + n("tableIdx") + " rows=1 onchange=\"this.form.submit()\">\n");
00239           for (int i = 0; i < tables.length; ++i)
00240           {
00241             String selected = i == tableIdx ? " selected " : "";
00242             p("  <option value=" + i + selected + ">" + tables[i].name + "</option>\n");
00243           }
00244           p("</select>\n");
00245           action.display("Go", CHOOSE);
00246           
00247           if (oid != -1)
00248           {
00249             p("<p align=center>");
00250             action.display("Insert new row...", EDIT, -1);
00251             p("</p>");
00252           }
00253           
00254           Table t = tables[tableIdx];
00255           p("<table border=1>\n<tr>\n");
00256           for(int i = 0; i < t.columns.length; ++i)
00257           {
00258             p("  <td><strong>" + t.columns[i] + "</strong></td>\n");
00259           }
00260           p("  <td>&nbsp;</td>\n</tr>\n");
00261           
00262           if (oid == -1)
00263           {
00264             p("<tr>\n");
00265             for (int i = 0; i < t.columns.length; ++i)
00266             {
00267               p("  <td>");
00268               if (i == t.autoColumn)
00269                 p("<i>auto</i>");
00270               else
00271           fields[i].display();
00272         p("</td>\n");
00273             }
00274             p("  <td>");
00275             action.display("Save", SAVE);
00276             p("</td>\n</tr>\n");          
00277           }
00278           
00279           Query q = new Query(form.connectDb(), form.pw);
00280           try
00281           {
00282             q.query(t.select);
00283             if (q.r != null)
00284             while(q.r.next())
00285             {
00286               int oid = q.r.getInt(1);
00287               
00288               if (oid == this.oid)
00289               {
00290                 p("<tr>\n");
00291                 for (int i = 0; i < t.columns.length; ++i)
00292                 {
00293                   p("  <td>");
00294                   if (fields[i].text == null)
00295                     fields[i].text = q.r.getString(i+2);
00296                   fields[i].display();
00297                   p("</td>\n");
00298                 }
00299                 p("  <td>");
00300                 action.display("Save", SAVE);
00301                 p("</td>\n</tr>\n");
00302               }
00303               else
00304               {
00305                 p("<tr>\n");
00306                 for (int i = 0; i < t.columns.length; ++i)
00307                   p("  <td>" + htmlencode(q.r.getString(i+2)) + "</td>\n");
00308                 p("  <td>");
00309                 action.display("Edit", EDIT, oid);
00310                 action.display("Delete", DELETE, oid);
00311                 p("</td>\n</tr>\n");
00312               }
00313             }
00314           }
00315           catch (SQLException e)
00316           {
00317             p(e);
00318           }
00319           finally
00320           {
00321             q.close();
00322           }
00323           p("</table>");
00324         }

void TableEditor::loadValues  )  [inline]
 

Reimplemented from Widget.

Definition at line 112 of file TableEditor.java.

References ActionButton::action, action, Widget::addChild(), TableEditor::Table::columns, DELETE, deleteRow(), EDIT, fields, Widget::form, Widget::loadAttribute(), TextBox::loadValues(), Widget::n(), oid, ActionButton::param, SAVE, saveRow(), tableIdx, tables, and Widget::toInt().

00113         {
00114           super.loadValues();
00115           tableIdx = toInt(loadAttribute("tableIdx"),0);
00116           int o = toInt(loadAttribute("oid"), 0);
00117 
00118     if (action.action == EDIT)
00119       oid = action.param;
00120     else
00121       oid = o;
00122 
00123     if (oid != 0)
00124     {
00125       fields = new TextBox[tables[tableIdx].columns.length];
00126       int cols = 100 / fields.length;
00127       if (cols > 20) cols = 20;
00128       for(int i = 0; i < fields.length; ++i)
00129       {
00130         TextBox t = new TextBox(0, cols, n("field" + i), form);
00131         if (oid == o) t.loadValues();
00132         fields[i] = t;
00133         addChild(t);
00134       }
00135     }
00136     
00137     switch (action.action)
00138     {
00139       case SAVE:
00140         if (saveRow()) oid = 0;
00141         break;
00142       case DELETE:
00143         deleteRow(action.param);
00144         break;
00145     }
00146         }

boolean TableEditor::saveRow  )  [inline]
 

Definition at line 148 of file TableEditor.java.

References TableEditor::Table::autoColumn, Form::conn, Form::connectDb(), Form::disconnectDb(), fields, Widget::form, TableEditor::Table::insert, message, oid, tableIdx, tables, and TableEditor::Table::update.

00149         {
00150           Table t = tables[tableIdx];
00151           form.connectDb();
00152           try
00153           {
00154             PreparedStatement s = form.conn.prepareStatement(oid == -1 ? t.insert : t.update);
00155             try
00156             {
00157               int f = 0;
00158               for (int i = 0; i < fields.length; ++i)
00159               {
00160                 if (oid != -1 || i != t.autoColumn)
00161                 {
00162                   ++f;
00163                   s.setString(f, fields[i].text);
00164                 }
00165               }
00166               if (oid != -1)
00167               {
00168                 ++f;
00169                 s.setInt(f, oid);
00170               }
00171               
00172               s.executeUpdate();
00173             }
00174             finally
00175             {
00176               s.close();
00177             }
00178           }
00179           catch (SQLException e)
00180           {
00181             message = "<p><b><font color=red>Unable to save row. Postgres gives this error message:</font></b></p>\n"
00182               + "<blockquote style=\"background-color: #EEEEEE\">" + e.toString() + "</blockquote>\n";
00183             
00184             // XXX: due to what is apparently a JDBC bug, the connection goes bad
00185             // if the exception is caused by a referrential integrity violation.
00186             // when this happens subsequent queries always throw this exception:
00187       //     
00188       //   No results were returned by the query.
00189       //        at org.postgresql.jdbc2.Statement.executeQuery(Statement.java:58)
00190       //
00191       // My workaround is just to always close the connection.
00192             form.disconnectDb();
00193             return false;
00194           }
00195           message = "<p><b><font color=blue>Row saved successfully</font></b></p>\n";
00196           return true;
00197         }


Member Data Documentation

ActionButton TableEditor::action
 

Definition at line 102 of file TableEditor.java.

final int TableEditor::CHOOSE = 1 [static]
 

Definition at line 95 of file TableEditor.java.

final int TableEditor::DELETE = 3 [static]
 

Definition at line 97 of file TableEditor.java.

final int TableEditor::EDIT = 2 [static]
 

Definition at line 96 of file TableEditor.java.

TextBox [] TableEditor::fields
 

Definition at line 103 of file TableEditor.java.

String TableEditor::message = ""
 

Definition at line 104 of file TableEditor.java.

int TableEditor::oid = 0
 

Definition at line 101 of file TableEditor.java.

final int TableEditor::SAVE = 4 [static]
 

Definition at line 98 of file TableEditor.java.

int TableEditor::tableIdx = 0
 

Definition at line 100 of file TableEditor.java.

Table [] TableEditor::tables = new Table[10] [static, package]
 

Definition at line 61 of file TableEditor.java.


The documentation for this class was generated from the following file:
Generated on Mon Mar 6 23:34:35 2006 by  doxygen 1.4.4