ABAP 多行文本编辑框实现方式

from:http://saptechnical.com/Tutorials/ABAP/TextControl/Demo.htm

Text Edit control - Usage and Demo

As an ABAP developer, we all know that if we drop a field of type char and length 255 in to the conventional screen painter it will be displayed in a single line. It will not be good interface unless we provide a multi-line input field in the screen. This can be achieved by text editor control. 

The following is our attempt to explain how to implement the text editor control on classic dynpros. 

Text editor is displayed on screen using custom control. So we need a container for the custom control. And text editor control is implemented using class CL_GUI_TEXTEDIT. 

Our declaration part contains

      DATA: LINE_LENGTH      TYPE I VALUE 254,
      EDITOR_CONTAINER TYPE REF TO CL_GUI_CUSTOM_CONTAINER,
      TEXT_EDITOR      TYPE REF TO CL_GUI_TEXTEDIT,
      TEXT             TYPE STRING. 

 Then call the screen (can be any number) 

 START-OF-SELECTION.
 CALL SCREEN '100'. 

 Go to screen 100 by double clicking on '100'.

Create a custom control on screen with name TEXTEDIT. 

 

Uncomment both PBO and PAI modules in flow logic screen 

 PROCESS BEFORE OUTPUT.
 MODULE STATUS_0100.
*
 PROCESS AFTER INPUT.
 MODULE USER_COMMAND_0100. 

 
     

 Define and implement both the modules in the main program itself. 

In PBO create object container EDITOR_CONTAINER. Then create text editor object by exporting the container EDITOR_CONTAINER. 

     CREATE OBJECT EDITOR_CONTAINER
      EXPORTING
        CONTAINER_NAME              = 'TEXTEDITOR'
      EXCEPTIONS
        CNTL_ERROR                  = 1
        CNTL_SYSTEM_ERROR           = 2
        CREATE_ERROR                = 3
        LIFETIME_ERROR              = 4
        LIFETIME_DYNPRO_DYNPRO_LINK = 5.

    CREATE OBJECT TEXT_EDITOR
      EXPORTING
        PARENT           = EDITOR_CONTAINER
        WORDWRAP_MODE    = CL_GUI_TEXTEDIT=>WORDWRAP_AT_FIXED_POSITION
        WORDWRAP_POSITION          = LINE_LENGTH
        WORDWRAP_TO_LINEBREAK_MODE = CL_GUI_TEXTEDIT=>TRUE. 
*You can hide the toolbar and as well as status bar for the text editor control. 
     CALL METHOD TEXT_EDITOR->SET_TOOLBAR_MODE
      EXPORTING
        TOOLBAR_MODE = CL_GUI_TEXTEDIT=>FALSE.

    CALL METHOD TEXT_EDITOR->SET_STATUSBAR_MODE
      EXPORTING
        STATUSBAR_MODE = CL_GUI_TEXTEDIT=>FALSE. 

 Define and create a GUI Status in the PBO. 

  SET PF-STATUS 'STATUS_0100'. 

 Very minimum set of tool bar buttons defined in this example 

 

In PAI of the screen 100, handle the save and other user commands. 

   CASE SY-UCOMM.
    WHEN 'EXIT'.
      LEAVE PROGRAM.
    WHEN 'SAVE'.
      CALL METHOD TEXT_EDITOR->GET_TEXTSTREAM
*         EXPORTING
*             ONLY_WHEN_MODIFIED     = CL_GUI_TEXTEDIT=>TRUE
          IMPORTING
              TEXT                   = TEXT
*             IS_MODIFIED            =
          EXCEPTIONS
              ERROR_CNTL_CALL_METHOD = 1
              NOT_SUPPORTED_BY_GUI   = 2
              OTHERS                 = 3.

      IF SY-SUBRC <> 0.
        MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
                   WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
      ENDIF.

      CALL METHOD CL_GUI_CFW=>FLUSH
        EXCEPTIONS
          CNTL_SYSTEM_ERROR = 1
          CNTL_ERROR        = 2
          OTHERS            = 3.
      MESSAGE TEXT TYPE 'I'.
  ENDCASE. 

 To read the text that is typed in the editor we need to call the method GET_TEXTSTREAM of the editor instance. 

We are just displaying the text typed in the editor in an informative message; the same can be inserted / updated into a database table also. 

The complete coding of the executable program is given below.. 

REPORT  ZTEXT_EDITOR.

DATA: LINE_LENGTH      TYPE I VALUE 254,
      EDITOR_CONTAINER TYPE REF TO CL_GUI_CUSTOM_CONTAINER,
      TEXT_EDITOR      TYPE REF TO CL_GUI_TEXTEDIT,
      TEXT             TYPE STRING.

START-OF-SELECTION.
  CALL SCREEN '100'.

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

  SET PF-STATUS 'STATUS_0100'.

  IF TEXT_EDITOR IS INITIAL.

    CREATE OBJECT EDITOR_CONTAINER
      EXPORTING
        CONTAINER_NAME              = 'TEXTEDITOR'
      EXCEPTIONS
        CNTL_ERROR                  = 1
        CNTL_SYSTEM_ERROR           = 2
        CREATE_ERROR                = 3
        LIFETIME_ERROR              = 4
        LIFETIME_DYNPRO_DYNPRO_LINK = 5.

    CREATE OBJECT TEXT_EDITOR
      EXPORTING
        PARENT                     = EDITOR_CONTAINER
        WORDWRAP_MODE              = CL_GUI_TEXTEDIT=>WORDWRAP_AT_FIXED_POSITION
        WORDWRAP_POSITION          = LINE_LENGTH
        WORDWRAP_TO_LINEBREAK_MODE = CL_GUI_TEXTEDIT=>TRUE.


*3)HIDE TOOLBAR AND STATUSBAR

    CALL METHOD TEXT_EDITOR->SET_TOOLBAR_MODE
      EXPORTING
        TOOLBAR_MODE = CL_GUI_TEXTEDIT=>FALSE.

    CALL METHOD TEXT_EDITOR->SET_STATUSBAR_MODE
      EXPORTING
        STATUSBAR_MODE = CL_GUI_TEXTEDIT=>FALSE.

  ENDIF.
ENDMODULE.                 " STATUS_0100  OUTPUT
*&---------------------------------------------------------------------*
*&      Module  USER_COMMAND_0100  INPUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
MODULE USER_COMMAND_0100 INPUT.
  CASE SY-UCOMM.
    WHEN 'EXIT'.
      LEAVE PROGRAM.
    WHEN 'SAVE'.
      CALL METHOD TEXT_EDITOR->GET_TEXTSTREAM
*         EXPORTING
*             ONLY_WHEN_MODIFIED     = CL_GUI_TEXTEDIT=>TRUE
          IMPORTING
              TEXT                   = TEXT
*             IS_MODIFIED            =
          EXCEPTIONS
              ERROR_CNTL_CALL_METHOD = 1
              NOT_SUPPORTED_BY_GUI   = 2
              OTHERS                 = 3.

      IF SY-SUBRC <> 0.
        MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
                   WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
      ENDIF.

      CALL METHOD CL_GUI_CFW=>FLUSH
        EXCEPTIONS
          CNTL_SYSTEM_ERROR = 1
          CNTL_ERROR        = 2
          OTHERS            = 3.
      MESSAGE TEXT TYPE 'I'.
  ENDCASE.
ENDMODULE.                 " USER_COMMAND_0100  INPUT 

 The final Screen shot of the editor: 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值