I had a thought one day to use a code saved in database table for easy change of some calculation rules and I’ve prepared code to run SE38 like editor for such code. Maybe some of you also had such need, so today I will explain briefly how to do this. I will use FM ‘EDITOR_TABLE_WITH_STATUS’ to display an editor which you know from SE38/SE80 , ‘SAPGUI_SET_FUNCTIONCODE’ for setting OK_CODE for editor, ‘PRETTY_PRINTER’ to make our code looks readable. I will also use one trick that allows you to assign data which is available in calling program but not in our function. But before we need to prepare our own GUI status with some basic buttons like ‘Patern’ or ‘Pretty Printer’.
After we create status we can create the ABAP code:
report editor_test.
data: ft_code type standard table of string.
data: fs_code type char255.
data: fs_zparm type zparm.
data: f_table type string.
data: f_fieldname type string.
data: f_msg(120) type c.
data: g_prog(8) type c. “temporary program name
data: g_okcode type sy–ucomm.
data: f_changed type c.
start-of-selection.
fs_code = ‘PROGRAM DYNAMIC_FORMS.’.
append fs_code to ft_code.
fs_code = ‘form test changing f_string type string.’.
append fs_code to ft_code.
fs_code = ‘f_string = sy-uname.’.
append fs_code to ft_code.
fs_code = ‘endform.’.
append fs_code to ft_code.
call function ‘RS_NAVIGATION_BREAK’.
call function ‘EDITOR_TABLE_WITH_STATUS’
exporting
* DISPLAY = ‘ ‘
* NAME = ‘ ‘
program = sy–repid
callback_usercom = ‘UCOMM’
callback_set_pfkey = ‘SET_PF’
title_text = ‘Our test editor’
importing
changed = f_changed
* SUBRC = SUBRC
tables
content = ft_code
.
*&———————————————————————*
*& Form set_pf
*&———————————————————————*
form set_pf .“using EDITORMODE.
set pf-status ‘OUR_GUISTATUS’.
if g_okcode = ‘ED_INS_FRA’. “Insert Pattern
call function ‘SAPGUI_SET_FUNCTIONCODE’
exporting
functioncode = ‘ED_INS_FRAME’
exceptions
function_not_supported = 1
others = 2.
if sy–subrc <> 0.
endif.
elseif g_okcode = ‘PRETTY’.
“set dummy okcode to avoid messages “Function not found”
call function ‘SAPGUI_SET_FUNCTIONCODE’
exporting
FUNCTIONCODE = ‘DUMY’
exceptions
FUNCTION_NOT_SUPPORTED = 1
others = 2.
if sy–subrc <> 0.
endif.
endif.
endform. “set_pf
*&———————————————————————*
*& Form ucomm
*&———————————————————————*
form ucomm .
field-symbols: <editor> type ref to cl_wb_editor.
data: fo_source type ref to cl_wb_source.
data: ft_sourcetab type standard table of char255.
data: ft_sourcetab_pretty type standard table of char255.
data: f_editor type string.
f_editor = ‘(SAPLS38E)ABAP_EDITOR’.
assign (f_editor) to <editor>.
move sy–ucomm to g_okcode.
sy–ucomm = ‘DUMY’.
case g_okcode.
when ‘PRETTY’.
data: line type i.
<editor>->get_source_instance( importing source_object = fo_source ).
fo_source->get_source_tab( importing source = ft_sourcetab[] ).
call function ‘PRETTY_PRINTER’
exporting
inctoo = space
* IMPORTING
* INDENTATION_MAYBE_WRONG = INDENTATION_MAYBE_WRONG
tables
ntext = ft_sourcetab_pretty[]
otext = ft_sourcetab[]
exceptions
enqueue_table_full = 1
include_enqueued = 2
include_readerror = 3
include_writeerror = 4
others = 5
.
if sy–subrc <> 0.
endif.
fo_source->set_source_tab( exporting source = ft_sourcetab_pretty[]
not_actualize_control = ‘X’ ).
<editor>->visualize_source( ).
endcase.
suppress dialog.
endform. “ucomm
And here is our result :-)
You should take care about saving function but that should not be a problem as you already seen in the code how to get the table with the code to local table. You can also add more functions for example: ‘Where used list’ (FM ‘RS_TOOL_ACCESS’) or syntax check (SYNTAX-CHECK).
When you have your final code you can then generate a temporary program from it using syntax found below and then call dynamically your forms created there.
generate subroutine pool ft_code name g_prog message f_msg.
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/20176.html