[ABAP] Tree und SALV-Grid auf dem Selektionsbild ohne zus. Dynpro anzeigen, Eventhandling

**********************************************************************
*
* Datentypen, Variablen, Konstanten
*
**********************************************************************
TYPES: ty_it_events TYPE STANDARD TABLE OF cntl_simple_event WITH DEFAULT KEY.
TYPES: ty_it_nodes TYPE STANDARD TABLE OF mtreesnode WITH DEFAULT KEY.

DATA: o_splitter_main TYPE REF TO cl_gui_splitter_container.
DATA: o_container_l   TYPE REF TO cl_gui_container.
DATA: o_container_r   TYPE REF TO cl_gui_container.
DATA: o_tree TYPE REF TO cl_gui_simple_tree.
DATA: o_salv TYPE REF TO cl_salv_table.

DATA: it_tree_spfli TYPE STANDARD TABLE OF spfli.
DATA: it_salv_spfli TYPE STANDARD TABLE OF spfli.
DATA: it_nodes TYPE ty_it_nodes.
**********************************************************************
*
* Eventhandler
*
**********************************************************************
CLASS lcl_events DEFINITION.
  PUBLIC SECTION.
    CLASS-METHODS: on_selection_changed FOR EVENT selection_changed OF cl_gui_simple_tree
      IMPORTING
          node_key
          sender.
ENDCLASS.

CLASS lcl_events IMPLEMENTATION.
* Klick auf ein aktives Baumelement
  METHOD on_selection_changed.
    IF o_salv IS BOUND.
* Element in der Nodes-Tabelle lesen
      ASSIGN it_nodes[ node_key = node_key ] TO FIELD-SYMBOL(<fs_node>).

* iTab für SALV-Table neu aufbauen, abhängig vom gewählten Baumelement
      it_salv_spfli = VALUE #( FOR l IN it_tree_spfli WHERE ( carrid = <fs_node>-text )
                               ( mandt = l-mandt
                                 carrid = l-carrid
                                 connid = l-connid
                                 countryfr = l-countryfr
                                 cityfrom = l-cityfrom
                                 airpfrom = l-airpfrom
                                 countryto = l-countryto
                                 cityto = l-cityto
                                 airpto = l-airpto
                                 fltime = l-fltime
                                 deptime = l-deptime
                                 arrtime = l-arrtime
                                 distance = l-distance
                                 distid = l-distid
                                 fltype = l-fltype
                                 period = l-period ) ).

* SALV-Table neu anzeigen
      o_salv->refresh( ).
    ENDIF.
  ENDMETHOD.
ENDCLASS.
**********************************************************************
*
* SELECTION-SCREEN
*
**********************************************************************
* leeres Selektionsbild als Dummy für die Container-Objekte (o_splitter_main ...)
SELECTION-SCREEN BEGIN OF SCREEN 2000.
SELECTION-SCREEN END OF SCREEN 2000.
**********************************************************************
*
* INITIALIZATION
*
**********************************************************************
INITIALIZATION.
  SELECT * INTO TABLE it_tree_spfli FROM spfli.
**********************************************************************
*
* START-OF-SELECTION
*
**********************************************************************
START-OF-SELECTION.
* leeres Selektionbild 2000 anzeigen
  CALL SELECTION-SCREEN 2000.
**********************************************************************
*
* AT SELECTION-SCREEN OUTPUT
*
**********************************************************************
AT SELECTION-SCREEN OUTPUT.
* Doppelte Objekterzeugung unterbinden
  IF NOT o_splitter_main IS BOUND.
* Splitter auf default_screen (SELECTION-SCREEN 2000) erzeugen
    o_splitter_main = NEW #( parent  = cl_gui_container=>default_screen
                             rows    = 1
                             columns = 2 ).

* Breite in % (linke Spalte für den Tree)
    o_splitter_main->set_column_width( id = 1 width = 15 ).

* linken und rechten Splitcontainer holen
    o_container_l = o_splitter_main->get_container( row = 1 column = 1 ).
    o_container_r = o_splitter_main->get_container( row = 1 column = 2 ).

