Start of Content Area

Procedure documentationExercise 4: Responding to an Event  Locate the document in its SAP Library structure

Usage

In the previous exercise, you have registered an event. You have used the SET HANDLER statement to link the event handler method to the event. To respond to this event, you only need to extend the event handler method.

In this exercise, you implement a function that converts a line into a comment line when the line is double-clicked. If the line is a comment line already, the comment marker at the beginning of the line is deleted. Implement this function by retrieving additional control information in the event handler method and using methods to trigger control actions.

Procedure

Add the following code blocks to method catch_dblclick before set_new_ok_code is called:

  1. Read the position of the double-click, and define corresponding local variables in advance:
  2. DATA: FROM_LINE TYPE I,
           FROM_POS TYPE I,
           TO_LINE TYPE I,
           TO_POS TYPE I.
    CALL METHOD SENDER->GET_SELECTION_POS
        IMPORTING
                FROM_LINE = FROM_LINE
                FROM_POS = FROM_POS
                TO_LINE = TO_LINE
                TO_POS = TO_POS.

  3. Since the text in the text buffer of the editor may have changed, first reload it into your internal table mytable . Using flag g_loaded , you can determine if any text at all has been loaded into the text buffer:
  4. IF NOT g_loaded IS INITIAL.
       CALL METHOD SENDER->GET_TEXT_AS_R3TABLE
         importing
                TABLE = MYTABLE.
    ENDIF.

  5. Read the line of the internal table that was clicked. Depending on whether the line is marked as a comment line or not, insert an asterisk or remove it.
  6. READ TABLE MYTABLE INDEX FROM_LINE INTO TEXTSTRUCT.
    IF SY-SUBRC = 0.
        IF TEXTSTRUCT+0(1) CS '*'.
          SHIFT TEXTSTRUCT.
        ELSEIF TEXTSTRUCT+0(1) NS '*'.
          SHIFT TEXTSTRUCT RIGHT.
          TEXTSTRUCT+0(1) = '*'.
        ENDIF.

  7. Reload the text of the internal table into the editor.
  8. MODIFY MYTABLE FROM TEXTSTRUCT INDEX FROM_LINE.
        CALL METHOD SENDER->SET_TEXT_AS_R3TABLE
           EXPORTING TABLE = MYTABLE
        EXCEPTIONS
           OTHERS = 1.
    ENDIF.

  9. Transfer the methods to the frontend using command FLUSH .
  10. CALL METHOD cl_gui_cfw=>FLUSH.    
    IF SY-SUBRC NE 0.
          CALL FUNCTION 'POPUP_TO_INFORM'
              EXPORTING
                    TITEL = REPID
                    TXT2 = sy-subrc
                    TXT1 = 'Method CATCH_DBLCLICK: Error in Flush!'(602).
    ENDIF.

  11. Activate and start your program.

Check Your Work

To display the generated text of the internal table in the text window, click on pushbutton Import . If you now double-click a line, an asterisk (' * ') appears at the beginning of that line, and the text is indented to the right. If you double-click the line again, the asterisk is removed.

Discussion

Method get_text_as_r3table loads the text in the text window of the control into internal table mytable . In the next step, this table is modified. However, from a chronological point of view, the method should not be executed before flush time. This would mean that the modification is carried out prematurely. Why does the program behave correctly nevertheless?

The reason is that another control is used when large volumes of data are transferred by control methods. This control is the Data Provider. Currently, the Data Provider implicitly triggers another flush to transfer the data.

Note

You can detect any implicit flush in the trace mode. This mode is described in lesson 4 in Exercise 1: Activating the Trace Mode.

However, it is not to your disadvantage to define another flush after method get_text_as_r3table . If a flush is called and no control methods are called before, the Control Framework does not perform an RFC.

Note

SAP recommends you to use an additional flush call and not to rely on an implicit flush. This ensures that your programs are more robust. If the implicit flush is eliminated in later versions of the Data Provider, this will not effect your existing programs.

The next lesson describes in detail how flush calls are used correctly.