Friday, 31 July 2015

Building Multi level tree ALV with class CL_GUI_LIST_TREE

----------------------------------------------------------------------------------------

DATA :    lr_cust TYPE REF TO cl_gui_custom_container,
              lr_list_tree TYPE REF TO cl_gui_list_tree,
              lt_flight TYPE TABLE OF scarr,
              ls_flight TYPE scarr,
              lt_spfli TYPE TABLE OF spfli,
              ls_spfli TYPE spfli,
              lt_node TYPE  treev_ntab,
              ls_node TYPE LINE OF treev_ntab,
              lt_item TYPE TABLE OF mtreeitm,
              ls_item TYPE mtreeitm .

START-OF-SELECTION.
  CALL SCREEN 0001.
*&---------------------------------------------------------------------*
*&      Module  STATUS_0001  OUTPUT
*&---------------------------------------------------------------------*
MODULE status_0001 OUTPUT.
  SET PF-STATUS 'LIST_STATUS'.
ENDMODULE.                 " STATUS_0001  OUTPUT
*&---------------------------------------------------------------------*
*&      Module  GET_DATA  OUTPUT
*&---------------------------------------------------------------------*
MODULE get_data OUTPUT.
  IF lt_flight IS INITIAL.
    SELECT FROM scarr INTO TABLE lt_flight.
    IF lt_flight IS NOT INITIAL.
      SELECT FROM spfli INTO TABLE lt_spfli FOR ALL ENTRIES IN lt_flight
                                           WHERE carrid lt_flight-carrid.
    ENDIF.
  ENDIF.
ENDMODULE.                 " GET_DATA  OUTPUT
*&---------------------------------------------------------------------*
*&      Module  BUILD_LIST_TREE  OUTPUT
*&---------------------------------------------------------------------*
MODULE build_list_tree OUTPUT.
  IF lr_cust IS NOT BOUND.
    CREATE OBJECT lr_cust
      EXPORTING
        container_name 'LIST_TREE'
        repid          sy-repid
        dynnr          sy-dynnr.
  ENDIF.

  IF lr_list_tree IS NOT BOUND.
    CREATE OBJECT lr_list_tree
      EXPORTING
        parent                      lr_cust
        node_selection_mode         cl_gui_list_tree=>node_sel_mode_single
*       hide_selection              =     " Visibility of Selection
        item_selection              'X'  " Can Individual Items be Selected?
        with_headers                ' '    " 'X': With Headers
*       hierarchy_header            =     " Hierarchy Header
*       list_header                 =     " List Header
*       name                        =     " Name
      EXCEPTIONS
        lifetime_error              1
        cntl_system_error           2
        create_error                3
        illegal_node_selection_mode 4
        failed                      5
        OTHERS                      6.
  ENDIF.

  IF lr_list_tree IS BOUND.
* Building Root Node
    ls_node-node_key 'Flight'.
    ls_node-hidden ' '.
    ls_node-hidden ' '.
    ls_node-isfolder 'X'.
    APPEND ls_node TO lt_node.
* Building sub node and relationship with root node
    LOOP AT  lt_flight INTO ls_flight.
      ls_node-node_key ls_flight-carrid.
      ls_node-relatkey 'Flight'.
      ls_node-relatship cl_gui_list_tree=>relat_last_child.
      ls_node-isfolder 'X'.
      APPEND ls_node TO lt_node.
      LOOP AT lt_spfli INTO ls_spfli WHERE carrid ls_flight-carrid.
        ls_node-node_key ls_spfli-connid.
        ls_node-relatkey ls_flight-carrid.
        ls_node-relatship cl_gui_list_tree=>relat_last_child.
        ls_node-isfolder 'X'.
        APPEND ls_node TO lt_node.
      ENDLOOP.
    ENDLOOP.
* Building root Node Text
    ls_item-node_key 'Flight'.
    ls_item-item_name '1'.
    ls_item-class cl_gui_list_tree=>item_class_text.
    ls_item-alignment cl_gui_list_tree=>align_auto.
    ls_item-font cl_gui_list_tree=>item_font_prop.
    ls_item-text 'Flight_Info'.
    APPEND ls_item TO lt_item.