* Tree-Objekt erzeugen
    o_tree = NEW #( parent = o_container_l
                    node_selection_mode = cl_gui_simple_tree=>node_sel_mode_single ).

* Eventtypten müssen gesondert registriert werden
    DATA(it_events) = VALUE ty_it_events( ( eventid = cl_gui_simple_tree=>eventid_selection_changed
                                            appl_event = abap_true ) ).

    o_tree->set_registered_events( events = it_events ).

    SET HANDLER lcl_events=>on_selection_changed FOR o_tree.

* Root-Node einfügen
    it_nodes = VALUE #( ( node_key  = 'ROOT'           " Node-Bezeichner
                          relatship = cl_gui_simple_tree=>relat_last_child
                          disabled  = abap_true
                          isfolder  = abap_true        " Typ Ordner für Root-Element
                          n_image   = icon_folder      " Icon Ordner
                          exp_image = icon_open_folder " Icon geöffneter Ordner
                          style     = cl_gui_simple_tree=>style_default
                          text      = 'Airlines' ) ).

* Childs an Root-Node anhängen
    LOOP AT it_tree_spfli ASSIGNING FIELD-SYMBOL(<fs_line>).
* bei Änderung der carrid neue carrid als Child anhängen
      AT NEW carrid.
        APPEND VALUE #( node_key  = |NODE{ sy-tabix }| " Node-Bezeichner
                        relatship = cl_gui_simple_tree=>relat_last_child
                        relatkey  = 'ROOT'             " an ROOT-Element anhängen
                        style     = cl_gui_simple_tree=>style_intensified
                        text      = |{ <fs_line>-carrid }| ) TO it_nodes.
      ENDAT.
    ENDLOOP.

* Nodes im Baum einfügen
    o_tree->add_nodes( table_structure_name = 'MTREESNODE' " Typ muss gleich mit Zeilentyp von ty_it_nodes sein
                       node_table           = it_nodes ).

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

* leeres SALV-Grid erzeugen
    cl_salv_table=>factory( EXPORTING
                              r_container    = o_container_r
                            IMPORTING
                              r_salv_table   = o_salv
                            CHANGING
                              t_table        = it_salv_spfli ).

    o_salv->get_display_settings( )->set_striped_pattern( abap_true ).
    o_salv->get_columns( )->set_optimize( abap_true ).
    o_salv->get_functions( )->set_all( ).
    o_salv->get_selections( )->set_selection_mode( if_salv_c_selection_mode=>row_column ).
    o_salv->display( ).
  ENDIF.
**********************************************************************
*
* AT SELECTION-SCREEN
*
**********************************************************************
AT SELECTION-SCREEN.
* wenn "Ausführen" (F8) geklickt wurde
  IF sy-ucomm = 'CRET'.
* Selektionbild 2000 wieder anzeigen
    CALL SELECTION-SCREEN 2000.
  ENDIF.

[ABAP] Selektionsbildschirm: Auf Änderung der Auswahl in einer Listbox reagieren (OnChange-Ereignis)

* Type für Listbox-Inhalt
TYPES: ty_it_listbox TYPE STANDARD TABLE OF vrm_value WITH DEFAULT KEY.

* Merker für Kommando für ListBox-Change
DATA: lv_ucomm TYPE sy-ucomm.
      
* Listbox auf dem Selektionsbild mit Kommando LB_CHG
* TYPE / Länge des Typs hat Einfluss auf key
PARAMETERS: p_lst TYPE char1 AS LISTBOX VISIBLE LENGTH 20 USER-COMMAND lb_chg.

INITIALIZATION.
* Listbox vorbelegen
  DATA(it_lb) = VALUE ty_it_listbox( ( key = 'A' text = 'Punkt 1' )
                                     ( key = 'B' text = 'Punkt 2' )
                                     ( key = 'C' text = 'Punkt 3' ) ).

  CALL FUNCTION 'VRM_SET_VALUES'
    EXPORTING
      id              = 'P_LST'
      values          = it_lb
    EXCEPTIONS
      id_illegal_name = 1
      OTHERS          = 2.

