
Before you respond to user actions, you usually check the current status of the control. To do this, you use control methods that pass values to your program. If you change or read these values, you must be sure to use the current values after the method call. This depends on the correct flush time.
In this exercise, you implement a function that allows the user to protect selected lines. To do this, you determine the selected area using method GET_SELECTION_POS and then protect this area using method PROTECT_LINES.
Create a pushbutton on your screen with the following properties:
Function code: PROTECT
Icon name: ICON_LOCKED
In the next steps, you add more lines to the CASE block in the PAI module:
Declare global variables for the selection area, and a run index:
WHEN 'PROTECT'.
DATA: FROM_IDX TYPE I,
TO_IDX TYPE I,
INDEX TYPE I.
Determine the area selected by the user with the mouse:
CALL METHOD EDITOR->GET_SELECTION_POS
IMPORTING
FROM_LINE = FROM_IDX
TO_LINE = TO_IDX
exceptions
ERROR_CNTL_CALL_METHOD = 1.
Synchronize execution in the control with your ABAP program:
CALL METHOD cl_gui_cfw=>FLUSH.
IF SY-SUBRC NE 0.
CALL FUNCTION 'POPUP_TO_INFORM'
EXPORTING
TITEL = REPID
TXT2 = sy-subrc
TXT1 = 'PAI USER_COMMAND_100(1): Error in Flush!'(603).
ENDIF.
Without this synchronization, variables FROM_IDX and TO_IDX have obsolete values in the following IF query (initial value of both is 0).
Protect the lines selected by means of a loop. To do this, add one method call for each line to the Automation Queue and flush them afterwards:
LOOP AT MYTABLE INTO TEXTSTRUCT.
IF ( SY-TABIX >= FROM_IDX AND SY-TABIX <= TO_IDX ).
INDEX = SY-TABIX.
CALL METHOD EDITOR->PROTECT_LINES
EXPORTING
FROM_LINE = INDEX
TO_LINE = INDEX
ENABLE_EDITING_PROTECTED_TEXT = cl_gui_textedit=>true.
endif.
ENDLOOP.
CALL METHOD cl_gui_cfw=>FLUSH.
IF SY-SUBRC NE 0.
CALL FUNCTION 'POPUP_TO_INFORM'
EXPORTING
TITEL = REPID
TXT2 = sy-subrc
TXT1 = 'PAI USER_COMMAND_100(2): Error in Flush!'(604).
ENDIF.
Using method PROTECT_LINES is rather naive. It would be easier to just use a method call where you pass from_idx and to_idx. The example is designed to make you aware of the special effects involved in control programming.
Activate your new objects, and start your program.
Check Your Work
Import your internal table by choosing the pushbutton you created in a previous exercise. If you place your cursor on a line and click your new pushbutton, the line is highlighted in gray and can no longer be modified. Using the left mouse button, you can select an entire area to write-protect multiple lines.
Discussion
Synchronization after method call GET_SELECTION_POS is absolutely essential for this solution. As a test, comment out the FLUSH statement and check the values of the from_idx and to_idx variables in the debugger. The runtime behavior is as follows (due to space restrictions, the graphic does not show parameter ENABLE_EDITING_PROTECTED_TEXT of method PROTECT_LINES):

If there is no FLUSH after GET_SELECTION_POS is called, the values of from_idx and to_idx are checked prematurely. When the PAI module is processed for the first time, both variables are initial. Due to the loop condition, no PROTECT_LINES method call is added to the Automation Queue. If the function is called again afterwards, the loop condition is always checked against obsolete values. In this case, although lines are protected, these are only those of the most recent call.
You can eliminate the second flush in this exercise, if you use method PROTECT_SELECTION. This way, you do not need to read the line numbers of the selected area.
Values in the Automation Queue
There is no method in this example that is called before PROTECT_LINES in the same queue and imports actual parameter INDEX. This is why the Control Framework buffers the actual parameters of the methods as values in the queue. The next exercise provides an example where the Framework stores actual parameter references in the Automation Queue.