* Bulding all Item node text
    LOOP AT  lt_flight INTO ls_flight.
      ls_item-node_key ls_flight-carrid.
      ls_item-item_name '1'.
      ls_item-class cl_gui_list_tree=>item_class_text.
      ls_item-alignment cl_gui_list_tree=>align_auto.
      ls_item-font cl_gui_list_tree=>item_font_prop.
      ls_item-text ls_flight-carrname.
      APPEND ls_item TO lt_item.
      LOOP AT lt_spfli INTO ls_spfli WHERE carrid ls_flight-carrid.
        ls_item-node_key ls_spfli-connid.
        ls_item-item_name '1'.
        ls_item-class cl_gui_list_tree=>item_class_text.
        ls_item-alignment cl_gui_list_tree=>align_auto.
        ls_item-font cl_gui_list_tree=>item_font_prop.
        CONCATENATE ls_spfli-countryfr '-' ls_spfli-cityfrom '-'
                   ls_spfli-countryto '-' ls_spfli-cityto INTO ls_item-text.

        APPEND ls_item TO lt_item.

      ENDLOOP.
    ENDLOOP.

    CALL METHOD lr_list_tree->add_nodes_and_items
      EXPORTING
        node_table                     lt_node
        item_table                     lt_item
        item_table_structure_name      'MTREEITM'
      EXCEPTIONS
        failed                         1
        cntl_system_error              2
        error_in_tables                3
        dp_error                       4
        table_structure_name_not_found 5
        OTHERS                         6.

  ENDIF.
ENDMODULE.                 " BUILD_LIST_TREE  OUTPUT
*&---------------------------------------------------------------------*
*&      Module  USER_COMMAND_0001  INPUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
MODULE user_command_0001 INPUT.
  CASE sy-ucomm.
    WHEN 'BACK' OR 'EXIT' OR 'CANCEL'.
      LEAVE TO SCREEN 0.
  ENDCASE.
ENDMODULE.                 " USER_COMMAND_0001  INPUT


----------------------------------------------------------------------------------------

Execute.



----------------------------------------------------------------------------------------

Building tree ALV with class CL_GUI_LIST_TREE

----------------------------------------------------------------------------------------------------

DATA :   lr_cust TYPE REF TO cl_gui_custom_container,
              lr_list_tree TYPE REF TO cl_gui_list_tree,
              lt_flight TYPE TABLE OF scarr,
              ls_flight TYPE scarr,
              lt_node TYPE  treev_ntab,
              ls_node TYPE LINE OF treev_ntab,
              lt_item TYPE TABLE OF mtreeitm,
              ls_item TYPE mtreeitm .

START-OF-SELECTION.
  CALL SCREEN 0001.
*&---------------------------------------------------------------------*
*&      Module  STATUS_0001  OUTPUT
*&---------------------------------------------------------------------*
MODULE status_0001 OUTPUT.
  SET PF-STATUS 'LIST_STATUS'.
ENDMODULE.                 " STATUS_0001  OUTPUT
*&---------------------------------------------------------------------*
*&      Module  GET_DATA  OUTPUT
*&---------------------------------------------------------------------*
MODULE get_data OUTPUT.
  IF lt_flight IS INITIAL.
    SELECT FROM scarr INTO TABLE lt_flight.
  ENDIF.
ENDMODULE.                 " GET_DATA  OUTPUT
*&---------------------------------------------------------------------*
*&      Module  BUILD_LIST_TREE  OUTPUT
*&---------------------------------------------------------------------*
MODULE build_list_tree OUTPUT.
  IF lr_cust IS NOT BOUND.
    CREATE OBJECT lr_cust
      EXPORTING
        container_name 'LIST_TREE'
        repid          sy-repid
        dynnr          sy-dynnr.
  ENDIF.

  IF lr_list_tree IS NOT BOUND.
    CREATE OBJECT lr_list_tree
      EXPORTING
        parent                      lr_cust
        node_selection_mode         cl_gui_list_tree=>node_sel_mode_single
*       hide_selection              =     " Visibility of Selection
        item_selection              'X'  " Can Individual Items be Selected?
        with_headers                ' '    " 'X': With Headers
*       hierarchy_header            =     " Hierarchy Header
*       list_header                 =     " List Header
*       name                        =     " Name
      EXCEPTIONS
        lifetime_error              1
        cntl_system_error           2
        create_error                3
        illegal_node_selection_mode 4
        failed                      5
        OTHERS                      6.
  ENDIF.

  IF lr_list_tree IS BOUND.
