Showing posts with label ALV Tree Using Child and Parent Relationship using OOPs Concept. Show all posts
Showing posts with label ALV Tree Using Child and Parent Relationship using OOPs Concept. Show all posts

28/03/2015

ALV Tree Using Child and Parent Relationship using OOPs Concept

*-----------------------------------------------------------------------
*                         P R O G R A M
*                     D E S C R I P T I O N
*
*  To view area types the Building structure and  Measurement types are
*   defined for business entity, building, property, rental object
*-----------------------------------------------------------------------
*  Transaction:
*-----------------------------------------------------------------------
*  Author:
*  Date  :  2015-02-25
*-----------------------------------------------------------------------
* Table Types
*-----------------------------------------------------------------------
types: begin of ty_data_st,
    meassum type char4,
       measured type char4,
       end of   ty_data_st,
       gt_data_tt  type standard table of  ty_data_st,


       begin of ty_text_st,
       meas    type rebdmeas,
       text    type rebdxmmeas,
       end of   Ty_text_st,
       GT_text_tt type standard  table of  Ty_text_st.



*-----------------------------------------------------------------------
* Global variables
*-----------------------------------------------------------------------
data:  gt_hier  type gt_data_tt, " Tree hierarchy
       gt_text  type gt_text_tt,  " Text table
       gt_mesr type gt_data_tt,
       gt_tree type gt_data_tt,
       go_alv_tree         type ref to cl_gui_alv_tree,
       go_custom_container type ref to cl_gui_custom_container,
       gv_flag  type i value 1.

*-----------------------------------------------------------------------
* Selection screen.
*-----------------------------------------------------------------------
"Source Measurement Type for Transformation
parameters p_msrc type char4 obligatory.

*-----------------------------------------------------------------------
* INITIALIZATION
*-----------------------------------------------------------------------
initialization.
  perform get_data changing gt_mesr
                            gt_text .

*-----------------------------------------------------------------------
* AT SELECTION SCREEN
*-----------------------------------------------------------------------
  " Perform execute when User not taking any F4 help
  " get the master data and text table data

at selection-screen on value-request for p_msrc.
  perform get_data changing   gt_mesr
                              gt_text.
  " F4 help function module
  perform f4_help .

end-of-selection.

  if gt_text is not initial and gt_mesr is not initial.
    call screen 100.
  else.
    if gv_flag eq 0.
     clear p_msrc.

    leave list-processing.
      return.
      endif.
  endif.

*&---------------------------------------------------------------------*
*&      Module  PBO  OUTPUT
*&---------------------------------------------------------------------*
*       process before output
*----------------------------------------------------------------------*
module pbo output.
  data gv_title type string .
  "set pf status
  set pf-status 'MAIN100'.
  " Set Dynamic Title of the screen
  read table gt_text into data(gs_text) with key meas = p_msrc .
  if sy-subrc eq 0.
    concatenate p_msrc '-' gs_text-text ')' into gv_title separated by space.
  endif.

  set titlebar 'YRER1300' with gv_title.

  if go_alv_tree is initial.
    perform init_tree .

    call method cl_gui_cfw=>flush
      exceptions
        cntl_system_error = 1
        cntl_error        = 2.
    if sy-subrc ne 0.
      call function 'POPUP_TO_INFORM'
        exporting
          titel = 'Automation Queue failure'(004)
          txt1  = 'Internal error:'(003)
          txt2  = 'A method in the automation queue'(005)
          txt3  = 'caused a failure.'(006).
    endif.
  endif.

endmodule.                             " PBO  OUTPUT
*&---------------------------------------------------------------------*
*&      Module  PAI  INPUT
*&---------------------------------------------------------------------*
*       process after input
*----------------------------------------------------------------------*
module pai input.

  case sy-ucomm.
    when 'EXIT' or 'BACK' or 'CANC'.
      perform exit_program.
    when others.
      " Call dispatch to process toolbar functions
      call method cl_gui_cfw=>dispatch.
  endcase.

  call method cl_gui_cfw=>flush.
endmodule.                             " PAI  INPUT

