Você está na página 1de 13

ABAP OO Example - Company/Plant/Storage Location Hierarchy by Column

This program displays the hierarchy in a list tree. Additional custom toolbar allow expand / collapse the tree. Note. SAP Toolbar requires the Microsoft Common Control. You therefore need to install Microsoft Internet Explorer Version 4.0. Used objects and methods:

cl_gui_splitter_container cl_gui_container cl_gui_toolbar cl_gui_column_tree handling application events (with triggering PAI) context menus

GUI Status 'MAIN' has one function: BACK of Type ' ' (Normal application function), assigned to standard button: . Screen 100 has only field g_ok_code for handling user command:
PROCESS BEFORE OUTPUT. MODULE PBO_100. PROCESS AFTER INPUT. MODULE PAI_100. report z message-id tree_control_msg. *--------------------------------------------------------------------* * Data declaration *--------------------------------------------------------------------* constants c_width type i value 500. class lcl_application definition deferred. class cl_gui_cfw definition load. types: item_table_type like standard table of mtreeitm with default key. data: g_application type ref to lcl_application, splitter_h type ref to cl_gui_splitter_container, g_gui_container type ref to cl_gui_container, g_gui_container_r type ref to cl_gui_container, g_tree type ref to cl_gui_column_tree, *------node key = CCCCPPPPSSSS * \--/ Company t001-bukrs * \--/ Plant t001k-bwkey * \--/ Storage Location t001l-lgort g_cntl type ref to cl_gui_toolbar, g_ok_code type sy-ucomm. "screen 100 field (only) *--------------------------------------------------------------------* * CLASS LCL_APPLICATION DEFINITION *--------------------------------------------------------------------* class lcl_application definition. public section.

*--- methods for nodes and items of tree methods: handle_node_double_click for event node_double_click of cl_gui_column_tree importing node_key, handle_header_click for event header_click of cl_gui_column_tree importing header_name, handle_expand_no_children for event expand_no_children of cl_gui_column_tree importing node_key, handle_item_double_click for event item_double_click of cl_gui_column_tree importing node_key item_name, handle_button_click for event button_click of cl_gui_column_tree importing node_key item_name, handle_link_click for event link_click of cl_gui_column_tree importing node_key item_name, handle_checkbox_change for event checkbox_change of cl_gui_column_tree importing node_key item_name checked, *--- methods for sap toolbar handle_pushbutton_click for event function_selected of cl_gui_toolbar importing fcode, *--- methods for context menus for nodes and items of tree handle_node_context_menu_req for event node_context_menu_request of cl_gui_column_tree importing node_key menu, handle_node_context_menu_sel for event node_context_menu_select of cl_gui_column_tree importing node_key fcode, handle_header_context_menu_req for event header_context_menu_request of cl_gui_column_tree importing header_name menu, handle_header_context_menu_sel for event header_context_menu_select of cl_gui_column_tree importing header_name fcode, handle_dft_context_menu_req for event default_context_menu_request of cl_gui_column_tree importing menu, handle_dft_context_menu_sel for event default_context_menu_select of cl_gui_column_tree importing fcode. endclass.

*--------------------------------------------------------------------* * CLASS LCL_APPLICATION IMPLEMENTATION *--------------------------------------------------------------------* class lcl_application implementation. method handle_node_double_click. data: text1 type string, text2 type string, text3 type string. if node_key(4) <> 'Root'. concatenate 'Company:' node_key(4) into text1 separated by space. endif. if node_key+4(4) <> space. concatenate 'Plant:' node_key+4(4) into text2 separated by space. endif. if node_key+8(4) <> space. concatenate 'Storage Location:' node_key+8(4) into text3 separated by space. endif. message i398(00) with 'Handle' text1 text2 text3. endmethod. method handle_header_click. message i398(00) with 'Handle header:' header_name. endmethod. method handle_item_double_click. data: text0 type string, text1 type string, text2 type string, text3 type string. case item_name. when 'Column1'. text0 = 'Handle code for'. when 'Column2'. text0 = 'Handle name for'. when 'Column3'. text0 = 'Handle city for'. when others. text0 = 'Handle'. endcase. if node_key(4) <> 'Root'. concatenate 'Company:' node_key(4) into text1 separated by space. endif. if node_key+4(4) <> space. concatenate 'Plant:' node_key+4(4) into text2 separated by space. endif. if node_key+8(4) <> space. concatenate 'Storage Location:' node_key+8(4) into text3 separated by space. endif. message i398(00) with text0 text1 text2 text3. endmethod. method handle_link_click. endmethod. method handle_button_click.

