home *** CD-ROM | disk | FTP | other *** search
/ BUG 15 / BUGCD1998_06.ISO / aplic / jbuilder / jsamples.z / SchemaPanel.java < prev    next >
Encoding:
Java Source  |  1997-07-19  |  26.5 KB  |  793 lines

  1. package borland.samples.apps.SchemaEditor;
  2.  
  3. import java.lang.*;
  4. import java.lang.reflect.*;
  5. import java.awt.*;
  6. import java.util.*;
  7. import java.math.*;
  8. import java.io.*;
  9.      
  10. import borland.jbcl.control.*;
  11. import borland.jbcl.dataset.*;
  12. import borland.jbcl.view.*;
  13. import borland.jbcl.model.*;
  14. import borland.jbcl.util.*;
  15. import borland.jbcl.layout.*;
  16. import java.awt.event.*;
  17.  
  18. public class SchemaPanel extends BevelPanel {
  19.   //Column indices
  20.   final static int INDEX=0;
  21.   final static int NAME=1;
  22.   final static int TYPE=2;
  23.   final static int SIZE=3;
  24.   
  25.   //SQL Data Type strings
  26.   final static String BIGDECIMAL="BIGDECIMAL";
  27.   //final static String BOOLEAN="BOOLEAN";
  28.   final static String BOOLEAN="BIT";
  29.   //final static String BYTE="BYTE";        
  30.   final static String BYTE="TINYINT";        
  31.   final static String DATE="DATE";
  32.   final static String DOUBLE="DOUBLE";
  33.   final static String FLOAT="FLOAT";
  34.   //final static String INT="INT";
  35.   final static String INT="INTEGER";   
  36.   final static String LONG="LONG";
  37.   final static String SHORT="SHORT";
  38.   //final static String STRING="STRING";
  39.   final static String STRING="VARCHAR";
  40.   final static String TIME="TIME";
  41.   final static String TIMESTAMP="TIMESTAMP";
  42.  
  43.    
  44.   XYLayout xYLayout1 = new XYLayout();
  45.   GridControl gridControl1 = new GridControl();
  46.   Label labelDescription = new Label();
  47.   Button addField = new Button();
  48.   Button delField = new Button();
  49.   TableDataSet schema = new TableDataSet();
  50.   private int fieldCounter=0;
  51.   
  52.   public SchemaPanel() {
  53.     try {
  54.       jbInit();
  55.     }
  56.     catch (Exception e) {
  57.       e.printStackTrace();
  58.     };
  59.   }
  60.  
  61.   public void jbInit() throws Exception {
  62.     Column columns[] = new Column[4];
  63.     
  64.     xYLayout1.setWidth(434);
  65.     xYLayout1.setHeight(300);
  66.     labelDescription.setText("Define table columns and column types.");
  67.     delField.setLabel("Remove Field");
  68.     delField.addActionListener(new SchemaPanel_delField_actionAdapter(this));
  69.     addField.setLabel("Insert Field");
  70.     addField.addActionListener(new SchemaPanel_addField_actionAdapter(this));
  71.     this.setLayout(xYLayout1);
  72.     columns[INDEX]=new Column();
  73.     columns[INDEX].setDataType(Variant.INT);
  74.     columns[INDEX].setColumnName("Index");
  75.     columns[INDEX].setCaption("Index");
  76.     columns[INDEX].setWidth(50);
  77.     //The index property is for internal use only and should not
  78.     //be seen by the user.
  79.     columns[INDEX].setVisible(TriStateProperty.FALSE);
  80.           
  81.     columns[NAME]=new Column();
  82.     columns[NAME].setDataType(Variant.STRING);
  83.     columns[NAME].setColumnName("Field_Name");
  84.     columns[NAME].setCaption("Field Name");
  85.     columns[NAME].setWidth(150);
  86.     columns[NAME].setRequired(true);
  87.  
  88.     columns[TYPE]=new Column();
  89.     columns[TYPE].setDataType(Variant.STRING);
  90.     columns[TYPE].setColumnName("Field_Type");
  91.     columns[TYPE].setCaption("Field Type");
  92.     columns[TYPE].setWidth(90);
  93.     columns[TYPE].setRequired(true);
  94.     columns[TYPE].setItemEditor(new SchemaFieldTypeEditor());
  95.  
  96.     columns[SIZE]=new Column();
  97.     columns[SIZE].setDataType(Variant.INT);
  98.     columns[SIZE].setColumnName("Size");
  99.     columns[SIZE].setCaption("Size");
  100.     columns[SIZE].setMin("0");
  101.     columns[SIZE].setWidth(50);
  102.     
  103.     schema.setColumns(columns);
  104.     schema.setSort(new SortDescriptor("INDEX"));
  105.     gridControl1.setDataSet(schema);
  106.     gridControl1.setSortOnHeaderClick(false);
  107.     addRow();  //Create the first row
  108.     
  109.     this.add(labelDescription, new XYConstraints(8, 10, 309, 30));
  110.     this.add(gridControl1, new XYConstraints(9, 42, 414, 195));
  111.     this.add(addField, new XYConstraints(10, 244, 101, 28));
  112.     this.add(delField, new XYConstraints(128, 244, 104, 28));
  113.   }
  114.  
  115.   //Inserts a new default row to the table
  116.   private void addRow() {
  117.     DataRow row;
  118.     try {
  119.      if (!schema.isOpen()) {
  120.         schema.open();
  121.       }
  122.       //Renumber index from end to current row
  123.       //Since dataset is sorted, each renumber causes a resort.
  124.       //By renumbering from the end to the cursor, we keep records
  125.       //in the correct order
  126.  
  127.       // This is an alternate way to do the renumbering by using
  128.       // a dataset view instead of moving the cursor in the current
  129.       // dataset.  A dataset view has it's own cursor and sort order
  130.       //if (schema.rowCount() > 0 ){
  131.       //  DataSetView view = schema.cloneDataSetView();
  132.       //  view.open();
  133.       //  view.goToRow(schema.getRow());
  134.       //  view.last();
  135.       //  while(view.getRow() != schema.getRow()) {
  136.       //    view.setInt(INDEX, view.getInt(INDEX)+1);
  137.       //    view.prior();
  138.       //  }
  139.       //  view.post();
  140.       //  view.close();
  141.       //}
  142.       
  143.       row=new DataRow(schema);
  144.       int currentRow=schema.getRow();
  145.       //This turns off grid painting
  146.       schema.enableDataSetEvents(false);
  147.       
  148.       schema.last();
  149.       while(schema.getRow() != currentRow) {
  150.         schema.getDataRow(row);
  151.         row.setInt(INDEX,row.getInt(INDEX)+1);
  152.         schema.updateRow(row);
  153.         schema.prior();
  154.       }
  155.                     
  156.       row.setInt(INDEX,schema.getInt(INDEX)+1);
  157.       row.setString(NAME,"Field"+fieldCounter++);
  158.       row.setString(TYPE,STRING);
  159.       row.setInt(SIZE,25);
  160.       schema.addRow(row);
  161.       
  162.       schema.next(); //Goto new row
  163.       //The add button got focus when we clicked it, return
  164.       //focus to the grid
  165.       gridControl1.requestFocus();
  166.       schema.enableDataSetEvents(true);
  167.       // In case grid doesn't repaint correctly after enabling
  168.       //gridControl1.repaintCells();
  169.     }
  170.     catch (Exception e) {e.printStackTrace();}
  171.    
  172.   }
  173.  
  174.   //Start with a clean schema
  175.   public void newDataSet() {
  176.     try {  
  177.       if (!schema.isOpen()) {
  178.         schema.open();
  179.       }
  180.       schema.deleteAllRows();
  181.       fieldCounter=0;
  182.       addRow();
  183.     }
  184.     catch (Exception e) {e.printStackTrace();}
  185.   }
  186.   
  187.   //Given an existing DataSet, display the schema for editing
  188.   public void setDataSet(StorageDataSet d) {
  189.     Column columns[];
  190.     DataRow row;
  191.     
  192.     try {  
  193.       if (!schema.isOpen()) {
  194.         schema.open();
  195.       }
  196.       schema.deleteAllRows();
  197.       //For each column in dataset, create a row in schema
  198.       columns = d.getColumns();
  199.       for (int i=0; i< columns.length ; i++) {
  200.         row = new DataRow(schema);
  201.         row.setInt(INDEX,i);
  202.         row.setString(NAME,columns[i].getColumnName());
  203.         row.setInt(SIZE,columns[i].getPrecision());
  204.  
  205.         switch (columns[i].getDataType()) {
  206.           case Variant.BIGDECIMAL:
  207.             row.setString(TYPE,BIGDECIMAL);
  208.             break;
  209.           case Variant.BOOLEAN:
  210.             row.setString(TYPE,BOOLEAN);
  211.             break;
  212.           case Variant.BYTE:
  213.             row.setString(TYPE,BYTE);
  214.             break;
  215.           case Variant.DATE:
  216.             row.setString(TYPE,DATE);
  217.             break;
  218.           case Variant.DOUBLE:
  219.             row.setString(TYPE,DOUBLE);
  220.             break;
  221.           case Variant.FLOAT:
  222.             row.setString(TYPE,FLOAT);
  223.             break;
  224.           case Variant.INT:
  225.             row.setString(TYPE,INT);
  226.             break;
  227.           case Variant.LONG:
  228.             row.setString(TYPE,LONG);
  229.             break;
  230.           case Variant.SHORT:
  231.             row.setString(TYPE,SHORT);
  232.             break;
  233.           case Variant.STRING:
  234.             row.setString(TYPE,STRING);
  235.             break;
  236.           case Variant.TIME:
  237.             row.setString(TYPE,TIME);
  238.             break;
  239.           case Variant.TIMESTAMP:
  240.             row.setString(TYPE,TIMESTAMP);
  241.             break;
  242.         }
  243.         schema.addRow(row);
  244.         schema.post();
  245.       }
  246.       gridControl1.repaintCells();
  247.     } catch (Exception e) {
  248.       e.printStackTrace();
  249.     }
  250.   }
  251.  
  252.   public StorageDataSet getDataSet(boolean sampleData) {
  253.     // Construct a temporary dataset from schema and use it to create
  254.     // the schema file by saving it.  This hides the details of the
  255.     // schema syntax in case it changes in the future.
  256.     TableDataSet ds = new TableDataSet();
  257.     Column column;
  258.     String s=null;
  259.     Variant v = new Variant();
  260.     int recordCounter=0;
  261.     String defString;
  262.     DataRow sampleRow;
  263.     int numRows;
  264.     String columnName;
  265.     String columnType;
  266.     int columnPrecision;
  267.     
  268.     // Walk through the schema and construct columns
  269.     try {
  270.       if (!schema.isOpen()) {
  271.         schema.open();
  272.       }
  273.       schema.first(); //Make sure dataset is reset to beginning
  274.       numRows=schema.getRowCount();
  275.  
  276.       for (int row=0; row < numRows; row++) {
  277.         //Is there a field that needs to be created?
  278.       
  279.         schema.getVariant(SchemaPanel.NAME,row,v);
  280.         if (!v.isNull()) {
  281.           column = new Column();
  282.           //Set the column name
  283.           columnName=v.getString();
  284.           column.setColumnName(columnName);
  285.  
  286.           //Set the size of the field
  287.           schema.getVariant(SchemaPanel.SIZE,row,v);
  288.           columnPrecision=0;
  289.           if (!v.isNull()) {
  290.             columnPrecision=v.getInt();
  291.           }
  292.           column.setPrecision(columnPrecision);
  293.        
  294.           //Set the field type
  295.           schema.getVariant(SchemaPanel.TYPE,row,v);
  296.           columnType=v.getString();
  297.           if (columnType==BIGDECIMAL) {
  298.             column.setDataType(Variant.BIGDECIMAL);
  299.           }else if (columnType==BOOLEAN) {
  300.             column.setDataType(Variant.BOOLEAN);
  301.           }else if (columnType==BYTE) {
  302.             column.setDataType(Variant.BYTE);
  303.           }else if (columnType==DATE) {
  304.             column.setDataType(Variant.DATE);
  305.           }else if (columnType==DOUBLE) {
  306.             column.setDataType(Variant.DOUBLE);
  307.           }else if (columnType==FLOAT) {
  308.             column.setDataType(Variant.FLOAT);
  309.           }else if (columnType==INT) {
  310.             column.setDataType(Variant.INT);
  311.           }else if (columnType==LONG) {
  312.             column.setDataType(Variant.LONG);
  313.           }else if (columnType==SHORT) {
  314.             column.setDataType(Variant.SHORT);
  315.           }else if (columnType==STRING) {
  316.             column.setDataType(Variant.STRING);
  317.           }else if (columnType==TIME) {
  318.             column.setDataType(Variant.TIME);
  319.           }else if (columnType==TIMESTAMP) {
  320.             column.setDataType(Variant.TIMESTAMP);
  321.           }
  322.  
  323.           //Add the column to the template
  324.           ds.addColumn(column);
  325.         } //end if (schema.getRow...)
  326.       } //end for(...)
  327.  
  328.       if (sampleData) {
  329.         // Create 10 records of sample data
  330.         ds.open();
  331.         for (int i=0; i<10;i++) {
  332.           sampleRow=new DataRow(ds);
  333.           for (int j=0; j<numRows; j++) {
  334.             columnPrecision=ds.getColumn(j).getPrecision();
  335.             switch (ds.getColumn(j).getDataType()) {
  336.               case Variant.BIGDECIMAL:
  337.                 sampleRow.setBigDecimal(j,new BigDecimal(123.45));
  338.                 break;
  339.               case Variant.BOOLEAN:
  340.               sampleRow.setBoolean(j,true);
  341.                 break;
  342.               case Variant.BYTE:
  343.                 sampleRow.setByte(j,127);
  344.                 break;
  345.               case Variant.DATE:
  346.                 sampleRow.setDate(j,new java.sql.Date(97,7,1));
  347.                 break;
  348.               case Variant.DOUBLE:
  349.                 sampleRow.setDouble(j,123.45);
  350.                 break;
  351.               case Variant.FLOAT:
  352.                 sampleRow.setFloat(j,1234.56F);
  353.                 break;
  354.               case Variant.INT:
  355.                 sampleRow.setInt(j,10);
  356.                 break;
  357.               case Variant.LONG:
  358.                 sampleRow.setLong(j,1000000L);
  359.                 break;
  360.               case Variant.SHORT:
  361.                 sampleRow.setShort(j,(short)1000);
  362.                 break;
  363.               case Variant.STRING:
  364.                 defString=ds.getColumn(j).getColumnName()+recordCounter;
  365.                 if (defString.length() > ds.getColumn(j).getPrecision()) {
  366.                   defString=defString.substring(0,columnPrecision);
  367.                 }
  368.                 sampleRow.setString(j,defString);
  369.                 break;
  370.               case Variant.TIME:
  371.                 sampleRow.setTime(j,new java.sql.Time(12,30,30));
  372.                 break;
  373.               case Variant.TIMESTAMP:
  374.                 sampleRow.setTimestamp(j,new java.sql.Timestamp(97,7,1,12,30,30,30));
  375.                 break;
  376.             }
  377.           }
  378.           ds.addRow(sampleRow);
  379.           ds.post();
  380.           recordCounter++;
  381.         }
  382.  
  383.       }
  384.       ds.close();
  385.     }
  386.     catch (Exception x) {
  387.       x.printStackTrace();
  388.     }
  389.   return ds;
  390.   } // end getDataSet
  391.  
  392.   //Generates a sample application to view text schema
  393.   //Strings beginning with "$" in this array result in a method
  394.   //call which generates form specific code in place.
  395.   //For example, "$codeGen1", will call codeGen1(PrintWriter out)
  396.   //This is an interesting example of using reflection to call a method
  397.   //specified by a string.
  398.   private String codeBlock[] = {
  399.     "// Change package name to match actual directory where created",
  400.     "package borland.samples.apps.SchemaEditor;",
  401.     "import java.awt.*;",
  402.     "import java.awt.event.*;",
  403.     "import borland.jbcl.layout.*;",
  404.     "import borland.jbcl.control.*;",
  405.     "import borland.jbcl.dataset.*;",
  406.     "",
  407.     "$codeGenC0", //"public class TestSchema {",
  408.     "  // !!! These variables must be hand edited to make sample work",
  409.     "  boolean useTableDataSet = true; //false->use a query DataSet",
  410.     "  // If you are using a local data file, set the following",
  411.     "$codeGenS0",
  412.     "  // If you are using a local or remote SQL server, set the following",
  413.     "  String sqlDataBaseName=\"TestSchema\";",
  414.     "  String sqlTableName=\"TestSchema\";",
  415.     "  String sqlUserName=\"SYSDBA\";",
  416.     "  String sqlPassword=\"masterkey\";",
  417.     "",
  418.     "  StorageDataSet dataSet;",
  419.        
  420.     "  Database database1 = new Database();",
  421.     "  TextDataFile textDataFile1 = new TextDataFile();",
  422.     "",
  423.     "  BorderLayout borderLayout1 = new BorderLayout();",
  424.     "  DecoratedFrame frame = new DecoratedFrame();",
  425.     "  TestPanel panel = new TestPanel();",
  426.     "  NavigatorControl navigatorControl1 = new NavigatorControl();",
  427.     "  StatusBar statusBar1 = new StatusBar();",
  428.     "",
  429.     "  //Construct the application",
  430.     "$codeGenC1", //"  public TestSchema() {",
  431.     "    try {",
  432.     "      jbInit();",
  433.     "    }",
  434.     "    catch (Exception e) {",
  435.     "      e.printStackTrace();",
  436.     "    }",
  437.     "    frame.pack();",
  438.     "    //Center the window",
  439.     "    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();",
  440.     "    Dimension frameSize = frame.getPreferredSize();",
  441.     "    if (frameSize.height > screenSize.height)",
  442.     "      frameSize.height = screenSize.height;",
  443.     "    if (frameSize.width > screenSize.width)",
  444.     "      frameSize.width = screenSize.width;",
  445.     "    frame.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);",
  446.     "    frame.setVisible(true);",
  447.     "  }",
  448.     "",
  449.     "  void jbInit() throws Exception{",
  450.     "    frame.setExitOnClose(false); //Allows us to grab the window closing event",
  451.     "$codeGenC4", //"    frame.addWindowListener(new TestSchema_frame_windowAdapter(this));",
  452.     "    frame.setLayout(borderLayout1);",
  453.     "    frame.add(navigatorControl1, BorderLayout.NORTH);",
  454.     "    frame.add(panel,BorderLayout.CENTER);",
  455.     "    frame.add(statusBar1, BorderLayout.SOUTH);",
  456.     "    frame.setTitle(\"Schema Editor\");",
  457.     "    if (useTableDataSet) {",
  458.     "      textDataFile1.setFileName(tableDataSetName);",
  459.     "      dataSet=new TableDataSet();",
  460.     "      dataSet.setDataFile(textDataFile1);",
  461.     "    } else {",
  462.     "      database1.setConnection(new borland.jbcl.dataset.ConnectionDescriptor(\"jdbc:odbc:\" + sqlDataBaseName, sqlUserName, sqlPassword, false, \"sun.jdbc.odbc.JdbcOdbcDriver\"));",
  463.     "      QueryDataSet qds = new QueryDataSet();",
  464.     "      qds.setQuery(new borland.jbcl.dataset.QueryDescriptor(database1, \"select * from \"+sqlTableName, null, true, false));",
  465.     "      dataSet=qds;",
  466.     "    }",
  467.     "    panel.setDataSet(dataSet);",
  468.     "    navigatorControl1.setDataSet(dataSet);",
  469.     "    statusBar1.setDataSet(dataSet);",
  470.     "   }",
  471.     "",
  472.     "   void frame_windowClosing(WindowEvent e) {",
  473.     "     try {",
  474.     "       //Save any changes",
  475.     "       if (useTableDataSet) {",
  476.     "         textDataFile1.save(dataSet);",
  477.     "       } else {",
  478.     "         dataSet.saveChanges(); //Save query data set",
  479.     "       }",
  480.     "     } catch (Exception x) {x.printStackTrace();}",
  481.     "     System.exit(0);",
  482.     "   }",
  483.     "",
  484.     "  //Main method",
  485.     "  static public void main(String[] args) {",
  486.     "$codeGenC2", //"    TestSchema ts = new TestSchema();",
  487.     "    System.out.println(ts.getCreateTableSQL());",
  488.     "  }",
  489.     "",  
  490.     "  //Returns a SQL string that can be used to create this table",
  491.     "  //in Local Interbase",
  492.     "  String getCreateTableSQL() {",
  493.     "    String sql;",
  494.     "$codeGen0", //Create SQL string
  495.     "  }",
  496.     "}",
  497.     "",
  498.     "// This panel can be reused in other applications",
  499.     "// Note the use of a split panel to allow resizable panes",
  500.     "// You will get a warning when you compile this until you extract it to a separate file",
  501.     "public class TestPanel extends SplitPanel {",
  502.     "  BevelPanel panel1=new BevelPanel();",
  503.     "  BevelPanel panel2=new BevelPanel();",
  504.     "  GridLayout gridLayout1 = new GridLayout();",
  505.     "  DataSet dataSet;",
  506.     "",
  507.     "$codeGen1", //Create field controls
  508.     "",
  509.     "  public TestPanel() {",
  510.     "    try {",
  511.     "      jbInit();",
  512.     "    }",
  513.     "    catch (Exception e) {",
  514.     "      e.printStackTrace();",
  515.     "    }",
  516.     "  }",
  517.     "",
  518.     "  public void setDataSet (StorageDataSet s) {",
  519.     "    dataSet=s;",
  520.     "$codeGen3", //Set the dataset for all fields
  521.     "  }",
  522.     "",
  523.     "  void jbInit() throws Exception{",
  524.     "    gridLayout1.setRows(-1);",
  525.     "    gridLayout1.setColumns(1);",
  526.     "    panel1.setLayout(gridLayout1);",
  527.     "    panel2.setLayout(gridLayout1);",
  528.     "$codeGen2", //Init field controls
  529.     "    //Create the split panels",
  530.     "    this.add(panel1,new PaneConstraints(\"panel1\", \"panel1\", PaneConstraints.ROOT, 1.0f));",
  531.     "    this.add(panel2,new PaneConstraints(\"panel2\", \"panel2\", PaneConstraints.RIGHT, 0.70f));",    
  532.     "  }",
  533.     "}",
  534.     "",
  535.     "$codeGenC3",
  536.     "    this.adaptee = adaptee;",
  537.     "  }",
  538.     "",
  539.     "  public void windowClosing(WindowEvent e) {",
  540.     "    adaptee.frame_windowClosing(e);",
  541.     "  }",
  542.     "}",
  543.     ""
  544.   };
  545.  
  546.   //filename specifies the name of the data table to browse    
  547.   public void generateApp(String directory, String fileName, String lastSchema) {
  548.     Method m;
  549.     Class c = this.getClass();
  550.     String methodName;
  551.     String className; //Derived from fileName, wo .java suffix
  552.     if (fileName.endsWith(".java")) {
  553.         className=(fileName.substring(0,fileName.lastIndexOf(".java")));  
  554.       }
  555.     else {
  556.       className = fileName;
  557.     }
  558.     //Replace any "\" in the string with "\\" so that we can generate legal code
  559.     String schemaName="";
  560.     for (int i=0; i<lastSchema.length(); i++) {
  561.       if (lastSchema.charAt(i) == '\\') {
  562.         schemaName=schemaName+"\\\\";
  563.       } else {
  564.         schemaName=schemaName+lastSchema.charAt(i);
  565.       }
  566.     }
  567.     try {
  568.       PrintWriter out = new PrintWriter(new FileOutputStream(directory+fileName));
  569.       schema.enableDataSetEvents(false); //Stop painting
  570.       for (int i=0; i<codeBlock.length; i++) {
  571.         if (codeBlock[i].startsWith("$")) {
  572.           //Code generating function
  573.           methodName=codeBlock[i].substring(1);
  574.           m = c.getDeclaredMethod(methodName,new Class[] {out.getClass(),String.class,String.class});
  575.           m.invoke(this,new Object[] {out,className, schemaName});
  576.         } else {
  577.           out.println(codeBlock[i]);
  578.         }
  579.       }
  580.       out.close();
  581.       schema.enableDataSetEvents(true);
  582.     } catch (Exception e) { e.printStackTrace(); }
  583.   }
  584.   private void codeGenS0(PrintWriter out, String cn, String sn) {
  585.     out.println("  String tableDataSetName=\"" + sn +"\";");
  586.   }
  587.   //Code gen for all classname specific code
  588.   private void codeGenC0(PrintWriter out, String cn, String sn) {
  589.     out.println("public class " + cn + " {");
  590.   }
  591.   private void codeGenC1(PrintWriter out, String cn, String sn) {
  592.     out.println("  public " + cn + "() {");
  593.   }
  594.   private void codeGenC2(PrintWriter out, String cn, String sn) {
  595.     out.println("    " + cn + " ts = new " + cn + "();");
  596.   }      
  597.   private void codeGenC3(PrintWriter out, String cn, String sn) {
  598.     out.println("class "+cn+"_frame_windowAdapter extends java.awt.event.WindowAdapter {");
  599.     out.println("  "+cn+" adaptee;");
  600.     out.println("  "+cn+"_frame_windowAdapter("+cn+" adaptee) {");
  601.   }       
  602.   private void codeGenC4(PrintWriter out, String cn, String sn) {
  603.     out.println("    frame.addWindowListener(new "+cn+"_frame_windowAdapter(this));");  
  604.   }  
  605.   //GenCode1 - Create Field controls
  606.   private void codeGen1(PrintWriter out, String cn, String sn) {
  607.     try { 
  608.       DataRow row=new DataRow(schema);
  609.       String fn; //Field name
  610.       schema.first();
  611.       for (int i=0; i<schema.getRowCount(); i++,schema.next()) {
  612.         schema.getDataRow(row);
  613.         fn=row.getString(NAME);
  614.         //Label
  615.         out.println("  LabelControl label"+i+" = new LabelControl();");
  616.         //Field Control
  617.         out.println("  FieldControl field"+i+" = new FieldControl(); //"+fn);
  618.       }
  619.     } catch (Exception e) {e.printStackTrace();}
  620.   }      
  621.   //GenCode2 - Init the fields
  622.   private void codeGen2(PrintWriter out, String cn, String sn) {
  623.     try {
  624.       DataRow row=new DataRow(schema);
  625.       String fn; //Field name
  626.       schema.first();
  627.       for (int i=0; i<schema.getRowCount(); i++,schema.next()) {
  628.         schema.getDataRow(row);
  629.         fn=row.getString(NAME);
  630.         //Label
  631.         out.println("    label" + i + ".setText(\"" + fn + "\");");
  632.         //Field Control
  633.         out.println("    field" + i + ".setColumnName(\"" + fn + "\");");
  634.         //Add to panel
  635.         out.println("    panel1.add(label" + i + ",null);");
  636.         out.println("    panel2.add(field" + i + ",null);");
  637.       }
  638.     } catch (Exception e) {e.printStackTrace();}
  639.   }
  640.   
  641.   //Generate the rest of the setDataSet method
  642.   private void codeGen3(PrintWriter out, String cn, String sn) {
  643.     try {
  644.       DataRow row=new DataRow(schema);
  645.       schema.first();
  646.       for (int i=0; i<schema.getRowCount(); i++,schema.next()) {
  647.         schema.getDataRow(row);
  648.         out.println("    field" + i + ".setDataSet(dataSet);");
  649.       }
  650.     } catch (Exception e) {e.printStackTrace();}
  651.   }                                                
  652.                                                   
  653.   // GenCode0 - Generate a method to return a SQL string to create table
  654.   private void codeGen0(PrintWriter out, String cn, String sn) {
  655.     try {
  656.       String sql=new String();
  657.       String type=new String();
  658.       DataRow row=new DataRow(schema);
  659.       schema.first();
  660.       for (int i=0; i<schema.getRowCount(); i++,schema.next()) {
  661.         schema.getDataRow(row);
  662.         type=row.getString(TYPE);
  663.         sql=sql + row.getString(NAME) + " " + type;
  664.         if (type != INT) { //INTEGER has no size parameter in SQL
  665.           sql=sql+ " (" + row.getInt(SIZE) + ")";
  666.         }
  667.         //First field is considered the primary key
  668.         if (i==0) {
  669.           sql=sql+" NOT NULL PRIMARY KEY";
  670.         }
  671.         if (i<schema.getRowCount()-1) {
  672.           sql=sql+","; //Last field can't have trailing comma
  673.         }
  674.       }
  675.       out.println("    sql=\"create table \"+sqlTableName+\" (" + sql + ")\";");
  676.       out.println("    return sql;");
  677.     } catch (Exception e) {e.printStackTrace();}
  678.   }
  679.   
  680.   void addField_actionPerformed(ActionEvent e) {
  681.     addRow();
  682.   }
  683.  
  684.   //Remove the current field
  685.   void delField_actionPerformed(ActionEvent e) {
  686.     try {
  687.       schema.deleteRow();
  688.     } catch (Exception x) {
  689.       x.printStackTrace();
  690.     }
  691.   }
  692.   
  693. } //end class
  694.  
  695. class SchemaFieldTypeEditor extends Choice implements ItemEditor {
  696.   SchemaFieldTypeEditor() {
  697.     super();
  698.     setBounds(0,0,0,0);
  699.     setVisible(false);
  700.     //Display all valid datatypes for a TextDataSet
  701.     addItem(SchemaPanel.BIGDECIMAL);
  702.     addItem(SchemaPanel.BOOLEAN);
  703.     addItem(SchemaPanel.BYTE);
  704.     addItem(SchemaPanel.DATE);
  705.     addItem(SchemaPanel.DOUBLE);
  706.     addItem(SchemaPanel.FLOAT);
  707.     addItem(SchemaPanel.INT);
  708.     addItem(SchemaPanel.LONG);
  709.     addItem(SchemaPanel.SHORT);
  710.     addItem(SchemaPanel.STRING);
  711.     addItem(SchemaPanel.TIME);
  712.     addItem(SchemaPanel.TIMESTAMP);
  713.  
  714.   }
  715.  
  716.   public Object getValue() {
  717.     return getSelectedItem();
  718.   }
  719.  
  720.   public Component getComponent() { return this; }
  721.  
  722.   public void startEdit(Object value, Rectangle bounds, ItemEditSite site) {
  723.     select(value.toString()); //Get whatever is in the grid and select it
  724.     changeBounds(bounds);
  725.     setVisible(true);
  726.   }
  727.  
  728.   public void changeBounds(Rectangle bounds) {
  729.     setBounds(bounds.x, bounds.y, bounds.width, bounds.height);
  730.   }
  731.  
  732.   public boolean canPost() {
  733.     return true;
  734.   }
  735.  
  736.   public void endEdit(boolean post) {}
  737. }
  738.  
  739.  
  740. class SchemaPrimaryKeyEditor extends Checkbox implements ItemEditor {
  741.   SchemaPrimaryKeyEditor() {
  742.     super();
  743.     setBounds(0,0,0,0);
  744.     setVisible(false);
  745.   }
  746.  
  747.   public Object getValue() {
  748.     return new Checkbox();
  749.   }
  750.  
  751.   public Component getComponent() { return this; }
  752.  
  753.   public void startEdit(Object value, Rectangle bounds, ItemEditSite site) {
  754.     changeBounds(bounds);
  755.     setVisible(true);
  756.   }
  757.  
  758.   public void changeBounds(Rectangle bounds) {
  759.     setBounds(bounds.x, bounds.y, bounds.width, bounds.height);
  760.   }
  761.  
  762.   public boolean canPost() {
  763.     return true;
  764.   }
  765.  
  766.   public void endEdit(boolean post) {}
  767. }
  768.  
  769. class SchemaPanel_addField_actionAdapter implements java.awt.event.ActionListener {
  770.   SchemaPanel adaptee;
  771.  
  772.   SchemaPanel_addField_actionAdapter(SchemaPanel adaptee) {
  773.     this.adaptee = adaptee;
  774.   }
  775.  
  776.   public void actionPerformed(ActionEvent e) {
  777.     adaptee.addField_actionPerformed(e);
  778.   }
  779. }
  780.  
  781. class SchemaPanel_delField_actionAdapter implements java.awt.event.ActionListener {
  782.   SchemaPanel adaptee;
  783.  
  784.   SchemaPanel_delField_actionAdapter(SchemaPanel adaptee) {
  785.     this.adaptee = adaptee;
  786.   }
  787.  
  788.   public void actionPerformed(ActionEvent e) {
  789.     adaptee.delField_actionPerformed(e);
  790.   }
  791. }
  792.  
  793.