Entering content frame

Events: Complex Example Locate the document in its SAP Library structure

The following example shows how to declare, call, and handle events in ABAP Objects.

Overview

This object works with the interactive list displayed below. Each user interaction triggers an event in ABAP Objects. The list and its data is created in the class c_list. There is a class status for processing user actions. It triggers an event button_clickedin the AT USER-COMMAND event. The event is handled in the class c_list. It contains an object of the class c_sHIP or c_truckfor each line of the list. Both of these classes implement the interface i_vehicle. Whenever the speed of one of these objects changes, the event speed_change is triggered. The class c_listreacts to this and updates the list.

This graphic is explained in the accompanying text

Constraints

Caution

The ABAP statements used for list processing are not yet fully available in ABAP Objects. However, to produce a simple test output, you can use the following statements:

·         WRITE [AT] /offset(length) f

·         ULINE

·         SKIP

·         NEW-LINE

Note: The behavior of formatting and interactive list functions in their current state are not guaranteed. Incompatible changes could occur in a future release.

Declarations

 

This example is implemented using local interfaces and classes. Below are the declarations of the interfaces and classes:

*****************************************************************
* 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.

*----------------------------------------------------------------

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.

*----------------------------------------------------------------

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.

*----------------------------------------------------------------

CLASS status DEFINITION.

  PUBLIC SECTION.

  CLASS-EVENTS button_clicked EXPORTING VALUE(fcode) LIKE sy-ucomm.

  CLASS-METHODS: class_constructor,
                user_action.

ENDCLASS.

*----------------------------------------------------------------

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(1) TYPE c,
          iref  TYPE REF TO i_vehicle,
          speed TYPE i,
        END OF LINE,
        LIST LIKE SORTED TABLE OF line WITH UNIQUE KEY id.

ENDCLASS.

*****************************************************************

 

The following events are declared in the example:

·        The instance event speed_change in the interface i_vehicle

·        The static event button_clicked in the class status.

The class c_list contains event handler methods for both events.

Note that the class status does not have any attributes, and therefore only works with static methods and events.

Implementations

Below are the implementations of the methods of the above classes:

*****************************************************************
* Implementations
*****************************************************************

CLASS c_ship IMPLEMENTATION.

  METHOD constructor.
    max = 30.
  ENDMETHOD.

  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.

  METHOD i_vehicle~stop.
    CHECK ship_speed > 0.
    ship_speed = 0.
    RAISE EVENT i_vehicle~speed_change
                EXPORTING new_speed = ship_speed.
  ENDMETHOD.

ENDCLASS.

*----------------------------------------------------------------

CLASS c_truck IMPLEMENTATION.

  METHOD constructor.
    max = 150.
  ENDMETHOD.

  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.

  METHOD i_vehicle~stop.
    CHECK truck_speed > 0.
    truck_speed = 0.
    RAISE EVENT i_vehicle~speed_change
                EXPORTING new_speed = truck_speed.
  ENDMETHOD.

ENDCLASS.

*----------------------------------------------------------------

CLASS status IMPLEMENTATION.

  METHOD class_constructor.
    SET pf-status 'VEHICLE'.
    WRITE 'Click a button!'.
  ENDMETHOD.

  METHOD user_action.
    RAISE event button_clicked EXPORTING fcode = sy-ucomm.
  ENDMETHOD.

ENDCLASS.

*----------------------------------------------------------------

CLASS c_list IMPLEMENTATION.

  METHOD fcode_handler.
    CLEAR line.
    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.

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

  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.

ENDCLASS.

*****************************************************************

 

The static method user_action of the class status triggers the static event button_clicked. The instance methods i_vehicle~drive and i_vehicle~stop trigger the instance event i_vehicle~speed_change in the classes c_ship and c_truck.

Using the Classes in a Program

The following program uses the above classes:

REPORT demo_abap_objects_events NO STANDARD PAGE HEADING.
*****************************************************************
* 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.

*****************************************************************

The program creates an object of the class c_listand registers the event handler method fcode_handler of the object for the class event button_clicked, and the event handler method list_change for the event speed_change of all instances that implement the interface i_vehicle.

 

 

 

Leaving content frame