2017年12月30日 星期六

ABAP/4 : How to create multi line textbox on screen

在 SAP Screen 中加入 Textbox 不像一般 Visual Studio 把 Textbox multiline 設為 True 即可,需要先建立 Custom Control,再透過 Customer Control 建立 cl_gui_custom_container 用來裝載 cl_gui_textedit。


前置作業是建立一個 Screen,並且建立 Status 與相關 PBO/PAI,接下來在 Screen 中加入 Custom Control 物件,目前取名為 G_MLINE_TEXT。
接下來在 PBO 加入 MODULE INIT_SCREEN_0100.
在 Module 中加入以下 Code,宣告兩個變數 g_editor 和 g_editor_container,分別是用來將 cl_gui_custom_container 建立在畫面上 G_MLINE_TEXT 的容器,再使用 cl_gui_textedit 產生 multi line 的 textbox。


CONSTANTS: line_length TYPE i VALUE 58.   "Custom Control length - 2
DATA: g_editor TYPE REF TO cl_gui_textedit,
      g_editor_container TYPE REF TO cl_gui_custom_container.

MODULE INIT_SCREEN_0100 OUTPUT.

  IF g_editor_container IS INITIAL.
    "create editor container on custom control
    CREATE OBJECT g_editor_container
      EXPORTING
        container_name = 'G_MLINE_TEXT'
      EXCEPTIONS
        cntl_error = 1
        cntl_system_error = 2
        create_error = 3
        lifetime_error = 4
        lifetime_dynpro_dynpro_link = 5.
    IF sy-subrc NE 0.
      "Error Handling
    ELSE.
      "create text editor object on editor container
      CREATE OBJECT g_editor
        EXPORTING
          parent = g_editor_container
          wordwrap_mode = cl_gui_textedit=>wordwrap_at_fixed_position  "使用此 type 才能 g_editor->get_text_as_r3table
                 "cl_gui_textedit=>wordwrap_off
                 "cl_gui_textedit=>wordwrap_at_fixed_position
                 "cl_gui_textedit=>WORDWRAP_AT_WINDOWBORDER
          wordwrap_position = line_length                              "限定寬度
          wordwrap_to_linebreak_mode = cl_gui_textedit=>true.

      "hide toolbar
      CALL METHOD g_editor->set_toolbar_mode
        EXPORTING
          toolbar_mode = cl_gui_textedit=>false.

      "show Status Bar
      CALL METHOD g_editor->set_statusbar_mode
        EXPORTING
          statusbar_mode = cl_gui_textedit=>true.
    ENDIF.
  ENDIF.

ENDMODULE.                 " INIT_SCREEN_0100  OUTPUT

接下來則是如何取得目前 Text Editor 的值,則需要透過 cl_gui_textedit 的 Method get_text_as_r3table 將目前 Text Editor 的值取回,如果 User 選擇離開 Screen 則必需用 object->free and free object 釋放物件

MODULE USER_COMMAND_0100 INPUT.

  TYPES: BEGIN OF it_txline,
           line(line_length) TYPE c,
         END OF it_txline.
  DATA: it_tline TYPE STANDARD TABLE OF txline.

  IF g_editor IS NOT INITIAL.
    CALL METHOD g_editor->get_text_as_r3table
      IMPORTING table = it_tline.
  ENDIF.

  CASE OK_CODE.
    WHEN 'EXIT' OR 'BACK' OR 'CANC'.
       IF g_editor IS NOT INITIAL.
         CALL METHOD g_editor->free
           EXCEPTIONS
             OTHERS = 1.
         FREE g_editor.
       ENDIF.

      IF g_editor_container IS NOT INITIAL.
        CALL METHOD g_editor_container->free
          EXCEPTIONS
            cntl_error = 1
            cntl_system_error = 2.
        FREE g_editor_container.
      ENDIF.

      LEAVE TO SCREEN 0.
  ENDCASE.

ENDMODULE.                 " USER_COMMAND_0100  INPUT

以上即為主要控制 Text Editor 的程式,讓程式可正常在畫面編輯多行的文字,接下來在畫面中輸入多行字串
在 debug mode 中即可看到成功取得 multi line text editor 的每一行值,基本上可透過
LOOP it_tline TO <work area>.
ENDLOOP.
取得 Internal Table 中的值
SAMPLE CODE

REPORT  Z_EX_TEXT_MUTILINE.

DATA: OK_CODE LIKE SY-UCOMM.

CONSTANTS: line_length TYPE i VALUE 58.   "Customer Container 寬度 - 2
DATA: g_editor TYPE REF TO cl_gui_textedit,
      g_editor_container TYPE REF TO cl_gui_custom_container.