* Vorselektion
  IF lines( it_lb ) > 0.
    p_lst = p_lst[ 1 ]-key.
  ENDIF.

AT SELECTION-SCREEN.
* hier das User-Kommando merken
  lv_ucomm = sy-ucomm.

  IF sy-ucomm EQ 'LB_CHG'.
    ...
  ENDIF.
  
AT SELECTION-SCREEN OUTPUT.
* bei Ausgabe auf das gemerkte User-Kommando reagieren
  CASE lv_ucomm.
    WHEN 'LB_CHG'.
      LOOP AT SCREEN INTO DATA(wa).
        ...
        MODIFY SCREEN FROM wa.
      ENDLOOP.
  ENDCASE.

[ABAP] Oberflächenelemente per Button ausblenden

TABLES: sscrfields.

CONSTANTS: BEGIN OF e_status,
             open  TYPE i VALUE 0,
             close TYPE i VALUE 1,
           END OF e_status.

DATA: status_blk2 TYPE i.

SELECTION-SCREEN: BEGIN OF BLOCK blk1 WITH FRAME TITLE title1.
PARAMETERS: p1 TYPE string.
SELECTION-SCREEN: END OF BLOCK blk1.

SELECTION-SCREEN BEGIN OF LINE.
SELECTION-SCREEN PUSHBUTTON (4) btn_oc USER-COMMAND btn_cmd_oc.
SELECTION-SCREEN COMMENT 6(25) lbl_oc.
SELECTION-SCREEN END OF LINE.

SELECTION-SCREEN: BEGIN OF BLOCK blk2 WITH FRAME TITLE title2.
PARAMETERS: s1 TYPE string MODIF ID b2.
PARAMETERS: s2 TYPE string MODIF ID b2.
SELECTION-SCREEN: END OF BLOCK blk2.

INITIALIZATION.
  title1 = 'Kundennummer'.
  title2 = 'Eingabe'.

  lbl_oc  = 'Eingabe'.

  status_blk2 = e_status-close.

  PERFORM set_btn_icon USING status_blk2 CHANGING btn_oc.

AT SELECTION-SCREEN OUTPUT.
  LOOP AT SCREEN INTO DATA(wa).
    CASE wa-group1.       " Name der Modifikationsgruppe
      WHEN 'B2'.          " wenn in Modifikationsgruppe 'B2'
        IF status_blk2 = e_status-open.
          wa-active = '1'.
        ELSE.
          wa-active = '0'.
        ENDIF.
    ENDCASE.

    MODIFY SCREEN FROM wa.
  ENDLOOP.

  PERFORM set_btn_icon USING status_blk2 CHANGING btn_oc.

AT SELECTION-SCREEN.
  CASE sscrfields-ucomm.
    WHEN 'BTN_CMD_OC'.
      IF status_blk2 = e_status-open.
        status_blk2 = e_status-close.
      ELSE.
        status_blk2 = e_status-open.
      ENDIF.
  ENDCASE.

FORM set_btn_icon USING iv_status TYPE i CHANGING btn_icon TYPE any.
  CASE iv_status.
    WHEN e_status-close.
      btn_icon = icon_data_area_expand.
    WHEN e_status-open.
      btn_icon = icon_data_area_collapse.
  ENDCASE.
ENDFORM.

[ABAP] Checkboxen, Selektionsbildschirm Oberflächenelemente An- und Ausschalten

PARAMETERS show_all AS CHECKBOX DEFAULT 'X' USER-COMMAND show. " Checkbox namens 'show_all' welche das USRCMD 'SHOW' sendet

SELECTION-SCREEN BEGIN OF BLOCK select2 WITH FRAME.
PARAMETERS: p1 TYPE c LENGTH 10 MODIF ID grp1, " Eingabefelder zu Gruppe 'grp1' zusammenfassen
            p2 TYPE c LENGTH 10 MODIF ID grp1,
            p3 TYPE c LENGTH 10 MODIF ID grp1.
