Thursday, May 10, 2012

TREE ALV


REPORT  zalv_tree_02.

* §1a. Define reference variables
DATA: g_alv_tree         TYPE REF TO cl_gui_alv_tree,
      g_custom_container 
TYPE REF TO cl_gui_custom_container.

DATA: gt_sflight      TYPE qmsm OCCURS 0,      "Output-Table
      ok_code 
LIKE sy-ucomm,
      save_ok 
LIKE sy-ucomm,           "OK-Code
      g_max 
TYPE i VALUE 255.

END-OF-SELECTION.

  
CALL SCREEN 100.

*&---------------------------------------------------------------------*
*&      Module  PBO  OUTPUT
*&---------------------------------------------------------------------*
*       process before output
*----------------------------------------------------------------------*
MODULE pbo OUTPUT.

  
SET PF-STATUS 'MAIN100'.
  
SET TITLEBAR 'MAINTITLE'.

  
IF g_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'(801)
          txt1  = 
'Internal error:'(802)
          txt2  = 
'A method in the automation queue'(803)
          txt3  = 
'caused a failure.'(804).
    
ENDIF.
  
ENDIF.

ENDMODULE.                             " PBO  OUTPUT
*&---------------------------------------------------------------------*
*&      Module  PAI  INPUT
*&---------------------------------------------------------------------*
*       process after input
*----------------------------------------------------------------------*
MODULE pai INPUT.
  save_ok = ok_code.
  
CLEAR ok_code.

  
CASE save_ok.
    
WHEN 'EXIT' OR 'BACK' OR 'CANC'.
      
PERFORM exit_program.

    
WHEN OTHERS.
* §6. 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
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM init_tree.
* §1b. Create ALV Tree Control and corresponding Container.

* create container for alv-tree
  
