I know I told very simple stories in the previous chapter. Now let’s get into some details. I think this chapter is the most exciting, at least for me.
In this chapter:
- Different Code Versions
- Story 3: Splitting Text into 48-Char Sentences and Starting on New Lines
- Story 4: Splitting Text into 57-Char Sentences and Starting on New Lines
Before we begin, there is something important I need to tell you.
Different Code Versions
Source code versions may differ between systems. While writing the 3rd and 4th stories, I tested my sample codes on 2 different systems. During my tests, I noticed that the line spacing of the sentences was different.
First, I compared the versions of the programs that have the BUILD_HTML
subroutine. Since the last update dates are different, I scanned the codes to see how the HTML codes were generated. In debug mode, I grabbed the generated HTML codes. As you can see the “line-height” values are different.
<html><head></head> <body bgcolor="#FFFFFF" scroll="auto"> <span style="line-height:1.75em; font-size:11pt; font-family:Tahoma, ARIAL"> lorem ipsum lorem ipsum </span></body> </html> System 1 (last changed: 26.11.2018) |
<html><head></head> <body bgcolor="#FFFFFF" scroll="auto"> <span style="line-height:34px; font-size:11pt; font-family:'Tahoma', 'ARIAL'"> <font color="#000000"> lorem ipsum lorem ipsum </span></body> </html> System 1 (last changed: 19.11.2020) |
It’s important to know that line spacing can differ across systems because stories here are about splitting text into sentences and starting on new lines.
Line spacing affects the number of lines that are visible without any action. In other words, it affects the number of lines that are visible in the visible area of the popup window. Of course, you can use the scrollbar to see the rest of the text.
💡 We get a better result if we have more control over the function module.
Story 3: Splitting Text into 48-Char Sentences and Starting on New Lines
I would like to display the following long text in a popup but each sentence must start on a new line:
Hello POPUP_TO_CONFIRM function module. Could you please display this message? But every sentence must start on a new line! There are a few limitations, but I know you can! You can even display one more sentence.
If we pass this text to the function, the output will be:
As we know the maximum width of the text can only be 48 or 57 characters (the first part of this blog series explains this), let’s stick with it and try to display the message as follows:
Hello POPUP_TO_CONFIRM function module.
Could you please display this message?
But every sentence must start on a new line!
There are a few limitations, but I know you can!
You can even display one more sentence.
OK, but how can we do this?
My plan is to split the text into segments, add the necessary spaces at the end of each, and then concatenate them again. The function module takes care of the rest (I hope).
But we have an issue.
⚠️ Mr. and Mrs. Brown ProblemSince the ending (punctuation) marks that determine the end of a sentence are not used only at the end (as in the Mr. and Mrs. Brown example), I will create a text of sentences separated by a newline character in this story. |
OK, identifying a complete sentence is a different story. Let’s execute the plan. Here is the recipe:
Step | Description |
Step 1 | Build a text by combining 5 sentences with a newline character between them. Since we do not use a user-defined button, each sentence can be up to 48 characters long. |
Step 2 | Split the text into segments by “newline” characters. |
Step 3 | Add the required number of spaces at the end of each segment to complete up to 48 characters. Then append the segment to the end of the text.
(i) There must be at least one space between sentences. Otherwise, the last word of the current sentence and the first word of the next sentence can be considered as a single word. |
Step 4 | Add an invisible character to preserve the trailing spaces at the very end of the text. If we do not add any character other than space, they will be ignored/removed. (Here again, I opt to use the newline character).
When we merge all the segments (sentences), there will be a different number of spaces between them in the final text. |
Step 5 | Call the function module with the manipulated text. |
The following table shows the number of spaces we need to add for the sentences in our example.
Sentence | Sentence Length | Max Length per Line |
Number of Spaces to Add |
Hello POPUP_TO_CONFIRM function module. | 39 | 48 | 9 |
Could you please display this message? | 38 | 48 | 10 |
But every sentence must start on a new line! | 44 | 48 | 4 |
There are a few limitations, but I know you can! | 48 | 48 | 0 → See step 3(i) |
You can even display one more sentence. | 39 | 48 | 9 |
What about the flowchart?
I intend to show “calling program” and “called program” in the same source code. Hence, consider step 1 as the “calling program” that prepares the question text, and the next steps as the “called program” that takes the text and sends it to the function module after manipulating it.
📄 ABAP code:
*--------------------------------------------------------------------*
* Anatomy of a Function Module: POPUP_TO_CONFIRM - Part 3
*
* Story 3: Splitting Long Text into 48-Character-Wide Sentences and
* Starting Them on New Lines
*--------------------------------------------------------------------*
REPORT sy-repid.
CONSTANTS: newline VALUE cl_abap_char_utilities=>newline,
maxlen TYPE i VALUE 48.
DATA: question TYPE string,
answer(1).
*--------------------------------------------------------------------*
* Step 1:
* Build a text by combining 5 sentences with a newline character
* between them. Since we do not use a user-defined button, each
* sentence can be up to 48 characters long.
*--------------------------------------------------------------------*
question &&=:
|Hello POPUP_TO_CONFIRM function module.| , newline,
|Could you please display this message?| , newline,
|But every sentence must start on a new line!| , newline,
|There are a few limitations, but I know you can!| , newline,
|You can even display one more sentence.| , newline.
*--------------------------------------------------------------------*
* Step 2:
* Split the text into segments by newline characters.
*--------------------------------------------------------------------*
SPLIT question AT newline INTO TABLE DATA(segments).
CLEAR question.
*--------------------------------------------------------------------*
* Step 3:
* Add the required number of spaces at the end of each segment to
* complete up to 48 characters. Then append the segment to the end
* of the text.
* (i) There must be at least one space between sentences. Otherwise,
* the last word of the current sentence and the first word of the next
* sentence can be considered as a single word.
*--------------------------------------------------------------------*
LOOP AT segments ASSIGNING FIELD-SYMBOL(<segment>).
DO COND #( WHEN strlen( <segment> ) EQ maxlen THEN 1 ELSE ( maxlen - strlen( <segment> ) ) ) TIMES.
<segment> &&= | |.
ENDDO.
question &&= <segment>.
ENDLOOP.
*--------------------------------------------------------------------*
* Step 4:
* Add an invisible character to preserve the trailing spaces at the
* very end of the text. If we do not add any character other than
* space, they will be ignored/removed. (Here again, I opt to use the
* newline character).
*--------------------------------------------------------------------*
question &&= newline.
*--------------------------------------------------------------------*
* Step 5:
* Call the function module with the manipulated text.
*--------------------------------------------------------------------*
CALL FUNCTION 'POPUP_TO_CONFIRM'
EXPORTING
text_question = question
IMPORTING
answer = answer
EXCEPTIONS
OTHERS = 2.
🖥️ Output:
After all, the goal here was to convince the function module. 🙂
Now the question is:
With the
QUESTION_TEXT
parameter and the “start each sentence on a new line” logic, how many sentences can we show at most if all the sentences are 48 characters wide?
🧮 Since I’ve used CL_ABAP_CHAR_UTILITIES=>NEWLINE
as separator and its length is 1, here is the formula:
Question text max length / ( Output text max length + Length of separator )
400 / ( 48 + 1 ) = 8.163265306122449
Since we use a separator char when building the text, we can display a maximum of 8 sentences with a length of 48 characters. If better logic is written that determines sentence endings, the calculation will be different.
📄 ABAP code:
*--------------------------------------------------------------------*
* Anatomy of a Function Module: POPUP_TO_CONFIRM - Part 3
*
* Story 3: Splitting Text into 48-Char Sentences and Starting on New Lines
*--------------------------------------------------------------------*
REPORT sy-repid.
CONSTANTS: newline VALUE cl_abap_char_utilities=>newline,
maxlen TYPE i VALUE 48.
DATA: question TYPE string,
answer(1).
*--------------------------------------------------------------------*
* SELECTION-SCREEN
*--------------------------------------------------------------------*
PARAMETERS p_line01 TYPE c LENGTH 48 LOWER CASE DEFAULT 'This is the first sentence.'.
PARAMETERS p_line02 TYPE c LENGTH 48 LOWER CASE DEFAULT 'This is the second sentence.'.
PARAMETERS p_line03 TYPE c LENGTH 48 LOWER CASE DEFAULT 'This is the third sentence.'.
PARAMETERS p_line04 TYPE c LENGTH 48 LOWER CASE DEFAULT 'This is the fourth sentence.'.
PARAMETERS p_line05 TYPE c LENGTH 48 LOWER CASE DEFAULT 'This is the fifth sentence.'.
PARAMETERS p_line06 TYPE c LENGTH 48 LOWER CASE DEFAULT 'This is the sixth sentence.'.
PARAMETERS p_line07 TYPE c LENGTH 48 LOWER CASE DEFAULT 'This is the seventh sentence.'.
PARAMETERS p_line08 TYPE c LENGTH 48 LOWER CASE DEFAULT 'This is the eigth sentence.'.
PARAMETERS p_line09 TYPE c LENGTH 48 LOWER CASE DEFAULT 'This is the nineth sentence.'.
"Start each sentence on a new line
PARAMETERS p_newlin TYPE xfeld.
*--------------------------------------------------------------------*
* START-OF-SELECTION
*--------------------------------------------------------------------*
START-OF-SELECTION.
DO 9 TIMES.
DATA(line) = 'P_LINE0' && sy-index.
ASSIGN (line) TO FIELD-SYMBOL(<line>).
IF <line> IS NOT INITIAL.
question &&=: <line>, COND #( WHEN p_newlin EQ abap_false THEN | | ELSE newline ).
ENDIF.
ENDDO.
SPLIT question AT newline INTO TABLE DATA(segments).
CLEAR question.
LOOP AT segments ASSIGNING FIELD-SYMBOL(<segment>).
DO COND #( WHEN strlen( <segment> ) EQ maxlen THEN 1 ELSE ( maxlen - strlen( <segment> ) ) ) TIMES.
<segment> &&= | |.
ENDDO.
question &&= <segment>.
ENDLOOP.
question &&= newline.
CALL FUNCTION 'POPUP_TO_CONFIRM'
EXPORTING
text_question = question
IMPORTING
answer = answer
EXCEPTIONS
OTHERS = 2.
🖥️ Output:
Top image: newline is off |
Top image: newline is off |
As you can see there is not enough room for the 9th sentence. Just enter different sentences and run the program with the newline on/off modes to see the results.
Story 4: Splitting Text into 57-Char Sentences and Starting on New Lines
This time I will try to display sentences up to a maximum length of 57 characters starting on new lines. For example:
The length of this sentence is exactly fifty-seven chars.
But for this to happen, the popup window needs to be a bit wider. This is only possible if we can enable the USER_DEFINED_F1_HELP
button to appear. The good news is we don’t need to provide a value that actually exists. (The first part of this blog series explains this).
🧮 First, let’s calculate how many sentences we can display:
Question text max length / ( Output text max length + Length of separator )
400 / ( 57 + 1 ) = 6.896551724137931
Since we use a separator char when building the text, we can display a maximum of 6 sentences with a length of 57 characters. If better logic is written that determines sentence endings, the calculation will be different.
📄 ABAP code:
*--------------------------------------------------------------------*
* Anatomy of a Function Module: POPUP_TO_CONFIRM - Part 3
*
* Story 4: Splitting Text into 57-Char Sentences and Starting on New Lines
*--------------------------------------------------------------------*
REPORT sy-repid.
CONSTANTS: newline VALUE cl_abap_char_utilities=>newline,
maxlen TYPE i VALUE 57.
DATA: question TYPE string,
answer(1).
DO 7 TIMES.
question &&=: 'The length of this sentence is exactly fifty-seven chars.', newline.
ENDDO.
SPLIT question AT newline INTO TABLE DATA(segments).
CLEAR question.
LOOP AT segments ASSIGNING FIELD-SYMBOL(<segment>).
DO COND #( WHEN strlen( <segment> ) EQ maxlen THEN 1 ELSE ( maxlen - strlen( <segment> ) ) ) TIMES.
<segment> &&= | |.
ENDDO.
question &&= <segment>.
ENDLOOP.
question &&= newline.
CALL FUNCTION 'POPUP_TO_CONFIRM'
EXPORTING
text_question = question
userdefined_f1_help = '_' "Displaying this button increases the width of the popup window.
IMPORTING
answer = answer
EXCEPTIONS
OTHERS = 2.
🖥️ Output:
As you can see, the window is wider.
⚠️ The only side effect of displaying the Info button by setting the parameter USER_DEFINED_F1_HELP
with a value that doesn’t exist is that the following message is displayed when the user clicks that button.
Conclusion
In this blog post, you have learned how to split a long text into 48 or 57-char-wide sentences and display those sentences on a popup, starting on new lines. You’ve also learned that there is a length limitation when using the QUESTION_TEXT
parameter.
It can be useful to use that logic in cases where the length of the sentences does not exceed 48 or 57 characters.
The examples in stories 3 and 4 use the newline character as the separator. In real programs, sentences must be determined by checking for ending marks (or ending punctuation). To achieve this, the “Mr. and Mrs. Brown Problem” must first be solved.
Next Step?
So, “Operation 48/57” was completed. The following stories will be told in the last chapter of the series:
- Story 5: Displaying an Unordered List
- Story 6: Displaying Longer Text Using a Document Object
See you in the last chapter.
More Resources
|
Trademarks
Disclaimer
|