How to use Web Dynpro UI element – “ButtonChoice” with Static and Dynamic Programming
We can use this UI element ButtonChoice to choose among the various options offered by the menu. Hereunder, we present a list with some of the ButtonChoice properties that can be bound, and the attribute type in case the property is bindable (Table).
We create aWDapplication, where we use the ButtonChoice UI element to offer to the end user a menu list with two options: power and divide.
To realise these calculations, we have used the methods of the class CL_FOEV_BUILTINS.As we can see in Fig.this class has many calculation methods,from the PLUS method (that performs a simple addition) to functions,in order to obtain the Integer Part or Hyperbola Sinus.
The structure of the POWER method
This method has two importing parameters, named IM_ARG1 and IM_ARG2, and an exporting parameter, named EX_RESULT, of float type. The method DIVIDE has the same parameters.
For this scope, we create our context node named CALCULATE, with the context attributes ARG1 of f type, ARG2 of f type and RESULT of f type, required to perform the calculations and to holdthe result.The WD component structure and the view context structure are presented in Fig.
WD component structure and view context structure
The View layout
In the ButtonChoice UI element, we insert two options: menuactionitem1 and menuactionitem2.For the two created options, we set actions(divide and power), and we use the hotkey property to offer to the end user the capability to press the respective key combination to trigger the associated event handler method.
When the user interacts the first time with the ButtonChoice UI element,an action can be selected,and the Framework triggers the proper event handler method. The last selected action remains on the ButtonChoice UI element after the action has been executed. This behaviour is possible through the property repeat SelectedAction (Fig).
Some of the ButtonChoice UI element properties
The Framework triggers the event handler method onactiondivide when the user clicks the respective choice option button or he presses the CTRL_D key combination. Listing shows the coding of this method.
DATA lr_oref TYPE REF TO cx_foev_error_in_function.
DATA ls_calculate TYPE wd_this->element_calculate.
DATA lv_arg1 LIKE ls_calculate-arg1.
DATA lv_arg2 LIKE ls_calculate-arg2.
DATA lv_result LIKE ls_calculate-result.
wd_this->attribute_get( IMPORTING p_arg1 = lv_arg1
p_arg2 = lv_arg2).
cl_foev_builtins=>divide( EXPORTING im_arg1 = lv_arg1
im_arg2 = lv_arg2
IMPORTING ex_result = lv_result).
CATCH cx_foev_error_in_function INTO lr_oref.
wd_this->attribute_set(EXPORTING p_result = lv_result).
As we can see, to read the context attributes ATR1 and ATR2, we use the user defined method named ATTRIBUTE_GET that has two exporting parameters (Fig.).
To pass the result of the division calculation into the context attribute RESULT, we have used the user defined method ATTRIBUTE_SET that has an importing parameter
User defined method required to read the context attributes
User defined method required to populate the RESULT attribute
Runtime for the Divide operation
To rise to power a number (number1 risen to number2), the Framework triggers the event handler method onactionpower (Listing).
cl_foev_builtins=power( EXPORTING im_arg1 = lv_arg1 im_arg2 = lv_arg2 IMPORTING ex_result = lv_result).
CATCH cx_foev_error_in_function INTO lr_oref.
We have used the static method POWER, of the calculation class CL_FOEV_BUILTINS, to perform the number1 risen to number2 operation.To read the context attributes ATR1 and ATR2, we call the same user defined method ATTRIBUTE_ GET; to pass the calculation result, we call the method ATTRIBUTE_SET. In this example, we have caught errors that can occur, but we didn’t display their message to the user. The way we can use try. . . endtry, the way we can raise, catch and display exceptions will be detailed described in Chap10.
RUNTIME CLASS: CL_WD_BUTTON_CHOICE
Hereunder,we present a table showing the correspondence between the view designer name and the runtime name, with the proper types,in case of dynamic programming of a ButtonChoice UI element (Table).
The implementation of a dynamic ButtonChoice UI element with one mean action item, named DIVIDE, contains the following statements (Listing ):
DATA lr_button_choice TYPE REF TO cl_wd_button_choice.
DATA lr_flow_data TYPE REF TO cl_wd_flow_data.
DATA lr_container TYPE REF TO cl_wd_uielement_container.
DATA lr_menu_action TYPE REF TO cl_wd_menu_action_item.
IF first_time EQ abap_true.
lr_container ?= view->get_element('ROOTUIELEMENTCONTAINER').
lr_button_choice = cl_wd_button_choice=>new_button_choice( id = 'BTN_CHOICE'
text = 'Choose'
repeat_selected_action = abap_false ).
lr_flow_data = cl_wd_flow_data=>new_flow_data(element = lr_button_choice).
lr_menu_action = cl_wd_menu_action_item=>new_menu_action_item(id ='MENUACTIONITEM1'
text = 'Divide'
on_action = 'DIVIDE'
hotkey = cl_wd_menu_action_item=>e_hotkey-ctrl_p ).
lr_button_choice->add_choice(the_choice = lr_menu_action).