* Building Root Node
    ls_node-node_key 'Flight'.
    ls_node-hidden ' '.
    ls_node-hidden ' '.
    ls_node-isfolder 'X'.
    APPEND ls_node TO lt_node.
* Building sub node and relationship with root node
    LOOP AT  lt_flight INTO ls_flight.
      ls_node-node_key ls_flight-carrid.
      ls_node-relatkey 'Flight'.
      ls_node-relatship cl_gui_list_tree=>relat_last_child.
      ls_node-isfolder ' '.
      APPEND ls_node TO lt_node.
    ENDLOOP.
* Building root Node Text
    ls_item-node_key 'Flight'.
    ls_item-item_name '1'.
    ls_item-class cl_gui_list_tree=>item_class_text.
    ls_item-alignment cl_gui_list_tree=>align_auto.
    ls_item-font cl_gui_list_tree=>item_font_prop.
    ls_item-text 'Flight_Info'.
    APPEND ls_item TO lt_item.
* Bulding all Item node text
    LOOP AT  lt_flight INTO ls_flight.
      ls_item-node_key ls_flight-carrid.
      ls_item-item_name '1'.
      ls_item-class cl_gui_list_tree=>item_class_text.
      ls_item-alignment cl_gui_list_tree=>align_auto.
      ls_item-font cl_gui_list_tree=>item_font_prop.
      ls_item-text ls_flight-carrname.
      APPEND ls_item TO lt_item.
    ENDLOOP.

    CALL METHOD lr_list_tree->add_nodes_and_items
      EXPORTING
        node_table                     lt_node
        item_table                     lt_item
        item_table_structure_name      'MTREEITM'
      EXCEPTIONS
        failed                         1
        cntl_system_error              2
        error_in_tables                3
        dp_error                       4
        table_structure_name_not_found 5
        OTHERS                         6.

  ENDIF.
ENDMODULE.                 " BUILD_LIST_TREE  OUTPUT
*&---------------------------------------------------------------------*
*&      Module  USER_COMMAND_0001  INPUT
*&---------------------------------------------------------------------*

MODULE user_command_0001 INPUT.
  CASE sy-ucomm.
    WHEN 'BACK' OR 'EXIT' OR 'CANCEL'.
      LEAVE TO SCREEN 0.
  ENDCASE.
ENDMODULE.                 " USER_COMMAND_0001  INPUT


----------------------------------------------------------------------------------------------------
Screen flow logic.

Screen Layout:


 Execute the program and expand the root node.
Here all  items of the root node.





----------------------------------------------------------------------------------------------------

Thursday, 30 July 2015

Creating ToolBar buttons in containers Using CL_GUI_TOOLBAR class and Handling it

---------------------------------------------------------------------------------------------------------------

DATA lr_toolbar TYPE REF TO cl_gui_toolbar,
                      lr_cust_cont TYPE REF TO cl_gui_custom_container,
           lt_event TYPE cntl_simple_events,
           ls_event LIKE LINE OF lt_event.

*----------------------------------------------------------------------*
*       CLASS LCL_HANDLE_TOOLBAR DEFINITION
*----------------------------------------------------------------------*

CLASS lcl_handle_toolbar DEFINITION.
  PUBLIC SECTION.
    METHODS handle_button FOR EVENT function_selected
                            OF cl_gui_toolbar IMPORTING fcode .
ENDCLASS.                    "LCL_HANDLE_TOOLBAR DEFINITION

*----------------------------------------------------------------------*
*       CLASS lcl_handle_toolbar IMPLEMENTATION
*----------------------------------------------------------------------*

CLASS lcl_handle_toolbar IMPLEMENTATION.
  METHOD handle_button .
  CASE fcode.
    WHEN 'TSAVE'.
     MESSAGE 'Save button clicked' TYPE 'I'.
  ENDCASE.
  ENDMETHOD.                    "handle_button
ENDCLASS.                    "lcl_handle_toolbar IMPLEMENTATION

START-OF-SELECTION.
  DATA lr_handle TYPE REF TO lcl_handle_toolbar.
  CREATE OBJECT lr_handle.

  CALL SCREEN 0001.
*&---------------------------------------------------------------------*
*&      Module  CREATE_PFSTATUS  OUTPUT
*&---------------------------------------------------------------------*
MODULE create_pfstatus OUTPUT.
  SET PF-STATUS 'STATUS'.
