SAP Control framework–DIALOGBOX_CONTAINER
引用:翱翔云天
我们前面看了几个container,包括customer container和splitter container,下面我再把其他两个常用的container介绍给大家。
1.1 CL_GUI_DIALOGBOX_CONTAINER
这个从名字上我们就可以看出来它是干什么的,就是弹出对话框的container。那么有人会问,有很多函数可以弹出对话框,即使没有相应的函数,我们也可以选择新建一个dialog的屏幕,然后调用这个屏幕就可以。没错,我们先说函数,有很多popup的函数可以使用,可是这些函数都是有固定的屏幕格式的,如果不适用怎么办?如果要新建屏幕,可是又可能我们的程序根本用不到这个屏幕,那么你新建的屏幕岂不是浪费资源?
所以这里引入这个container,让我们使用popup更加灵活。
7.1.1 定义:
弹出屏幕对话框,有点像popup confirmation。
7.1.2 方法:
Constructor:
PARENT: 指定super container
WIDTH: 宽度
HEIGHT: 高度
REPID: report ID
DYNNR: 屏幕号
TOP: 定义位置
LEFT: 定义位置
CAPTION: title
7.1.3 使用例子:
我们在屏幕上放一个按钮,然后放一个输入字段。
当点击这个按钮的时候,就会弹出对话框,里面我们可以输入一些信息,或者从本地托一个文本文件进去。
然后我们可以选择这个文本文件中的一段或者一块儿文字,作为输入字段的内容。
这或许是另外一种地input help的形式,我们可以把help的字段存为本地文件,然后用这种方法做输入帮助。
OK,现在开始做。
à定义屏幕,以及屏幕字段,设置status和ok_code
Screen fields:
VALUE I/O
P1 Push
OK_CODE OK
Flow logic:
PROCESS BEFORE OUTPUT.
MODULE STATUS_0100.
PROCESS AFTER INPUT.
MODULE USER_COMMAND_0100.
Main program declare screen fields:
data: ok_code like sy-ucomm.
data: value(100) type c.
à定义container
data:
* Dialog box
dialog_ref type ref to cl_gui_dialogbox_container,
* Text editor
text_ref type ref to cl_gui_textedit.
à定义事件处理
* For event process
data:
event type cntl_simple_event,
events type cntl_simple_events.
* Set F1 event for text editor
catch_f1 for event F1
of cl_gui_textedit importing sender,
àSet handler
* Register events
set handler me->catch_f1 for text_ref.
event-eventid = cl_gui_textedit=>EVENT_F1.
event-appl_event = 'X'.
append event to events.
CALL METHOD text_ref->SET_REGISTERED_EVENTS
EXPORTING
EVENTS = events .
à创建dialogbox,并插入text editor
CREATE OBJECT dialog_ref
EXPORTING
PARENT = cl_gui_container=>screen0
WIDTH = 400
HEIGHT = 200
REPID = repid
DYNNR = dynnr
TOP = 80
LEFT = 50
CAPTION = 'Test BOBO' .
CREATE OBJECT text_ref
EXPORTING
PARENT = dialog_ref .
à取得选择的部分并放到输入字段
* Get selected content
CALL METHOD text_ref->GET_SELECTED_TEXT_AS_STREAM
IMPORTING
SELECTED_TEXT = sel_text.
* Move the selected content to screen input field
read table sel_text into str index 1.
value = str.
源程序代码
REPORT ZBOBO_DIALOGBOX_CONTROL .
* Define the screen field and OK CODE
data: ok_code like sy-ucomm.
data: value(100) type c.
* Init dialog screen
class dialog_init definition.
public section.
* Load cl_gui_cfw
class cl_gui_cfw definition load.
methods:
* Create dialog box
create_dialog,
* Insert text edit control to dialog box
insert_control,
* Set F1 event for text editor
catch_f1 for event F1
of cl_gui_textedit importing sender,
* Get the selection field to screen field
process_control.
private section.
data:
* Dialog box
dialog_ref type ref to cl_gui_dialogbox_container,
* Text editor
text_ref type ref to cl_gui_textedit.
data: repid like sy-repid,
dynnr like sy-dynnr.
* For event process
data:
event type cntl_simple_event,
events type cntl_simple_events.
endclass.
class dialog_init implementation.
method create_dialog.
if dialog_ref is initial.
CREATE OBJECT dialog_ref
EXPORTING
PARENT = cl_gui_container=>screen0
WIDTH = 400
HEIGHT = 200
REPID = repid
DYNNR = dynnr
TOP = 80
LEFT = 50
CAPTION = 'Test BOBO'
EXCEPTIONS
CNTL_ERROR = 1
CNTL_SYSTEM_ERROR = 2
CREATE_ERROR = 3
LIFETIME_ERROR = 4
LIFETIME_DYNPRO_DYNPRO_LINK = 5
EVENT_ALREADY_REGISTERED = 6
ERROR_REGIST_EVENT = 7
others = 8
.
endif.
* Insert text editor to dialog box
call method insert_control.
endmethod.
method insert_control.
if not dialog_ref is initial.
CREATE OBJECT text_ref
EXPORTING
PARENT = dialog_ref .
* Register events
set handler me->catch_f1 for text_ref.
event-eventid = cl_gui_textedit=>EVENT_F1.
event-appl_event = 'X'.
append event to events.
CALL METHOD text_ref->SET_REGISTERED_EVENTS
EXPORTING
EVENTS = events .
endif.
endmethod.
method catch_f1.
* Catch the F1 event and process
call method process_control.
call method cl_gui_cfw=>Dispatch.
endmethod.
method process_control.
data: str(100) type c,
sel_text like table of str.
if not text_ref is initial.
* Get selected content
CALL METHOD text_ref->GET_SELECTED_TEXT_AS_STREAM
IMPORTING
SELECTED_TEXT = sel_text
.
* Move the selected content to screen input field
read table sel_text into str index 1.
value = str.
endif.
* Free container
call method text_ref->free.
call method dialog_ref->free.
endmethod.
endclass.
* Define reference of dialog_init
data: ref type ref to dialog_init.
start-of-selection.
call screen 100.
*&---------------------------------------------------------------------*
*& Module STATUS_0100 OUTPUT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
MODULE STATUS_0100 OUTPUT.
SET PF-STATUS 'TT'.
ENDMODULE. " STATUS_0100 OUTPUT
*&---------------------------------------------------------------------*
*& Module USER_COMMAND_0100 INPUT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
MODULE USER_COMMAND_0100 INPUT.
case ok_code.
when 'BACK'.
leave program.
when 'GET'.
* Create instance of dialog_init
create object ref.
* Main process of dialog box
call method ref->create_dialog.
endcase.
ENDMODULE. " USER_COMMAND_0100 INPUT
测试结果
执行程序:
点get value
按F1键
1.1 CL_GUI_DOCKING_CONTAINER
这个类比较简单,其功能有点类似于splitter container,我就不详细介绍了。把主要的方法以及属性列出来即可。
1.1.1 方法
CONSTRUCTOR:
PARENT Type Ref To CL_GUI_CONTAINER 你要dock的屏幕或者container
REPID Type SYREPID 连接的report
DYNNR Type SYDYNNR 连接的屏幕
SIDE Type I DOCK_AT_LEFT dock的位置,左,右……
CAPTION Type C 名字
1.1.2 属性
主要有几个constant比较常用
DOCK_AT_LEFT
DOCK_AT_TOP
DOCK_AT_BOTTOM
DOCK_AT_RIGHT