endmethod. method handle_checkbox_change. endmethod. method handle_expand_no_children. data: node_table type treev_ntab, node type treev_node, item_table type item_table_type, item type mtreeitm, wa_t001l type t001l. select lgort lgobe from t001l into (wa_t001l-lgort, wa_t001l-lgobe) where werks = node_key+4. clear node. node-node_key = node_key. node-node_key+8 = wa_t001l-lgort. node-relatkey = node_key. node-relatship = cl_gui_column_tree=>relat_last_child. append node to node_table. clear item. item-node_key = node-node_key. item-item_name = 'Column1'. item-class = cl_gui_column_tree=>item_class_text. item-font = cl_gui_column_tree=>item_font_default. item-text = wa_t001l-lgort. item-t_image = '@AC@'. item-usebgcolor = 'X'. append item to item_table. clear item. item-node_key = node-node_key. item-item_name = 'Column2'. item-class = cl_gui_column_tree=>item_class_text. item-font = cl_gui_column_tree=>item_font_default. item-text = wa_t001l-lgobe. append item to item_table. endselect. call method g_tree->add_nodes_and_items exporting node_table = node_table item_table = item_table item_table_structure_name = 'MTREEITM' exceptions failed = 1 cntl_system_error = 3 error_in_tables = 4 dp_error = 5 table_structure_name_not_found = 6. if sy-subrc <> 0. message a000. endif. endmethod. method handle_pushbutton_click. data level type i.

case fcode. when 'COMPANY'. level = 0. when 'PLANT'. level = 1. when 'STORAGE'. level = 2. when 'COLLAPSE'. level = -1. endcase. call method g_tree->collapse_all_nodes. if level >= 0. call method g_tree->expand_node exporting node_key = 'Root' level_count = level expand_subtree = space. endif. endmethod. method handle_node_context_menu_req. data: text type gui_text. if node_key(4) <> 'Root'. concatenate 'Company:' node_key(4) into text separated by space. call method menu->add_function exporting text = text fcode = 'COMPANY'. endif. if node_key+4(4) <> space. concatenate 'Plant:' node_key+4(4) into text separated by space. call method menu->add_function exporting text = text fcode = 'PLANT'. endif. if node_key+8(4) <> space. concatenate 'Storage Location:' node_key+8(4) into text separated by space. call method menu->add_function exporting text = text fcode = 'STORAGE'. endif. text = 'About...'. call method menu->add_function exporting text = text fcode = 'ABOUT'. endmethod. method handle_node_context_menu_sel. data text type string. case fcode. when 'COMPANY'. concatenate 'Do something for company' node_key(4) into text separated by space. when 'PLANT'. concatenate 'Do something for plant' node_key+4(4) into text separated by space. when 'STORAGE'. concatenate 'Do something for storage location' node_key+8(4) into text separated by space. when 'ABOUT'. text = 'Object Oriented Abap Demo'. endcase. message i398(00) with text. endmethod. method handle_header_context_menu_req. data: text type gui_text.

