Enhancing the Simple To-Do List Application

When you created the simple VisualAge application, you used an IListBox* part to display the items in a to-do list. The items you put in the IListBox* part were text strings created by entering text in an entry field and selecting a push button. In this case, the text strings displayed in the list box are just text strings.

Besides viewing a list of text strings, you can also use a list box to view a collection of nonvisual objects. The following list boxes that can display objects:

By connecting a collection of objects, such as an IVSequence* part, to one of these list boxes, you can view a collection of objects instead of simple text strings.

The procedure outlined in this section modifies the original To-Do List application so that the list box contains objects rather than text strings. In this case, text strings are again displayed in the list box, but are used to show the names of the objects in the sequence. When you select an item in the list box after completing this procedure, you are really selecting an object in the sequence, not the text string that represents the object.

The modified To-Do List application differs from the original application in that it uses the following parts:

The Sequence of Objects in the List Box

The following prerequisites must be met to reflect a sequence of objects in a list box:

Opening a Nonvisual Part

  1. From the Configuration tab of the IDE, select the target file in the left panel.
  2. Open the pop-up and select Add Nonvisual part into... and the Parts SmartGuide appears.
  3. In the Parts name field, enter ToDoItem and select Next.
  4. Select OK.
  5. From the Project tab of the IDE, open the pop-up and select Refresh view.
  6. Select the ToDoItem part in the parts list.
  7. Select Edit part from the right pane and the Composition Editor appears.

Creating a New Attribute

  1. Open the Part Interface Editor by selecting the right-most pie in the bottom right corner.
  2. On the Attribute page, fill in the following fields:
    Attribute name toDoItemName
    Attribute type IString
  3. Select Add with defaults. VisualAge adds the toDoItemName attribute to the ToDoItem part.

Adding an Attribute to the Preferred Features List

  1. Select the Preferred tab.
  2. Select the toDoItemName attribute.
  3. Select Add>>. VisualAge adds the toDoItemName attribute to the list of preferred features so that it will appear in the pop-up connection menu.

Specifying the .hpv and .cpv Files

When you specify the .hpv and .cpv files, VisualAge uses them for the default feature code of the toDoItemName attribute. Specify these files by doing the following:

  1. Switch to the Class Editor.
  2. Select the Generation Options tab and check the following boxes in the User files included in generation box.

Switch to the User Code tab with Required include files selected, enter <istring.h> in the bottom pane.

Generating the Default Code

  1. Select File, Save and generate, and then Part source.

    VisualAge generates the code for the ToDoItem part and stores it in files named ToDoItem.hpp and ToDoItem.cpp. These files contain the appropriate include statements for the ToDoItem.hpv and ToDoItem.cpv files. VisualAge also creates a header file for the declarations (ToDoItem.hv) and a resource include file for the class (ToDoItem.rci).

  2. Select File, Save and generate, and then Feature source. VisualAge displays the ToDoItem--Generate Feature Source Code window.
  3. Select Generate all. VisualAge generates the default code for the toDoItemName attribute and displays a message telling you where the files containing feature source code are stored. Select OK to continue.

Replacing the List Box

You need a list box that can display objects instead of just text strings. Therefore, you must replace the IListBox* part with an ICollectionViewListBox* part, and modify the ICollectionViewListBox* part to specify the type of objects it can display.

  1. Open the ToDoList part.
  2. Delete the IListBox* part.

    The Composition Editor displays a message advising you that it will delete the connections as well as the list box if you continue. You want to delete the connections because you need different connections for this example.

  3. Select OK. The Composition Editor deletes the list box and the connections.
  4. Select the Lists category from the left column of the parts palette.
  5. Select the ICollectionViewListBox* icon, from the right column of the parts palette.
  6. Place the crosshairs below the lower-left corner of the second static text part and click mouse button 1.

    A list box part that displays the items in a collection appears beneath the second static text part.

Specifying the Type of Items the List Box Can Contain

  1. Move the mouse pointer to the list box, click mouse button 2.
  2. Select Change Template Arguments.
  3. Select the value column of the class Element field, and enter ToDoItem* and select OK.

The list box can now accept only ToDoItem* objects.

Placing and Modifying an Object Factory Part in the To-Do List Application

You need a Factory part to create the objects displayed in the list box. You must also specify the type of objects the Factory part can create.

  1. Select the Models category, from the left column of the parts palette.
  2. Select the Factory icon, from the right column of the parts palette.
  3. Place an object factory part on the free-form surface to the right of the To-Do List window adjacent to the entry field.
  4. Change the text beneath the object factory icon to ItemFactory.
  5. Move the mouse pointer to the object factory, click mouse button 2, and select Change type. The Composition Editor displays a window which provides the option of designating the type of objects that the factory will create.
  6. Enter ToDoItem*.
  7. Select OK.

    The object factory can now create only ToDoItem objects.

Placing and Modifying a Variable and IVSequence* Part in the To-Do List Application

You need to use a Variable and IVSequence* part to sequentially order the objects that the Factory part creates, and you must specify the type of objects the sequence can contain.

  1. Select Options and Add part and the Composition Editor displays the Add Part window.
  2. Enter ToDoItem* in the class Name field.
  3. Enter ItemVariable in the Part Name field.
  4. Select Add and the pointer becomes crosshairs.
  5. Move the pointer to the free-form surface and click mouse button 1 and a variable part appears.
  6. Open the part pop-up and select Change type.
  7. In the field, enter ToDoItem.

Next, place the IVSequence part.

  1. Select the Models category, from the left column of the parts palette.
  2. Select the IVSequence icon, from the right column of the parts palette.
  3. Place an sequence part on the free-form surface.
  4. Open the part pop-up and select Change name.
  5. Change the name to ItemList.
  6. Select Change Template Arguments from the part pop-up and enter ToDoItem* in the class Element field.

The sequence can now accept only ToDoItem objects.

Making the New Connections

The following table outlines the connections that this example requires to add objects to and remove objects from the list box.

Table 29. Object Connections

Connect Item... Feature... To Item... Feature...
Add push button (1) buttonClickEvent ItemFactory new
ItemFactory (2) toDoItemName entry field text
ItemFactory (3) newEvent ItemList addAsLast
previous connection element ItemVariable this
ItemFactory newEvent ItemVariable this
ItemList (4) this list box items
Remove push button (5) buttonClickEvent ItemList removeAtPosition
previous connection (6) position list box selectedCollectionPosition

1. This connection causes the object factory to create a new ToDoItem object whenever a user selects Add.

2. This connection identifies text in the entry field as the name of ToDoItem objects created by the object factory. Notice that the connection line is violet, the color of a parameter connection, instead of cyan, which is the color of an attribute-to-attribute connection. The connection line is violet because the text attribute of the entry field supplies a value for the ToDoItemName attribute only when the object factory creates a new object, just as if it were satisfying a parameter of the new action.

3. This connection causes each new ToDoItem object to be added as the last object in the sequence.

4. This connection causes the list box to display all of the ToDoItem objects that the sequence contains.

5. This connection causes the object at a specified position in the sequence to be removed. This connection is incomplete because the removeAtPosition parameter must be satisfied.

6. This connection provides the position of the selected item in the collection of objects, which is the required parameter the previous connection needs to remove the object.

The next step is Building and Executing Your To-Do List Application.