BCALV_TREE_01 Analysis
Ssole
Report bcalv_tree_01.
* &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
* Purpose:
* ~~~~~~~~
* This Report Shows The Essential Steps to Build Up A Hierarchy
* USING AN ALV TREE Control (class cl_gui_alv_tree).
* Note That IT IS _NOT_ POSSIBLE TO Build Up this Hierarchy
* USING A Simple Alv Tree Control (Class Cl_gui_alv_tree_simple).
* ------------------------------------------------- ----------------
* To Check Program Behavior
* ~~~~~~~~~~~~~~~~~
* Start this report. The Hierarchy Tree Consists of Nodes for Each
* MONTH ON TOP Level (this Level Can NOT BE Build By A Simple Alv Tree
* Because There is no field for months in output Table sflight.
* Thus, you can not define this hierarchy by sorting.
* Nor Initial Calculation Neither A Special Layout Has Been Applied
* (The Lines On The Right Do Not Show Anything).
* Note Also That this Example Does Not Build Up and Change THE BUILD UP
* Fieldcatalog of the Output Table. For this reason, _all_fields
* of the output Table Are Shown in the columns althought the fields
* Carrid and fldate is the tree on the left.
* (Of course, this is not a good style. See bcalv_tree_02 on how to
* Hide columns).
* ------------------------------------------------- ------------------
* Essential Steps (Search for '')
* ~~~~~~~~~~~~~~~
* 1.usual steps when using control technology.
* 1a. Define Reference variables.
* 1b. Create Alv Tree Control and Corresponding Container. *
* 2.create hierarchy-header
* 3.Create Empty Tree Control
* 4.create hierarchy (Nodes and Leaves)
* 4a. Select Data
* 4b. Sort output Table According to your conceited hierarchy
* 4c. Add data to tree
*
* 5.send Data TO Frontend.
* 6.call dispatch to process Toolbar functions
*
* &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
* §1a. Define Reference Variables
* Define the class of the crusted ALV. And custom container
Data: g_alv_tree type ref to cl_gui_alv_tree,
G_custom_container type ref to cl_gui_custom_container.
* Define the table structure of the output
Data: GT_SFLight Type Sflight 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.
* Initialization this tree
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:' (802) TXT2 = 'a Method in the automation queue' (803)
TXT3 = 'Caused A Failure.' (804).
ENDIF.
ENDIF.
"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 'can'.
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.
"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 (30) Type C.
L_tree_container_name = 'ccontainer1'. "This is already defined in Screen1000.
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 = 4Lifetime_Dynpro_Dynpro_link = 5.
IF SY-SUBRC <> 0.
Message X208 (00) with '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 (00) with '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 us
* 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 = 'sflight'
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
INIT_TREE
* & ------------------------------------------------ --------------------- *
* & Form build_hirarchy_header
* & ------------------------------------------------ --------------------- *
* Build Hierarchy-Header-Information
* ------------------------------------------------- --------------------- *
* -> P_L_HIERARCHY_HEADER STRUCXTURE for Hierarchy-HEADER
* ------------------------------------------------- --------------------- *
Form build_hirarchy_header changing
P_hierarchy_header type treev_hhdr.
* Create Tree's header, below is some attributes
P_hierarchy_header-heading = 'Month / Carrier / Date' (300).
P_hierarchy_header-tooltip = 'flights in a month' (400).
P_hierarchy_header-width = 30.
P_hierarchy_header-width_pix = ''.
"Build_hierarchy_header
* & ------------------------------------------------ --------------------- *
* & Form EXIT_PROGRAM
* & ------------------------------------------------ --------------------- *
* Free Object and Leave Program 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: ls_sflight type sflight,
LT_SFLIGHT TYPE SFLight Occurs 0,
L_YYYYMM (6) Type C, "Year and Month of Sflight-Fldate
L_YYYYMM_LAST (6) Type C,
L_carrid like sflight-carrid,
L_carrid_last like sflight-carrid.
Data: l_month_key type lvc_nkey,
L_carrid_key type lvc_nkey,
L_last_key type lvc_nkey.
* §4a. SELECT DATA
Select * from sflight INTO TABLE LT_SFLIGHT UP to G_MAX ROWS.
* §4b. Sort output table accounting to your conceited hierarchy
* We sort in this Order:
* Year and Month (Top Level Nodes, YYYYMM OF DATS)
* Carrier ID (Next Level)
* Day of Month (Leaves, DD of Dats)
Sort lt_sflight by fldate 0 (6) Carrid Fldate 6 (2).
* Note: The Top Level Nodes Do Not Correspond to a Field of The
* Output Table. INSTEPUT TABLE. INSTEPUT TABLE. INSTEPUT TABLE THE TO INVENT ANOTHER
* hierarchy level Above the levels pot 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-fldate 0 (6).
L_carrid = ls_sflight-carrid.
* 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
'' '
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.
Perform add_carrid_line using ls_sflight
L_Month_Key
CHANGING L_CARRID_KEY.
ENDIF.
* Leaf:
* (Always Inserted As Child of The Last Carrier
* Which is identified by 'l_carrid_key')
Perform add_complete_line using ls_sflight
L_carrid_key
CHANGING L_LAST_KEY.
Endloop.
"CREATE_HIERARCHY
* & ------------------------------------------------ --------------------- *
* & Form add_month
* Here you start adding nodes of the month
* & ------------------------------------------------ --------------------- *
Form add_month using p_yyyymm type c
P_RELAT_KEY TYPE LVC_NKEY
CHANGING P_NODE_KEY TYPE LVC_NKEY.
Data: l_node_text Type LVC_VALUE,
Ls_sflight type sflight,
L_Month (15) Type C. "Output String for Month
* Get Month Name for Node Text
Perform get_month using p_yyyymm
CHANGING L_MONTH.
L_node_text = l_month.
* Add Node:
* ALV Tree Firstly Inserts this Node As a Leaf if you do not supplies
* 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 = ls_sflight
Importing
E_NEW_NODE_KEY = p_node_key.
"Add_mont * ----------------------------------------------------------------------------------------------------------------------------------- -----------------------
Form add_carrid_line using ps_sflight type sflight
P_RELAT_KEY TYPE LVC_NKEY
CHANGING P_NODE_KEY TYPE LVC_NKEY.
Data: l_node_text Type LVC_VALUE,
LS_SFLight Type Sflight.
* Add Node
* ALV Tree Firstly Inserts this Node As a Leaf if you do not supplies
* 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-carrid.
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.
Addressform. "Add_carrid_line
* & ------------------------------------------------ --------------------- *
* & Form add_complete_line
* & ------------------------------------------------ --------------------- *
Form add_complete_line using ps_sflight type sflight
P_RELAT_KEY TYPE LVC_NKEY
CHANGING P_NODE_KEY TYPE LVC_NKEY.
Data: l_node_text Type LVC_Value.
Write ps_sflight-fldate to l_node_text mm / dd / yyyy.
* Add Leaf:
* ALV Tree Firstly Inserts this Node As a Leaf if you do not supplies
* 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.
"Add_complete_line
* & ------------------------------------------------ --------------------- *
* & 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 aciding to the digits in p_yyyymm
Data: l_monthdigits (2) Type 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_YYYYYMM 0 (4) '->' P_MONTH INTO P_MONTH.
"GET_MONTH
* ------------------------------------------------- ----------------------