*&---------------------------------------------------------------------*
*&      Form  init_tree
*&---------------------------------------------------------------------*

form init_tree.


  data :   ls_hierarchy_header type treev_hhdr,
           lt_fieldcatalog type lvc_t_fcat,
           ls_fieldcatlog type lvc_s_fcat. "Fieldcatalog
  "create container for alv-tree
  data: lv_tree_container_name(30) type c.

  lv_tree_container_name = 'CCONTAINER1'.

  create object go_custom_container
    exporting
      container_name              = lv_tree_container_name
    exceptions
      cntl_error                  = 1
      cntl_system_error           = 2
      create_error                = 3
      lifetime_error              = 4
      lifetime_dynpro_dynpro_link = 5.
  if sy-subrc <> 0.
    message x208(00) with 'ERROR'(007).
  endif.

  "create tree control
  create object go_alv_tree
    exporting
      parent                      = go_custom_container
      node_selection_mode         = cl_gui_column_tree=>node_sel_mode_single
      item_selection              = 'X'
      no_html_header              = 'X'
      no_toolbar                  = ''
    exceptions
      cntl_error                  = 1
      cntl_system_error           = 2
      create_error                = 3
      lifetime_error              = 4
      illegal_node_selection_mode = 5
      failed                      = 6
      illegal_column_name         = 7.
  if sy-subrc <> 0.
    message x208(00) with 'ERROR'(007).                     "#EC NOTEXT
  endif.


  "perpare for fieldcatlog table
  ls_fieldcatlog-tabname   = 'GT_MESR'.
  ls_fieldcatlog-fieldname = 'MEASSUM'.
  ls_fieldcatlog-coltext   = 'Measurement Source'(002).
  ls_fieldcatlog-col_opt   = 'X'.
  ls_fieldcatlog-emphasize = 'X'.
  ls_fieldcatlog-seltext   = 'Measurement Source'(002).
  append ls_fieldcatlog to lt_fieldcatalog.

  "Header information

  perform build_hierarchy_header changing ls_hierarchy_header.

  call method go_alv_tree->set_table_for_first_display
    exporting
      is_hierarchy_header = ls_hierarchy_header
    changing
      it_outtab           = gt_tree
      it_fieldcatalog     = lt_fieldcatalog.

  call method go_alv_tree->column_optimize.


  " Create hierarchy (nodes and leaves)
  perform create_hierarchy .

  "Send data to frontend.
  call method go_alv_tree->frontend_update.


endform.                               " init_tree
*&---------------------------------------------------------------------*
*&      Form  build_hierarchy_header
*&---------------------------------------------------------------------*

form build_hierarchy_header changing  cs_hierarchy_header type treev_hhdr.

  cs_hierarchy_header-heading = '         Measurement Types Description'(001).
  cs_hierarchy_header-tooltip = 'Measurement Types'(009).
  cs_hierarchy_header-width = 1000.
  cs_hierarchy_header-width_pix = ' '.

endform.                               " build_hierarchy_header
*&---------------------------------------------------------------------*
*&      Form  exit_program
*&---------------------------------------------------------------------*
*       free object and leave program
*----------------------------------------------------------------------*
form exit_program.

  call method go_custom_container->free.
  leave to screen 0.

endform.                               " exit_program
*&---------------------------------------------------------------------*
*&      Form  create_hierarchy
*&---------------------------------------------------------------------*

form create_hierarchy .


  data: lv_parent type lvc_nkey,
        lt_hier  type gt_data_tt .
  "perpaare the tree with hierarchy
  perform  perpare_chain using p_msrc    changing gt_hier.

  if gt_hier is not initial .
    lt_hier = gt_hier.
    sort lt_hier ascending by meassum  ascending meassrc .
    delete lt_hier where meassum ne p_msrc .
    loop at lt_hier  into data(ls_hier).
      at new meassrc.
        "add siblling or branch
        perform add_siblling  using   ls_hier
                                        ''
                             changing lv_parent.
      endat.
      " add child or leaf
      perform add_child  using ls_hier lv_parent.

    endloop.
  else.
    gv_flag = 0.
    leave to screen 0.
  endif.
