home *** CD-ROM | disk | FTP | other *** search
Java Source | 1997-07-24 | 5.6 KB | 133 lines |
- package borland.samples.intl.application;
-
- import java.awt.*;
- import java.text.*;
- import java.util.*;
- import borland.jbcl.control.*;
- import borland.jbcl.dataset.*;
-
- import borland.samples.intl.beans.event.*;
-
- /**
- * Resourceable DataSetExceptionHandler for the application. In
- * AppDataModule's constructor, ResourcedDataSetExceptionHandler is
- * registered as the handler for DataSetExceptions for the
- * application. Doing so results in our error handler, rather than
- * the standard handler, being called whenever a DataSetException
- * occurs. By examining the Exception event's error code or message,
- * we can localize error messages displayed to the user for the proper
- * locale. It is possible to provide error handlers to display
- * localized messages for specific error conditions, e.g, on
- * 'addError' or 'deleteError' events. However, in this application
- * we take advantage of the fact that all errors are funneled into a
- * single error handler.
- */
- public class ResourcedDataSetExceptionHandler implements ExceptionListener, LocaleChangeListener {
- private AppDataModule appDataModule;
- ResourceBundle textRes = ResourceBundle.getBundle("borland.samples.intl.application.resources.TextRes");
- ResourceBundle textRes2 = ResourceBundle.getBundle("borland.samples.intl.gui.resources.TextRes");
- protected MessageFormat formattedMessage = new MessageFormat(textRes.getString("enter_value"));
-
- public ResourcedDataSetExceptionHandler(AppDataModule appDataModule) {
- this.appDataModule = appDataModule;
- LocaleChangeManager.getLocaleChangeManager().addLocaleChangeListener(this);
- }
-
- public void exception(ExceptionEvent event) {
- Component component = event.getComponent();
- Frame frame = null;
-
- while (component != null) {
- if (component instanceof Frame) {
- frame = (Frame) component;
- break;
- }
- component = component.getParent();
- }
-
- if (frame == null) {
- frame = new Frame();
- }
- Message message = new Message();
- message.setFrame(frame);
- Object [] messageArgs;
- String messageString = null;
- int errorCode;
-
- if (event.getException() instanceof DataSetException) {
- errorCode = ((DataSetException) event.getException()).getErrorCode();
- } else {
- errorCode = -1;
- }
-
- System.out.println("Errordialog (" + errorCode + "): " + event.getException().getMessage());
-
- // At least one required field is missing.
- // Find the empty field and display its name in an error message dialog.
- if (errorCode == ValidationException.INVALID_ROW_VALUES) {
- try {
- if (event.getDataSet() == appDataModule.getOrderDataSet()) {
- String [] requiredFields = new String [] { "payment_method", "credit_card_no", "card_expiration_date" };
-
- for (int i = 0; i < requiredFields.length; i++) {
- if (appDataModule.getOrderDataSet().getString(requiredFields[i]).length() == 0) {
- messageArgs = new Object [] { textRes2.getString(requiredFields[i]) };
- messageString = formattedMessage.format(messageArgs);
- break;
- }
- }
- } else if (event.getDataSet() == appDataModule.getCustomerDataSet()) {
- String [] requiredFields = new String [] { "last_name", "first_name", "address1", "city", "province",
- "country" };
- for (int i = 0; i < requiredFields.length; i++) {
- if (appDataModule.getCustomerDataSet().getString(requiredFields[i]).length() == 0) {
- messageArgs = new Object [] { textRes.getString(requiredFields[i]) };
- messageString = formattedMessage.format(messageArgs);
- break;
- }
- }
- }
- } catch (Exception e) {
- borland.jbcl.util.Diagnostic.printStackTrace(e);
- }
- // A value in a column is less than the minimum allowed value.
- // Since the only minimum validation constraint we placed was on the
- // credit card expiration date field, display a localized
- // message indicating that the credit card has already expired.
- } else if ((errorCode == ValidationException.LESS_THAN_MIN) &&
- ((ValidationException) event.getException()).getErrorColumn().getColumnName().equals("card_expiration_date")) {
- messageString = textRes.getString("card_expired");
- // One or more characters necessary to satisfy an edit mask pattern
- // are missing.
- } else if (errorCode == ValidationException.INVALID_FORMAT) {
- messageString = textRes.getString("format_error");
- // User tried to edit a value in a read-only column.
- } else if (errorCode == ValidationException.READ_ONLY_COLUMN) {
- messageString = textRes.getString("read_only");
- // User tried inserting, deleting, or indirectly posting a row
- // which we vetoed on an 'inserting', 'deleting', 'updating', or
- // 'adding' event. After checking that it was indeed an event
- // we want to silently ignore, we simply return without displaying
- // any error message.
- } else if ((errorCode == ValidationException.APPLICATION_ERROR) &&
- (event.getException().getMessage().equals("<ignore>"))) {
- return;
- }
-
- // If we didn't have a localized message for the error condition,
- // go ahead and display the message in the language of the current
- // locale.
- if (messageString == null) {
- messageString = event.getException().getMessage();
- }
-
- message.setLabels(new String[] {textRes.getString("OK")});
- message.setMessage(messageString);
- message.setVisible(true);
- }
-
- public void localeChanged(LocaleChangeEvent e) {
- textRes = ResourceBundle.getBundle("borland.samples.intl.application.resources.TextRes");
- }
- }
-