Skip to content

ABAP Keyword Documentation →  ABAP − Reference →  SAP GUI User Dialogs →  General Dynpros →  Screen and Screen Elements →  Screen Elements - Examples 

Dynpros, CFW Events

This example shows how CFW events are handled.

Other versions: 7.31 | 7.40 | 7.54

Source Code

REPORT demo_custom_control .

<span class="blue">* Declarations *****************************************************</span>

CLASS event_handler DEFINITION.
  PUBLIC SECTION.
    METHODS: handle_f1 FOR EVENT f1 OF cl_gui_textedit
             IMPORTING sender,
             handle_f4 FOR EVENT f4 OF cl_gui_textedit
             IMPORTING sender.
ENDCLASS.

DATA: ok_code LIKE sy-ucomm,
      save_ok LIKE sy-ucomm.

DATA: init,
      container TYPE REF TO cl_gui_custom_container,
      editor    TYPE REF TO cl_gui_textedit.

DATA: event_tab TYPE cntl_simple_events,
      event     TYPE cntl_simple_event.

DATA: line(256) TYPE c,
      text_tab LIKE STANDARD TABLE OF line,
      field LIKE line.

DATA handle TYPE REF TO event_handler.


<span class="blue">* Reporting Events ***************************************************</span>

START-OF-SELECTION.

  line = 'First line in TextEditControl'.
  APPEND line TO text_tab.
  line = '--------------------------------------------------'.
  APPEND line TO text_tab.
  line = '...'.
  APPEND line TO text_tab.

  CALL SCREEN 100.

<span class="blue">* Dialog Modules *****************************************************</span>

MODULE status_0100 OUTPUT.
  SET PF-STATUS 'SCREEN_100'.
  IF init is initial.
    init = 'X'.
    CREATE OBJECT: container EXPORTING container_name = 'TEXTEDIT',
                   editor    EXPORTING parent = container,
                   handle.
    event-eventid = cl_gui_textedit=>event_f1.
    event-appl_event = ' '.                     "system event
    APPEND event TO event_tab.
    event-eventid = cl_gui_textedit=>event_f4.
    event-appl_event = 'X'.                     "application event
    APPEND event TO event_tab.
    editor->set_registered_events(
                 EXPORTING events = event_tab ).
    SET HANDLER handle->handle_f1
                handle->handle_f4 FOR editor.
  ENDIF.
  editor->set_text_as_stream( EXPORTING text = text_tab ).
ENDMODULE.

MODULE cancel INPUT.
  LEAVE PROGRAM.
ENDMODULE.

MODULE user_command_0100 INPUT.
  save_ok = ok_code.
  CLEAR ok_code.
  CASE save_ok.
    WHEN 'INSERT'.
      editor->get_text_as_stream( IMPORTING text = text_tab ).
    WHEN 'F1'.
      MESSAGE i888(sabapdemos) WITH text-001.
    WHEN OTHERS.
      MESSAGE i888(sabapdemos) WITH text-002.
      cl_gui_cfw=>dispatch( ).                "for application events
      MESSAGE i888(sabapdemos) WITH text-003.
  ENDCASE.
  SET SCREEN 100.
ENDMODULE.

<span class="blue">* Class Implementations **********************************************</span>

CLASS event_handler IMPLEMENTATION.
  METHOD handle_f1.
    DATA row TYPE i.
    MESSAGE i888(sabapdemos) WITH text-004.
    sender->get_selection_pos(
         IMPORTING from_line = row ).
    sender->get_line_text(
         EXPORTING line_number = row
         IMPORTING text = field ).
    cl_gui_cfw=>set_new_ok_code(                "raise PAI for
         EXPORTING new_code = 'F1' ).           "system events
    cl_gui_cfw=>flush( ).
  ENDMETHOD.
  METHOD handle_f4.
    DATA row TYPE i.
    MESSAGE i888(sabapdemos) WITH text-005.
    sender->get_selection_pos(
         IMPORTING from_line = row ).
    sender->get_line_text(
         EXPORTING line_number = row
         IMPORTING text = field ).
    cl_gui_cfw=>flush( ).
  ENDMETHOD.
ENDCLASS.

Description

Dynpro 100 contains an output field field and a custom control called textedit. The flow logic of dynpro 100 is:

PROCESS BEFORE OUTPUT.
  MODULE status_0100.
PROCESS AFTER INPUT.
  MODULE cancel AT EXIT-COMMAND.
  MODULE user_command_0100.

In the GUI status SCREEN_100, the function codes BACK, EXIT, and CANCEL are created with type E and the function code INSERT is created without any specific type.

The program contains a local class called event_handler with event handlers for the events F1 and F4 of global class CL_GUI_TEXTEDIT. When the program is executed, dynpro 100 instantiates objects of the classes CL_GUI_CUSTOM_CONTAINER, CL_GUI_TEXTEDIT, and event_handler after PBO.

The container control is associated with the custom control on the dynpro, and the instance of the textedit control is associated with this container. The events F1 and F4 of the textedit control are registered using the method SET_REGISTERED_EVENTS to ensure that they are passed to the current AS Instance. F1 is defined as a system event and F4 as an application event. The event handling methods of the instance handle of the class event_handler are registered as handlers for the events.

Before the dynpro is sent, the textedit control is filled with the contents of table text_tab. As long as the dynpro is displayed, the user is allowed to edit the text. When INSERT is selected, PAI is raised, and the current text from the textedit control is passed to the table text_tab.

If F1 is selected on the textedit control, the method handle_f1 is executed. This assigns the contents of the line to the field field: Calling the method SET_NEW_OK_CODE raises PAI. This is the only way to ensure that the PBO is processed and the contents of field are sent to the dynpro.

If F4 is selected on the textedit control, PAI is raised. The method DISPATCH is called and raises the method handle_f4. This assigns the contents of the line to the field field: Since the system then automatically returns to PAI processing, PBO is also processed and the field contents are transported.

In either case, regardless of whether the user chooses F1 or F4, the contents of the textedit control are not passed to the internal table text_tab. Consequently, the textedit control is overwritten with the previous content of text_tab after PBO.