< Previous | Next >

Lesson 2.6: Set up actions and bindings for adding a new employee

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:

  • When you click the New button, the following behavior occurs:
    • The selection is cleared on the employees table, and the table is disabled.
    • Clearing the table selection causes the Delete button to be disabled.
    • The Filter field is disabled.
    • The details fields are cleared of any values, except for a new employee ID.
    • The text on the Update button switches to Add.
  • When you click the Add button, the following behavior occurs:
    • The values entered into the details fields are added to the directory as a new employee record.
    • The table is enabled and the values are refreshed.
    • The Filter field is enabled.
    • The text on the Add button switches back to Update.

Add a new Data Source Data Object that calls createNewFullEmployeeRecord()

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.

  1. On the palette of the Java visual editor, expand the Data Objects drawer and select Data Source Data Object.
  2. Move your mouse pointer over the blank area of the design view, or free-form area, and left-click to drop the Data Source Data Object. A new Data Source Data Object is added and shown on the free-form area:

    Data Source Data Object on free-form. Not configured.

  3. Right-click the Data Source Data Object, and select Rename field. Rename the data object to newEmployeeRecord.
  4. Right-click the newEmployeeRecord data object, and select Binding Properties. The Data Binding dialog box opens.
  5. In the Data source field, select webServiceDataSource
  6. In the Service field, select createNewFullEmployeeRecord()
  7. Click OK.
On the free-form area, you can see that the newEmployeeRecord data source data object is bound to the Web service.

newEmployeeRecord data object, connected to webServiceDataSource

Add a Basic Data Object to facilitate the switching of data objects

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.

The new Basic Data Object simply points to another data object (selectedEmployeeRecord) that you defined in an earlier exercise. This new data object will become useful when you create a method that tells this basic data object to use the newEmployeeRecord that you created earlier. In other words, this basic data object will function as an intermediate data object that switches between the selectedEmployeeRecord data object and the newEmployeeRecord data object, allowing visual components in your application to work with two different data objects.
  1. On the visual editor palette, select Basic Data Object, and drop it onto the free-form area. A basicDataObject is added.

    Basic Data Object on free-form, not configured

  2. Rename the data object to switchingDataObject
  3. In the Properties view for switchingDataObject, set the sourceObject property to selectedEmployeeRecord. You can select selectedEmployeeRecord from the drop-down menu in the Value column for the property.

    Now, switchingDataObject refers to selectedEmployeeRecord and reflects the same values:

    switchingDataObject on free-form, connected to selectedEmployeeRecord data object

Rebind each employee field to the switchingDataObject

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.

For each of the fields in the Employee details section, complete the following steps:
  1. Select the field and click the Bind tab.
  2. On the Field Data Bindings dialog box, select the switchingDataObject. You previously bound the fields to the selectedEmployeeRecord.

    Field Data Bindings dialog box showing lastNameField bound to lastName property on switchingDataObject

  3. Make sure the field is still bound to the correct data object property, and click OK. If you select the field on the design view, you can see that the binder lines now point to switchingDataObject.

    Details field bound to switchingDataObject

Define a flag and a method for updating and switching modes

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".

Add the following code to your DirectoryApp.java class just before the last closing curly brace:
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");
   }
}

Add an actionPerformed event to the New button

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.

  1. In the design view, right-click the New button, and select Events > actionPerformed. The following code is generated in the getNewButton() method:
    newButton.addActionListener(new java.awt.event.ActionListener() { 
       public void actionPerformed(java.awt.event.ActionEvent e) {    
          System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
       }
    });
  2. Replace this generated stub with the following code:
    newButton.addActionListener(new java.awt.event.ActionListener() { 
       public void actionPerformed(java.awt.event.ActionEvent e) {    
          getSwitchingDataObject().setSourceObject(getNewEmployeeRecord());
          getNewEmployeeRecord().refresh();
          isNewMode = true; //sets application to new mode
          updateMode(); //changes UI according to new mode
          getLastNameField().grabFocus();
       }
    });
    

Rebind the Update button

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.

  1. In the design view, select the Update button. Notice the pink, dotted arrow showing that the selectedEmployeeRecord is the argument for the service call.
  2. Click the Bind tab on the Update button.
  3. In the Argument field, select switchingDataObject.

    Component Action Bindings for Update button now using the switchingDataObject

  4. Click OK.

    Now, notice that the button's action is now configured to use the switchingDataObject as its argument to the modifyEmployee method:

    Update button bound to Web service with switchingDataObject as the argument

Add an event to the Update button's binder to reset the mode

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.

Add the following code to the getModifyEmployeeAction() method for the Update button:
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) {} 
});

Lesson checkpoint

Now, when you run the My Company Directory application, you can click the New button and add a new employee record.

< Previous | Next >