Skip to Content

Archived discussions are read-only. Learn more about SAP Q&A

Set handler Questions

Hi,

i try to follow this code and i miss something,

why after the commend :

SET HANDLER: list-> fcode_handler,

see below

the go to the class status and not fcode handler ?

REPORT  zztest_events.
TYPE-POOLS: icon.
*****************************************************************
* Interface and Class declarations
*****************************************************************
INTERFACE i_vehicle.
  DATA     max_speed TYPE i.
  EVENTS speed_change EXPORTING value(new_speed) TYPE i.
  METHODS: drive,
           stop.
ENDINTERFACE.                    "I_VEHICLE
*----------------------------------------------------------------
CLASS c_ship DEFINITION.
  PUBLIC SECTION.
    METHODS constructor.
    INTERFACES i_vehicle.
  PRIVATE SECTION.
    ALIASES max FOR i_vehicle~max_speed.
    DATA ship_speed TYPE i.
ENDCLASS.                    "C_SHIP DEFINITION
*----------------------------------------------------------------
CLASS c_truck DEFINITION.
  PUBLIC SECTION.
    METHODS constructor.
    INTERFACES i_vehicle.
  PRIVATE SECTION.
    ALIASES max FOR i_vehicle~max_speed.
    DATA truck_speed TYPE i.
ENDCLASS.                    "C_TRUCK DEFINITION
*----------------------------------------------------------------
CLASS status DEFINITION.
  PUBLIC SECTION.
    CLASS-EVENTS button_clicked EXPORTING value(fcode) LIKE sy-ucomm.
    CLASS-METHODS: class_constructor,
                  user_action.
ENDCLASS.                    "STATUS DEFINITION
*----------------------------------------------------------------
CLASS c_list DEFINITION.
  PUBLIC SECTION.
    METHODS: fcode_handler FOR EVENT button_clicked OF status
                               IMPORTING fcode,
             list_change   FOR EVENT speed_change OF i_vehicle
                               IMPORTING new_speed,
             list_output.
  PRIVATE SECTION.
    DATA: id TYPE i,
          ref_ship  TYPE REF TO c_ship,
          ref_truck TYPE REF TO c_truck,
          BEGIN OF line,
            id TYPE i,
            flag,
            iref  TYPE REF TO i_vehicle,
            speed TYPE i,
          END OF line,
          list LIKE SORTED TABLE OF line WITH UNIQUE KEY id.
ENDCLASS.                    "C_LIST DEFINITION
*****************************************************************

*****************************************************************
* Implementations
*****************************************************************
CLASS c_ship IMPLEMENTATION.
  METHOD constructor.
    max = 30.
  ENDMETHOD.                    "CONSTRUCTOR
  METHOD i_vehicle~drive.
    CHECK ship_speed < max.
    ship_speed = ship_speed + 10.
    RAISE EVENT i_vehicle~speed_change
                EXPORTING new_speed = ship_speed.
  ENDMETHOD.                    "I_VEHICLE~DRIVE
  METHOD i_vehicle~stop.
    CHECK ship_speed > 0.
    ship_speed = 0.
    RAISE EVENT i_vehicle~speed_change
                EXPORTING new_speed = ship_speed.
  ENDMETHOD.                    "I_VEHICLE~STOP
ENDCLASS.                    "C_SHIP IMPLEMENTATION
*----------------------------------------------------------------
CLASS c_truck IMPLEMENTATION.
  METHOD constructor.
    max = 150.
  ENDMETHOD.                    "CONSTRUCTOR
  METHOD i_vehicle~drive.
    CHECK truck_speed < max.
    truck_speed = truck_speed + 50.
    RAISE EVENT i_vehicle~speed_change
                EXPORTING new_speed = truck_speed.
  ENDMETHOD.                    "I_VEHICLE~DRIVE
  METHOD i_vehicle~stop.
    CHECK truck_speed > 0.
    truck_speed = 0.
    RAISE EVENT i_vehicle~speed_change
                EXPORTING new_speed = truck_speed.
  ENDMETHOD.                    "I_VEHICLE~STOP
ENDCLASS.                    "C_TRUCK IMPLEMENTATION
*----------------------------------------------------------------
CLASS status IMPLEMENTATION.
  METHOD class_constructor.
    SET PF-STATUS 'VEHICLE'.
    WRITE 'Click a button!'.

  ENDMETHOD.                    "CLASS_CONSTRUCTOR
  METHOD user_action.
    RAISE EVENT button_clicked EXPORTING fcode = sy-ucomm.
  ENDMETHOD.                    "USER_ACTION