DATA: p_Text.

START-OF-SELECTION.

  CALL SCREEN 0100.


*&---------------------------------------------------------------------*
*&      Module  STATUS_0100  OUTPUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
MODULE STATUS_0100 OUTPUT.

  SET PF-STATUS 'S0100'.
  SET TITLEBAR 'T0100'.

ENDMODULE.                 " STATUS_0100  OUTPUT

*&---------------------------------------------------------------------*
*&      Module  USER_COMMAND_0100  INPUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
MODULE USER_COMMAND_0100 INPUT.

  TYPES: BEGIN OF it_txline,
           line(line_length) TYPE c,
         END OF it_txline.
  DATA: it_tline TYPE STANDARD TABLE OF txline.

  IF g_editor IS NOT INITIAL.
    CALL METHOD g_editor->get_text_as_r3table
      IMPORTING table = it_tline.
  ENDIF.

  CASE OK_CODE.
    WHEN 'EXIT' OR 'BACK' OR 'CANC'.
       IF g_editor IS NOT INITIAL.
         CALL METHOD g_editor->free
           EXCEPTIONS
             OTHERS = 1.
         FREE g_editor.
       ENDIF.

      IF g_editor_container IS NOT INITIAL.
        CALL METHOD g_editor_container->free
          EXCEPTIONS
            cntl_error = 1
            cntl_system_error = 2.
        FREE g_editor_container.
      ENDIF.

      LEAVE TO SCREEN 0.
  ENDCASE.

ENDMODULE.                 " USER_COMMAND_0100  INPUT
MODULE INIT_SCREEN_0100 OUTPUT.

  IF g_editor_container IS INITIAL.
    "create editor container on custom control
    CREATE OBJECT g_editor_container
      EXPORTING
        container_name = 'G_MLINE_TEXT'
      EXCEPTIONS
        cntl_error = 1
        cntl_system_error = 2
        create_error = 3
        lifetime_error = 4
        lifetime_dynpro_dynpro_link = 5.
    IF sy-subrc NE 0.
      "Error Handling
    ELSE.
      "create text editor object on editor container
      CREATE OBJECT g_editor
        EXPORTING
          parent = g_editor_container
          wordwrap_mode = cl_gui_textedit=>wordwrap_at_fixed_position  "使用此 type 才能 g_editor->get_text_as_r3table
                 "cl_gui_textedit=>wordwrap_off
                 "cl_gui_textedit=>wordwrap_at_fixed_position
                 "cl_gui_textedit=>WORDWRAP_AT_WINDOWBORDER
          wordwrap_position = line_length                              "限定寬度
          wordwrap_to_linebreak_mode = cl_gui_textedit=>true.

      "hide toolbar
      CALL METHOD g_editor->set_toolbar_mode
        EXPORTING
          toolbar_mode = cl_gui_textedit=>false.

      "show Status Bar
      CALL METHOD g_editor->set_statusbar_mode
        EXPORTING
          statusbar_mode = cl_gui_textedit=>true.
    ENDIF.
  ENDIF.

ENDMODULE.                 " INIT_SCREEN_0100  OUTPUT

上面的動作是將值讀出,但如果要預設值呢? 那就要改用 cl_gui_textedit 的 set_text_as_r3table method,其傳入的值等同於 get_text_as_r3table,只要傳一個 Internal Table 即可。


  TYPES: BEGIN OF it_txline,
           line(line_length) TYPE c,
         END OF it_txline.
  DATA: it_tline TYPE TABLE OF it_txline.

  CALL METHOD cl_gui_textedit->set_text_as_r3table
    EXPORTING
      table = it_tline
    EXCEPTIONS
      error_dp = 1
      error_dp_create = 2
      others = 3.

下面程式即在畫面加入 SET VALUE 與 CLEAR VALUE 的 Button,進行測試


REPORT  Z_EX_TEXT_MUTILINE.

DATA: OK_CODE LIKE SY-UCOMM.

CONSTANTS: line_length TYPE i VALUE 58.   "Customer Container 寬度 - 2
DATA: g_editor TYPE REF TO cl_gui_textedit,
      g_editor_container TYPE REF TO cl_gui_custom_container.

DATA: p_Text TYPE STRING.

START-OF-SELECTION.

  CALL SCREEN 0100.


*&---------------------------------------------------------------------*
*&      Module  STATUS_0100  OUTPUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
MODULE STATUS_0100 OUTPUT.

  SET PF-STATUS 'S0100'.
  SET TITLEBAR 'T0100'.

ENDMODULE.                 " STATUS_0100  OUTPUT

