Documentation Archive Developer
Search
PATH  Documentation > WebObjects 4.5 > Getting Started With WebObjects


   

Specifying Default Values for New Enterprise Objects

When new enterprise objects are created in your application, it's common to assign default values to some of their properties. For example, in your Movies application it makes sense to assign a default value for the title attribute so a new movie won't be displayed in the list of movies as a blank line.

You could write an action method for the Insert/New button instead of binding it directly to the display group insert action method. In the custom action, you would create a new Movie object, assign default values to it, and then insert the new object into the display group. However, there are two additional ways to specify default values for new enterprise objects, without making explicit assignments:

For a particular situation, one of the approaches is usually better than the other. If the default values are intrinsic to the enterprise object, assign them in the enterprise object class. For example, consider a Member class with a memberSince property. It's likely that you would automatically assign the current date to memberSince instead of forcing a user to supply a value. You'll see how to use this technique in Adding Behavior to Your Enterprise Objects .

On the other hand, if the default values are specific to an application or to a particular user interface, explicitly initialize the object in code or specify the default values using a display group. In the Movies application, the need for default values is motivated by Main's user interface: you need to provide a default value so users can tell when a newly inserted record is selected. In another situation, you might not want a new movie to have a default title; you might instead want a new movie's title to be blank.

The Movies application specifies default values for newly created Movie objects using the display group, movieDisplayGroup.

  1. Open Main.java in Project Builder.

  2. Add the following constructor:

    public Main() {
       super();
       NSMutableDictionary defaultValues = new NSMutableDictionary();
       defaultValues.setObjectForKey("New Movie Title", "title");
       movieDisplayGroup.setInsertedObjectDefaultValues(defaultValues);
    }

    This method assigns the value "New Movie Title" as the default value for a new movie's title attribute. When movieDisplayGroup inserts a new movie (as it does when a user clicks the Insert/New button), it creates a new movie and assigns this default value to that movie.


© 1999 Apple Computer, Inc. – (Last Updated 24 Aug 99)