home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PC World 1998 October
/
PCWorld_1998-10_cd.bin
/
software
/
prehled
/
inprise
/
JSAMPLES.Z
/
ResourcedDataSetExceptionHandler.java
< prev
next >
Wrap
Text File
|
1998-05-08
|
7KB
|
152 lines
/*
* Copyright (c) 1997-1998 Borland International, Inc. All Rights Reserved.
*
* This SOURCE CODE FILE, which has been provided by Borland as part
* of a Borland product for use ONLY by licensed users of the product,
* includes CONFIDENTIAL and PROPRIETARY information of Borland.
*
* USE OF THIS SOFTWARE IS GOVERNED BY THE TERMS AND CONDITIONS
* OF THE LICENSE STATEMENT AND LIMITED WARRANTY FURNISHED WITH
* THE PRODUCT.
*
* IN PARTICULAR, YOU WILL INDEMNIFY AND HOLD BORLAND, ITS RELATED
* COMPANIES AND ITS SUPPLIERS, HARMLESS FROM AND AGAINST ANY CLAIMS
* OR LIABILITIES ARISING OUT OF THE USE, REPRODUCTION, OR DISTRIBUTION
* OF YOUR PROGRAMS, INCLUDING ANY CLAIMS OR LIABILITIES ARISING OUT OF
* OR RESULTING FROM THE USE, MODIFICATION, OR DISTRIBUTION OF PROGRAMS
* OR FILES CREATED FROM, BASED ON, AND/OR DERIVED FROM THIS SOURCE
* CODE FILE.
*/
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.*;
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 = java.util.ResourceBundle.getBundle("borland.samples.intl.application.resources.TextRes",
LocaleChangeManager.getLocale());
ResourceBundle textRes2 = java.util.ResourceBundle.getBundle("borland.samples.intl.gui.resources.TextRes",
LocaleChangeManager.getLocale());
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;
}
// 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 deliberately denied using setEnableInsert
// or setEnableDelete false. Silently ignore it.
} else if ((errorCode == ValidationException.INSERT_NOT_ALLOWED) ||
(errorCode == ValidationException.DELETE_NOT_ALLOWED)) {
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 = java.util.ResourceBundle.getBundle("borland.samples.intl.application.resources.TextRes", e.getLocale());
textRes2 = java.util.ResourceBundle.getBundle("borland.samples.intl.gui.resources.TextRes", e.getLocale());
}
}