In this lesson you enable the My Company Directory application to add a new employee record.
Because the behavior of the application is more complicated and dynamic for adding a new employee, this exercise is inherently more complex and requires you to make some manual changes to the source code. Also, this exercise demonstrates some advanced capabilities of the data objects, and it gives you a creative example for ways that you can use the binders and data objects to fit your needs.
The following list describes the required behavior of the application:
The sample Web service provides a createNewFullEmployeeRecord service that provides a new, blank employee record that is populated with the next available employee ID number. This blank record can then be populated with a new employee's information and submitted back to the Web service.
Because the details fields and the Update button need to switch modes (for both performing an update and creating a new employee), they need to be bound to two different data objects at different times. To facilitate this step, you will add a Basic Data Object named switchingDataObject. You will use this Basic Data Object to switch the binding for the text fields between the selectedEmployeeRecord and the newEmployeeRecord.
Even though each of the employee details fields is already bound to selectedEmployeeRecord, you will now bind them to switchingDataObject. After binding the fields, you can dynamically switch between data objects for the fields, depending on whether you are modifying an existing employee record or adding a new employee record.
The following updateMode() method checks to see if the mode flag is set to new, then changes the application's behavior accordingly. By default, the Boolean flag isNewMode is set to false, and the updateMode() method enables the employees table and the filter field, and sets the text on the Update button to "Update". If isNewMode is set to true, the employees table is disabled and cleared of any selection, the filter field is disabled, and the text on the Update button is set to "Add".
private boolean isNewMode = false; private void updateMode() { if (isNewMode) { getEmployeesTable().clearSelection(); getEmployeesTable().setEnabled(false); getFilterField().setEditable(false); getUpdateCreateButton().setText("Add"); } else { getEmployeesTable().setEnabled(true); getFilterField().setEditable(true); getUpdateCreateButton().setText("Update"); } }
In this step, you add event code for when the New button is clicked. The event tells the switchingDataObject to use the newEmployeeRecord data object, sets the mode flag to "new," and runs the updateMode() method that you added in the previous step.
In a previous lesson, you programmed the Update button to use the modifyEmployee method on the Web service. That action is implemented as a SwingDataServiceAction. One of the properties of the SwingDataServiceAction is the source object, which acts as the argument for the service. The source object for the modify action is currently set to selectedEmployeeRecord. In order to program the button to control both an update and an addition, you will reconfigure the button's action to use switchingDataObject as an argument to the modifyEmployee service.
After the Update button is clicked and the action is complete on the Web service, you want the application to go back into its default mode and behavior. To do this, you add an event listener on the button's action binder that will update the mode and refresh the table after the update or addition is performed.
modifyEmployeeAction.addActionBinderListener( new jve.generated.IActionBinder.ActionBinderListener() { public void afterActionPerformed(jve.generated.IActionBinder.ActionBinderEvent e) { if (isNewMode) { //Go back to using the selectedEmployeeRecord getSwitchingDataObject().setSourceObject(getSelectedEmployeeRecord()); //Revert out of new mode isNewMode = false; updateMode(); } // Refresh the table's data object getLightEmployeeRecordRows().refresh(); } public void beforeActionPerformed(jve.generated.IActionBinder.ActionBinderEvent e) {} });