concatenate 'Header:' header_name into text separated by space. call method menu->add_function exporting text = text fcode = 'HEADER'. text = 'About...'. call method menu->add_function exporting text = text fcode = 'ABOUT'. endmethod. method handle_header_context_menu_sel. data text type string. case fcode. when 'HEADER'. concatenate 'Do something for header' header_name into text separated by space. when 'ABOUT'. text = 'Object Oriented Abap Demo'. endcase. message i398(00) with text. endmethod. method handle_dft_context_menu_req. data: text type gui_text. text = 'About...'. call method menu->add_function exporting text = text fcode = 'ABOUT'. text = 'Exit...'. call method menu->add_function exporting text = text fcode = 'BYE'. endmethod. method handle_dft_context_menu_sel. case fcode. when 'ABOUT'. message i398(00) with 'Object Oriented Abap Demo'. when 'BYE'. perform finish_program. leave program. endcase. endmethod. endclass. *--------------------------------------------------------------------* * MODULE PBO_100 OUTPUT * *--------------------------------------------------------------------* module pbo_100 output. set pf-status 'MAIN'. if g_tree is initial. perform create_and_init_tree. endif. endmodule. " PBO_0100 OUTPUT *--------------------------------------------------------------------* * MODULE PAI_100 INPUT * *---------------------------------------------------------------------

* module pai_100 input. data: return_code type i. * CL_GUI_CFW=>DISPATCH must be called if events are registered * that trigger PAI * this method calls the event handler method of an event call method cl_gui_cfw=>dispatch importing return_code = return_code. if return_code <> cl_gui_cfw=>rc_noevent. " a control event occured => exit PAI clear g_ok_code. exit. endif. case g_ok_code. when 'BACK'. " Finish program perform finish_program. leave program. endcase. * CAUTION: clear ok code! clear g_ok_code. endmodule.

" PAI_0100

INPUT

*--------------------------------------------------------------------* * Form CREATE_AND_INIT_TREE *--------------------------------------------------------------------* form create_and_init_tree. data: node_table type treev_ntab, item_table type item_table_type, events type cntl_simple_events, event type cntl_simple_event, hierarchy_header type treev_hhdr. create object splitter_h exporting parent = cl_gui_container=>screen0 rows = 1 columns = 2. call method splitter_h->set_border exporting border = cl_gui_cfw=>false. call method splitter_h->set_column_mode exporting mode = splitter_h->mode_absolute. call method splitter_h->set_column_width exporting id = 1 width = c_width. g_gui_container = splitter_h->get_container( row = 1 column = 1 ). g_gui_container_r = splitter_h->get_container( row = 1 column = 2 ). * setup the hierarchy header hierarchy_header-heading = 'Company/Plant/Storage Location'. hierarchy_header-width = 35. * create a column tree control create object g_tree exporting parent = g_gui_container node_selection_mode = cl_gui_column_tree=>node_sel_mode_single item_selection = 'X'

hierarchy_column_name = 'Column1' hierarchy_header = hierarchy_header exceptions cntl_system_error = 1 create_error = 2 failed = 3 illegal_node_selection_mode = 4 illegal_column_name = 5 lifetime_error = 6. if sy-subrc <> 0. message a000. endif. * insert two additional columns call method g_tree->add_column exporting name = 'Column2' width = 40 header_text = 'Name' exceptions column_exists illegal_column_name too_many_columns illegal_alignment different_column_types cntl_system_error failed predecessor_column_not_found if sy-subrc <> 0. message a000. endif. call method g_tree->add_column exporting name = 'Column3' width = 30 header_text = 'City' exceptions column_exists illegal_column_name too_many_columns illegal_alignment different_column_types cntl_system_error failed predecessor_column_not_found if sy-subrc <> 0. message a000. endif.

= = = = = = = =

1 2 3 4 5 6 7 8.

= = = = = = = =

1 2 3 4 5 6 7 8.

* create a tool bar control create object g_cntl exporting parent = g_gui_container_r display_mode = cl_gui_toolbar=>m_mode_vertical exceptions cntl_install_error = 1 cntl_error = 2. if sy-subrc <> 0. message a000. "not very appropriate msg, but... endif.

