![]() |
|
Using the PFC TreeView Object U_TV (PB 7,
8, and 9)
1. Overview
The PFC TreeView class u_tv has been precoded to allow Powerbuilder
programmers quick and easy use of the TreeView control. The problem is,
you need to know how it's been implemented in order to take advantage of
it. This faxline walks through a couple examples of a u_tv implementation.
These examples are from pbl's shipped with the PFC
A very high level description of how u_tv has been implemented in the PFC (Powerbuilder Foundation Class library) follows. Each level of the treeview is associated with a datastore. When the user expands the treeview by clicking the "+" the next datastore is retrieved and displayed in the treeview control. This association between the treeview level and a datastore is called "registration" and is done by the of_SetDatasource function. Basically, the programmer registers each treeview level and as the user expands each level the precoded methods from u_tv handle the rest.
2. Linking a Treeview and Listview together
This example window (w_treeviewlistview) has a treeview, a listview and a commandbutton on it. Each of these controls has only one coded event; and the window open event is also coded to make a total of four coded events. The commandbutton's clicked event is a one liner that closes the window; and the listview's constructor event is also a one liner that sets an instance variable. The two events that are of importance for this example are;
When the user opens this window, it initially displays five regions in the treeview (Canada, Eastern, Central, South, Western) and the states that exist in Canada in the listview. The user can then click one of the regions in the treeview and the states for the region will display in the listview. The user has caused the treeview's SelectionChanged event to fire which in turn populates the listview control with states. If the user clicks a "+" to the left of one of the regions, that region will expand displaying the states that exist in that region. The states were setup in the window's open event by calling the of_SetDatasource function. If the "+" next to a state is clicked, the customers in that state are displayed (also coded in the windows open event). This causes the treeview's "ItemExpanding" event to fire which populates the next level of the treeview control. Notice that this example does not have any code in the "ItemExpanding" event of tv_1; it's all precoded in the PFC ancestors. Registering the treeview levels using the of_SetDatasource functions allows for this.
2.1 The Window "Open" Event
Populate the window title with the classname of the window
tv_1.AddPicture("globals!") This sets up PictureIndex 1 for the
treeview
lv_1.AddLargePicture("globals!") This sets up LargePictureIndex
1 for the listview
lv_1.AddSmallPicture("globals!") This sets up SmallPictureIndex
1 for the listview
The next five calls to of_SetDatasource register the datasource for
each level of the treeview. This prepares the treeview to be populated
later (by the of_populatelevel function). There are five levels,
The parameters for the of_SetDatasource function are: level, datawindow object, transaction object, column to display as the label, the retrieval arguments, and whether the DW object is to be used recursively. Optional parameters are: picture index, selected picture index, state picture index, and overlay picture index
The first call to of_SetDatasource registers level 1 of the treeview
with the Region from the d_region datawindow object. It will not be recursive
(false), and the PictureIndex to be used is 1 which is "globals!" (see
above), and the SelectedPictureIndex is 7 "custom050", which looks like
a open file folder. See figure 1
The second call to of_SetDatasource registers level 2 of the treeview
with the State from the d_regionstate datawindow object. It will get its
retrieval argument from the parent treeviewitem one level above using the
"region" column from the datawindow object. It will not be recursive (false),
and the PictureIndex to be used is 2 which is "parameter!" (see above),
and the SelectedPictureIndex is 7 "custom050" which looks like a open file
folder.
The third call to of_SetDatasource registers level 3 of the treeview
with the Company name from the d_regionstatecust datawindow object. It
will get the first retrieval argument from the parent treeviewitem 2 levels
above using the "region" column from the datawindow object, and the second
argument from the parent treeviewitem 1 level above using the State (states_state_id)
column. It will not be recursive (false), and the PictureIndex to be used
is 3 which is "library!" (see above), and the SelectedPictureIndex is 7
"custom050" which looks like a open file folder.
The fourth call to of_SetDatasource registers level 4 of the treeview
with the Sales Reps. (employee_emp_lname) from the d_regionstatecustrep
datawindow object. It will get its retrieval argument from the parent treeviewitem
1 level above using the "customer_id" column from the datawindow object.
It will not be recursive (false), and the PictureIndex to be used is 4
which is "custom076!" (see above), and the SelectedPictureIndex is 7 "custom050"
which looks like a open file folder.
The fifth call to of_SetDatasource registers level 5 of the treeview
with the Purchase Order # (order_id_string) from the d_regionstatecustrepord
datawindow object. It will get the first retrieval argument from the parent
treeviewitem 2 levels above using the "customer_id" column from the datawindow
object, and the second argument from the parent treeviewitem 1 level above
using the Sales Reps. ID (employee_emp_id) column. It will not be recursive
(false), and the PictureIndex to be used is 5 which is "scriptyes!" (see
above), and the SelectedPictureIndex is 7 "custom050" which looks like
a open file folder.
Make the initial retrieve for the first level of the above registered
TreeViews.
2.2 The Treeview tv_1 "SelectionChanged" Event Following is a line by line explanation of the treeview tv_1 selectionchanged event.
Long ll_Parent, ll_Row Declare local variables.
SetPointer(HourGlass!) Set the mouse pointer to an hourglass.
Turn off Redraw for the listview so there is no flicker every time an
item is added.
Use the "newhandle" event argument to get the new item the user selected.
The next block of code determines the level of the treeview item the
user selected and then uses that information to populate the listview control
with the same data.
Case 2
If there is a problem with the return code of of_SetDatasource then
display a message and exit out of this event
In this example, the Treeview and Listview are displaying the same information
(the listview has more details if in report mode) and using the same datawindow
objects. Therefore, they use the same retrieval arguments. U_tv provides
an object function that will get the arguments of a previously populated
treeview item (that used of_SetDatasource)
lv_1.SetRedraw(True) 3. Linking a Treeview and Datawindow together
When the user opens this window, it initially displays five regions in the treeview (Canada, Eastern, Central, South, Western) in the datawindow. The user can then click one of the regions in the treeview and that region will display in the datawindow. By doing this the user has caused the treeview's SelectionChanging event to fire which in turn populates the datawindow control with the region. If the user clicks a "+" to the left of one of the regions, that region will expand displaying the states that exist in that region. The states were setup in the windows open event by calling the of_SetDatasource function. If the "+" next to a state is clicked, the customers in that state are displayed (also setup in the windows open event). By doing this the user has caused the treeview's "ItemExpanding" event to fire which populates the next level of the treeview control. Notice that this example doesn't have any code in the "ItemExpanding" event of tv, it's all precoded in the PFC ancestors. Registering the treeview levels using the of_SetDatasource functions allows for this. When the user selects a treeview item by clicking on one, the treeview's SelectionChanging event fires which will populate the datawindow control with the appropriate data depending on the level of the treeview item. That is if a region is selected in the treeview, the datawindow will display the region again; if a state is selected in the treeview the datawindow will display the state name, capital and country; and if customer is selected in the treeview, the data for that customer is displayed in the datawindow. 3.1 The Window "Open" Event Following is a line by line explanation of the window w_treeview open event.
Populate the window title with the classname of the window
tv_1.AddPicture("globals!") This sets up PictureIndex 1 for the
treeview
The next six calls to of_SetDatasource register the datasource for each
level of the treeview. This prepares the treeview to be populated later
(by the of_populatelevel function). There are five levels, 1. the region
(Canada, Eastern, Central, South, Western), 2. state (MA, TX, CA etc),
3. customer company name, 4. sales rep. name, and 5. purchase order number.
The first call to of_SetDatasource registers level 1 of the treeview
with the Region from the d_region datawindow object. It will not be recursive
(false), and the PictureIndex to be used is 1 which is "globals!" (see
above), and the SelectedPictureIndex is 7 "custom050" which looks like
a open file folder.
The second call to of_SetDatasource registers level 2 of the treeview
with the State from the d_regionstate datawindow object. It will get its
retrieval argument from the parent treeviewitem 1 level above using the
"region" column from the datawindow object. It will not be recursive (false),
and the PictureIndex to be used is 2 which is "parameter!" (see above),
and the SelectedPictureIndex is 7 "custom050" which looks like a open file
folder.
The third call to of_SetDatasource registers level 3 of the treeview
with the Company name from the d_regionstatecust datawindow object. It
will get the first retrieval argument from the parent treeviewitem 2 levels
above using the "region" column from the datawindow object, and the second
argument from the parent treeviewitem 1 level above using the State (states_state_id)
column. It will not be recursive (false), and the PictureIndex to be used
is 3 which is "library!" (see above), and the SelectedPictureIndex is 7
"custom050" which looks like a open file folder.
The fourth call to of_SetDatasource registers level 4 of the treeview
with the Sales Reps. (employee_emp_lname) from the d_regionstatecustrep
datawindow object. It will get its retrieval argument from the parent treeviewitem
1 level above using the "customer_id" column from the datawindow object.
It will not be recursive (false), and the PictureIndex to be used is 4
which is "custom076!" (see above), and the SelectedPictureIndex is 7 "custom050"
which looks like a open file folder.
The fifth call to of_SetDatasource registers level 5 of the treeview
with the Purchase Order # (order_id_string) from the d_regionstatecustrepord
datawindow object. It will get the first retrieval argument from the parent
treeviewitem 2 levels above using the "customer_id" column from the datawindow
object, and the second argument from the parent treeviewitem 1 level above
using the Sales Reps. ID (employee_emp_id) column. It will not be recursive
(false), and the PictureIndex to be used is 5 which is "scriptyes!" (see
above), and the SelectedPictureIndex is 7 "custom050!" which looks like
a open file folder.
Make the initial retrieve for the first level of the above registered
TreeViews.
3.2 The Treeview tv_1 "SelectionChanging" Event Following is a line by line explanation of the treeview tv_1 selectionchanging event.
n_ds ls_DataStore Declare local variables
SetPointer(HourGlass!) Set the mouse pointer to an hourglass.
Turn off Redraw for the dw so there is no flicker when data is added.
Use the "newhandle" event argument to get the new treeview item the
user selected.
Get the data and row# of the clicked treeview item associated with "newhandle",
display an error message if a problem occurs. The il_DSRow instance variable
is significant and is used to store the row # in the datastore of the associated
selected treeview item. This il_DSRow is referenced by other methods in
the window.
dw_1.Reset()
Copy only the data from the selected item to the datawindow.
Set status flag of new row (the new row is copied as NewModified!).
dw_1.SetRedraw(True) 3.3 The Datawindow dw_1 "Clicked" Event Following is a line by line explanation of the datawindow dw_1 clicked event:
SelectRow(0, False) Select 0 rows (deselect all rows).
3.4 The CommandButton cb_RefreshLevel "Clicked" Event Following is a line by line explanation of the commandbutton cb_RefrshLevel clicked event.
dw_1.Reset() Clear out the datawindow.
3.5 The CommandButton cb_RefreshItem "Clicked" Event Following is a line by line explanation of the commandbutton cb_RefreshItem clicked event. dw_1.Reset() Clear out the datawindow.
Refresh the Treeview item that was most recently selected by the user
(il_DSRow).
3.6 The CommandButton cb_RefreshTree "Clicked" Event Following is a line by line explanation of the commandbutton cb_RefreshTree clicked event: dw_1.Reset() Clear out the datawindow.
Removes all Treeview items below a specified item (0 means all levels).
The function also removes the item's rows from the DataStores.
Make an initial retrieve for the first level of the registered Treeviews.
3.7 The CommandButton cb_InsertRow "Clicked" Event Following is a line by line explanation of the commandbutton cb_InsertRow clicked event:
dw_1.RowsDiscard(1, 1, Primary!) Delete the row from the datawindow.
3.8 The CommandButton cb_DeleteItem "Clicked" Event Following is a line by line explanation of the commandbutton cb_DeleteItem clicked event: Long ll_Current
ll_Current = tv_1.FindItem(CurrentTreeItem!, 0) Get the handle
to the current treeview item.
3.9 The CommandButton cb_UpdateTree "Clicked" Event Following is a line by line explanation of the commandbutton cb_UpdateTree clicked event:
Long ll_Current Declare local variables.
ll_Current = tv_1.FindItem(CurrentTreeItem!, 0) Get the handle
to the current treeview item.
Populate lds_DataStore with the properties of the datastore for the
current treeview level.
Copy the data from the datawindow to the treeview item's datastore.
If the dw itemstatus is new! or newmodified!
Copy the rows to the datastore
Add an item to the parent level of the treeview.
If the dw itemstatus is datamodified!
Discard the original row from the DataStore.
Copy the rows to the datastore.
Reset the itemstatus flag in the ds.
Refresh the current treeview item.
3.10 The CommandButton cb_close "Clicked" Event Following is a line by line explanation of the commandbutton cb_close clicked event: parent.event pfc_close() Close the window.
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||