ENDMODULE.                 " CREATE_PFSTATUS  OUTPUT
*&---------------------------------------------------------------------*
*&      Module  CREATE_TOOLBAR  OUTPUT
*&---------------------------------------------------------------------*
MODULE create_toolbar OUTPUT.
  IF lr_cust_cont IS NOT BOUND.
    CREATE OBJECT lr_cust_cont
      EXPORTING
        container_name 'TOOLBAR'
        repid          sy-repid
        dynnr          sy-dynnr.

  ENDIF.

  IF lr_toolbar IS NOT BOUND.
    CREATE OBJECT lr_toolbar
      EXPORTING
        parent             lr_cust_cont
        display_mode       cl_gui_toolbar=>m_mode_horizontal " Display mode
        name               'Demo Toolbar'
      EXCEPTIONS
        cntl_install_error 1
        cntl_error         2
        cntb_wrong_version 3
        OTHERS             4.

  ENDIF.

  CALL METHOD lr_toolbar->add_button
    EXPORTING
      fcode            'TSAVE'  " Function Code of Button
      icon             '@2L@'    " Icon Name
*     is_disabled      =     " Button Status
      butn_type        0   " Button Types Defined in CNTB
      text             'Save'  " Text Shown to the Right of the Image
      quickinfo        'Click to update the database'
*     is_checked       =     " Button Selected
    EXCEPTIONS
      cntl_error       1
      cntb_btype_error 2
      cntb_error_fcode 3
      OTHERS           4.

  ls_event-eventid cl_gui_toolbar=>m_id_function_selected.
  ls_event-appl_event ''.
  APPEND ls_event TO lt_event.
  CALL METHOD lr_toolbar->set_registered_events
    EXPORTING
      events lt_event.

  SET HANDLER lr_handle->handle_button FOR lr_toolbar.
ENDMODULE.                 " CREATE_TOOLBAR  OUTPUT
*&---------------------------------------------------------------------*
*&      Module  HANDLE_USERACTION  INPUT
*&---------------------------------------------------------------------*
MODULE handle_useraction INPUT.
  CASE sy-ucomm.
    WHEN 'BACK' OR 'EXIT' OR 'CANCEL'.
      LEAVE TO SCREEN 0.
    WHEN OTHERS.
  ENDCASE.
ENDMODULE.                 " HANDLE_USERACTION  INPUT

---------------------------------------------------------------------------------------------------------------




---------------------------------------------------------------------------------------------------------------

Creating ToolBar buttons group in containers Using CL_GUI_TOOLBAR class

---------------------------------------------------------------------------------------------------------
DATA lr_toolbar TYPE REF TO cl_gui_toolbar,
              lr_cust_cont TYPE REF TO cl_gui_custom_container,
             lt_btn TYPE ttb_button,
             ls_btn TYPE stb_button.

CALL SCREEN 0001.
*&---------------------------------------------------------------------*
*&      Module  CREATE_PFSTATUS  OUTPUT
*&---------------------------------------------------------------------*
MODULE create_pfstatus OUTPUT.
  SET PF-STATUS 'STATUS'.
ENDMODULE.                 " CREATE_PFSTATUS  OUTPUT
*&---------------------------------------------------------------------*
*&      Module  CREATE_TOOLBAR  OUTPUT
*&---------------------------------------------------------------------*
MODULE create_toolbar OUTPUT.
  IF lr_cust_cont IS NOT BOUND.
    CREATE OBJECT lr_cust_cont
      EXPORTING
        container_name 'TOOLBAR'
        repid          sy-repid
        dynnr          sy-dynnr.

  ENDIF.

  IF lr_toolbar IS NOT BOUND.
    CREATE OBJECT lr_toolbar
      EXPORTING
        parent             lr_cust_cont
        display_mode       cl_gui_toolbar=>m_mode_horizontal " Display mode
        name               'Demo Toolbar'
      EXCEPTIONS
        cntl_install_error 1
        cntl_error         2
        cntb_wrong_version 3
        OTHERS             4.

  ENDIF.

  CLEAR ls_btn.
  ls_btn-butn_type '0'.
  ls_btn-function 'TSAVE'.
  ls_btn-icon '@2L@'.
  ls_btn-text 'Save'.
  APPEND ls_btn TO lt_btn.

  CLEAR ls_btn.
  ls_btn-butn_type '3'"Separator
  APPEND ls_btn TO lt_btn.

  CLEAR ls_btn.
  ls_btn-butn_type '0'.
  ls_btn-function 'EDIT'.
  ls_btn-icon '@TE@'.
  ls_btn-text 'Edit'.
  APPEND ls_btn TO lt_btn.

  CLEAR ls_btn.
  ls_btn-butn_type '3'" Separator
  APPEND ls_btn TO lt_btn.

  CLEAR ls_btn.
  ls_btn-butn_type '0'.
  ls_btn-function 'TCANCEL'.
  ls_btn-icon '@0W@'.
  ls_btn-text 'Cancel'.
  APPEND ls_btn TO lt_btn.

  CALL METHOD lr_toolbar->add_button_group
    EXPORTING
      data_table       lt_btn
    EXCEPTIONS
      dp_error         1
      cntb_error_fcode 2
      OTHERS           3.

