home *** CD-ROM | disk | FTP | other *** search
/ PC World 1998 October / PCWorld_1998-10_cd.bin / software / prehled / inprise / JSAMPLES.Z / ResolverFrame.java < prev    next >
Text File  |  1998-05-08  |  8KB  |  182 lines

  1. /*
  2.  * Copyright (c) 1997-1998 Borland International, Inc. All Rights Reserved.
  3.  * 
  4.  * This SOURCE CODE FILE, which has been provided by Borland as part
  5.  * of a Borland product for use ONLY by licensed users of the product,
  6.  * includes CONFIDENTIAL and PROPRIETARY information of Borland.  
  7.  *
  8.  * USE OF THIS SOFTWARE IS GOVERNED BY THE TERMS AND CONDITIONS 
  9.  * OF THE LICENSE STATEMENT AND LIMITED WARRANTY FURNISHED WITH
  10.  * THE PRODUCT.
  11.  *
  12.  * IN PARTICULAR, YOU WILL INDEMNIFY AND HOLD BORLAND, ITS RELATED
  13.  * COMPANIES AND ITS SUPPLIERS, HARMLESS FROM AND AGAINST ANY CLAIMS
  14.  * OR LIABILITIES ARISING OUT OF THE USE, REPRODUCTION, OR DISTRIBUTION
  15.  * OF YOUR PROGRAMS, INCLUDING ANY CLAIMS OR LIABILITIES ARISING OUT OF
  16.  * OR RESULTING FROM THE USE, MODIFICATION, OR DISTRIBUTION OF PROGRAMS
  17.  * OR FILES CREATED FROM, BASED ON, AND/OR DERIVED FROM THIS SOURCE
  18.  * CODE FILE.
  19.  */
  20. package borland.samples.tutorial.dataset.resolverevents;
  21.  
  22. import java.awt.*;
  23. import java.awt.event.*;
  24. import borland.jbcl.control.*;
  25. import borland.jbcl.layout.*;
  26. import borland.sql.dataset.*;
  27. import borland.jbcl.dataset.*;
  28. import borland.jbcl.util.*;
  29. import java.lang.*;
  30.  
  31. public class ResolverFrame extends DecoratedFrame {
  32.   BorderLayout borderLayout1 = new BorderLayout();
  33.   BevelPanel bevelPanel1 = new BevelPanel();
  34.   Database database1 = new Database();
  35.   QueryDataSet queryDataSet1 = new QueryDataSet();
  36.   QueryResolver queryResolver1 = new QueryResolver();
  37.   ListControl listControl1 = new ListControl();
  38.   GridControl gridControl1 = new GridControl();
  39.   ButtonControl buttonControl1 = new ButtonControl();
  40.   GridBagLayout gridBagLayout1 = new GridBagLayout();
  41.  
  42.   //Construct the frame
  43.   public ResolverFrame() {
  44.     try {
  45.       jbInit();
  46.     }
  47.     catch (Exception e) {
  48.       borland.jbcl.util.Diagnostic.printStackTrace(e);
  49.     }
  50.   }
  51.  
  52.   //Component initialization
  53.   private void jbInit() throws Exception{
  54.     this.setLayout(borderLayout1);
  55.     this.setTitle("Resolver Example");
  56.     bevelPanel1.setLayout(gridBagLayout1);
  57.     database1.setConnection(new borland.sql.dataset.ConnectionDescriptor("jdbc:odbc:DataSet Tutorial", "sysdba", "masterkey", false, "sun.jdbc.odbc.JdbcOdbcDriver;"));
  58.     queryDataSet1.setQuery(new borland.sql.dataset.QueryDescriptor(database1, "select * from customer", null, true, Load.ALL));
  59.     queryDataSet1.setResolver(queryResolver1);
  60.     queryResolver1.setUpdateMode(borland.jbcl.dataset.UpdateMode.KEY_COLUMNS);
  61.     queryResolver1.addResolverListener(new ResolverFrame_queryResolver1_resolverAdapter(this));
  62.     gridControl1.setDataSet(queryDataSet1);
  63.     buttonControl1.setLabel("Save Changes");
  64.     buttonControl1.addActionListener(new ResolverFrame_buttonControl1_actionAdapter(this));
  65.     this.add(bevelPanel1, BorderLayout.CENTER);
  66.     bevelPanel1.add(gridControl1, new GridBagConstraints2(0, 0, 1, 1, 1.0, 1.0
  67.             ,GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(2, 2, 2, 2), -800, -100));
  68.     this.add(listControl1, BorderLayout.SOUTH);
  69.     this.add(buttonControl1, BorderLayout.NORTH);
  70.   }
  71.  
  72.  
  73.   // Restrict an action.
  74.   void queryResolver1_deletingRow(ReadWriteRow readWriteRow, ResolverResponse resolverResponse) throws DataSetException{
  75.     // Don't allow anybody to delete a customer record.
  76.     resolverResponse.abort();
  77.   }
  78.  
  79.   
  80.   // Ignore action errors.
  81.   void queryResolver1_insertError(DataSet dataSet, ReadWriteRow readWriteRow, DataSetException dataSetException, ErrorResponse errorResponse) throws DataSetException{
  82.     // Display error message with customer name and exception.
  83.     listControl1.addItem("Error inserting " + readWriteRow.getString("customer"));
  84.     listControl1.addItem(" " + dataSetException.getMessage());
  85.  
  86.     // Ignore the error.
  87.     errorResponse.ignore();
  88.   }
  89.  
  90.   
  91.   // Perform some basic validation of a column value before the action.
  92.   void queryResolver1_insertingRow(ReadWriteRow readWriteRow, ResolverResponse resolverResponse) throws DataSetException{
  93.     // Get the country value from the row to be inserted
  94.     String  country = readWriteRow.getString("country");
  95.  
  96.     // Validate that the new customers are only from the United States.
  97.     if (!country.equals("USA"))
  98.       resolverResponse.abort();
  99.   }
  100.  
  101.   
  102.   // Indicate that the action was performed.
  103.   void queryResolver1_insertedRow(ReadWriteRow readWriteRow) throws DataSetException{
  104.     // Add a line to the list control indicating that the new row was inserted.
  105.     listControl1.addItem(readWriteRow.getString("customer") + " was inserted.");
  106.   }
  107.  
  108.   
  109.   // Abort action if error occurs.
  110.   void queryResolver1_updateError(DataSet dataSet, ReadWriteRow readWriteRow, ReadRow readRow, ReadWriteRow readWriteRow1, DataSetException dataSetException, ErrorResponse errorResponse) throws DataSetException{
  111.     errorResponse.abort();
  112.   }
  113.  
  114.   
  115.   // Using column value as a criteria, indicate that the action was performed.
  116.   void queryResolver1_updatedRow(ReadWriteRow readWriteRow, ReadRow readRow) throws DataSetException{
  117.     String  newCustomer = readWriteRow.getString("customer");
  118.     String  oldCustomer = readRow.getString("customer");
  119.  
  120.     // If the customer name was changed, use a different message.
  121.     if (!oldCustomer.equals(newCustomer))
  122.       listControl1.addItem("Customer " + oldCustomer + " was changed to " + newCustomer);
  123.     else
  124.       listControl1.addItem("Customer " + oldCustomer + " was updated." ); 
  125.   }
  126.  
  127.  
  128.   void buttonControl1_actionPerformed(ActionEvent e) {
  129.     try {
  130.       database1.saveChanges(queryDataSet1);
  131.     }
  132.     catch(DataSetException dse){
  133.       listControl1.addItem(dse);
  134.     }
  135.   }
  136. }
  137.  
  138. class ResolverFrame_queryResolver1_resolverAdapter extends borland.jbcl.dataset.ResolverAdapter{
  139.   ResolverFrame adaptee;
  140.  
  141.   ResolverFrame_queryResolver1_resolverAdapter(ResolverFrame adaptee) {
  142.     this.adaptee = adaptee;
  143.   }
  144.  
  145.   public void deletingRow(ReadWriteRow readWriteRow, ResolverResponse resolverResponse) throws DataSetException{
  146.     adaptee.queryResolver1_deletingRow(readWriteRow, resolverResponse);
  147.   }
  148.  
  149.   public void insertError(DataSet dataSet, ReadWriteRow readWriteRow, DataSetException dataSetException, ErrorResponse errorResponse) throws DataSetException{
  150.     adaptee.queryResolver1_insertError(dataSet, readWriteRow, dataSetException, errorResponse);
  151.   }
  152.  
  153.   public void insertingRow(ReadWriteRow readWriteRow, ResolverResponse resolverResponse) throws DataSetException{
  154.     adaptee.queryResolver1_insertingRow(readWriteRow, resolverResponse);
  155.   }
  156.  
  157.   public void insertedRow(ReadWriteRow readWriteRow) throws DataSetException{
  158.     adaptee.queryResolver1_insertedRow(readWriteRow);
  159.   }
  160.  
  161.   public void updateError(DataSet dataSet, ReadWriteRow readWriteRow, ReadRow readRow, ReadWriteRow readWriteRow1, DataSetException dataSetException, ErrorResponse errorResponse) throws DataSetException{
  162.     adaptee.queryResolver1_updateError(dataSet, readWriteRow, readRow, readWriteRow1, dataSetException, errorResponse);
  163.   }
  164.  
  165.   public void updatedRow(ReadWriteRow readWriteRow, ReadRow readRow) throws DataSetException{
  166.     adaptee.queryResolver1_updatedRow(readWriteRow, readRow);
  167.   }
  168. }
  169.  
  170. class ResolverFrame_buttonControl1_actionAdapter implements java.awt.event.ActionListener {
  171.   ResolverFrame adaptee;
  172.  
  173.   ResolverFrame_buttonControl1_actionAdapter(ResolverFrame adaptee) {
  174.     this.adaptee = adaptee;
  175.   }
  176.  
  177.   public void actionPerformed(ActionEvent e) {
  178.     adaptee.buttonControl1_actionPerformed(e);
  179.   }
  180. }
  181.  
  182.