[ABAP] Baumstruktur mit Hilfe des cl_gui_list_tree anzeigen, Events abfangen

TYPES: ty_it_events TYPE STANDARD TABLE OF cntl_simple_event WITH DEFAULT KEY.

DATA: it_nodes TYPE treev_ntab.
DATA: it_items TYPE STANDARD TABLE OF mtreeitm WITH DEFAULT KEY.

CLASS lcl_event DEFINITION.
  PUBLIC SECTION.
    CLASS-METHODS: on_node_double_click FOR EVENT node_double_click OF cl_gui_list_tree
      IMPORTING
          node_key.
    CLASS-METHODS: on_expand_no_children FOR EVENT expand_no_children OF cl_gui_list_tree
      IMPORTING
          node_key.
    CLASS-METHODS: on_item_double_click FOR EVENT item_double_click OF cl_gui_list_tree
      IMPORTING
          node_key
          item_name.
    CLASS-METHODS: on_button_click FOR EVENT button_click OF cl_gui_list_tree
      IMPORTING
          node_key
          item_name.
    CLASS-METHODS: on_link_click FOR EVENT link_click OF cl_gui_list_tree
      IMPORTING
          node_key
          item_name.
    CLASS-METHODS: on_checkbox_change FOR EVENT checkbox_change OF cl_gui_list_tree
      IMPORTING
          node_key
          item_name
          checked.
ENDCLASS.

CLASS lcl_event IMPLEMENTATION.
  METHOD on_node_double_click.
    WRITE: / |Node double click on node: { node_key }|.
  ENDMETHOD.

  METHOD on_item_double_click.
    WRITE: / |Item double click on node: { node_key }, item: { item_name }|.
  ENDMETHOD.

  METHOD on_link_click.
    WRITE: / |Link click on node: { node_key }, item: { item_name }|.
  ENDMETHOD.

  METHOD on_button_click.
    WRITE: / |Button click on node: { node_key }, item: { item_name }|.
  ENDMETHOD.

  METHOD on_checkbox_change.
    WRITE: / |Checkbox change on node: { node_key }, item: { item_name }|.
  ENDMETHOD.

  METHOD on_expand_no_children.
    WRITE: / |Expand no children on node: { node_key }|.
  ENDMETHOD.
ENDCLASS.

START-OF-SELECTION.

* Container-Objekt erzeugen
  DATA(o_dock) = NEW cl_gui_docking_container( no_autodef_progid_dynnr = abap_true
                                               side = cl_gui_docking_container=>dock_at_left
                                               ratio = 20
                                               caption = 'Datenausgabe' ).

* Tree-Objekt erzeugen
  DATA(o_tree) = NEW cl_gui_list_tree( parent = o_dock
                                       node_selection_mode = cl_gui_list_tree=>node_sel_mode_single
                                       item_selection = abap_true
                                       with_headers = abap_false ).

* Eventtypten müssen gesondert registriert werden
  DATA(it_events) = VALUE ty_it_events( ( eventid    = cl_gui_list_tree=>eventid_node_double_click
                                          appl_event = abap_true )
                                        ( eventid    = cl_gui_list_tree=>eventid_item_double_click
                                          appl_event = abap_true )
                                        ( eventid    = cl_gui_list_tree=>eventid_expand_no_children
                                          appl_event = abap_true )
                                        ( eventid    = cl_gui_list_tree=>eventid_link_click
                                          appl_event = abap_true )
                                        ( eventid    = cl_gui_list_tree=>eventid_button_click
                                          appl_event = abap_true )
                                        ( eventid    = cl_gui_list_tree=>eventid_checkbox_change
                                          appl_event = abap_true ) ).

  o_tree->set_registered_events( events = it_events ).

  SET HANDLER lcl_event=>on_node_double_click FOR o_tree.
  SET HANDLER lcl_event=>on_item_double_click FOR o_tree.
  SET HANDLER lcl_event=>on_expand_no_children FOR o_tree.
  SET HANDLER lcl_event=>on_link_click FOR o_tree.
  SET HANDLER lcl_event=>on_button_click FOR o_tree.
  SET HANDLER lcl_event=>on_checkbox_change FOR o_tree.

* Tree-Nodes einfügen -> die Bezeichner müssen eindeutig sein
  it_nodes = VALUE #( ( node_key  = 'ROOT'
                        hidden    = abap_false
                        disabled  = abap_false
                        isfolder  = abap_true )

                      ( node_key  = 'NODE1'
                        relatkey  = 'ROOT'
                        relatship = cl_gui_list_tree=>relat_last_child
                        hidden    = abap_false
                        disabled  = abap_false
                        isfolder  = abap_true )

                      ( node_key  = 'NODE1_1'
                        relatkey  = 'NODE1'
                        relatship = cl_gui_list_tree=>relat_last_child
                        hidden    = abap_false
                        disabled  = abap_false
                        isfolder  = abap_false )

                      ( node_key  = 'NODE1_2'
                        relatkey  = 'NODE1'
                        relatship = cl_gui_list_tree=>relat_last_child
                        hidden    = abap_false
                        disabled  = abap_false
                        isfolder  = abap_false )

                      ( node_key  = 'NODE1_3'
                        relatkey  = 'NODE1'
                        relatship = cl_gui_list_tree=>relat_last_child
                        hidden    = abap_false
                        disabled  = abap_false
                        isfolder  = abap_false )

                      ( node_key  = 'NODE2'
                        relatkey  = 'ROOT'
                        relatship = cl_gui_list_tree=>relat_last_child
                        hidden    = abap_false
                        disabled  = abap_false
                        isfolder  = abap_false )

                      ( node_key  = 'NODE3'
                        relatkey  = 'ROOT'
                        relatship = cl_gui_list_tree=>relat_last_child
                        hidden    = abap_false
                        disabled  = abap_false
                        isfolder  = abap_false ) ).