ENDMODULE.                 " CREATE_TOOLBAR  OUTPUT
*&---------------------------------------------------------------------*
*&      Module  HANDLE_USERACTION  INPUT
*&---------------------------------------------------------------------*
MODULE handle_useraction INPUT.
  CASE sy-ucomm.
    WHEN 'BACK' OR 'EXIT' OR 'CANCEL'.
      LEAVE TO SCREEN 0.
    WHEN OTHERS.
  ENDCASE.
ENDMODULE.                 " HANDLE_USERACTION  INPUT

---------------------------------------------------------------------------------------------------------
Screen flow logic.
 Screen layout with container.

Execute the program.



---------------------------------------------------------------------------------------------------------

Creating ToolBar buttons in containers Using CL_GUI_TOOLBAR class

----------------------------------------------------------------------------------------------------------
Class - CL_GUI_TOOLBAR provides methods to create toll bar buttons ( of different types) on the container.
----------------------------------------------------------------------------------------------------------
DATA lr_toolbar TYPE REF TO cl_gui_toolbar,
                     lr_cust_cont TYPE REF TO cl_gui_custom_container.

CALL SCREEN 0001.
*&---------------------------------------------------------------------*
*&      Module  CREATE_PFSTATUS  OUTPUT
*&---------------------------------------------------------------------*
MODULE create_pfstatus OUTPUT.
  SET PF-STATUS 'STATUS'.
ENDMODULE.                 " CREATE_PFSTATUS  OUTPUT
*&---------------------------------------------------------------------*
*&      Module  CREATE_TOOLBAR  OUTPUT
*&---------------------------------------------------------------------*
MODULE create_toolbar OUTPUT.
  IF lr_cust_cont IS NOT BOUND.
    CREATE OBJECT lr_cust_cont
      EXPORTING
        container_name 'TOOLBAR'
        repid          sy-repid
        dynnr          sy-dynnr.

  ENDIF.

  IF lr_toolbar IS NOT BOUND.
    CREATE OBJECT lr_toolbar
      EXPORTING
        parent             lr_cust_cont
        display_mode       cl_gui_toolbar=>m_mode_horizontal
        name               'Demo Toolbar'
      EXCEPTIONS
        cntl_install_error 1
        cntl_error         2
        cntb_wrong_version 3
        OTHERS             4.

  ENDIF.

  CALL METHOD lr_toolbar->add_button
    EXPORTING
      fcode            'TSAVE'  " Function Code of Button
      icon             '@2L@'    " Icon Name
*     is_disabled      =     " Button Status
      butn_type        0   " Button Types Defined in CNTB
      text             'Save'  " Text Shown to the Right of the Image
      quickinfo        'Click to update the database'
*     is_checked       =     " Button Selected
    EXCEPTIONS
      cntl_error       1
      cntb_btype_error 2
      cntb_error_fcode 3
      OTHERS           4.

ENDMODULE.                 " CREATE_TOOLBAR  OUTPUT
*&---------------------------------------------------------------------*
*&      Module  HANDLE_USERACTION  INPUT
*&---------------------------------------------------------------------*
module HANDLE_USERACTION input.
CASE sy-ucomm.
  WHEN 'BACK' OR 'EXIT' OR 'CANCEL'.
    LEAVE TO SCREEN 0.
  WHEN OTHERS.
ENDCASE.
endmodule.                 " HANDLE_USERACTION  INPUT


--------------------------------------------------------------------------------------------------------

Domain fixed value defines Button Types.

Flow logic of the screen.



 Screen container.


 Execute the program and a button is created on the Container .


------------------------------------------------------------------------------------------------------------

Comments system

Disqus Shortname