*&---------------------------------------------------------------------*
*&      Module  USER_COMMAND_0100  INPUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
MODULE USER_COMMAND_0100 INPUT.

  TYPES: BEGIN OF it_txline,
           line(line_length) TYPE c,
         END OF it_txline.
  DATA: it_tline TYPE TABLE OF it_txline,
        wa_tline TYPE it_txline.

  IF g_editor IS NOT INITIAL.
    CALL METHOD g_editor->get_text_as_r3table
      IMPORTING table = it_tline.
  ENDIF.

  CASE OK_CODE.
    WHEN 'EXIT' OR 'BACK' OR 'CANC'.
       IF g_editor IS NOT INITIAL.
         CALL METHOD g_editor->free
           EXCEPTIONS
             OTHERS = 1.
         FREE g_editor.
       ENDIF.

      IF g_editor_container IS NOT INITIAL.
        CALL METHOD g_editor_container->free
          EXCEPTIONS
            cntl_error = 1
            cntl_system_error = 2.
        FREE g_editor_container.
      ENDIF.

      LEAVE TO SCREEN 0.
    WHEN 'BUT2'.
      CLEAR: P_TEXT.
      PERFORM f_multi_line_set_value USING g_editor P_TEXT.
    WHEN 'BUT1'.
      CONCATENATE 'AAAAA' 'BBBB' 'CCCC' INTO P_TEXT
        SEPARATED BY cl_abap_char_utilities=>CR_LF.
      PERFORM f_multi_line_set_value USING g_editor P_TEXT.
  ENDCASE.

ENDMODULE.                 " USER_COMMAND_0100  INPUT
*&---------------------------------------------------------------------*
*&      Module  INIT_SCREEN_0100  OUTPUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
MODULE INIT_SCREEN_0100 OUTPUT.

  IF g_editor_container IS INITIAL.
    "填入 Customer Container 的 Editer Container
    CREATE OBJECT g_editor_container
      EXPORTING
        container_name = 'G_MLINE_TEXT'
      EXCEPTIONS
        cntl_error = 1
        cntl_system_error = 2
        create_error = 3
        lifetime_error = 4
        lifetime_dynpro_dynpro_link = 5.
    IF sy-subrc NE 0.
      "Error 則繼續呈現畫面不顯示 Remark (理論上不會 Error,除非傳錯 Container)
    ELSE.
      "產生 Multi Line Edit
      CREATE OBJECT g_editor
        EXPORTING
          parent = g_editor_container
          "使用此 type 才能 g_editor->get_text_as_r3table
          wordwrap_mode = cl_gui_textedit=>wordwrap_at_fixed_position  
                 "cl_gui_textedit=>wordwrap_off
                 "cl_gui_textedit=>wordwrap_at_fixed_position
                 "cl_gui_textedit=>WORDWRAP_AT_WINDOWBORDER
          wordwrap_position = line_length                              "限定寬度
          wordwrap_to_linebreak_mode = cl_gui_textedit=>true.

      "不要 toolbar
      CALL METHOD g_editor->set_toolbar_mode
        EXPORTING
          toolbar_mode = cl_gui_textedit=>false.

      "要 Status Bar
      CALL METHOD g_editor->set_statusbar_mode
        EXPORTING
          statusbar_mode = cl_gui_textedit=>true.
    ENDIF.
  ENDIF.

ENDMODULE.                 " INIT_SCREEN_0100  OUTPUT

*&---------------------------------------------------------------------*
*&      Form  F_MULTI_LINE_SET_VALUE
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM F_MULTI_LINE_SET_VALUE  USING p_editor TYPE REF TO cl_gui_textedit
                                   p_value.

  TYPES: BEGIN OF it_txline,
           line(line_length) TYPE c,
         END OF it_txline.
  DATA: it_tline TYPE TABLE OF it_txline,
        wa_tline TYPE it_txline.

  IF p_value IS INITIAL.
    CLEAR: it_tline[].
  ELSE.
    SPLIT p_value AT cl_abap_char_utilities=>CR_LF INTO TABLE it_tline.
  ENDIF.

  CALL METHOD p_editor->set_text_as_r3table
    EXPORTING
      table = it_tline
    EXCEPTIONS
      error_dp = 1
      error_dp_create = 2
      others = 3.

ENDFORM.                    " F_MULTI_LINE_SET_VALUE

沒有留言:

張貼留言

How to install & specified python version or distreibtuion package version in google colab

在買了 RTX 3080 要來 挖礦...  嗯~是跑機器學習後,結果發現了 GOOGLE COLAB,其實之前在「GAN 對抗式生成網路」一書就有看到,但資訊人就是什麼都想自己安裝,在本機用 Anaconda + pyCharm 弄了 GPU 環境,結果有天從新竹回家發現家裡沒...