1. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2. * Descripción  : Plantilla para crear Arboles
  3. * Autor        : MGUTIERREZ
  4. * Ultima Modif : 20.02.2008
  5. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  6. *
  7. * Buscar y reemplazar
  8. *     QQQQQ : Nombre de la estructura que aparecerá en el Arbol
  9. *
  10.  
  11. REPORT Z_TREE.
  12.  
  13. TABLES: /gex/01, /gex/07.
  14.  
  15. DATA: g_alv_tree         TYPE REF TO cl_gui_alv_tree,
  16.      g_custom_container TYPE REF TO cl_gui_custom_container,
  17.      g_toolbar          TYPE REF TO cl_gui_toolbar.
  18.  
  19. DATA: gt_datos        TYPE /gex/01 OCCURS 0,
  20.      wa_datos        TYPE /gex/01,
  21.      gt_fieldcatalog TYPE lvc_t_fcat,
  22.      ok_code LIKE sy-ucomm,
  23.      save_ok LIKE sy-ucomm,
  24.      g_fav_key TYPE lvc_nkey.
  25.  
  26. *###############################################################
  27. * LOCAL CLASSES
  28. *###############################################################
  29. CLASS lcl_toolbar_event_receiver DEFINITION.
  30.  
  31.  PUBLIC SECTION.
  32. * Define an event handler method to react to fired function codes
  33. * of the toolbar.                   .
  34.    METHODS: on_function_selected
  35.               FOR EVENT function_selected OF cl_gui_toolbar
  36.                 IMPORTING fcode.
  37.  
  38. * Evento para el dobleclick
  39.    METHODS handle_item_double_click
  40.      FOR EVENT item_double_click OF cl_gui_alv_tree
  41.      IMPORTING node_key
  42.                fieldname.
  43.  
  44. ENDCLASS.                    "lcl_toolbar_event_receiver DEFINITION
  45.  
  46. CLASS lcl_toolbar_event_receiver IMPLEMENTATION.
  47.  
  48.  
  49.  METHOD handle_item_double_click.
  50.  
  51.    READ TABLE gt_datos INTO wa_datos INDEX 1.
  52.    CASE fieldname.
  53.      WHEN 'CLASE' OR 'EJEEXP' OR 'NUMEXP' OR 'EXPEDIENTE'.
  54.  
  55.    ENDCASE.
  56.  ENDMETHOD.                    "handle_item_double_click
  57. *
  58.  METHOD on_function_selected.
  59.  
  60.    DATA: lt_selected_nodes TYPE lvc_t_nkey,
  61.          l_selected_node   TYPE lvc_nkey,
  62.          l_rc              TYPE c.
  63.  
  64.    DATA: node TYPE lvc_nkey,
  65.          gt_texto(120).
  66. * Query the function codes of the toolbar in your implementation.
  67.    CASE fcode.
  68.      WHEN 'DELETE'.
  69.  
  70.        CALL METHOD g_alv_tree->get_selected_nodes
  71.          CHANGING
  72.            ct_selected_nodes = lt_selected_nodes.
  73.        CALL METHOD cl_gui_cfw=>flush.
  74.  
  75.        READ TABLE lt_selected_nodes INTO l_selected_node INDEX 1.
  76.        IF sy-subrc EQ 0.
  77.          CALL FUNCTION 'POPUP_TO_CONFIRM_STEP'
  78.            EXPORTING
  79.              textline1      = 'Realmente quiere borrar'
  80.              textline2      = 'si confirma borrará la carpeta!!'
  81.              titel          = 'Confirmacion'
  82.              cancel_display = ' '
  83.            IMPORTING
  84.              answer         = l_rc.
  85.          IF l_rc EQ 'J'.
  86.            CALL METHOD g_alv_tree->delete_subtree
  87.              EXPORTING
  88.                i_node_key = l_selected_node.
  89. *   update frontend
  90.            CALL METHOD g_alv_tree->frontend_update.
  91.          ENDIF.
  92.        ELSE. "sy-subrc EQ 0
  93.          MESSAGE i000(0k) WITH 'Por favor marca una carpeta'.
  94.        ENDIF.
  95.  
  96.      WHEN 'DELEXP'.
  97.        CALL METHOD g_alv_tree->get_selected_item
  98.          IMPORTING
  99.            e_selected_node = node.
  100.  
  101.        CALL METHOD cl_gui_cfw=>flush.
  102.        IF NOT node IS INITIAL.
  103. *          READ TABLE gt_datos ."INDEX node.
  104. *          CONCATENATE gt_datos-clase gt_datos-ejeexp gt_datos-numexp
  105. *                      INTO gt_texto SEPARATED BY '/'.
  106.          CALL FUNCTION 'POPUP_TO_CONFIRM_STEP'
  107.            EXPORTING
  108.              textline1      = 'Realmente quiere borrar el expediente:'
  109.              textline2      = gt_texto
  110.              titel          = 'Confirmacion'
  111.              cancel_display = ' '
  112.            IMPORTING
  113.              answer         = l_rc.
  114.          IF l_rc EQ 'J'.
  115.            CALL METHOD g_alv_tree->delete_subtree
  116.              EXPORTING
  117.                i_node_key = node.
  118. *   update frontend
  119.            CALL METHOD g_alv_tree->frontend_update.
  120.          ENDIF.
  121.        ELSE. "sy-subrc EQ 0
  122.          MESSAGE i000(0k) WITH 'Por favor marca un expediente'.
  123.        ENDIF.
  124.    ENDCASE.
  125.  ENDMETHOD.                    "on_function_selected
  126.  
  127. ENDCLASS.                    "lcl_toolbar_event_receiver IMPLEMENTATION
  128.  
  129. DATA: l_event_receiver TYPE REF TO lcl_toolbar_event_receiver.
  130.  
  131. SELECTION-SCREEN BEGIN OF BLOCK bloque1 WITH FRAME TITLE text-001.
  132.  
  133. PARAMETERS:  p_clase LIKE /gex/01-clase OBLIGATORY,
  134.             p_ejer LIKE /gex/01-ejeexp OBLIGATORY.
  135.  
  136. SELECTION-SCREEN END OF BLOCK bloque1.
  137.  
  138. * START-OF-SELECTION --------------------------------------------------*
  139. START-OF-SELECTION.
  140.  CALL SCREEN 100.
  141.  
  142. * END-OF-SELECTION ----------------------------------------------------*
  143. END-OF-SELECTION.
  144.  
  145. MODULE pbo OUTPUT.
  146.  
  147.  SET PF-STATUS 'MAIN100'.
  148.  SET TITLEBAR 'TITULO_100'.
  149.  
  150.  IF g_alv_tree IS INITIAL.
  151.    PERFORM init_tree.
  152.  
  153.    CALL METHOD cl_gui_cfw=>flush
  154.      EXCEPTIONS
  155.        cntl_system_error = 1
  156.        cntl_error        = 2.
  157.  ENDIF.
  158. ENDMODULE.                             " PBO  OUTPUT
  159.  
  160. MODULE pai INPUT.
  161.  save_ok = ok_code.
  162.  CLEAR ok_code.
  163.  
  164.  CASE save_ok.
  165.    WHEN 'EXIT' OR 'BACK' OR 'CANC'.
  166.      PERFORM exit_program.
  167.  
  168.    WHEN OTHERS.
  169. * Toolbar events are registered in constructur method of
  170. * CL_ALV_TREE_BASE as application events. So the dispatch call
  171. * is a must if you want to use the standard toolbar.
  172.      CALL METHOD cl_gui_cfw=>dispatch.
  173.  ENDCASE.
  174.  
  175.  CALL METHOD cl_gui_cfw=>flush.
  176. ENDMODULE.                             " PAI  INPUT
  177.  
  178. FORM init_tree.
  179. * Creamos el container
  180.  DATA: l_tree_container_name(30) TYPE c.
  181.  DATA: l_cabecera TYPE treev_hhdr.
  182.  DATA: l_comentario  TYPE slis_t_listheader.
  183.  
  184.  PERFORM comentarios USING l_comentario.
  185.  
  186.  l_tree_container_name = 'CCONTAINER1'.
  187.  
  188.  CREATE OBJECT g_custom_container
  189.    EXPORTING
  190.      container_name              = l_tree_container_name
  191.    EXCEPTIONS
  192.      cntl_error                  = 1
  193.      cntl_system_error           = 2
  194.      create_error                = 3
  195.      lifetime_error              = 4
  196.      lifetime_dynpro_dynpro_link = 5.
  197.  
  198. * Creamos un control para el arbol
  199.  CREATE OBJECT g_alv_tree
  200.    EXPORTING
  201.      parent                      = g_custom_container
  202.      node_selection_mode         = cl_gui_column_tree=>node_sel_mode_single
  203.      item_selection              = 'X'
  204.      no_html_header              = ''
  205.      no_toolbar                  = ''
  206.    EXCEPTIONS
  207.      cntl_error                  = 1
  208.      cntl_system_error           = 2
  209.      create_error                = 3
  210.      lifetime_error              = 4
  211.      illegal_node_selection_mode = 5
  212.      failed                      = 6
  213.      illegal_column_name         = 7.
  214.  
  215.  
  216.  PERFORM crear_cabecera_jerarquia CHANGING l_cabecera.
  217.  PERFORM catalogo_campos.
  218.  
  219. * IMPORTANT: Table 'gt_datos' must be empty. Do not change this table
  220. * (even after this method call). You can change data of your table
  221. * by calling methods of CL_GUI_ALV_TREE.
  222. * Furthermore, the output table 'gt_outtab' must be global and can
  223. * only be used for one ALV Tree Control.
  224.  
  225.  CALL METHOD g_alv_tree->set_table_for_first_display
  226.    EXPORTING
  227.      is_hierarchy_header = l_cabecera
  228.      it_list_commentary  = l_comentario
  229.      i_logo              = 'NAVARRA'
  230.      i_background_id     = 'ALV_BACKGROUND'
  231.    CHANGING
  232.      it_fieldcatalog     = gt_fieldcatalog
  233.      it_outtab           = gt_datos. "table must be empty !
  234.  
  235.  PERFORM crear_jerarquia.
  236.  PERFORM modificar_barra.
  237.  PERFORM registrar_eventos.
  238.  
  239. * Send data to frontend.
  240.  CALL METHOD g_alv_tree->frontend_update.
  241.  
  242. ENDFORM.                               " init_tree
  243.  
  244. FORM crear_cabecera_jerarquia CHANGING
  245.                               p_hierarchy_header TYPE treev_hhdr.
  246.  
  247.  p_hierarchy_header-heading = 'Expedientes'.
  248.  p_hierarchy_header-t_image = '@7K@'.
  249.  p_hierarchy_header-tooltip = 'Texto explicativo al pasar el cursor!'.
  250.  p_hierarchy_header-width = 50.
  251.  p_hierarchy_header-width_pix = 'X'.
  252.  
  253. ENDFORM.                               " crear_cabecera_jerarquia
  254.  
  255. FORM exit_program.
  256.  
  257.  CALL METHOD g_custom_container->free.
  258.  LEAVE PROGRAM.
  259.  
  260. ENDFORM.                               " exit_program
  261.  
  262. FORM catalogo_campos.
  263.  DATA: ls_fieldcatalog TYPE lvc_s_fcat.
  264.  
  265. * Creamos el catalogo de campos con la estructura que le pasamos!
  266.  CALL FUNCTION 'LVC_FIELDCATALOG_MERGE'
  267.    EXPORTING
  268.      i_structure_name = '/GEX/01'
  269.    CHANGING
  270.      ct_fieldcat      = gt_fieldcatalog.
  271.  
  272. * Cambiamos el catalogo de campos!
  273.  LOOP AT gt_fieldcatalog INTO ls_fieldcatalog.
  274.    CASE ls_fieldcatalog-fieldname.
  275.      WHEN 'CLASE'.
  276.        ls_fieldcatalog-key = 'X'.
  277.    ENDCASE.
  278.    MODIFY gt_fieldcatalog FROM ls_fieldcatalog.
  279.  ENDLOOP.
  280. ENDFORM.                               " catalogo_campos
  281.  
  282. FORM registrar_eventos.
  283.  DATA: lt_events TYPE cntl_simple_events,
  284.        l_event TYPE cntl_simple_event.
  285.  
  286.  CALL METHOD g_alv_tree->get_registered_events
  287.    IMPORTING
  288.      events = lt_events.
  289.  
  290. * register events on frontend
  291.  CALL METHOD g_alv_tree->set_registered_events
  292.    EXPORTING
  293.      events                    = lt_events
  294.    EXCEPTIONS
  295.      cntl_error                = 1
  296.      cntl_system_error         = 2
  297.      illegal_event_combination = 3.
  298.  
  299.  CREATE OBJECT l_event_receiver.
  300.  SET HANDLER l_event_receiver->on_function_selected FOR g_toolbar.
  301.  SET HANDLER l_event_receiver->handle_item_double_click FOR g_alv_tree.
  302.  
  303. ENDFORM.                               " registrar_eventos
  304.  
  305. FORM crear_jerarquia.
  306.  
  307.  DATA: lt_datos TYPE TABLE OF /gex/01,
  308.        wa_datos TYPE /gex/01,
  309.        l_top_key TYPE lvc_nkey.
  310.  
  311.  DATA: ls_texto TYPE lvc_value.
  312.  DATA: l_last_key TYPE lvc_nkey.
  313.  
  314. * Buscamos los datos.
  315.  SELECT * FROM /gex/07 WHERE clase_p = p_clase AND
  316.                              ejeexp_p = p_ejer.
  317.  
  318.    wa_datos-clase = /gex/07-clase_p.
  319.    wa_datos-ejeexp = /gex/07-ejeexp_p.
  320.    wa_datos-numexp = /gex/07-numexp_p.
  321.  
  322.    COLLECT  wa_datos INTO lt_datos.
  323.  ENDSELECT.
  324.  
  325.  
  326.  SORT lt_datos BY clase ejeexp numexp.
  327.  
  328.  LOOP AT lt_datos INTO  wa_datos.
  329.    SELECT SINGLE titulo FROM /gex/01 INTO /gex/01-titulo
  330.                                      WHERE clase = wa_datos-clase
  331.                                        AND ejeexp = wa_datos-ejeexp
  332.                                        AND numexp = wa_datos-numexp.
  333.  
  334.    ls_texto = /gex/01-titulo.
  335.    PERFORM add_carpeta USING wa_datos space ls_texto space
  336.                         CHANGING l_top_key.
  337.  
  338.  
  339.    SELECT * FROM /gex/07 WHERE clase_p = wa_datos-clase
  340.                            AND ejeexp_p = wa_datos-ejeexp
  341.                            AND numexp_p = wa_datos-numexp.
  342.  
  343.      SELECT SINGLE *  FROM /gex/01 WHERE clase = /gex/07-clase_h
  344.                            AND ejeexp = /gex/07-ejeexp_h
  345.                            AND numexp = /gex/07-numexp_h.
  346.  
  347.      ls_texto = /gex/01-titulo.
  348.      MOVE-CORRESPONDING /gex/01 TO wa_datos.
  349.      PERFORM add_nodo USING  wa_datos
  350.                                l_top_key  ls_texto
  351.                                '@IF@'
  352.                         CHANGING  l_last_key .
  353.    ENDSELECT.
  354.  ENDLOOP.
  355. ENDFORM.                               " crear_jerarquia
  356.  
  357. FORM add_nodo USING  p_datos TYPE /gex/01
  358.                       p_relat_key TYPE lvc_nkey
  359.                       p_node_text TYPE lvc_value
  360.                       p_node_image TYPE tv_image
  361.              CHANGING p_new_key.
  362.  
  363.  DATA: l_layout_node TYPE lvc_s_layn,
  364.        l_handle_line TYPE i.
  365.  
  366.  l_layout_node-n_image = p_node_image.
  367.  
  368.  CALL METHOD g_alv_tree->add_node
  369.    EXPORTING
  370.      i_relat_node_key = p_relat_key
  371.      i_relationship   = cl_gui_column_tree=>relat_last_child
  372.      i_node_text      = p_node_text
  373.      is_outtab_line   = p_datos
  374.      is_node_layout   = l_layout_node
  375.    IMPORTING
  376.      e_new_node_key   = p_new_key.
  377.  
  378. ENDFORM.                    "add_nodo
  379.  
  380. FORM add_carpeta USING   p_datos TYPE /gex/01
  381.                         p_relat_key TYPE lvc_nkey
  382.                         p_node_text TYPE lvc_value
  383.                         p_node_image TYPE tv_image
  384.                CHANGING p_new_key.
  385.  
  386.  DATA: l_layout_node TYPE lvc_s_layn,
  387.        l_handle_favourite_folder TYPE i.
  388.  
  389.  l_layout_node-isfolder = 'X'.         "Carpeta por defecto!
  390. *  l_layout_node-n_image = p_node_image. "En vez de la carpeta
  391. * estos campos al expandir y contraer!!
  392. *l_layout_node-N_IMAGE    = '@01@'.
  393. *l_layout_node-EXP_IMAGE  = '@02@'.
  394.  
  395.  CALL METHOD g_alv_tree->add_node
  396.    EXPORTING
  397.      i_relat_node_key = p_relat_key
  398.      i_relationship   = cl_gui_column_tree=>relat_last_child
  399.      i_node_text      = p_node_text
  400.      is_outtab_line   = p_datos
  401.      is_node_layout   = l_layout_node
  402.    IMPORTING
  403.      e_new_node_key   = p_new_key.
  404. ENDFORM.                    "add_carpeta
  405.  
  406. FORM comentarios USING e04_lt_top_of_page TYPE slis_t_listheader.
  407.  
  408.  DATA: ls_line TYPE slis_listheader.
  409.  
  410.  CLEAR ls_line.
  411.  REFRESH:  e04_lt_top_of_page.
  412.  
  413. * TITULO ****
  414. *  H = Header, S = Selection, A = Action
  415.  
  416.  ls_line-typ  = 'H'.
  417.  ls_line-info+20  = 'Expedientes'.
  418.  APPEND ls_line TO e04_lt_top_of_page.
  419.  CLEAR ls_line.
  420.  
  421.  
  422. ENDFORM.                    "comentarios
  423.  
  424. FORM modificar_barra.
  425.  
  426.  CALL METHOD g_alv_tree->get_toolbar_object
  427.    IMPORTING
  428.      er_toolbar = g_toolbar.
  429.  
  430.  CHECK NOT g_toolbar IS INITIAL.
  431.  
  432.  CALL METHOD g_toolbar->add_button
  433.    EXPORTING
  434.      fcode     = ''
  435.      icon      = ''
  436.      butn_type = cntb_btype_sep.
  437.  
  438.  CALL METHOD g_toolbar->add_button
  439.    EXPORTING
  440.      fcode     = 'DELETE'
  441.      icon      = '@11@'
  442.      butn_type = cntb_btype_button
  443.      text      = 'Borrar Nodo'
  444.      quickinfo = text-901.   "Delete subtree
  445.  
  446.  CALL METHOD g_toolbar->add_button
  447.    EXPORTING
  448.      fcode     = 'DELEXP'
  449.      icon      = '@11@'
  450.      butn_type = cntb_btype_button
  451.      text      = 'Borrar Hijo'
  452.      quickinfo = text-901.   "Delete subtree
  453.  
  454. ENDFORM.                    "modificar_barra