Form tree controls are a
user-friendly way of displaying a hierarchy of related records like a
company organizational structure, a bill of materials, projects, etc.
Such a hierarchy can also be used in custom lookups allowing user to
browse and select the required node in more convenient ways.
One of the previous recipes explained how to display the ledger budget model hierarchy in the Budget model form. In this recipe, we will reuse the previously created BudgetModelTree class to build a budget model tree lookup.
How to do it...
1. Create a new form in AOT:
Property
|
Value
|
---|
Name
|
BudgetModelLookup
|
2. Set its design properties to:
Property
|
Value
|
---|
Frame
|
Border
|
WindowType
|
Popup
|
3. Add a new Tree control to the form's design.
Property
|
Value
|
---|
Name
|
ModelTree
|
4. Add the following line to the form's class declaration:
BudgetModelTree budgetModelTree;
5. Override the form's init() with:
public void init()
{
FormStringControl callingControl;
;
callingControl = SysTableLookup::getCallerStringControl(
this.args());
super();
budgetModelTree = BudgetModelTree::construct(
ModelTree,
callingControl.text());
budgetModelTree.buildTree();
}
6. Override mouseDblClick() and mouseUp() of the ModelTree control:
public int mouseDblClick(
int _x,
int _y,
int _button,
boolean _ctrl,
boolean _shift)
tree lookuptree lookupbuilding, steps{
int ret;
;
ret = super(_x, _y, _button, _ctrl, _shift);
element.closeSelect(
this.getItem(this.getSelection()).text());
return ret;
}
public int mouseUp(
int _x,
int _y,
int _button,
boolean _ctrl,
boolean _shift)
{;
super(_x, _y, _button, _ctrl, _shift);
return 1;
}
7. The form should look like this in AOT:
8. Open the extended data type BudgetModelHeadId in AOT, and set its FormHelp property:
Property
|
Value
|
---|
FormHelp
|
BudgetModelLookup
|
9. To see the results, open General ledger | Ledger budget, and expand Model lookup:
How it works...
First, we create a new form called BudgetModelLookup, which is an actual lookup. We set the Frame and WindowType design properties to Border and Popup respectively to change the layout of the form to be like lookup.
In the form class declaration, we define the BudgetModelTree class which we have already created in one of the previous recipes.
The code in the form's init() builds the tree. Here, we create a new budgetModelTree object by calling the constructor construct(), which accepts two arguments:
1. A control of type Tree, which represents the actual tree.
2.
Budget model, which is going to be preselected initially. Normally,
it's a value of the calling control, which can be detected by using getCallerStringControl() method of SysTableLookup application class..
The code in mouseDblClick() returns the user-selected value from the tree back to the calling control and closes the lookup.
mouseUp() has to be overridden to return 1 to make sure that the lookup does not close while the user expands or collapses tree nodes.