Tuesday, December 11, 2007

Tutorial: Moving items between lists

A knights tale ...
Suppose you're creating an application to equip knights with armor and weapons equipment. One of the constraints is that a knight can only carry a certain amount of weight, so we're only allowed to let him pick three items.

In battle it's important not to rely on one fighting strategy solely so we will add another constraint: A knight can only carry one of each item.

So, we want to develop a user interface where the user can pick up to three items on a list, transfer those items to another list (the knights backpack), and when an item is added to our backpack it should disappear from the first list.

Dynamic lists
Let’s create two dynamic lists to hold the added items and the not-added items. We need to tell Socrates that the lists are dynamic, that’s done on the lists info pane, section Inference.

We could name the lists ItemsNotAdded and ItemsAdded.

Add the two lists to a dialog, dlg_Equipment:

Double-click in the OnEnter field of the dialog to let Socrates create an OnEnter procedure (dlg_Equipment_OnEnter):

We also need a list to hold the initial items a knight can carry. Create a list called KnightItems and add the following records to it:Locate the newly created procedure dlg_Equipment_OnEnter and add the following code to it:

@dim i:n

@rem Populate the dynamic list with the content from KnightItems
@for i = 1 to KnightItems.nValues
@do ItemsNotAdded.addInstance(KnightItems.ItemName[i])
@next i

Now, when the dialog starts up it will populate the ItemsNotAdded list with the contents of the KnightItems list.

To test the application so far we just need to add a call to the dlg_Equipment dialog in the Main Agenda:
@do dlg_Equipment

Move equipment from one list to another
Ok, now it's time to do the fun stuff :-) To move the knights equipment from one list to another we need two buttons; one for moving and one for re-moving.

Add them to the dialog:

Create a procedure prc_addEquipment and set the move button to activate this when clicked. Add the following code to prc_addEquipment:

@dim nSelectedIndex:n

@rem Only three items is allowed
@if ItemsAdded.nValues < 3
@rem Only do this if something is selected
@if ItemsNotAdded.nSelected = 1
@assign nSelectedIndex = ItemsNotAdded.selectedIndex
@rem Move it!
@do ItemsAdded.addInstance(ItemsNotAdded.ItemName[nSelectedIndex])
@do ItemsNotAdded.delInstance(nSelectedIndex)

Similar, we'll create a procedure for removing equipment and let the Remove button activate that on the click event, prc_removeEquipment:

@dim nSelectedIndex:n

@if ItemsAdded.nValues > 0
@if ItemsAdded.nSelected = 1
@assign nSelectedIndex = ItemsAdded.selectedIndex
@do ItemsNotAdded.addInstance(ItemsAdded.ItemName[nSelectedIndex])
@do ItemsAdded.delInstance(nSelectedIndex)

@rem Remove the pointer to "shadow" entry on list
@clear ItemsAdded
Press F9 and test the application:

With two dynamic lists we showed it was relative easy to make an application where items where moved back and forth from list to list.

No comments: