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_CLICKED in 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_TRUCK for 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_LIST reacts to this and updates the list.
Constraints
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:
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 STATUSPRIVATE 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.
*****************************************************************
The following events are declared in the example:
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.
METHOD I_VEHICLE~STOP.
CHECK SHIP_SPEED > 0.
SHIP_SPEED = 0.
ENDCLASS.
*----------------------------------------------------------------
CLASS C_TRUCK IMPLEMENTATION.
METHOD CONSTRUCTOR.
MAX = 150.
ENDMETHOD.
METHOD I_VEHICLE~DRIVE.
CHECK TRUCK_SPEED < MAX.
TRUCK_SPEED = TRUCK_SPEED + 50.
METHOD I_VEHICLE~STOP.
CHECK TRUCK_SPEED > 0.
TRUCK_SPEED = 0.
ENDCLASS.
*----------------------------------------------------------------
CLASS STATUS IMPLEMENTATION.
METHOD CLASS_CONSTRUCTOR.
SET PF-STATUS 'VEHICLE'.
WRITE 'Click a button!'.
ENDMETHOD.
METHOD USER_ACTION.
ENDCLASS.
*----------------------------------------------------------------
CLASS C_LIST IMPLEMENTATION.
METHOD
FCODE_HANDLER .METHOD
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.
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 OO_EVENTS_DEMO 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_LIST and 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.