endform.                               " create_hierarchy

*&---------------------------------------------------------------------*
*&      Form  Add_siblling
*&---------------------------------------------------------------------*
form add_siblling   using     ps_hier      type ty_data_st
                              pv_relat_key type lvc_nkey
                changing      cv_node_key  type lvc_nkey.


  data: lv_node_text type lvc_value.

  " read the text of the branch
  read table gt_text into data(ls_text) with key meas = ps_hier-meassrc.
  if sy-subrc eq 0.

    lv_node_text =  ls_text-text..
  endif.

  call method go_alv_tree->add_node
    exporting
      i_relat_node_key = pv_relat_key
      i_relationship   = cl_gui_column_tree=>relat_last_child
      i_node_text      = lv_node_text
      is_outtab_line   = ps_hier-meassrc
    importing
      e_new_node_key   = cv_node_key.

endform.                               " Add_siblling
*--------------------------------------------------------------------
form add_sub_sibbling  using     ps_hier       type ty_data_st
                                 pv_relat_key  type lvc_nkey
                     changing    cv_node_key   type lvc_nkey.

  data: lv_node_text type lvc_value.

  read table gt_text into data(ls_text) with key meas = ps_hier-meassrc.
  if sy-subrc eq 0.
    lv_node_text = ls_text-text.
  endif.


  call method go_alv_tree->add_node
    exporting
      i_relat_node_key = pv_relat_key
      i_relationship   = cl_gui_column_tree=>relat_last_child
      i_node_text      = lv_node_text
      is_outtab_line   = ps_hier-meassrc
    importing
      e_new_node_key   = cv_node_key.

endform.                               " Add_sub_sibbling



*&---------------------------------------------------------------------*
*&      Form  PERPARE_CHAIN
*&---------------------------------------------------------------------*

form perpare_chain  using    pv_parent type  char4
                    changing ct_hier  type  gt_data_tt.



  loop at gt_mesr into data(ls_data) where   meassum = pv_parent.

    append ls_data to ct_hier.

    perform perpare_chain using ls_data-meassrc  changing ct_hier.
  endloop.

endform.                    " PERPARE_CHAIN
*&---------------------------------------------------------------------*
*&      Form  Add_Child
*&---------------------------------------------------------------------*

form add_child   using    ps_hier    type ty_data_st
                          pv_parent  type lvc_nkey .

  data lv_child type lvc_nkey.

  " loop the table reach to leaf node .
  loop at gt_hier into data(ls_hier) Where meassum = ps_hier-meassrc .
    perform add_sub_sibbling  using    ls_hier
                                       pv_parent
                            changing   lv_child.
    perform add_child  using ls_hier lv_child.
  endloop.

endform.                    " Add_Child
*&---------------------------------------------------------------------*
*&      Form  GET_DATA
*&---------------------------------------------------------------------*

form get_data  changing ct_mesr type gt_data_tt
                        ct_text type gt_text_tt.

  if gt_mesr is initial.
    " Get data
    select meassum
           meassrc
    from   tivbdmeassum
    into  table ct_mesr.
  endif.

  if ct_mesr is not initial.
    " Get text
    select meas
           xmmeas
    from   tivbdmeast
    into table ct_text
    for all entries in ct_mesr
    where ( meas = ct_mesr-meassrc
    or      meas = ct_mesr-meassum ) .
     if sy-subrc ne 0.

      endif.
  endif.

endform.                    " GET_DATA
*&---------------------------------------------------------------------*
*&      Form  F4_HELP
*&---------------------------------------------------------------------*

form f4_help .
  if gt_text is not initial.
    " Sort  table befor display in F4 help .
    sort gt_text ascending by meas.
    call function 'F4IF_INT_TABLE_VALUE_REQUEST'
      exporting
        retfield        = 'MEAS'
        dynpprog        = sy-repid
        dynpnr          = sy-dynnr
        dynprofield     = 'P_MSRC'
        value_org       = 'S'
      tables
        value_tab       = gt_text
      exceptions
        parameter_error = 1
        no_values_found = 2
        others          = 3.

  endif.

endform.                    " F4_HELP