* define the events which will be passed to the backend * from tree event-appl_event = 'X'. event-eventid = append event to event-eventid = append event to event-eventid = append event to event-eventid = append event to event-eventid = append event to event-eventid = append event to event-eventid = append event to event-eventid = append event to event-eventid = append event to event-eventid = append event to

"

cl_gui_column_tree=>eventid_node_double_click. events. cl_gui_column_tree=>eventid_header_click. events. cl_gui_column_tree=>eventid_item_double_click. events. cl_gui_column_tree=>eventid_expand_no_children. events. cl_gui_column_tree=>eventid_link_click. events. cl_gui_column_tree=>eventid_button_click. events. cl_gui_column_tree=>eventid_checkbox_change. events. cl_gui_column_tree=>eventid_node_context_menu_req. events. cl_gui_column_tree=>eventid_header_context_men_req. events. cl_gui_column_tree=>eventid_def_context_menu_req. events.

call method g_tree->set_registered_events exporting events = events exceptions cntl_error = 1 cntl_system_error = 2 illegal_event_combination = 3. if sy-subrc <> 0. message a000. endif. * from toolbar clear: event, events[]. event-appl_event = 'X'. event-eventid = cl_gui_toolbar=>m_id_function_selected. append event to events. call method g_cntl->set_registered_events exporting events = events exceptions cntl_error = 1 cntl_system_error = 2 illegal_event_combination = 3. if sy-subrc <> 0. message a000. endif. * assign event handlers in the application class to each desired event set handler g_application->handle_node_double_click for g_tree. set handler g_application->handle_header_click for g_tree. set handler g_application->handle_item_double_click for g_tree. set handler g_application->handle_expand_no_children for g_tree. set handler g_application->handle_link_click for g_tree.

"

set handler set handler set handler set handler set handler set handler g_tree. set handler g_tree. set handler set handler * * * *

g_application->handle_button_click for g_tree. g_application->handle_checkbox_change for g_tree. g_application->handle_pushbutton_click for g_cntl. g_application->handle_node_context_menu_req for g_tree. g_application->handle_node_context_menu_sel for g_tree. g_application->handle_header_context_menu_req for g_application->handle_header_context_menu_sel for g_application->handle_dft_context_menu_req for g_tree. g_application->handle_dft_context_menu_sel for g_tree.

add some nodes to the tree control NOTE: the tree control does not store data at the backend. If an application wants to access tree data later, it must store the tree data itself. perform build_node_and_item_table using node_table item_table. call method g_tree->add_nodes_and_items exporting node_table = node_table item_table = item_table item_table_structure_name = 'MTREEITM' exceptions failed = 1 cntl_system_error = 3 error_in_tables = 4 dp_error = 5 table_structure_name_not_found = 6. if sy-subrc <> 0. message a000. endif.

* add some pushbuttons to the toolbar call method: g_cntl->add_button exporting fcode = 'COMPANY' icon = '@D9@' butn_type = cntb_btype_button text = 'Expand Companies' exceptions cntl_error = 1, g_cntl->add_button exporting fcode = 'PLANT' icon = '@A8@' butn_type = cntb_btype_button text = 'Expand Plants' exceptions cntl_error = 1, g_cntl->add_button exporting fcode = 'STORAGE' icon = '@AC@' butn_type = cntb_btype_button text = 'Expand Storage Locations (viewed only)' exceptions cntl_error = 1, g_cntl->add_button exporting fcode = 'COLLAPSE' icon = '@69@' butn_type = cntb_btype_button text = 'Collapse All' exceptions cntl_error = 1. if sy-subrc <> 0. message a000. endif.

endform.

" CREATE_AND_INIT_TREE