DATA: l_tree_container_name(30TYPE c.

  l_tree_container_name = 
'CCONTAINER1'.

  
CREATE OBJECT g_custom_container
    
EXPORTING
      container_name              = l_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(00WITH 'ERROR'(100).
  
ENDIF.

* create tree control
  
CREATE OBJECT g_alv_tree
    
EXPORTING
      parent                      = g_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(00WITH 'ERROR'.                          "#EC NOTEXT
  
ENDIF.

* §2. Create Hierarchy-header
* The simple ALV Tree uses the text of the fields which were used
* for sorting to define this header. When you use
* the 'normal' ALV Tree the hierarchy is build up freely
* by the programmer this is not possible, so he has to define it
* himself.
  
DATA l_hierarchy_header TYPE treev_hhdr.
  
PERFORM build_hierarchy_header CHANGING l_hierarchy_header.

* §3. Create empty Tree Control
* IMPORTANT: Table 'gt_sflight' must be empty. Do not change this table
* (even after this method call). You can change data of your table
* by calling methods of CL_GUI_ALV_TREE.
* Furthermore, the output table 'gt_outtab' must be global and can
* only be used for one ALV Tree Control.
  
CALL METHOD g_alv_tree->set_table_for_first_display
    
EXPORTING
      i_structure_name    = 
'QMSM'
      is_hierarchy_header = l_hierarchy_header
    
CHANGING
      it_outtab           = gt_sflight. 
"table must be empty !

* §4. Create hierarchy (nodes and leaves)
  
PERFORM create_hierarchy.

* §5. Send data to frontend.
  
CALL METHOD g_alv_tree->frontend_update.

* wait for automatic flush at end of pbo
ENDFORM.                               " init_tree
*&---------------------------------------------------------------------*
*&      Form  build_hierarchy_header
*&---------------------------------------------------------------------*
*       build hierarchy-header-information
*----------------------------------------------------------------------*
*      -->P_L_HIERARCHY_HEADER  strucxture for hierarchy-header
*----------------------------------------------------------------------*
FORM build_hierarchy_header CHANGING
                               p_hierarchy_header 
TYPE treev_hhdr.

  p_hierarchy_header-heading = 
'Notification'(300).
  p_hierarchy_header-tooltip = 
'number'(400).
  p_hierarchy_header-width = 
40.
  p_hierarchy_header-width_pix = 
' '.

ENDFORM.                               " build_hierarchy_header
*&---------------------------------------------------------------------*
*&      Form  exit_program
*&---------------------------------------------------------------------*
*       free object and leave program
*----------------------------------------------------------------------*
FORM exit_program.

  
CALL METHOD g_custom_container->free.
  
LEAVE PROGRAM.

ENDFORM.                               " exit_program
*&---------------------------------------------------------------------*
*&      Form  create_hierarchy
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM create_hierarchy.

  
DATA: lt_sflight TYPE STANDARD TABLE OF qmsm ,
        ls_sflight 
LIKE LINE OF lt_sflight,
        l_yyyymm 
TYPE qmsm-qmnum,            "year and month of sflight-fldate
        l_yyyymm_last 
TYPE qmsm-qmnum,
        l_carrid 
LIKE qmsm-qmnum,
        l_carrid_last 
LIKE qmsm-qmnum.

  
DATA: l_month_key TYPE lvc_nkey,
        l_monthkey 
TYPE lvc_nkey,
        l_carrid_key 
TYPE lvc_nkey,
        l_carridkey 
TYPE lvc_nkey,
        l_last_key 
TYPE lvc_nkey,
        l_lastkey 
TYPE lvc_nkey.

  
DATA: lt_qmma TYPE STANDARD TABLE OF qmma ,
        ls_qmma 
LIKE LINE OF lt_qmma.
* §4a. Select data
  
SELECT * INTO CORRESPONDING FIELDS OF TABLE lt_sflight FROM qmsm ."WHERE qmnum = '000200000006 '. "UP TO g_max ROWS.

  
SORT lt_sflight BY qmnum .
* Note: The top level nodes do not correspond to a field of the
* output table. Instead we use data of the table to invent another
* hierarchy level above the levels that can be build by sorting.

* §4c. Add data to tree

  
LOOP AT lt_sflight INTO ls_sflight.
* Prerequesite: The table is sorted.
* You add a node everytime the values of a sorted field changes.
* Finally, the complete line is added as a leaf below the last
* node.
    l_yyyymm = ls_sflight-qmnum.
    l_carrid = ls_sflight-qmnum.
"ls_sflight-mncod.

* Top level nodes:
    
IF l_yyyymm <> l_yyyymm_last.      "on change of l_yyyymm
      l_yyyymm_last = l_yyyymm.

*Providing no key means that the node is added on top level:
      
PERFORM add_month USING    l_yyyymm ls_sflight
                                      
''
                             
CHANGING l_month_key.
* The month changed, thus, there is no predecessor carrier
      
CLEAR l_carrid_last.
    
ENDIF.

* Carrier nodes:
* (always inserted as child of the last month
*  which is identified by 'l_month_key')
    
IF l_carrid <> l_carrid_last.      "on change of l_carrid
      l_carrid_last = l_carrid.
*Folder Task
      
PERFORM add_carrid_line USING    ls_sflight
                                       l_month_key
                              
CHANGING l_carrid_key.
*Folder Activity
      l_carrid_last = l_carrid.
      
PERFORM add_carridline USING     ls_sflight
                                       l_month_key
                              
CHANGING l_carridkey.
    
ENDIF.

** Leaf:
** (always inserted as child of the last carrier
**  which is identified by 'l_carrid_key')
*Folder Task Item
    
PERFORM add_complete_line USING  ls_sflight
                                     l_carrid_key
                            
CHANGING l_last_key.
*Folder Activity Item
    
ON
 CHANGE OF ls_sflight-qmnum.
      
SELECT * INTO CORRESPONDING FIELDS OF TABLE lt_qmma FROM qmma WHERE qmnum = ls_sflight-qmnum.
      
LOOP AT lt_qmma INTO ls_qmma.
        
PERFORM add_completeline USING  ls_qmma
                                        l_carridkey
                            
CHANGING    l_lastkey.
      
ENDLOOP.
    
ENDON.
  
ENDLOOP.

ENDFORM.                               " create_hierarchy

*&---------------------------------------------------------------------*
*&      Form  add_month
*&---------------------------------------------------------------------*
FORM add_month  USING     p_yyyymm TYPE qmnum
                          p_flight  
TYPE qmsm
                          p_relat_key 
TYPE lvc_nkey
                
CHANGING  p_node_key TYPE lvc_nkey.

  
DATA: l_node_text TYPE lvc_value,
        ls_sflight 
TYPE qmsm,
        l_month(
15TYPE c.            "output string for month

* get month name for node text
  
PERFORM get_month USING p_yyyymm
                    
CHANGING l_month.
  
CONCATENATE 'Notification' '->'p_yyyymm INTO l_node_text .

* add node:
* ALV Tree firstly inserts this node as a leaf if you do not provide
* IS_NODE_LAYOUT with field ISFOLDER set. In form 'add_carrid_line'
* the leaf gets a child and thus ALV converts it to a folder
* automatically.
*
  
CALL METHOD g_alv_tree->add_node
    
EXPORTING
      i_relat_node_key = p_relat_key
      i_relationship   = cl_gui_column_tree=>relat_last_child
      i_node_text      = l_node_text
      is_outtab_line   =  p_flight
    
IMPORTING
      e_new_node_key   = p_node_key.

ENDFORM.                               " add_month
*--------------------------------------------------------------------
FORM add_carrid_line USING     ps_sflight TYPE qmsm
                               p_relat_key 
TYPE lvc_nkey
                     
CHANGING  p_node_key TYPE lvc_nkey.

  
DATA: l_node_text TYPE lvc_value,
        ls_sflight 
TYPE qmsm.

* add node
* ALV Tree firstly inserts this node as a leaf if you do not provide
* IS_NODE_LAYOUT with field ISFOLDER set. In form 'add_carrid_line'
* the leaf gets a child and thus ALV converts it to a folder
* automatically.
*
*  l_node_text =  ps_sflight-mncod.
  
CONCATENATE 'Task' '' INTO l_node_text.
  
CALL METHOD g_alv_tree->add_node
    
EXPORTING
      i_relat_node_key = p_relat_key
      i_relationship   = cl_gui_column_tree=>relat_last_child
      i_node_text      = l_node_text
      is_outtab_line   = ls_sflight
    
IMPORTING
      e_new_node_key   = p_node_key.

ENDFORM.                               " add_carrid_line
*&---------------------------------------------------------------------*
*&      Form  add_carridline
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->PS_SFLIGHT   text
*      -->P_RELAT_KEY  text
*      -->P_NODE_KEY   text
*----------------------------------------------------------------------*
FORM add_carridline USING      ps_sflight TYPE qmsm
                               p_relat_key 
TYPE lvc_nkey
                     
CHANGING  p_node_key TYPE lvc_nkey.

  
DATA: l_node_text TYPE lvc_value,
        ls_sflight 
TYPE qmsm.

* add node
* ALV Tree firstly inserts this node as a leaf if you do not provide
* IS_NODE_LAYOUT with field ISFOLDER set. In form 'add_carrid_line'
* the leaf gets a child and thus ALV converts it to a folder
* automatically.
*
*  l_node_text =  ps_sflight-mncod.
  
CONCATENATE 'Activity' '' INTO l_node_text.
  
CALL METHOD g_alv_tree->add_node
    
EXPORTING
      i_relat_node_key = p_relat_key
      i_relationship   = cl_gui_column_tree=>relat_last_child
      i_node_text      = l_node_text
      is_outtab_line   = ls_sflight
    
IMPORTING
      e_new_node_key   = p_node_key.

ENDFORM.                               " add_carrid_line
*&---------------------------------------------------------------------*
*&      Form  add_complete_line
*&---------------------------------------------------------------------*
FORM add_completeline USING   ps_sflight TYPE qmma
                               p_relat_key 
TYPE lvc_nkey
                     
CHANGING  p_node_key TYPE lvc_nkey.

  
DATA: l_node_text TYPE lvc_value.

  
WRITE ps_sflight-manum TO l_node_text MM/DD/YYYY.

* add leaf:
* ALV Tree firstly inserts this node as a leaf if you do not provide
* IS_NODE_LAYOUT with field ISFOLDER set.
* Since these nodes will never get children they stay leaves
* (as intended).
*
  
CALL METHOD g_alv_tree->add_node
    
EXPORTING
      i_relat_node_key = p_relat_key
      i_relationship   = cl_gui_column_tree=>relat_last_child
      is_outtab_line   = ps_sflight
      i_node_text      = l_node_text
    
IMPORTING
      e_new_node_key   = p_node_key.

ENDFORM.                               " add_complete_line
*&---------------------------------------------------------------------*
*&      Form  add_complete_line
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->PS_SFLIGHT   text
*      -->P_RELAT_KEY  text
*      -->P_NODE_KEY   text
*----------------------------------------------------------------------*

*&---------------------------------------------------------------------*
*&      Form  GET_MONTH
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_P_YYYYMM  text
*      <--P_L_MONTH  text
*----------------------------------------------------------------------*
FORM get_month USING    p_yyyymm
               
CHANGING p_month.
* Returns the name of month according to the digits in p_yyyymm

  
DATA: l_monthdigits(2TYPE c.

  l_monthdigits = p_yyyymm+
4(2).
  
CASE l_monthdigits.
    
WHEN '01'.
      p_month = 
'January'(701).
    
WHEN '02'.
      p_month = 
'February'(702).
    
WHEN '03'.
      p_month = 
'March'(703).
    
WHEN '04'.
      p_month = 
'April'(704).
    
WHEN '05'.
      p_month = 
'May'(705).
    
WHEN '06'.
      p_month = 
'June'(706).
    
WHEN '07'.
      p_month = 
'July'(707).
    
WHEN '08'.
      p_month = 
'August'(708).
    
WHEN '09'.
      p_month = 
'September'(709).
    
WHEN '10'.
      p_month = 
'October'(710).
    
WHEN '11'.
      p_month = 
'November'(711).
    
WHEN '12'.
      p_month = 
'December'(712).
  
ENDCASE.
  
CONCATENATE p_yyyymm+0(4'->' p_month INTO p_month.

ENDFORM.                               " GET_MONTH
*-----------------------------------------------------------------------
*&---------------------------------------------------------------------*
*&      Form  ADD_COMPLETE_LINE
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_LS_SFLIGHT  text
*      -->P_L_CARRID_KEY  text
*      <--P_L_LAST_KEY  text
*----------------------------------------------------------------------*
FORM add_complete_line USING   ps_sflight TYPE qmsm
                               p_relat_key 
TYPE lvc_nkey
                     
CHANGING  p_node_key TYPE lvc_nkey.

  
DATA: l_node_text TYPE lvc_value.

  
WRITE ps_sflight-mncod TO l_node_text MM/DD/YYYY.

* add leaf:
* ALV Tree firstly inserts this node as a leaf if you do not provide
* IS_NODE_LAYOUT with field ISFOLDER set.
* Since these nodes will never get children they stay leaves
* (as intended).
*
  
CALL METHOD g_alv_tree->add_node
    
EXPORTING
      i_relat_node_key = p_relat_key
      i_relationship   = cl_gui_column_tree=>relat_last_child
      is_outtab_line   = ps_sflight
      i_node_text      = l_node_text
    
IMPORTING
      e_new_node_key   = p_node_key.


ENDFORM.                    " ADD_COMPLETE_LINE

No comments:

Post a Comment