* Items für die Nodes definieren
  it_items = VALUE #( ( node_key  = 'ROOT'
                        item_name = '1'
                        style     = cl_gui_list_tree=>style_default
                        class     = cl_gui_list_tree=>item_class_text
                        alignment = cl_gui_list_tree=>align_auto
                        font      = cl_gui_list_tree=>item_font_fixed
                        text      = 'Root'
                        t_image   = icon_led_green )
* Ordner
                      ( node_key  = 'NODE1'
                        item_name = '1'
                        style     = cl_gui_list_tree=>style_default
                        class     = cl_gui_list_tree=>item_class_text
                        alignment = cl_gui_list_tree=>align_auto
                        font      = cl_gui_list_tree=>item_font_fixed
                        text      = 'Node 1'
                        t_image   = icon_led_yellow )
* Item Button
                      ( node_key  = 'NODE1_1'
                        item_name = '1'
                        style     = cl_gui_list_tree=>style_default
                        class     = cl_gui_list_tree=>item_class_button
                        alignment = cl_gui_list_tree=>align_auto
                        font      = cl_gui_list_tree=>item_font_fixed
                        text      = 'Node 1_1'
                        t_image   = icon_led_green )
* Item Checkbox
                      ( node_key  = 'NODE1_2'
                        item_name = '1'
                        style     = cl_gui_list_tree=>style_default
                        class     = cl_gui_list_tree=>item_class_checkbox
                        alignment = cl_gui_list_tree=>align_auto
                        font      = cl_gui_list_tree=>item_font_fixed
                        text      = 'Node 1_2'
                        t_image   = icon_led_green
                        editable  = abap_true )
* Item Link
                      ( node_key  = 'NODE1_3'
                        item_name = '1'
                        style     = cl_gui_list_tree=>style_default
                        class     = cl_gui_list_tree=>item_class_link
                        alignment = cl_gui_list_tree=>align_auto
                        font      = cl_gui_list_tree=>item_font_fixed
                        text      = 'Node 1_3'
                        t_image   = icon_led_green )
* Item mit 4 Spalten
                      ( node_key   = 'NODE2'
                        item_name  = '1'
                        style      = cl_gui_list_tree=>style_default
                        class      = cl_gui_list_tree=>item_class_text
                        alignment  = cl_gui_list_tree=>align_auto
                        font       = cl_gui_list_tree=>item_font_fixed
                        text       = ''
                        t_image    = icon_led_green
                        usebgcolor = abap_true )

                      ( node_key   = 'NODE2'
                        item_name  = '2'
                        style      = cl_gui_list_tree=>style_default
                        class      = cl_gui_list_tree=>item_class_text
                        alignment  = cl_gui_list_tree=>align_left
                        font       = cl_gui_list_tree=>item_font_fixed
                        length     = 4
                        text       = '1000'
                        usebgcolor = abap_true )

                      ( node_key   = 'NODE2'
                        item_name  = '3'
                        style      = cl_gui_list_tree=>style_default
                        class      = cl_gui_list_tree=>item_class_text
                        alignment  = cl_gui_list_tree=>align_left
                        font       = cl_gui_list_tree=>item_font_fixed
                        length     = 10
                        text       = 'Liter'
                        usebgcolor = abap_true )

                      ( node_key   = 'NODE2'
                        item_name  = '4'
                        style      = cl_gui_list_tree=>style_default
                        class      = cl_gui_list_tree=>item_class_text
                        alignment  = cl_gui_list_tree=>align_auto
                        font       = cl_gui_list_tree=>item_font_prop
                        text       = 'Testeintrag 1' )
* Item mit 4 Spalten
                      ( node_key   = 'NODE3'
                        item_name  = '1'
                        style      = cl_gui_list_tree=>style_default
                        class      = cl_gui_list_tree=>item_class_text
                        alignment  = cl_gui_list_tree=>align_auto
                        font       = cl_gui_list_tree=>item_font_fixed
                        text       = ''
                        t_image    = icon_led_green
                        usebgcolor = abap_true )

                      ( node_key   = 'NODE3'
                        item_name  = '2'
                        style      = cl_gui_list_tree=>style_default
                        class      = cl_gui_list_tree=>item_class_text
                        alignment  = cl_gui_list_tree=>align_left
                        font       = cl_gui_list_tree=>item_font_fixed
                        length     = 4
                        text       = '0100'
                        usebgcolor = abap_true )

                      ( node_key   = 'NODE3'
                        item_name  = '3'
                        style      = cl_gui_list_tree=>style_default
                        class      = cl_gui_list_tree=>item_class_text
                        alignment  = cl_gui_list_tree=>align_left
                        font       = cl_gui_list_tree=>item_font_fixed
                        length     = 10
                        text       = 'mm'
                        usebgcolor = abap_true )

                      ( node_key   = 'NODE3'
                        item_name  = '4'
                        style      = cl_gui_list_tree=>style_default
                        class      = cl_gui_list_tree=>item_class_text
                        alignment  = cl_gui_list_tree=>align_auto
                        font       = cl_gui_list_tree=>item_font_prop
                        text       = 'Testeintrag 2' ) ).

  o_tree->add_nodes_and_items( node_table                = it_nodes
                               item_table                = it_items
                               item_table_structure_name = 'MTREEITM' ). " Typ muss gleich mit Zeilentyp von it_items sein

* Root-Nodes des Trees expandieren
  o_tree->expand_root_nodes( ).

* leere Toolbar ausblenden
  cl_abap_list_layout=>suppress_toolbar( ).

  WRITE space. " wichtig für Erzwingung der Listenausgabe und Anzeige des Trees

Weiterführende Infos: Link und Link