*--------------------------------------------------------------------* * Form build_node_and_item_table *--------------------------------------------------------------------* form build_node_and_item_table using node_table type treev_ntab item_table type item_table_type. data: node type treev_node, item type mtreeitm, wa_t001 like t001, wa_t001l like t001l, wa_t001w like t001w, wa_t001k like t001k. * Caution: The nodes are inserted into the tree according to the order * in which they occur in the table. In consequence, a node must not * must not occur in the node table before its parent node. node-node_key = 'Root'. clear node-relatkey. clear node-relatship. node-hidden = ' '. node-disabled = ' '. node-isfolder = 'X'. clear node-n_image. clear node-exp_image. clear node-expander. append node to node_table. clear item. item-node_key = node-node_key. item-item_name = 'Column1'. item-class = cl_gui_column_tree=>item_class_text. item-font = cl_gui_column_tree=>item_font_default. item-text = 'Companies'. append item to item_table. select bukrs butxt ort01 from t001 into (wa_t001-bukrs, wa_t001-butxt, wa_t001-ort01). clear node. node-node_key = wa_t001-bukrs. node-relatkey = 'Root'. node-relatship = cl_gui_column_tree=>relat_last_child. node-isfolder = 'X'. append node to node_table. clear item. item-node_key = node-node_key. item-item_name = 'Column1'. item-class = cl_gui_column_tree=>item_class_text. item-font = cl_gui_column_tree=>item_font_default. item-text = wa_t001-bukrs. item-t_image = '@D9@'. item-usebgcolor = 'X'.

append item to item_table. clear item. item-node_key = node-node_key. item-item_name = 'Column2'. item-class = cl_gui_column_tree=>item_class_text. item-length = 30. item-font = cl_gui_column_tree=>item_font_default. item-text = wa_t001-butxt. append item to item_table. clear item. item-node_key = node-node_key. item-item_name = 'Column3'. item-class = cl_gui_column_tree=>item_class_text. item-font = cl_gui_column_tree=>item_font_default. item-text = wa_t001-ort01. append item to item_table. select bwkey bukrs from t001k into (wa_t001k-bwkey, wa_t001k-bukrs) where bukrs = wa_t001-bukrs. clear node. node-node_key = wa_t001-bukrs. node-node_key+4 = wa_t001k-bwkey. node-relatkey = wa_t001-bukrs. node-relatship = cl_gui_column_tree=>relat_last_child. node-isfolder = 'X'. node-expander = 'X'. " The node is marked with a '+', although " it has no children. When the user clicks on the " + to open the node, the event expand_nc is " fired. The programmer can " add the children of the " node within the event handler of the expand_nc " event (see callback handle_expand_nc). append node to node_table. select single name1 from t001w into wa_t001w-name1 where werks = wa_t001k-bwkey. clear item. item-node_key = node-node_key. item-item_name = 'Column1'. item-class = cl_gui_column_tree=>item_class_text. item-font = cl_gui_column_tree=>item_font_default. item-text = wa_t001k-bwkey. item-t_image = '@A8@'. item-usebgcolor = 'X'. append item to item_table. clear item. item-node_key = node-node_key. item-item_name = 'Column2'. item-class = cl_gui_column_tree=>item_class_text. item-font = cl_gui_column_tree=>item_font_default. item-text = wa_t001w-name1. append item to item_table. endselect. endselect.

endform.

" build_node_and_item_table

*--------------------------------------------------------------------* * Form finish_program *---------------------------------------------------------------------* form finish_program. if not g_gui_container is initial. " destroy tree container (detroys contained tree control, too) call method g_gui_container->free exceptions cntl_system_error = 1 cntl_error = 2. if sy-subrc <> 0. message a000. endif. clear: g_gui_container, g_tree. endif. if not g_gui_container_r is initial. " destroy right container (detroys contained toolbar, too) call method g_gui_container_r->free exceptions cntl_system_error = 1 cntl_error = 2. if sy-subrc <> 0. message a000. endif. clear: g_gui_container_r, g_cntl. endif. endform. " finish_program *--------------------------------------------------------------------* *--------------------------------------------------------------------* * Start of selection *--------------------------------------------------------------------* start-of-selection. * create the application object * this object is needed to handle the ABAP Objects Events of * Controls create object g_application. set screen 100. *--------------------------------------------------------------------*

Você também pode gostar