ENDCLASS.                    "STATUS IMPLEMENTATION
*----------------------------------------------------------------
CLASS c_list IMPLEMENTATION.
  METHOD fcode_handler .
    CLEAR line.
    REPLACE ALL OCCURRENCES OF  '/' in fcode with ''.
    CASE fcode.
      WHEN 'CREA_SHIP'.
        id = id + 1.
        CREATE OBJECT ref_ship.
        line-id = id.
        line-flag = 'C'.
        line-iref = ref_ship.
        APPEND line TO list.
      WHEN 'CREA_TRUCK'.
        id = id + 1.
        CREATE OBJECT ref_truck.
        line-id = id.
        line-flag = 'T'.
        line-iref = ref_truck.
        APPEND line TO list.
      WHEN 'DRIVE'.
        CHECK sy-lilli > 0.
        READ TABLE list INDEX sy-lilli INTO line.
        CALL METHOD line-iref->drive.
      WHEN 'STOP'.
        LOOP AT list INTO line.
          CALL METHOD line-iref->stop.
        ENDLOOP.
      WHEN 'CANCEL'.
        LEAVE PROGRAM.
    ENDCASE.
    CALL METHOD list_output.
  ENDMETHOD.                    "FCODE_HANDLER
  METHOD list_change .
    line-speed = new_speed.
    MODIFY TABLE list FROM line.
  ENDMETHOD.                    "LIST_CHANGE
  METHOD list_output.
    sy-lsind = 0.
    SET TITLEBAR 'TIT'.
    LOOP AT list INTO line.
      IF line-flag = 'C'.
        WRITE / icon_ws_ship AS ICON.
      ELSEIF line-flag = 'T'.
        WRITE / icon_ws_truck AS ICON.
      ENDIF.
      WRITE: 'Speed = ', line-speed.
    ENDLOOP.
  ENDMETHOD.                    "LIST_OUTPUT
ENDCLASS.                    "C_LIST IMPLEMENTATION
*****************************************************************

*****************************************************************
* Global data of program
*****************************************************************
DATA list TYPE REF TO c_list.
*****************************************************************
* Program Events
*****************************************************************
START-OF-SELECTION.
  CREATE OBJECT list.
  SET HANDLER: list->fcode_handler,
              list->list_change FOR ALL INSTANCES.
*----------------------------------------------------------------
AT USER-COMMAND.
  CALL METHOD status=>user_action.

Regards

replied

Hello Ricardo

I have to excuse for not studying your coding carefully enough. I assume it is a sample report from the ABAP Objects book (by Horst Keller).

To be honest I believe this sample report will confuse you more than it will help you to understand event handling in ABAP-OO.

All ok-codes (like CREA_SHIP, DRIVE, etc.) trigger the list-processing event AT USER-COMMAND. Here the static method

AT USER-COMMAND.
  CALL METHOD status=>user_action.

is called.

Within USER_ACTION the class-event BUTTON_CLICKED is triggered:

METHOD user_action.
    RAISE EVENT button_clicked EXPORTING fcode = sy-ucomm.
  ENDMETHOD.                    "USER_ACTION

This static event is handled by the instance method FCODE_HANDLER of class C_LIST.

Now let us assume the ok-code = 'DRIVE' has been executed. Within FCODE_HANDLER the corresponding action is executed:

METHOD fcode_handler .
    CLEAR line.
    REPLACE ALL OCCURRENCES OF  '/' IN fcode WITH ''.
    CASE fcode.
      WHEN 'CREA_SHIP'.
        ...
      WHEN 'DRIVE'.
        CHECK sy-lilli > 0.
        READ TABLE list INDEX sy-lilli INTO line.
        CALL METHOD line-iref->drive.
      WHEN 'STOP'.
      ...
    ENDCASE.
    CALL METHOD list_output.
  ENDMETHOD.                    "FCODE_HANDLER

The ok-code = 'DRIVE' eventually causes that the DRIVE method of instance LINE-IREF is called.

The instance "knows" whether it is a truck or a ship and calls the corresponding method, i.e. i_vehicle~drive:

METHOD i_vehicle~drive.
    CHECK ship_speed < max.
    ship_speed = ship_speed + 10.
    RAISE EVENT i_vehicle~speed_change
                EXPORTING new_speed = ship_speed.  " 'true' event raising / handling
  ENDMETHOD.                    "I_VEHICLE~DRIVE

Now within the DRIVE method I see for the first time the "normal" event raising / handling that I am used to. The method has increased the speed thereby raising event SPEED_CHANGE.

Is there anybody "interested" in this event (i.e. do we have a listener for it)?

Answer: Yes, the C_LIST class needs to handle this event because it must change the list output.

METHOD list_change .
    line-speed = new_speed.
    MODIFY TABLE list FROM line.
  ENDMETHOD.                    "LIST_CHANGE

In order to study "simple" examples of ALV grid event handling you may search the SDN for my sample reports (all of which begin with ZUS_SDN_...). In contrast to the BCALV_... sample reports I believe my reports are easier to understand.

Regards

Uwe

0 View this answer in context
Not what you were looking for? View more on this topic or Ask a question