Download
FAQ History |
API
Search Feedback |
Application Client
Sometimes, enterprise applications use a stand-alone client application for handling tasks such as system or application administration. For example, the Duke's Bank application uses an application client to administer customers and accounts. This capability is useful in the event that the site becomes inaccessible for any reason or if a customer prefers to communicate things such as changes to account information by phone.
The application client shown in Figure 36-4 handles basic customer and account administration for the banking application through a Swing user interface. The bank administrator can perform any of the following functions by making menu selections.
Figure 36-4 Application Client
Customer administration:
Account administration:
Error and informational messages appear in the left pane under
Application message watch:
, and data is entered and displayed in the right pane.The Classes and Their Relationships
The source code for the application client is in the
<
INSTALL
>/j2eetutorial14/examples/bank/src/com/sun/ebank/appclient/
directory. The application client is divided into three classes:BankAdmin
,EventHandle
, andDataModel
; the relationships among the classes are depicted in Figure 36-5.
Figure 36-5 Relationships among Application Client Classes
BankAdmin
builds the initial user interface, creates theEventHandle
object, and provides methods for theEventHandle
andDataModel
objects to call when they update the user interface.
EventHandle
listens for button clicks by the user, takes action based on which button the user clicks, creates theDataModel
object, calls methods in theDataModel
object to write data to and read data from the enterprise beans, and calls methods in theBankAdmin
object to update the user interface when actions complete.
DataModel
retrieves data from the user interface, performs data checks, writes valid data to and reads stored data from the underlying database, and calls methods in theBankAdmin
object to update the user interface based on the success of the database read or write operation.BankAdmin Class
The
BankAdmin
class, which creates the user interface, is the class that contains themain
method and providesprotected
methods for the otherBankAdmin
application classes to call.main Method
The
main
method creates instances of theBankAdmin
andEventHandle
classes. Arguments passed to themain
method are used to initialize a locale, which is passed to theBankAdmin
constructor.public static void main(String args[]) { String language, country; if(args.length == 1) { language = new String(args[0]); currentLocale = new Locale(language, ""); } else if(args.length == 2) { language = new String(args[0]); country = new String(args[1]); currentLocale = new Locale(language, country); } else currentLocale = Locale.getDefault(); frame = new BankAdmin(currentLocale); frame.setTitle(messages.getString( "CustAndAccountAdmin")); WindowListener l = new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }; frame.addWindowListener(l); frame.pack(); frame.setVisible(true); ehandle = new EventHandle(frame, messages); System.exit(0); } }Constructor
The
BankAdmin
constructor creates the initial user interface, which consists of a menu bar and two panels. The menu bar contains the customer and account menus, the left panel contains a message area, and the right panel is a data display or update area.Class Methods
The
BankAdmin
class provides methods that other objects call when they need to update the user interface. These methods are as follows:
clearMessages
: Clears the application messages that appear in the left panelresetPanelTwo
: Resets the right panel when the user selects OK to signal the end of a data view or update operationcreatePanelTwoActLabels
: Creates labels for account fields when account information is either viewed or updatedcreateActFields
: Creates account fields when account information is either viewed or updatedcreatePanelTwoCustLabels
: Creates labels for customer fields when customer information is either viewed or updatedcreateCustFields
: Creates customer fields when customer information is either viewed or updatedaddCustToActFields
: Creates labels and fields when an add customer to account operation is invokedmakeRadioButtons
: Makes radio buttons for selecting the account type when a new account is createdgetDescription
: Makes the radio button labels that describe each available account typeEventHandle Class
The
EventHandle
class implements theActionListener
interface, which provides a method interface for handling action events. Like all other interfaces in the Java programming language,ActionListener
defines a set of methods but does not implement their behavior. Instead, you provide the implementations because they take application-specific actions.Constructor
The constructor receives an instance of the
ResourceBundle
andBankAdmin
classes and assigns them to its private instance variable so that theEventHandle
object has access to the application client's localized text and can update the user interface as needed. The constructor also calls thehookupEvents
method to create the inner classes to listen for and handle action events.public EventHandle(BankAdmin frame, ResourceBundle messages) { this.frame = frame; this.messages = messages; this.dataModel = new DataModel(frame, messages); //Hook up action events hookupEvents(); }actionPerformed Method
The
ActionListener
interface has only one method, theactionPerformed
method. This method handles action events generated by theBankAdmin
user interface when users create a new account. Specifically, it sets the account description when a bank administrator selects an account type radio button, and it sets the current balance to the beginning balance for new accounts when a bank administrator presses the Return key in the Beginning Balance field.hookupEvents Method
The
hookupEvents
method uses inner classes to handle menu and button press events. An inner class is a class that is nested or defined inside another class. Using inner classes in this way modularizes the code, making it easier to read and maintain.EventHandle
inner classes manage the following application client operations:DataModel Class
The
DataModel
class provides methods for reading data from the database, writing data to the database, retrieving data from the user interface, and checking that data before it is written to the database.Constructor
The constructor receives an instance of the
BankAdmin
class and assigns it to its private instance variable so that theDataModel
object can display error messages in the user interface when itscheckActData
,checkCustData
, orwriteData
method detects errors. The constructor also receives an instance of theResourceBundle
class and assigns it to its private instance variable so that theDataModel
object has access to the application client's localized text.Because the
DataModel
class interacts with the database, the constructor also has the code to establish connections with the remote interfaces for theCustomerController
andAccountController
enterprise beans, and the code to use their remote interfaces to create instances of theCustomerController
andAccountController
enterprise beans.//Constructor public DataModel(BankAdmin frame, ResourceBundle messages) { this.frame = frame; this.messages = messages; //Look up and create CustomerController bean try { CustomerControllerHome customerControllerHome = EJBGetter.getCustomerControllerHome(); customer = customerControllerHome.create(); } catch (Exception namingException) { namingException.printStackTrace(); } //Look up and create AccountController bean try { AccountControllerHome accountControllerHome = EJBGetter.getAccountControllerHome(); account = accountControllerHome.create(); } catch (Exception namingException) { namingException.printStackTrace(); } }Methods
The
getData
method retrieves data from the user interface text fields and uses theString.trim
method to remove extra control characters such as spaces and returns. Its one parameter is aJTextfield
so that any instance of theJTextfield
class can be passed in for processing.private String getData(JTextField component) { String text, trimmed; if(component.getText().length() > 0) { text = component.getText(); trimmed = text.trim(); return trimmed; } else { text = null; return text; } }The
checkCustData
method stores customer data retrieved by thegetData
method, but first it checks the data to be sure that all required fields have data, that the middle initial is no longer than one character, and that the state is no longer than two characters. If everything checks out, thewriteData
method is called. If there are errors, they are printed to the user interface in theBankAdmin
object. ThecheckActData
method uses a similar model to check and store account data.The
createCustInf
andcreateActInf
methods are called by theEventHandle
class to refresh the customer and account information display in the event of a view, update, or add action event.Create Customer Information
For a view or update event, the
createCustInf
method gets the customer information for the specified customer from the database and passes it to thecreateCustFields
method in theBankAdmin
class. A Boolean variable is used to determine whether thecreateCustFields
method should create read-only fields for a view event or writable fields for an update event.For a create event, the
createCustInf
method calls thecreateCustFields
method in theBankAdmin
class with null data and a Boolean variable to create empty editable fields for the user to enter customer data.Create Account Information
For a view or update event, the
createActInf
method gets the account information for the specified account from the database and passes it to thecreateActFields
method in theBankAdmin
class. A Boolean variable is used to determine whether thecreateActFields
method should create read-only fields for a view event or writable fields for an update event.For a create event, the
createActInf
method calls thecreateActFields
method in theBankAdmin
class with null data and a Boolean variable to create empty editable fields for the user to enter customer data.Adding a customer to an account or removing an account events operate directly on the database without creating any user interface components.
Download
FAQ History |
API
Search Feedback |
All of the material in The J2EE(TM) 1.4 Tutorial is copyright-protected and may not be published in other works without express written permission from Sun Microsystems.