Documentation Archive Developer
Search
PATH  Documentation > WebObjects 4.5 > Creating a Java Client Application: A Tutorial

   

Writing Derived Methods

One kind of behavior you might want to add to your enterprise object class is the ability to perform computations based on the values of class properties. For example, studios have movies, and the total revenue of the movies times 1.5 constitutes the studio's portfolio value. To calculate a studio's portfolio value, you could have a method in Studio.java like the following:

Studio.java

public Number portfolioValue() {
   int i,count;
   double total;
   NSArray revenues;
   total = 0.0;
   revenues = (NSArray)(movies().valueForKey("revenue"));
   count = revenues.count();
   for (i=0; i<count; i++) {
      total +=
          ((Number)(revenues.objectAtIndex(i))).doubleValue();
   }
   return new BigDecimal(total * 1.5);
}


You can display the results of this method in the user interface by forming an association between a control and the method. That way, whenever a new studio is selected or when a selected studio's movie revenues change, its portfolio value is dynamically recalculated and displayed.

  1. Add the code above to the client-side Studio.java file.

  2. Add a method as a display-group property.

    Display the Attributes view of the Inspector for the Studio EODisplayGroup.

    Add the name of the method ( portfolioValue ) you want to use in an association.

    Click Add.

    Once you've added the method as a class key, you can use it in associations. But before you do this, add the necessary user-interface control.

  3. Add text fields to the user interface.

    Drag three text fields from the Views palette.

    Make them the same size and align them in a column.

    Add labels (as shown at right) to each text field.

    Justify the fields' contents (as shown).

    Now make an association between the Revenue text field and the portfolioValue method.

  4. Associate a method with a user interface control.

    Control-drag from the Revenue text field to the Studio EODisplayGroup.

    In the Connections Inspector, choose EOControlAssoc from the pop-up list at the top of the left column.

    Select value in the left column.

    In the right column select the method ( portfolioValue ) you want to associate with the control.

    Double-click portfolioValue to connect.

    Repeat the above steps, connecting the Name field to Studio's name attribute and the Budget field to the budget attribute.

    You now need to add a formatter to the Revenue and Budget fields. The formatter isn't added automatically, because the field has no way of knowing that it's going to be used to display currency values--it's just connected to a property.

  5. From the DataViews palette, drag the currency formatter into the new text field.

    Once you've added the formatter, you can use the Inspector to change the format.

  6. Set the format.

    Select the text field, and display the Formatter view of the NSTextField Inspector. Change the format as shown.

  7. Build and test the application on the client.

    (See " Building and Testing Your Application " for details.)


© 1999 Apple Computer, Inc. – (Last Updated 13 Sep 99)