SELECTION-SCREEN END OF BLOCK select2.

AT SELECTION-SCREEN.
  IF sy-ucomm = 'SHOW'                  " Kommandoflag 'flag' der Checkbox abfangen
    MESSAGE 'Switch.' TYPE 'I'.
  ENDIF.

AT SELECTION-SCREEN OUTPUT.
  LOOP AT SCREEN INTO DATA(screen_wa).  " SCREEN-Elemente durchgehen
    IF screen_wa-group1 = 'GRP1'.       " wenn Element in Gruppe 'grp1'
      IF show_all = 'X'.                " wenn Groupbox-Haken gesetzt
        screen_wa-active = '1'.         " Gruppenelement aktivieren (visible == true)
                                        " nicht unbedingt nötig, da screen_wa-active hier immer automatisch auf '1' gesetzt wird
      ELSE.
        screen_wa-active = '0'.         " Gruppenelement aktivieren (visible == false)
      ENDIF.

      MODIFY SCREEN FROM screen_wa.     " Änderung übernehmen
    ENDIF.
  ENDLOOP.

[ABAP] Buttons auf dem Selektionsbildschirm

* Testparameter: Pflichtfeld mit Groß-Kleinschreibung
PARAMETERS: p_text TYPE string LOWER CASE OBLIGATORY DEFAULT 'Test'. " Testtext für Ausgabe

* 1. Button sendet Kommando CMD1
SELECTION-SCREEN BEGIN OF LINE.
SELECTION-SCREEN PUSHBUTTON 2(20) btn1 USER-COMMAND cmd1.    " Button Pos 2 Länge 20, sendet Kommando 'CMD1'
SELECTION-SCREEN END OF LINE.

* 2. Button sendet Kommando CMD2
SELECTION-SCREEN BEGIN OF LINE.
SELECTION-SCREEN PUSHBUTTON 2(20) btn2 USER-COMMAND cmd2.    " Button Pos 2 Länge 20, sendet Kommando 'CMD2'
SELECTION-SCREEN END OF LINE.

* 3. Button sendet Kommando ONLI
SELECTION-SCREEN BEGIN OF LINE.
SELECTION-SCREEN PUSHBUTTON 2(20) btn3 USER-COMMAND onli.    " Button Pos 2 Länge 20, sendet Kommando 'ONLI'
SELECTION-SCREEN END OF LINE.

INITIALIZATION.
* Button-Texte mit Icons festlegen
  btn1 = |{ icon_abap } CMD1 - Weiter|.
  btn2 = |{ icon_activity } CMD2 - Ende|.

* Buttonbeschriftung mit Icon und Quickinfo für btn3 generieren lassen
  CALL FUNCTION 'ICON_CREATE'
    EXPORTING
      name                  = icon_information
      text                  = 'ONLI - Weiter'
      info                  = 'Quickinfo'
    IMPORTING
      result                = btn3
    EXCEPTIONS
      icon_not_found        = 1
      outputfield_too_short = 2
      OTHERS                = 3.

  IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ENDIF.

AT SELECTION-SCREEN.

* Benutzer-Kommandos des Selektionsbildes abfangen und prüfen
  CASE sy-ucomm.
    WHEN 'CMD1'.
* OK-Code 'ONLI' per Funktionsbaustein setzen
      CALL FUNCTION 'SAPGUI_SET_FUNCTIONCODE' " FktBaustein zum setzen von Funktionscodes aufrufen
        EXPORTING
          functioncode           = 'ONLI'     " dann bei START-OF-SELECTION weiter
        EXCEPTIONS
          function_not_supported = 1.
    WHEN 'CMD2'.
* bei Kommando CMD2 das Programm beenden
      LEAVE PROGRAM.
    WHEN 'ONLI'.
* wenn Kommando ONLI (Ausführen (F8)) kommt, dann ohne Aktion gleich zu START-OF-SELECTION weiter
  ENDCASE.

START-OF-SELECTION.
  WRITE: p_text.