Exercise 1: Writing an Event Handler Method
Use
Control events triggered by the user on the frontend are processed on the backend using ABAP Objects event processing. For each event to which you want to respond you must implement an event handler method.
Procedure
-
Define a local class before the START-OF-SELECTION event. Implement a method for this class that is called at the DBLCLICK event. This method only displays the event type in a text element:
DATA: EVENT_TYPE(20) TYPE C. CLASS lcl_event_handler DEFINITION. PUBLIC SECTION. CLASS-METHODS: CATCH_DBLCLICK FOR EVENT DBLCLICK OF CL_GUI_TEXTEDIT. IMPORTING SENDER. ENDCLASS. DATA: event_handler TYPE REF TO lcl_event_handler. CLASS lcl_event_handler IMPLEMENTATION. METHOD CATCH_DBLCLICK. event_type = text-002. ENDMETHOD. ENDCLASSDo not forget to define a text such as doubleclick for text element text-002.
-
Create an input/output field on screen 100 in which the event type can be output:
-
Name: EVENT_TYPE
-
Element type: input/output field
-
-
Link the method to the event. To do this, insert the following line of code in the IF block of the PBO module (the editor object must already have been created):
SET HANDLER lcl_event_handler=>catch_dblclick FOR editor.
-
Activate and start your program.
Check Your Work
The input/output field is not updated when you double-click somewhere in the text window. This means that the ABAP Objects event is not yet triggered.
Discussion
The steps in this exercise only reflect the handling of an event under ABAP objects. Event handling in the back end is also described. Since when the control is set to its initial state all events are filtered by the SAPGUI, the ABAP event for the double-click cannot be triggered on the backend. The reason is that the event has not yet been registered on the frontend (see Exercise 2:). Registering an Application Event).
Event Parameter sender
In the definition of the event handler method, event parameter sender is imported. You can access this parameter at any ABAP Objects event although it is not listed as an event parameter in the Class Builder. This parameter imports a reference to the instance that has triggered the event. This reference can then be used directly to call methods of this instance.
The Event Handler Method
-
Defining the Handler Method as a Static Method
In this exercise, you used the CLASS addition to define the event handler method as a static method of class CL_GUI_TEXTEDIT. This means that you do not need to instantiate an object of class lcl_event_handler to be able to call the method.
You use statement SET HANDLER to link static method lcl_event_handler=>catch_dblclick to event DBLCLICK for instance editor. You can create additional instances of the textedit control and link them to this method at this event. All instances registered with SET HANDLER then use the same method that exists only once at runtime.
Defining the event handler method as a static method makes sense if all instances of a class should behave identically at an event.
-
Defining the Handler Method as an Instance Method
Instance methods offer the advantage that you can create more than one object of your local class. This is necessary if you want to retain a state (for example, for a static variable) within the event handler method. If the method is used by several control instances, conflicts may arise if only one instance of the event handler class exists. In such a case, you should use different instance methods for different control instances of the same class.