Next Previous Up Top Contents Index

5.3.2 Adding and removing tasks from the task list

5.3.2.1 DUIM support for adding and removing tasks

This section describes the methods necessary to provide support in the task list manager GUI for adding and removing tasks.

Add the code described in this section to frame.dylan.

The code for frame-add-task is as follows:

define method frame-add-task (gadget :: <gadget>) => ()
  let frame = sheet-frame(gadget);
  let task-list = frame-task-list(frame);
  let (name, priority) = prompt-for-task(owner: frame);
  if (name & priority)
    let new-task = make(<task>, name: name, priority: priority);
    add-task(task-list, new-task);
    refresh-task-frame(frame);
    frame-selected-task(frame) := new-task
  end
end method frame-add-task;

The method takes a gadget as an argument and returns no values. The argument is the gadget which is used to invoke it, which in the case of the task list manager means either add-menu-button (in the Task menu of the application) or add-button (on the tool bar). The frame-add-task method then sets a number of local variables:

frame

The frame containing the gadget passed as an argument.

task-list

The value of the frame-task-list slot for frame. This identifies the instance of <task-list> to which a task is to be added.

name

The text of the task to be added.

priority

The priority of the task to be added.

As with other DUIM methods you have seen, frame and task-list are specified using known slot values about the gadget supplied to frame-add-task, and the frame that contains the gadget. The name and priority values are specified by calling the prompt-for-task method defined in Section 3.8 on page 25. This method displays a dialog into which the user types the text for the new task and chooses the priority, both of which values are returned from prompt-for-task.

Once all the local variables have been specified, the main body of code for the method, repeated below, is executed.

if (name & priority)
  let new-task = make(<task>, name: name, priority: priority);
  add-task(task-list, new-task);
  refresh-task-frame(frame);
  frame-selected-task(frame) := new-task
end

This consists of four expressions around which is wrapped an if statement.

1. The first expression creates a new task from the values of the name and priority local variables.
2. The second expression adds the new task to task list, by calling the add-task function.
3. The third expression refreshes the display of the task list in the task list manager, so that the new task is displayed on the screen once it has been added.
4. The fourth expression ensures that the new task is selected in the task list manager. The frame-selected--task method is described in Section 5.3.3 on page 64.

The if statement ensures that all the information needed to construct the new task is specified before the new task is created.

The add-task function is described in Section 5.3.2.2 on page 63.

The code for frame-remove-task is as follows:

define method frame-remove-task (gadget :: <gadget>) => ()
  let frame = sheet-frame(gadget);
  let task = frame-selected-task(frame);
  let task-list = frame-task-list(frame);
  if (notify-user(format-to-string
                    ("Really remove task %s", task.task-name),
                  owner: frame, style: #"question"))
    frame-selected-task(frame) := #f;
    remove-task(task-list, task);
    refresh-task-frame(frame)
  end
end method frame-remove-task;

As with frame-add-task, this method takes the gadget that is used to invoke it as an argument and returns no values. In the case of the task list manager, the gadget is either remove-menu-button (in the Task menu of the application) or remove-button (on the tool bar). The frame-remove-task method then sets a number of local variables:

frame

The frame containing the gadget passed as an argument.

task

The task that is to be removed. The task to be removed is the one selected in the list of tasks on screen. The method frame-selected-task is called to determine which task this is.

task-list

The value of the frame-task-list slot for frame. This identifies the instance of <task-list> from which a task is to be removed.

The method frame-selected-task is described in Section 5.3.3 on page 64.

Once these local variables have been set, the rest of the code goes about removing the task. The code consists of three expressions around which is wrapped an if statement, as shown below.

if (notify-user(format-to-string
                  ("Really remove task %s", task.task-name),
                owner: frame, style: #"question"))
  frame-selected-task(frame) := #f;
  remove-task(task-list, task);
  refresh-task-frame(frame)
end

Notice here that the method notify-user is used as the condition in the if statement: if the call to notify-user returns #t, then the subsequent expressions are executed. This use of notify-user illustrates how you can use the method to generate a yes-no question for the user to answer, by using the style: init-keyword. You might like to compare the user of notify-user in this method with its use in open-file or save-as-file; essentially, the only difference is in the use of the style: init-keyword.

If the call to notify-user returns #t, then three expressions are executed:

1. The first calls the setter for frame-selected-task, to ensure that no items in the task list are selected.
2. The second calls the function remove-task, which removes task from task-list.
3. Then, refresh-task-frame is called to ensure that the task that has been removed is no longer displayed in the list of tasks on the screen.

The methods defined for frame-selected-task are described in Section 5.3.3 on page 64. The function remove-task is described in Section 5.3.2.2 on page 63. The refresh-task-frame method is described in Section 5.3.3 on page 64.


Building Applications Using DUIM - 26 May 1999

Next Previous Up Top Contents Index