home *** CD-ROM | disk | FTP | other *** search
/ BUG 15 / BUGCD1998_06.ISO / aplic / jbuilder / jsamples.z / ResolverFrame.java < prev    next >
Text File  |  1997-07-24  |  7KB  |  161 lines

  1. package borland.samples.tutorial.dataset.resolverevents;
  2.  
  3. import java.awt.*;
  4. import java.awt.event.*;
  5. import borland.jbcl.control.*;
  6. import borland.jbcl.layout.*;
  7. import borland.jbcl.dataset.*;
  8. import borland.jbcl.util.*;
  9.  
  10. public class ResolverFrame extends DecoratedFrame {
  11.   BorderLayout borderLayout1 = new BorderLayout();
  12.   BevelPanel bevelPanel1 = new BevelPanel();
  13.   Database database1 = new Database();
  14.   QueryDataSet queryDataSet1 = new QueryDataSet();
  15.   QueryResolver queryResolver1 = new QueryResolver();
  16.   ListControl listControl1 = new ListControl();
  17.   GridControl gridControl1 = new GridControl();
  18.   ButtonControl buttonControl1 = new ButtonControl();
  19.   GridBagLayout gridBagLayout1 = new GridBagLayout();
  20.  
  21.   //Construct the frame
  22.   public ResolverFrame() {
  23.     try {
  24.       jbInit();
  25.     }
  26.     catch (Exception e) {
  27.       borland.jbcl.util.Diagnostic.printStackTrace(e);
  28.     }
  29.   }
  30.  
  31.   //Component initialization
  32.   public void jbInit() throws Exception{
  33.     this.setLayout(borderLayout1);
  34.     this.setTitle("Resolver Example");
  35.     bevelPanel1.setLayout(gridBagLayout1);
  36.     database1.setConnection(new borland.jbcl.dataset.ConnectionDescriptor("jdbc:odbc:DataSet Tutorial", "sysdba", "masterkey", false, "sun.jdbc.odbc.JdbcOdbcDriver;"));
  37.     queryDataSet1.setQuery(new borland.jbcl.dataset.QueryDescriptor(database1, "select * from customer", null, true, false));
  38.     queryDataSet1.setResolver(queryResolver1);
  39.     queryResolver1.setUpdateMode(borland.jbcl.dataset.UpdateMode.KEY_COLUMNS);
  40.     queryResolver1.addResolverListener(new ResolverFrame_queryResolver1_resolverAdapter(this));
  41.     gridControl1.setDataSet(queryDataSet1);
  42.     buttonControl1.setLabel("Save Changes");
  43.     buttonControl1.addActionListener(new ResolverFrame_buttonControl1_actionAdapter(this));
  44.     this.add(bevelPanel1, BorderLayout.CENTER);
  45.     bevelPanel1.add(gridControl1, new GridBagConstraints2(0, 0, 1, 1, 1.0, 1.0
  46.             ,GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(2, 2, 2, 2), -1737, -183));
  47.     this.add(listControl1, BorderLayout.SOUTH);
  48.     this.add(buttonControl1, BorderLayout.NORTH);
  49.   }
  50.  
  51.  
  52.   // Restrict an action.
  53.   void queryResolver1_deletingRow(ReadWriteRow readWriteRow, ResolverResponse resolverResponse) throws DataSetException{
  54.     // Don't allow anybody to delete a customer record.
  55.     resolverResponse.abort();
  56.   }
  57.  
  58.   
  59.   // Ignore action errors.
  60.   void queryResolver1_insertError(DataSet dataSet, ReadWriteRow readWriteRow, DataSetException dataSetException, ErrorResponse errorResponse) throws DataSetException{
  61.     // Display error message with customer name and exception.
  62.     listControl1.addItem("Error inserting " + readWriteRow.getString("customer"));
  63.     listControl1.addItem(" " + dataSetException.getMessage());
  64.  
  65.     // Ignore the error.
  66.     errorResponse.ignore();
  67.   }
  68.  
  69.   
  70.   // Perform some basic validation of a column value before the action.
  71.   void queryResolver1_insertingRow(ReadWriteRow readWriteRow, ResolverResponse resolverResponse) throws DataSetException{
  72.     // Get the country value from the row to be inserted
  73.     String  country = readWriteRow.getString("country");
  74.  
  75.     // Validate that the new customers are only from the United States.
  76.     if (!country.equals("USA"))
  77.       resolverResponse.abort();
  78.   }
  79.  
  80.   
  81.   // Indicate that the action was performed.
  82.   void queryResolver1_insertedRow(ReadWriteRow readWriteRow) throws DataSetException{
  83.     // Add a line to the list control indicating that the new row was inserted.
  84.     listControl1.addItem(readWriteRow.getString("customer") + " was inserted.");
  85.   }
  86.  
  87.   
  88.   // Abort action if error occurs.
  89.   void queryResolver1_updateError(DataSet dataSet, ReadWriteRow readWriteRow, ReadRow readRow, ReadWriteRow readWriteRow1, DataSetException dataSetException, ErrorResponse errorResponse) throws DataSetException{
  90.     errorResponse.abort();
  91.   }
  92.  
  93.   
  94.   // Using column value as a criteria, indicate that the action was performed.
  95.   void queryResolver1_updatedRow(ReadWriteRow readWriteRow, ReadRow readRow) throws DataSetException{
  96.     String  newCustomer = readWriteRow.getString("customer");
  97.     String  oldCustomer = readRow.getString("customer");
  98.  
  99.     // If the customer name was changed, use a different message.
  100.     if (!oldCustomer.equals(newCustomer))
  101.       listControl1.addItem("Customer " + oldCustomer + " was changed to " + newCustomer);
  102.     else
  103.       listControl1.addItem("Customer " + oldCustomer + " was updated." ); 
  104.   }
  105.  
  106.   
  107.   void buttonControl1_actionPerformed(ActionEvent e) {
  108.     try {
  109.       database1.saveChanges(queryDataSet1);
  110.     }
  111.     catch(DataSetException dse){
  112.       listControl1.addItem(dse);
  113.     }
  114.   }
  115. }
  116.  
  117. class ResolverFrame_queryResolver1_resolverAdapter extends borland.jbcl.dataset.ResolverAdapter{
  118.   ResolverFrame adaptee;
  119.  
  120.   ResolverFrame_queryResolver1_resolverAdapter(ResolverFrame adaptee) {
  121.     this.adaptee = adaptee;
  122.   }
  123.  
  124.   public void deletingRow(ReadWriteRow readWriteRow, ResolverResponse resolverResponse) throws DataSetException{
  125.     adaptee.queryResolver1_deletingRow(readWriteRow, resolverResponse);
  126.   }
  127.  
  128.   public void insertError(DataSet dataSet, ReadWriteRow readWriteRow, DataSetException dataSetException, ErrorResponse errorResponse) throws DataSetException{
  129.     adaptee.queryResolver1_insertError(dataSet, readWriteRow, dataSetException, errorResponse);
  130.   }
  131.  
  132.   public void insertingRow(ReadWriteRow readWriteRow, ResolverResponse resolverResponse) throws DataSetException{
  133.     adaptee.queryResolver1_insertingRow(readWriteRow, resolverResponse);
  134.   }
  135.  
  136.   public void insertedRow(ReadWriteRow readWriteRow) throws DataSetException{
  137.     adaptee.queryResolver1_insertedRow(readWriteRow);
  138.   }
  139.  
  140.   public void updateError(DataSet dataSet, ReadWriteRow readWriteRow, ReadRow readRow, ReadWriteRow readWriteRow1, DataSetException dataSetException, ErrorResponse errorResponse) throws DataSetException{
  141.     adaptee.queryResolver1_updateError(dataSet, readWriteRow, readRow, readWriteRow1, dataSetException, errorResponse);
  142.   }
  143.  
  144.   public void updatedRow(ReadWriteRow readWriteRow, ReadRow readRow) throws DataSetException{
  145.     adaptee.queryResolver1_updatedRow(readWriteRow, readRow);
  146.   }
  147. }
  148.  
  149. class ResolverFrame_buttonControl1_actionAdapter implements java.awt.event.ActionListener {
  150.   ResolverFrame adaptee;
  151.  
  152.   ResolverFrame_buttonControl1_actionAdapter(ResolverFrame adaptee) {
  153.     this.adaptee = adaptee;
  154.   }
  155.  
  156.   public void actionPerformed(ActionEvent e) {
  157.     adaptee.buttonControl1_actionPerformed(e);
  158.   }
  159. }
  160.  
  161.