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

  1. package borland.samples.intl.application;
  2.  
  3. import java.awt.*;
  4. import java.text.*;
  5. import java.util.*;
  6. import borland.jbcl.control.*;
  7. import borland.jbcl.dataset.*;
  8.  
  9. import borland.samples.intl.beans.event.*;
  10.  
  11. /**
  12.  * Resourceable DataSetExceptionHandler for the application.  In
  13.  * AppDataModule's constructor, ResourcedDataSetExceptionHandler is
  14.  * registered as the handler for DataSetExceptions for the
  15.  * application.  Doing so results in our error handler, rather than
  16.  * the standard handler, being called whenever a DataSetException
  17.  * occurs.  By examining the Exception event's error code or message,
  18.  * we can localize error messages displayed to the user for the proper
  19.  * locale.  It is possible to provide error handlers to display
  20.  * localized messages for specific error conditions, e.g, on
  21.  * 'addError' or 'deleteError' events.  However, in this application
  22.  * we take advantage of the fact that all errors are funneled into a
  23.  * single error handler.
  24.  */
  25. public class ResourcedDataSetExceptionHandler implements ExceptionListener, LocaleChangeListener {
  26.   private AppDataModule appDataModule;
  27.   ResourceBundle textRes = ResourceBundle.getBundle("borland.samples.intl.application.resources.TextRes");
  28.   ResourceBundle textRes2 = ResourceBundle.getBundle("borland.samples.intl.gui.resources.TextRes");
  29.   protected MessageFormat formattedMessage = new MessageFormat(textRes.getString("enter_value"));
  30.  
  31.   public ResourcedDataSetExceptionHandler(AppDataModule appDataModule) {
  32.     this.appDataModule = appDataModule;
  33.     LocaleChangeManager.getLocaleChangeManager().addLocaleChangeListener(this);
  34.   }
  35.  
  36.   public void exception(ExceptionEvent event) {
  37.     Component component = event.getComponent();
  38.     Frame frame = null;
  39.  
  40.     while (component != null) {
  41.       if (component instanceof Frame) {
  42.     frame = (Frame) component;
  43.     break;
  44.       }
  45.       component = component.getParent();
  46.     }
  47.  
  48.     if (frame == null) {
  49.       frame = new Frame();
  50.     }
  51.     Message message = new Message();
  52.     message.setFrame(frame);
  53.     Object [] messageArgs;
  54.     String messageString = null;
  55.     int errorCode;
  56.  
  57.     if (event.getException() instanceof DataSetException) {
  58.       errorCode = ((DataSetException) event.getException()).getErrorCode();
  59.     } else {
  60.       errorCode = -1;
  61.     }
  62.  
  63.     System.out.println("Errordialog (" + errorCode + "): " + event.getException().getMessage());
  64.  
  65.     // At least one required field is missing.
  66.     // Find the empty field and display its name in an error message dialog.
  67.     if (errorCode == ValidationException.INVALID_ROW_VALUES) {
  68.       try {
  69.     if (event.getDataSet() == appDataModule.getOrderDataSet()) {
  70.       String [] requiredFields = new String [] { "payment_method", "credit_card_no", "card_expiration_date" };
  71.  
  72.       for (int i = 0; i < requiredFields.length; i++) {
  73.         if (appDataModule.getOrderDataSet().getString(requiredFields[i]).length() == 0) {
  74.           messageArgs = new Object [] { textRes2.getString(requiredFields[i]) };
  75.           messageString = formattedMessage.format(messageArgs);
  76.           break;
  77.         }
  78.       }
  79.     } else if (event.getDataSet() == appDataModule.getCustomerDataSet()) {
  80.       String [] requiredFields = new String [] { "last_name", "first_name", "address1", "city", "province",
  81.                                "country" };
  82.       for (int i = 0; i < requiredFields.length; i++) {
  83.         if (appDataModule.getCustomerDataSet().getString(requiredFields[i]).length() == 0) {
  84.           messageArgs = new Object [] { textRes.getString(requiredFields[i]) };
  85.           messageString = formattedMessage.format(messageArgs);
  86.           break;
  87.         }
  88.       }
  89.     }
  90.       } catch (Exception e) {
  91.     borland.jbcl.util.Diagnostic.printStackTrace(e);
  92.       }
  93.       // A value in a column is less than the minimum allowed value.
  94.       // Since the only minimum validation constraint we placed was on the
  95.       // credit card expiration date field, display a localized
  96.       // message indicating that the credit card has already expired.
  97.     } else if ((errorCode == ValidationException.LESS_THAN_MIN) &&
  98.            ((ValidationException) event.getException()).getErrorColumn().getColumnName().equals("card_expiration_date")) {
  99.     messageString = textRes.getString("card_expired");
  100.       // One or more characters necessary to satisfy an edit mask pattern
  101.       // are missing.
  102.     } else if (errorCode == ValidationException.INVALID_FORMAT) {
  103.       messageString = textRes.getString("format_error");
  104.       // User tried to edit a value in a read-only column.
  105.     } else if (errorCode == ValidationException.READ_ONLY_COLUMN) {
  106.       messageString = textRes.getString("read_only");
  107.       // User tried inserting, deleting, or indirectly posting a row
  108.       // which we vetoed on an 'inserting', 'deleting', 'updating', or
  109.       // 'adding' event.  After checking that it was indeed an event
  110.       // we want to silently ignore, we simply return without displaying
  111.       // any error message.
  112.     } else if ((errorCode == ValidationException.APPLICATION_ERROR) &&
  113.                (event.getException().getMessage().equals("<ignore>"))) {
  114.       return;
  115.     }
  116.  
  117.     // If we didn't have a localized message for the error condition,
  118.     // go ahead and display the message in the language of the current
  119.     // locale.
  120.     if (messageString == null) {
  121.       messageString = event.getException().getMessage();
  122.     }
  123.  
  124.     message.setLabels(new String[] {textRes.getString("OK")});
  125.     message.setMessage(messageString);
  126.     message.setVisible(true);
  127.   }
  128.  
  129.   public void localeChanged(LocaleChangeEvent e) {
  130.     textRes = ResourceBundle.getBundle("borland.samples.intl.application.resources.TextRes");
  131.   }
  132. }
  133.