(原创)两种自定义texteditor的方式
在平常的需求中,可能会遇到需储存一些自定义的长文本的情况,这种时候,就需要用到textedictor,也就是文本编辑器,创建texteditor需要用到容器类(例如cl_gui_custom_container)和texteditor控制器类(cl_gui_textedit)。
本文所说的两种自定义texteditor方式,一种方式是直接使用这两种类,一种方式是使用SAP提供的封装好的函数,但是本质上这两种方式都是使用容器类和texteditor控制器类,只不过第二种方式SAP已经将其封装成一个函数供给我们直接使用而已。
下面给出用两种方式自定义texteditor的DEMO。
第一种:直接使用容器类和texteditor控制器类。
输出目标:点保存时,输出文本框中文本。
屏幕100:
源代码:
*&---------------------------------------------------------------------* *& Report Z15540_TEXT_EDITOR02 *&---------------------------------------------------------------------* *& *&---------------------------------------------------------------------* REPORT Z15540_TEXT_EDITOR02. DATA:OK_CODE TYPE SY-UCOMM, SAVE_CODE TYPE SY-UCOMM. DATA:LCL_CONTAINER TYPE REF TO CL_GUI_CUSTOM_CONTAINER, LCL_EDITOR TYPE REF TO CL_GUI_TEXTEDIT. DATA GT_LINES TYPE TABLE OF LINES. START-OF-SELECTION. CALL SCREEN 100. *&---------------------------------------------------------------------* *& Module STATUS_0100 OUTPUT *&---------------------------------------------------------------------* *& *&---------------------------------------------------------------------* MODULE STATUS_0100 OUTPUT. SET PF-STATUS 'STA'. SET TITLEBAR 'TEXTEDICTOR练习2'. IF LCL_EDITOR IS INITIAL. "实例化容器 CREATE OBJECT LCL_CONTAINER EXPORTING CONTAINER_NAME = 'CONTAINER'. "实例化texteditor控制器 CREATE OBJECT LCL_EDITOR EXPORTING * MAX_NUMBER_CHARS = * STYLE = 0 WORDWRAP_MODE = CL_GUI_TEXTEDIT=>WORDWRAP_AT_FIXED_POSITION "固定位置显示 * WORDWRAP_POSITION = -1 WORDWRAP_TO_LINEBREAK_MODE = CL_GUI_TEXTEDIT=>TRUE "可以回车换行 * FILEDROP_MODE = DROPFILE_EVENT_OFF PARENT = LCL_CONTAINER * LIFETIME = * NAME = EXCEPTIONS ERROR_CNTL_CREATE = 1 ERROR_CNTL_INIT = 2 ERROR_CNTL_LINK = 3 ERROR_DP_CREATE = 4 GUI_TYPE_NOT_SUPPORTED = 5 OTHERS = 6. "隐藏TOOLBAR CALL METHOD LCL_EDITOR->SET_TOOLBAR_MODE EXPORTING TOOLBAR_MODE = 0 EXCEPTIONS ERROR_CNTL_CALL_METHOD = 1 INVALID_PARAMETER = 2 OTHERS = 3 . IF SY-SUBRC <> 0. * Implement suitable error handling here ENDIF. ENDIF. ENDMODULE. *&---------------------------------------------------------------------* *& Module USER_COMMAND_0100 INPUT *&---------------------------------------------------------------------* * text *----------------------------------------------------------------------* MODULE USER_COMMAND_0100 INPUT. CLEAR SAVE_CODE. SAVE_CODE = OK_CODE. CLEAR OK_CODE. REFRESH GT_LINES. CASE SAVE_CODE. WHEN 'BACK' OR 'EXIT' OR 'CANCEL'. IF LCL_EDITOR IS NOT INITIAL. CALL METHOD LCL_EDITOR->FREE. ENDIF. IF LCL_CONTAINER IS NOT INITIAL. CALL METHOD LCL_CONTAINER->FREE. ENDIF. LEAVE TO SCREEN 0. WHEN 'SAVE'. "读取textedictor中的内容并输出 CALL METHOD LCL_EDITOR->GET_TEXT_AS_R3TABLE * EXPORTING * ONLY_WHEN_MODIFIED = FALSE IMPORTING TABLE = GT_LINES * IS_MODIFIED = EXCEPTIONS ERROR_DP = 1 ERROR_CNTL_CALL_METHOD = 2 ERROR_DP_CREATE = 3 POTENTIAL_DATA_LOSS = 4 OTHERS = 5. CL_DEMO_OUTPUT=>WRITE_DATA( GT_LINES ). CL_DEMO_OUTPUT=>DISPLAY( ). ENDCASE. ENDMODULE.
第二种:直接使用SAP封装好的函数。
输出目标:点保存时,输出文本框中文本。
屏幕100与第一种方式相同,这里就不贴图了。
源代码:
*&---------------------------------------------------------------------* *& Report Z15540_TEXT_EDITOR *&---------------------------------------------------------------------* *& *&---------------------------------------------------------------------* REPORT Z15540_TEXT_EDITOR. DATA GT_LINES TYPE TABLE OF LINE. DATA: OK_CODE TYPE SY-UCOMM, SAVE_CODE TYPE SY-UCOMM. START-OF-SELECTION. CALL SCREEN 100. *&---------------------------------------------------------------------* *& Module STATUS_0100 OUTPUT *&---------------------------------------------------------------------* *& *&---------------------------------------------------------------------* MODULE STATUS_0100 OUTPUT. SET PF-STATUS 'STA1'. SET TITLEBAR 'TEXT_EDICTOR练习'. "初始化TEXT_DICTOR CALL FUNCTION 'RH_EDITOR_SET' EXPORTING REPID = SY-REPID DYNNR = SY-DYNNR CONTROLNAME = 'CONTAINER' * MAX_COLS = 72 * MAX_LINES = MAX_LINES * SHOW_TOOL = 'X' * SHOW_STATUS = 'X' * STATUS_TEXT = STATUS_TEXT * DISPLAY_MODE = ' ' TABLES LINES = GT_LINES EXCEPTIONS CREATE_ERROR = 1 INTERNAL_ERROR = 2 OTHERS = 3. IF SY-SUBRC <> 0. * Implement suitable error handling here ENDIF. ENDMODULE. *&---------------------------------------------------------------------* *& Module USER_COMMAND_0100 INPUT *&---------------------------------------------------------------------* * text *----------------------------------------------------------------------* MODULE USER_COMMAND_0100 INPUT. DATA LT_LINES LIKE GT_LINES. CLEAR SAVE_CODE. SAVE_CODE = OK_CODE. CLEAR OK_CODE. CASE SAVE_CODE. WHEN 'EXIT'. LEAVE TO SCREEN 0. WHEN 'SAVE'. "读取text_edictor的文本 *DATA CONTROLNAME TYPE SCRFNAME. *DATA COL_WIDTH TYPE I. *DATA CHANGED TYPE C. CALL FUNCTION 'RH_EDITOR_GET' EXPORTING CONTROLNAME = 'CONTAINER' * COL_WIDTH = 0 * IMPORTING * CHANGED = CHANGED TABLES LINES = LT_LINES * EXCEPTIONS * INTERNAL_ERROR = 1 * OTHERS = 2 . cl_demo_output=>write_data( lt_lines[] ). cl_demo_output=>display( ). WHEN OTHERS. ENDCASE. ENDMODULE.
当然,在实际运用中,我们通常会去SE75自定义一个文本对象,然后通过SAVE_TEXT或CREATE_TEXT方式将texteditor中读取到的数据存入到底表中。