06/08/2010

How to Find a Screen Exit in a Given SAP Standard T-code


REPORT ZRRSAMPLE.

TABLES: modsap, modact, tstc.

PARAMETERS: input1 LIKE tstc-tcode DEFAULT ' ',
input2 LIKE modsap-typ DEFAULT ' '.

DATA: search1(6),
search2(3),
search3 LIKE modsap-member.

DATA : first_row VALUE 'Y'.

CONCATENATE: '%' input1 '%' INTO search1,
'%' input2 INTO search2.

SELECT * FROM tstc WHERE tcode LIKE search1.

first_row = 'Y'.

CHECK tstc-pgmna NE space.

CONCATENATE '%' tstc-pgmna '%' INTO search3.

SELECT * FROM modsap WHERE typ LIKE search2
AND member LIKE search3.

SELECT SINGLE * FROM modact WHERE member = modsap-name.

IF first_row EQ 'Y'.
WRITE: /0 tstc-tcode, 6 tstc-pgmna, 16 modsap-name, 32 modsap-typ,
45 modsap-member, 70 modact-name.
first_row = 'N'.
ELSE.
WRITE: /16 modsap-name, 32 modsap-typ, 45 modsap-member, 70 modact-name.
ENDIF.

CLEAR : modsap, modact.

ENDSELECT.

IF sy-subrc NE 0.
WRITE : /0 tstc-tcode, 6 tstc-pgmna, 30 'No exits found'.
ENDIF.

CLEAR tstc.

ENDSELECT.

END-OF-SELECTION.

CLEAR: search1, search2, search3.

Checkbox as a Radiobutton


REPORT ZRRSAMPLE.

PARAMETERS: p_check1 AS CHECKBOX USER-COMMAND check,
p_check2 AS CHECKBOX USER-COMMAND check,
p_check3 AS CHECKBOX USER-COMMAND check ,
p_check4 AS CHECKBOX USER-COMMAND check .

DATA: value TYPE char10.

AT SELECTION-SCREEN.

GET CURSOR FIELD value.

IF value = 'P_CHECK1'.
p_check2 = space.
p_check3 = space.
p_check4 = space.
ELSEIF value = 'P_CHECK2'.
p_check1 = space.
p_check3 = space.
p_check4 = space.
ELSEIF value = 'P_CHECK3'.
p_check1 = space.
p_check2 = space.
p_check4 = space.
ELSEIF value = 'P_CHECK4'.
p_check2 = space.
p_check3 = space.
p_check1 = space.

ENDIF.

Colors in REUSE_ALV_GRID_DISPLAY_LVC


REPORT ZRRSAMPLE.

*&---------------------------------------------------------------------*
* type pools
*----------------------------------------------------------------------*
TYPE-POOLS: slis.

*&---------------------------------------------------------------------*
* declarations for alv and internal tables
*----------------------------------------------------------------------*
DATA: it_fieldcat TYPE lvc_t_fcat,
wa_fieldcat TYPE lvc_s_fcat,
wa_layout TYPE lvc_s_layo,
v_position TYPE i ,
ls_cellcolor TYPE lvc_s_scol,
l_index TYPE sy-tabix.

DATA: BEGIN OF it_spfli OCCURS 0,
carrid LIKE spfli-carrid,
connid LIKE spfli-connid,
cellcolor TYPE lvc_t_scol,
END OF it_spfli.

*&---------------------------------------------------------------------*
* start of selection
*----------------------------------------------------------------------*
START-OF-SELECTION .

*---get data from db table
PERFORM get_data .

*---build layout for alv
PERFORM build_layout .

*---build fieldcat for alv
PERFORM build_fieldcat .

*---modify fieldcat for colors in alv
PERFORM modify_fieldcat .

*---display alv
PERFORM display_alv .

*&---------------------------------------------------------------------*
*& Form get_data
*----------------------------------------------------------------------*
FORM get_data .

SELECT carrid
connid
UP TO 25 ROWS
INTO CORRESPONDING FIELDS OF TABLE it_spfli
FROM spfli.

ENDFORM. " get_data
*&---------------------------------------------------------------------*
*& Form build_fieldcat
*----------------------------------------------------------------------*
FORM build_fieldcat .

wa_fieldcat-fieldname = 'CARRID'.
wa_fieldcat-reptext = 'CARRID'.

APPEND wa_fieldcat TO it_fieldcat.

CLEAR wa_fieldcat.

wa_fieldcat-fieldname = 'CONNID'.
wa_fieldcat-reptext = 'CONNID'.

APPEND wa_fieldcat TO it_fieldcat.

CLEAR wa_fieldcat.

ENDFORM. " build_fieldcat
*&---------------------------------------------------------------------*
*& Form build_layout
*----------------------------------------------------------------------*
FORM build_layout .

wa_layout-ctab_fname = 'CELLCOLOR'.
wa_layout-zebra = 'X'.

ENDFORM. " build_layout
*&---------------------------------------------------------------------*
*& Form modify_fieldcat
*----------------------------------------------------------------------*
FORM modify_fieldcat .

LOOP AT it_spfli.

l_index = sy-tabix.

IF l_index = 5 OR l_index = 15.

ls_cellcolor-fname = 'CARRID'.
ls_cellcolor-color-col = '6'.
ls_cellcolor-color-int = '1'.

APPEND ls_cellcolor TO it_spfli-cellcolor.

MODIFY it_spfli INDEX l_index TRANSPORTING cellcolor.

ENDIF.

IF l_index = 10 OR l_index = 20.

ls_cellcolor-fname = 'CONNID'.
ls_cellcolor-color-col = '4'.
ls_cellcolor-color-int = '1'.

APPEND ls_cellcolor TO it_spfli-cellcolor.

MODIFY it_spfli INDEX l_index TRANSPORTING cellcolor.

ENDIF.

IF it_spfli-carrid IS INITIAL .

DELETE it_spfli.

ENDIF.

ENDLOOP.

ENDFORM. " modify_fieldcat
*&---------------------------------------------------------------------*
*& Form display_alv
*----------------------------------------------------------------------*
FORM display_alv .

CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY_LVC'
EXPORTING
i_callback_program = sy-repid
is_layout_lvc = wa_layout
it_fieldcat_lvc = it_fieldcat
TABLES
t_outtab = it_spfli.

ENDFORM. " display_alv

Icon on Selection Screen - 2


REPORT ZRRSAMPLE.

TYPE-POOLS: icon.

SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE text-001.
SELECTION-SCREEN BEGIN OF LINE.
SELECTION-SCREEN COMMENT 1(17) fld_lab.
SELECTION-SCREEN COMMENT 18(5) icon1.
PARAMETERS: p_check.
SELECTION-SCREEN END OF LINE.
SELECTION-SCREEN END OF BLOCK b1.

AT SELECTION-SCREEN OUTPUT.

WRITE icon_configuration AS ICON TO icon1.
fld_lab = 'This is the label'.

START-OF-SELECTION.

Icon on Selection Screen - 1


REPORT ZRRSAMPLE.

TYPE-POOLS: icon.

SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE text-001.
PARAMETERS: p_check.
SELECTION-SCREEN COMMENT 40(20) icon1.
SELECTION-SCREEN END OF BLOCK b1.

AT SELECTION-SCREEN OUTPUT.

* Write pushbutton text
WRITE icon_configuration AS ICON TO icon1.
CONCATENATE icon1 'Your Icon' INTO icon1
SEPARATED BY space.

START-OF-SELECTION.

Insert Data into Hashed Table


REPORT ZRRSAMPLE.

TYPES: BEGIN OF t_foo,
field1 TYPE char2,
field2 TYPE char2,
field3 TYPE char2,
field4 TYPE i,
END OF t_foo.

DATA: lt_foo TYPE HASHED TABLE OF t_foo WITH UNIQUE KEY field1 field2 field3.

DATA: ls_foo LIKE LINE OF lt_foo.

FIELD-SYMBOLS: LIKE LINE OF lt_foo.

* Must assign it to use it.
ASSIGN ls_foo TO .

CLEAR .

-field1 = 'AB'.
-field2 = 'CD'.
-field3 = 'EF'.
-field4 = '2'.

INSERT INTO TABLE lt_foo.

CLEAR .

-field1 = 'AB'.
-field2 = 'CD'.
-field3 = 'EF'.
-field4 = '2'.

INSERT INTO TABLE lt_foo.

IF sy-subrc NE 0.

WRITE:/ ' this is the duplidate key'.

ENDIF.

Change of Case from Upper to Lower


REPORT ZRRSAMPLE.

PARAMETERS: p_str TYPE char100 .

DATA: string TYPE char100 .

INITIALIZATION .

p_str = 'THIS IS THE TEST' .

START-OF-SELECTION .

CALL FUNCTION 'STRING_UPPER_LOWER_CASE'
EXPORTING
delimiter = `_`
string1 = p_str
IMPORTING
string = string
EXCEPTIONS
not_valid = 1
too_long = 2
too_small = 3
OTHERS = 4.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.


WRITE:/ string .

Report Output Into Table


REPORT ZRRSAMPLE.

DATA: BEGIN OF listout OCCURS 0,
line(1024) TYPE c,
END OF listout.

* Submit the report and export list to memory
SUBMIT ZTESTE10 EXPORTING LIST TO MEMORY AND RETURN.

* Get list from memory and convert to ascii
PERFORM retrieve_list_from_memory TABLES listout.

LOOP AT listout.

WRITE:/ listout.

ENDLOOP.

************************************************************************
* RETRIEVE_LIST_FROM_MEMORY
************************************************************************
FORM retrieve_list_from_memory TABLES reportlines.

DATA: list LIKE abaplist OCCURS 0 WITH HEADER LINE.
DATA: txtlines(1024) TYPE c OCCURS 0 WITH HEADER LINE.

CLEAR : list , reportlines .

REFRESH : list , reportlines.

CALL FUNCTION 'LIST_FROM_MEMORY'
TABLES
listobject = list
EXCEPTIONS
not_found = 1
OTHERS = 2.

CHECK sy-subrc = 0.

CALL FUNCTION 'LIST_TO_ASCI'
TABLES
listobject = list
listasci = txtlines
EXCEPTIONS
empty_list = 1
list_index_invalid = 2
OTHERS = 3.

CHECK sy-subrc = 0.

reportlines[] = txtlines[].

CALL FUNCTION 'LIST_FREE_MEMORY'.

ENDFORM. "retrieve_list_from_memory

Add Toolbar Button on Selection Screen


REPORT ZRRSAMPLE.

TABLES: sscrfields.

DATA: d_butt1(4).

PARAMETERS: p_grpa1(10) MODIF ID a,
p_grpa2(10) MODIF ID a,
p_grpb1(10) MODIF ID b.

SELECTION-SCREEN FUNCTION KEY 1.
SELECTION-SCREEN FUNCTION KEY 2.

INITIALIZATION.

MOVE 'This is button 1' TO sscrfields-functxt_01.
MOVE 'Toggle 1' TO sscrfields-functxt_02.

d_butt1 = 'NO'.

AT SELECTION-SCREEN.
IF sy-ucomm = 'FC01'.

d_butt1 = 'YES'.
sscrfields-ucomm = 'ONLI'.

ELSEIF sy-ucomm = 'FC02'.

IF sscrfields-functxt_02 = 'Toggle 1'.

sscrfields-functxt_02 = 'Toggle 2'.

ELSE.

sscrfields-functxt_02 = 'Toggle 1'.

ENDIF.

ENDIF.

START-OF-SELECTION.

WRITE d_butt1.

Hide Parameter / Select Option in Selection Screen


REPORT ZRRSAMPLE.

DATA: d_ucomm LIKE sy-ucomm.

PARAMETERS: p_grpa1(10) MODIF ID a,
p_grpa2(5) MODIF ID a,
p_grpb1(2) MODIF ID b.

PARAMETERS: p_acta RADIOBUTTON GROUP rad1 USER-COMMAND act DEFAULT 'X',
p_actb RADIOBUTTON GROUP rad1.

AT SELECTION-SCREEN.
d_ucomm = sy-ucomm.

AT SELECTION-SCREEN OUTPUT.
LOOP AT SCREEN.
IF p_acta = 'X'.
IF screen-group1 = 'B'.
screen-active = 0.
ENDIF.
ELSEIF p_actb = 'X'.
IF screen-group1 = 'A'.
screen-active = 0.
ENDIF.
ENDIF.
MODIFY SCREEN.
ENDLOOP.

START-OF-SELECTION.

05/08/2010

Create Zip Files in Application Server


REPORT ZRRSAMPLE.

DATA: lt_data TYPE TABLE OF x255,
ls_data LIKE LINE OF lt_data.

DATA: lv_zip_content TYPE xstring ,
lv_dsn1(100) VALUE '/sap/NSP/sys/test.as',
lv_dsn2(100) VALUE '/sap/NSP/sys/test2.mxml',
lv_dsn3(100) VALUE '/sap/NSP/sys/testarchive.zip',
lv_file_length TYPE i ,
lv_content TYPE xstring,
lo_zip TYPE REF TO cl_abap_zip.

CREATE OBJECT lo_zip.

* Read the data as a string
CLEAR lv_content .

OPEN DATASET lv_dsn1 FOR INPUT IN BINARY MODE.

READ DATASET lv_dsn1 INTO lv_content .

CLOSE DATASET lv_dsn1.

lo_zip->add( name = 'test.as' content = lv_content ).

CLEAR lv_content .

OPEN DATASET lv_dsn2 FOR INPUT IN BINARY MODE.

READ DATASET lv_dsn2 INTO lv_content .

CLOSE DATASET lv_dsn2.

lo_zip->add( name = 'test2.mxml' content = lv_content ).

lv_zip_content = lo_zip->save( ).

* Conver the xstring content to binary
CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'
EXPORTING
buffer = lv_zip_content
IMPORTING
output_length = lv_file_length
TABLES
binary_tab = lt_data.


OPEN DATASET lv_dsn3 FOR OUTPUT IN BINARY MODE.

LOOP AT lt_data INTO ls_data.

TRANSFER ls_data TO lv_dsn3.

ENDLOOP.

CLOSE DATASET lv_dsn3.

Zooming and Moving Report Output


REPORT ZRRSAMPLE.

DATA: docking TYPE REF TO cl_gui_docking_container,
viewer TYPE REF TO cl_gui_ecl_2dviewer ,
repid LIKE sy-repid ,
file_name LIKE sapb-sapfiles,
file_type LIKE bdn_con-mimetype.

PARAMETERS: p_check TYPE c.

AT SELECTION-SCREEN OUTPUT.
PERFORM build_viewer.

START-OF-SELECTION.

*&---------------------------------------------------------------------
**& Form build_viewer
*----------------------------------------------------------------------
FORM build_viewer.

repid = sy-repid.

CREATE OBJECT docking
EXPORTING
repid = repid
dynnr = sy-dynnr
side = cl_gui_docking_container=>dock_at_right
extension = '600'
EXCEPTIONS
cntl_error = 1
cntl_system_error = 2
create_error = 3
lifetime_error = 4
lifetime_dynpro_dynpro_link = 5.


CREATE OBJECT viewer
EXPORTING
parent = docking
autoalign = ' '
EXCEPTIONS
cntl_error = 1
cntl_system_error = 2
create_error = 3
lifetime_error = 4
OTHERS = 5.

CHECK sy-subrc = 0.

CALL METHOD viewer->create_toolbar
EXPORTING
close_button = ' '
tools = 'X'
viewer = 'X'
navigation = 'X'
OPTIONS = ' '
viewer_openfile = ' '
viewer_savefile = ' '
tool_measurement = ' '
remove_document = ' '
EXCEPTIONS
cntl_system_error = 1
cntl_error = 2
cntb_btype_error = 3
dp_error = 4.

CHECK sy-subrc = 0.

*Populate the file path
file_name = 'C:\Documents and Settings\rafael.assis\Meus documentos\TEMP\Arquivo_ZCORVC001.txt'.

CALL METHOD viewer->open_document
EXPORTING
file = file_name
file_type = file_type
EXCEPTIONS
cntl_error = 1
cntl_system_error = 2
invalid_file_format = 3
permission_denied = 4
file_not_found = 5
bad_file_name = 6
invalid_data = 7
OTHERS = 8.

ENDFORM. "build_viewer

Save XLS Files to Presentation Server


REPORT zteste.

DATA: your_file_name TYPE string,
your_file_path TYPE string,
the_full_path TYPE string.

TYPES:

BEGIN OF the_alv_structure,
field1(10) TYPE c,
field2(10) TYPE c,
END OF the_alv_structure.

DATA: the_alv_itab TYPE TABLE OF the_alv_structure.
DATA: wa LIKE LINE OF the_alv_itab.

wa-field1 = 'A1'.
wa-field2 = 'B1'.
APPEND wa TO the_alv_itab.

wa-field1 = 'A2'.
wa-field2 = 'B2'.
APPEND wa TO the_alv_itab.

wa-field1 = 'A3'.
wa-field2 = 'B3'.
APPEND wa TO the_alv_itab.

CALL METHOD cl_gui_frontend_services=>file_save_dialog
EXPORTING
default_extension = '.xls'
default_file_name = 'Text'
CHANGING
filename = your_file_name
path = your_file_path
fullpath = the_full_path.

CALL METHOD cl_gui_frontend_services=>gui_download
EXPORTING
filename = the_full_path
write_field_separator = 'X'
CHANGING
data_tab = the_alv_itab.

Show Table Contents From Program


REPORT ZRRSAMPLE.

PARAMETERS: tname LIKE rsdxx-objname.

CALL FUNCTION 'RS_TOOL_ACCESS'
EXPORTING
operation = 'TAB_CONT'
object_name = tname
object_type = 'TABL'
EXCEPTIONS
not_executed.

Split Command in Files


REPORT ZRRSAMPLE.

PARAMETERS: p_file TYPE localfile DEFAULT '/usr/sap/TST/SYS/Test.txt'.

DATA: BEGIN OF itab OCCURS 0,
fielp_file(20) TYPE c,
field2(20) TYPE c,
field3(20) TYPE c,
END OF itab.

DATA: wa TYPE string.

CONSTANTS: con_tab TYPE c VALUE cl_abap_char_utilities=>horizontal_tab.

* if you have a older version, then you can use this instead.
* CONSTANTS: con_tab TYPE x VALUE '09'.
* and comment this line above: constants:
* con_tab type c value cl_abap_char_utilities=>HORIZONTAL_TAB.

START-OF-SELECTION.

OPEN DATASET p_file FOR INPUT IN TEXT MODE ENCODING DEFAULT.
IF sy-subrc = 0.
DO.
READ DATASET p_file INTO wa.
IF sy-subrc NE 0.
EXIT.
ENDIF.

* Here you are splitting at the hex value of "tab" not at
* the # sign.

SPLIT wa AT con_tab INTO itab-fielp_file itab-field2 itab-field3.
APPEND itab.
ENDDO.
ENDIF.
CLOSE DATASET p_file.

Unit Conversion in Reports


REPORT ZRRSAMPLE.

DATA: lv_input TYPE p DECIMALS 3 VALUE '10000.000',
lv_output TYPE p DECIMALS 3.

CALL FUNCTION 'UNIT_CONVERSION_SIMPLE'
EXPORTING
input = lv_input
unit_in = 'LB'
unit_out = 'KG'
IMPORTING
output = lv_output
EXCEPTIONS
conversion_not_found = 1
division_by_zero = 2
input_invalid = 3
output_invalid = 4
overflow = 5
type_invalid = 6
units_missing = 7
unit_in_not_found = 8
unit_out_not_found = 9
OTHERS = 10.

WRITE:/ lv_output.

Upload CSV Files Into Internal Table


REPORT ZRRSAMPLE.

TYPES: BEGIN OF ttab,
rec(1000) TYPE c,
END OF ttab.

TYPES: BEGIN OF tdat,
fld1(10) TYPE c,
fld2(10) TYPE c,
fld3(10) TYPE c,
END OF tdat.

DATA: itab TYPE TABLE OF ttab WITH HEADER LINE.
DATA: idat TYPE TABLE OF tdat WITH HEADER LINE.

DATA: file_str TYPE string.

PARAMETERS: p_file TYPE localfile.

AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.

CALL FUNCTION 'KD_GET_FILENAME_ON_F4'
EXPORTING
static = 'X'
CHANGING
file_name = p_file.

START-OF-SELECTION.

file_str = p_file.

CALL FUNCTION 'GUI_UPLOAD'
EXPORTING
filename = file_str
TABLES
data_tab = itab
EXCEPTIONS
file_open_error = 1
file_read_error = 2
no_batch = 3
gui_refuse_filetransfer = 4
invalid_type = 5
no_authority = 6
unknown_error = 7
bad_data_format = 8
header_not_allowed = 9
separator_not_allowed = 10
header_too_long = 11
unknown_dp_error = 12
access_denied = 13
dp_out_of_memory = 14
disk_full = 15
dp_timeout = 16
OTHERS = 17.


LOOP AT itab.

CLEAR idat.
SPLIT itab-rec AT ',' INTO idat-fld1
idat-fld2
idat-fld3.
APPEND idat.

ENDLOOP.


LOOP AT idat.

WRITE:/ idat-fld1, idat-fld2, idat-fld3.

ENDLOOP.

Displaying Percentage in ALV


REPORT ZRRSAMPLE.

TYPE-POOLS:slis .

DATA: it_fieldcat TYPE slis_t_fieldcat_alv ,
wa_fieldcat TYPE slis_fieldcat_alv .

DATA: BEGIN OF itab OCCURS 0,
name(10) TYPE c,
marks TYPE i ,
pers TYPE char5, "<---this should be character
grade TYPE char10,
END OF itab .

itab-name = 'IBM'.
itab-marks = 4.
APPEND itab .

itab-name = 'DELL'.
itab-marks = 15.
APPEND itab .

itab-name = 'GOOGLE'.
itab-marks = 21.
APPEND itab .

itab-name = 'MICROSOFT'.
itab-marks = 18.
APPEND itab .

itab-name = 'SUN'.
itab-marks = 9.
APPEND itab .

itab-name = 'SAP'.
itab-marks = 24.
APPEND itab .

LOOP AT itab .

itab-pers = ( itab-marks / 25 ) * 100 .

IF itab-pers LT 35 .
itab-grade = 'Bad'.

ELSEIF itab-pers GE 35 AND itab-pers LT 60 .
itab-grade = 'Average '.

ELSEIF itab-pers GE 60 AND itab-pers LT 80 .
itab-grade = 'Good'.

ELSEIF itab-pers GE 80 AND itab-pers LT 90 .
itab-grade = 'Very Good'.

ELSEIF itab-pers GE 90 AND itab-pers LT 100 .
itab-grade = 'Best'.
ENDIF.

CONCATENATE itab-pers '%' INTO itab-pers.

MODIFY itab.

ENDLOOP.

wa_fieldcat-fieldname = 'NAME'.
wa_fieldcat-seltext_m = 'name'.

APPEND wa_fieldcat TO it_fieldcat .
CLEAR wa_fieldcat .

wa_fieldcat-fieldname = 'MARKS'.
wa_fieldcat-seltext_m = 'marks'.

APPEND wa_fieldcat TO it_fieldcat .
CLEAR wa_fieldcat .

wa_fieldcat-fieldname = 'PERS'.
wa_fieldcat-seltext_m = 'percentage'.

APPEND wa_fieldcat TO it_fieldcat .
CLEAR wa_fieldcat .

wa_fieldcat-fieldname = 'GRADE'.
wa_fieldcat-seltext_m = 'grade'.

APPEND wa_fieldcat TO it_fieldcat .
CLEAR wa_fieldcat .

CALL FUNCTION 'REUSE_ALV_LIST_DISPLAY'
EXPORTING
i_callback_program = sy-repid
it_fieldcat = it_fieldcat
TABLES
t_outtab = itab.

Simple Email Sending Program



REPORT ZRRSAMPLE.

*----------------------------------------------------------*
*Description: this report will creates an output in email
* in HTML format
*----------------------------------------------------------*

DATA: it_maildata TYPE sodocchgi1 OCCURS 10 WITH HEADER LINE ,
it_mailtext LIKE solisti1 OCCURS 10 WITH HEADER LINE,
it_mailrecep LIKE somlrec90 OCCURS 10 WITH HEADER LINE,
it_listtable TYPE abaplist OCCURS 10 WITH HEADER LINE,
it_htmldata TYPE w3html OCCURS 10 WITH HEADER LINE,
it_ascidatatab TYPE char255 OCCURS 10 WITH HEADER LINE.


START-OF-SELECTION.
*----------------------------------------------------------*
**before submitting the report first create a test program for
**displaying the output in html program .copy and paste the
**below report
*report ztest_submit.
*start-of-selection.
*do 10 times.
*write:/ 'this is the test report for html output in email'.
*enddo.
*----------------------------------------------------------*

SUBMIT ztest_submit
EXPORTING LIST TO MEMORY AND RETURN.

CALL FUNCTION 'LIST_FROM_MEMORY'
TABLES
listobject = it_listtable
EXCEPTIONS
not_found = 1
OTHERS = 2.

CALL FUNCTION 'WWW_HTML_FROM_LISTOBJECT'
TABLES
html = it_htmldata
listobject = it_listtable.


CLEAR: it_maildata,
it_mailtext,
it_mailrecep.

REFRESH: it_mailtext,
it_mailrecep.

it_maildata-obj_name = 'TEST'.
it_maildata-obj_descr = 'RR TEST SUBJECT'.

LOOP AT it_htmldata.

it_mailtext = it_htmldata.

APPEND it_mailtext.

ENDLOOP.

it_mailrecep-receiver = 'teste@gmail.com'.
it_mailrecep-rec_type = 'U'.

APPEND it_mailrecep.

CALL FUNCTION 'SO_NEW_DOCUMENT_SEND_API1'
EXPORTING
document_data = it_maildata
document_type = 'HTM'
put_in_outbox = 'X'
TABLES
object_header = it_mailtext
object_content = it_mailtext
receivers = it_mailrecep
EXCEPTIONS
too_many_receivers = 1
document_not_sent = 2
document_type_not_exist = 3
operation_no_authorization = 4
parameter_error = 5
x_error = 6
enqueue_error = 7
OTHERS = 8.
IF sy-subrc NE 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
* WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.

COMMIT WORK.
WAIT UP TO 2 SECONDS.
SUBMIT rsconn01 WITH mode = 'INT'
WITH output = 'X'
AND RETURN.

Displaying Time as PopUp


REPORT ZRRSAMPLE.

DATA: help_info TYPE help_info,
select_value TYPE help_info-fldvalue,
dynpselect TYPE dselc OCCURS 0 WITH HEADER LINE,
dynpvaluetab TYPE dval OCCURS 0 WITH HEADER LINE.

INITIALIZATION.

help_info-call = 'V'.
help_info-object = 'F'.
help_info-program = sy-repid.
help_info-dynpro = sy-dynnr.
help_info-tabname = 'BTCH3071'.
help_info-fieldname = 'FROM_TIME'.
help_info-fieldtype = 'TIME'.
help_info-fieldlng = '08'.
help_info-spras = 'E'.
help_info-menufunct = 'HC'.

START-OF-SELECTION .

CALL FUNCTION 'DD_SHLP_CALL_FROM_DYNP'
EXPORTING
help_infos = help_info
IMPORTING
select_value = select_value
TABLES
dynpselect = dynpselect
dynpvaluetab = dynpvaluetab.

02/08/2010

Processing of more than one transaction in a single step.


*----------------------------------------------------------------------
* Processing of more than one transaction in a single step.
*----------------------------------------------------------------------
REPORT ztest NO STANDARD PAGE HEADING LINE-SIZE 255.

DATA: BEGIN OF bdc_tab OCCURS 10.
INCLUDE STRUCTURE bdcdata.
DATA: END OF bdc_tab.


CLEAR bdc_tab.
REFRESH bdc_tab.

PERFORM dynpro_screen USING 'SAPLCOKO1' '0110'.
PERFORM dynpro_field USING 'CAUFVD-AUFNR' '000501210974'.
PERFORM dynpro_field USING 'R62CLORD-FLG_KNOT' 'X'.
PERFORM dynpro_field USING 'BDC_OKCODE' '/00'.

PERFORM dynpro_screen USING 'SAPLCOKO1' '0115'.
PERFORM dynpro_field USING 'BDC_OKCODE' 'KOAZ'.

CALL TRANSACTION 'CO03' USING bdc_tab MODE 'E' UPDATE 'S'.


*----------------------------------------------------------------------
* Processing of Dynpros
*----------------------------------------------------------------------
FORM dynpro_screen USING dyn_name dyn_nr.

CLEAR bdc_tab.
bdc_tab-program = dyn_name.
bdc_tab-dynpro = dyn_nr.
bdc_tab-dynbegin = 'X'.
APPEND bdc_tab.

ENDFORM.


*----------------------------------------------------------------------
* Processing of fields
*----------------------------------------------------------------------
FORM dynpro_field USING feldname feldinhalt.

CLEAR bdc_tab.
bdc_tab-fnam = feldname.
bdc_tab-fval = feldinhalt.
APPEND bdc_tab.

ENDFORM.

Generates a Small Visual Basic Script File


*-----------------------------------------------------------------------
* This program generates a small Visual Basic Script file, which in
* turn shows a message box on the screen.
*-----------------------------------------------------------------------
REPORT ztest NO STANDARD PAGE HEADING.

DATA: mess_table LIKE STANDARD TABLE OF soli WITH HEADER LINE,
file_exe LIKE rlgrap-filename,
my_message(255).


*-----------------------------------------------------------------------
* This code creates the message box
*-----------------------------------------------------------------------
APPEND: 'Dim myolapp ' TO mess_table,
'Set myolapp = CreateObject("Access.Application") '
TO mess_table,
'Dim myVar' TO mess_table.
my_message = 'This is my message to the user'.
CONCATENATE 'myVar = MsgBox ("' my_message '", 0, "Warning")'
INTO my_message.
APPEND my_message TO mess_table.


*-----------------------------------------------------------------------
* Generated Script file must be downloaded into the TEMP folder of
* the Windows environment
*-----------------------------------------------------------------------
CALL FUNCTION 'WS_QUERY'
EXPORTING
environment = 'TEMP'
query = 'EN'
IMPORTING
return = file_exe
EXCEPTIONS
inv_query = 1
no_batch = 2
frontend_error = 3
OTHERS = 4.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.

ELSE.

CONCATENATE file_exe '\my_script.vbs' INTO file_exe.


*-----------------------------------------------------------------------
* Script file is downloaded
*-----------------------------------------------------------------------
CALL FUNCTION 'WS_DOWNLOAD'
EXPORTING
filename = file_exe
filetype = 'DAT'
mode = 'S'
TABLES
data_tab = mess_table
EXCEPTIONS
file_open_error = 1
file_write_error = 2
invalid_filesize = 3
invalid_table_width = 4
invalid_type = 5
no_batch = 6
unknown_error = 7
OTHERS = 8.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ELSE.

*-----------------------------------------------------------------------
* The Script file is executed
*-----------------------------------------------------------------------
CALL FUNCTION 'WS_EXECUTE'
EXPORTING
commandline = file_exe
program = 'WSCRIPT.EXE'
EXCEPTIONS
frontend_error = 1
no_batch = 2
prog_not_found = 3
illegal_option = 4
OTHERS = 5.

IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.

ENDIF.

ENDIF.

Read the Standard Text in Transaction SO10


*-----------------------------------------------------------------------
* With this program you can save any entered text as a standard text
* and retrieve it, when executing the program, again. You can read
* the standard text in transaction SO10
* Create a screen named 0100 and draw a container called 'TEXTEDIT'
* as big as you wish. You should also create a pf-status 'STATUS-100'
* which has a BACK button and a CANC button.
*-----------------------------------------------------------------------
REPORT ztest no standard page heading.

DATA: init,
okcode LIKE sy-ucomm,
text_header LIKE thead,
container TYPE REF TO cl_gui_custom_container,
editor TYPE REF TO cl_gui_textedit,
text_table TYPE STANDARD TABLE OF tline.


START-OF-SELECTION.
CALL SCREEN 0100.


*-----------------------------------------------------------------------
* Module STATUS_0100 OUTPUT
*-----------------------------------------------------------------------
MODULE status_0100 OUTPUT.

SET PF-STATUS 'STATUS-100'.

IF init IS INITIAL.
init = 'X'.
CREATE OBJECT: container EXPORTING container_name = 'TEXTEDIT',
editor EXPORTING parent = container.

CALL FUNCTION 'READ_TEXT'
EXPORTING
id = 'ST'
language = 'E'
name = 'YTEST1'
object = 'TEXT'
TABLES
lines = text_table
EXCEPTIONS
not_found = 1.

IF sy-subrc = 0.

CALL METHOD editor->set_text_as_stream
EXPORTING
text = text_table.

* Protect text lines
CALL METHOD editor->protect_lines
EXPORTING
from_line = 1
to_line = 200
enable_editing_protected_text = 0.

* Highlight in blue the text
CALL METHOD editor->highlight_lines
EXPORTING
from_line = 1
to_line = 20
highlight_mode = 1.

* Hide status bar
CALL METHOD editor->set_statusbar_mode
EXPORTING
statusbar_mode = 0.

* Hide tool bar
CALL METHOD editor->set_toolbar_mode
EXPORTING
toolbar_mode = 0.

ENDIF.

ENDIF.

ENDMODULE.


*-----------------------------------------------------------------------
* Module USER_COMMAND_0100 INPUT
* The entered text is saved in english (E), under the name 'YTEST1'
* and as standard text 'ST'.
*-----------------------------------------------------------------------
MODULE user_command_0100 INPUT.

CASE okcode.
WHEN 'BACK' OR 'CANC'.
SET SCREEN 0.

WHEN 'SAVE'.
text_header-tdobject = 'TEXT'.
text_header-tdname = 'YTEST1'.
text_header-tdid = 'ST'.
text_header-tdtitle = 'My Title'.
text_header-tdspras = 'E'.
text_header-tdfuser = sy-uname.

CALL METHOD editor->get_text_as_stream
IMPORTING
text = text_table.

CALL FUNCTION 'SAVE_TEXT'
EXPORTING
header = text_header
TABLES
lines = text_table.

ENDCASE.

ENDMODULE.

Create a Text Field


*-----------------------------------------------------------------------
* This program creates a text field which you can use as an editor.
* Create a screen 0100 and on it draw a container named 'TEXTEDIT' as
* big as you wish.
*-----------------------------------------------------------------------
REPORT ztest NO STANDARD PAGE HEADING.

DATA: init,
okcode LIKE sy-ucomm,
container TYPE REF TO cl_gui_custom_container,
editor TYPE REF TO cl_gui_textedit.

START-OF-SELECTION.
CALL SCREEN 0100.

*-----------------------------------------------------------------------
* Module STATUS_0100 OUTPUT
*-----------------------------------------------------------------------
MODULE status_0100 OUTPUT.

SET PF-STATUS 'STATUS-100'.
SET TITLEBAR '100'.

IF init IS INITIAL.
init = 'X'.
CREATE OBJECT: container EXPORTING container_name = 'TEXTEDIT',
editor EXPORTING parent = container.

ENDIF.
ENDMODULE.



*-----------------------------------------------------------------------
* Module USER_COMMAND_0100 INPUT
*-----------------------------------------------------------------------
MODULE user_command_0100 INPUT.

CASE okcode.
WHEN 'CANC' OR 'BACK'.
SET SCREEN 0.
WHEN OTHERS.
LEAVE PROGRAM.
ENDCASE.

ENDMODULE.

Different Progress Bar


*-----------------------------------------------------------------------
* This program generates a different Progress Bar on your screen.
*-----------------------------------------------------------------------
REPORT ztest NO STANDARD PAGE HEADING.

DATA: val TYPE i VALUE 0.

DO 20 TIMES.

val = val + 5.
CALL FUNCTION 'PROGRESS_POPUP'
EXPORTING
btn_txt = 'Cancel'
curval = val
maxval = 100
stat = '1'
text_1 = 'Text 1'
text_2 = 'Text 2'
text_3 = 'Text 3'
title = 'My Title'
winid = 100.

ENDDO.

CALL FUNCTION 'PROGRESS_POPUP'
EXPORTING
stat = '2'
winid = 100.

CALL FUNCTION 'GRAPH_DIALOG'
EXPORTING
close = 'X'.

Program to Generate a Progress Bar


*-----------------------------------------------------------------------
* This program generates a Progress Bar on your screen.
* Try to change the value in layout of the 'EPS_PROGRESS_POPUP'
* function (from 1 to 4).
*-----------------------------------------------------------------------
REPORT ztest NO STANDARD PAGE HEADING.

DATA: val TYPE i VALUE 0.

DO 20 TIMES.

val = val + 5.
CALL FUNCTION 'EPS_PROGRESS_POPUP'
EXPORTING
btn_txt = 'CANCEL'
curval_g1 = val
curval_g2 = val
layout = '1'
maxval_g1 = '100'
maxval_g2 = '100'
popup_link = 'ASYNC'
popup_stat = '1'
popup_title = 'This is my Title'
text_1 = 'TEXT 1'
text_2 = 'TEXT 2'
text_3 = 'TEXT 3'
text_4 = 'TEXT 4'
text_g1 = 'My Progress Bar'
text_g2 = 'Line 2'
title_g2 = 'Progress Status'
winid = 100.

ENDDO.

CALL FUNCTION 'PROGRESS_POPUP'
EXPORTING
stat = '2'
winid = 100.

CALL FUNCTION 'GRAPH_DIALOG'
EXPORTING
close = 'X'.

Report to Creates a Tree


*---------------------------------------------------------------------
* This report creates a tree with the favorite web sites as option
* nodes for viewing these pages in a browser.
* Don´t forget to define a PF-STATUS 'STATUS-100' which contains
* a BACK button, a CANC button and 5 Application toolbar buttons:
* EXPAND, COLLAPSE, NEW URL, PREVIOUS and NEXT site.
*---------------------------------------------------------------------
REPORT ztest NO STANDARD PAGE HEADING.

CLASS lcl_application DEFINITION DEFERRED.
CLASS cl_gui_cfw DEFINITION LOAD.

DATA: node TYPE treev_node,
node_table TYPE treev_ntab,
item_table LIKE STANDARD TABLE OF mtreeitm WITH DEFAULT KEY,
item TYPE mtreeitm,
okcode LIKE sy-ucomm,
url(255).

DATA: g_application TYPE REF TO lcl_application,
my_tree TYPE REF TO cl_gui_column_tree,
container_1 TYPE REF TO cl_gui_container,
container_2 TYPE REF TO cl_gui_container,
browser TYPE REF TO cl_gui_html_viewer,
splitter TYPE REF TO cl_gui_splitter_container.

DATA: event TYPE cntl_simple_event,
events TYPE cntl_simple_events,
hierarchy_header TYPE treev_hhdr,
my_node TYPE tv_nodekey.


INCLUDE .

* Necessary Class definition for object Tree
CLASS lcl_application DEFINITION.
PUBLIC SECTION.

METHODS:
handle_node_double_click
FOR EVENT node_double_click
OF cl_gui_column_tree
IMPORTING node_key,

handle_item_double_click
FOR EVENT item_double_click
OF cl_gui_column_tree
IMPORTING node_key.
ENDCLASS.



* Necessary Class implementation for object Tree
CLASS lcl_application IMPLEMENTATION.

METHOD handle_node_double_click.
my_node = node_key.
ENDMETHOD.

METHOD handle_item_double_click.
my_node = node_key.
ENDMETHOD.

ENDCLASS.


START-OF-SELECTION.
CALL SCREEN 0100.


*-----------------------------------------------------------------------
* Create the tree
*-----------------------------------------------------------------------
FORM crea_arbol.

* Split the container in 2 parts
CREATE OBJECT splitter
EXPORTING
parent = cl_gui_container=>screen0
rows = 1
columns = 2.

CALL METHOD splitter->set_border
EXPORTING
border = cl_gui_cfw=>true.

CALL METHOD splitter->set_column_mode
EXPORTING
mode = splitter->mode_absolute.

CALL METHOD splitter->set_column_width
EXPORTING
id = 1
width = 250.


* Assign a name to every column
CALL METHOD splitter->get_container
EXPORTING
row = 1
column = 1
RECEIVING
container = container_1.

CALL METHOD splitter->get_container
EXPORTING
row = 1
column = 2
RECEIVING
container = container_2.


* Container´s header
hierarchy_header-heading = 'My favorite web sites'.
hierarchy_header-width = 40.


* Create the tree
CREATE OBJECT my_tree
EXPORTING
parent = container_1
node_selection_mode = cl_gui_column_tree=>node_sel_mode_single
item_selection = 'X'
hierarchy_column_name = 'Column1'
hierarchy_header = hierarchy_header
EXCEPTIONS
cntl_system_error = 1
create_error = 2
failed = 3
illegal_node_selection_mode = 4
illegal_column_name = 5
lifetime_error = 6.


* Event double click
event-eventid = cl_gui_column_tree=>eventid_node_double_click.
event-appl_event = 'X'.
APPEND event TO events.

* item double click
event-eventid = cl_gui_column_tree=>eventid_item_double_click.
event-appl_event = 'X'.
APPEND event TO events.

CALL METHOD my_tree->set_registered_events
EXPORTING
events = events
EXCEPTIONS
cntl_error = 1
cntl_system_error = 2
illegal_event_combination = 3.


* assign event handlers in the application class to each desired event
CREATE OBJECT g_application.
SET HANDLER g_application->handle_node_double_click FOR my_tree.
SET HANDLER g_application->handle_item_double_click FOR my_tree.


* Create the tree´s nodes
PERFORM create_nodes.

* Add nodes to the tree
CALL METHOD my_tree->add_nodes_and_items
EXPORTING
node_table = node_table
item_table = item_table
item_table_structure_name = 'MTREEITM'
EXCEPTIONS
failed = 1
cntl_system_error = 3
error_in_tables = 4
dp_error = 5
table_structure_name_not_found = 6.


* Expand Root node when loading the program
CALL METHOD my_tree->expand_root_nodes.

ENDFORM.


*----------------------------------------------------------------------
* Creation of the browser
*----------------------------------------------------------------------
FORM create_browser.

CREATE OBJECT browser
EXPORTING
parent = container_2.

CALL METHOD browser->show_url
EXPORTING
url = 'http://www.sap.com'
EXCEPTIONS
cntl_error = 1.

ENDFORM.


*-----------------------------------------------------------------------
* Let´s create the nodes. Check the logical sequence on concatenation
* between the upper node and the lower node.
* Warning: upper and lower case affect when defining the node´s name
*-----------------------------------------------------------------------
FORM create_nodes.

* This is Root node
node-node_key = 'Root'.
CLEAR node-relatkey.
CLEAR node-relatship.
node-hidden = ' '.
node-disabled = ' '.
node-isfolder = 'X'.
node-n_image = icon_protocol.
node-exp_image = icon_protocol.
APPEND node TO node_table.

CLEAR item.
item-node_key = 'Root'.
item-item_name = 'Column1'.
item-class = cl_gui_column_tree=>item_class_text.
item-text = 'Options tree'.
APPEND item TO item_table.


* This is the first folder (Software)
node-node_key = 'NODE1'.
node-relatkey = 'Root'.
node-relatship = cl_gui_column_tree=>relat_last_child.
node-n_image = icon_closed_folder.
node-exp_image = icon_open_folder.
node-expander = 'X'.
APPEND node TO node_table.

item-node_key = 'NODE1'.
item-class = cl_gui_column_tree=>item_class_text.
item-text = 'Software'.
APPEND item TO item_table.


* Option 1 for Folder software:
node-node_key = 'OPT1'.
node-relatkey = 'NODE1'.
node-relatship = cl_gui_column_tree=>relat_last_child.
node-n_image = icon_execute_object.
node-expander = 'X'.
APPEND node TO node_table.

item-node_key = 'OPT1'.
item-class = cl_gui_column_tree=>item_class_text.
item-text = 'Computer Universe'.
APPEND item TO item_table.

* Option 2 for Folder software:
node-node_key = 'OPT2'.
node-relatkey = 'NODE1'.
node-relatship = cl_gui_column_tree=>relat_last_child.
node-n_image = icon_execute_object.
node-expander = 'X'.
APPEND node TO node_table.

item-node_key = 'OPT2'.
item-class = cl_gui_column_tree=>item_class_text.
item-text = 'Downloads - CNET'.
APPEND item TO item_table.

* Option 3 for Folder software:
node-node_key = 'OPT3'.
node-relatkey = 'NODE1'.
node-relatship = cl_gui_column_tree=>relat_last_child.
node-n_image = icon_execute_object.
node-expander = 'X'.
APPEND node TO node_table.

item-node_key = 'OPT3'.
item-class = cl_gui_column_tree=>item_class_text.
item-text = 'JavaScript source'.
APPEND item TO item_table.


* This is the second folder (travels)
node-node_key = 'NODE2'.
node-relatkey = 'Root'.
node-relatship = cl_gui_column_tree=>relat_last_child.
node-hidden = ' '.
node-disabled = ' '.
node-n_image = icon_closed_folder.
node-exp_image = icon_open_folder.
APPEND node TO node_table.

item-node_key = 'NODE2'.
item-class = cl_gui_column_tree=>item_class_text.
item-text = 'Travels'.
APPEND item TO item_table.

* Option 1 for Folder travels
node-node_key = 'OPT4'.
node-relatkey = 'NODE2'.
node-relatship = cl_gui_column_tree=>relat_last_child.
node-n_image = icon_execute_object.
node-expander = 'X'.
APPEND node TO node_table.

item-node_key = 'OPT4'.
item-class = cl_gui_column_tree=>item_class_text.
item-text = 'Air Berlin'.
APPEND item TO item_table.

* Option 2 for Folder travels
node-node_key = 'OPT5'.
node-relatkey = 'NODE2'.
node-relatship = cl_gui_column_tree=>relat_last_child.
node-n_image = icon_execute_object.
node-expander = 'X'.
APPEND node TO node_table.

item-node_key = 'OPT5'.
item-class = cl_gui_column_tree=>item_class_text.
item-text = 'Condor'.
APPEND item TO item_table.

* This is the third Folder (SAP help)
node-node_key = 'NODE3'.
node-relatkey = 'Root'.
node-relatship = cl_gui_column_tree=>relat_last_child.
node-hidden = ' '.
node-disabled = ' '.
node-n_image = icon_closed_folder.
node-exp_image = icon_open_folder.
APPEND node TO node_table.

item-node_key = 'NODE3'.
item-class = cl_gui_column_tree=>item_class_text.
item-text = 'SAP help'.
APPEND item TO item_table.

* Option 1 for Folder SAP help
node-node_key = 'OPT6'.
node-relatkey = 'NODE3'.
node-relatship = cl_gui_column_tree=>relat_last_child.
node-n_image = icon_execute_object.
node-expander = 'X'.
APPEND node TO node_table.

item-node_key = 'OPT6'.
item-class = cl_gui_column_tree=>item_class_text.
item-text = 'ABAP-Fans'.
APPEND item TO item_table.

ENDFORM.


*-----------------------------------------------------------------------
* Module STATUS_0100 OUTPUT
*-----------------------------------------------------------------------
MODULE status_0100 OUTPUT.

SET PF-STATUS 'STATUS-100'.
SET TITLEBAR '100'.

IF my_tree IS INITIAL.
PERFORM crea_arbol.
PERFORM create_browser.
ENDIF.

ENDMODULE.


*-----------------------------------------------------------------------
* Module USER_COMMAND_0100 INPUT
*-----------------------------------------------------------------------
MODULE user_command_0100 INPUT.

CASE okcode.
WHEN 'CANC' OR 'BACK'.
LEAVE PROGRAM.

WHEN 'EXPAND'.
PERFORM expand_nodes.

WHEN 'COLLAPSE'.
PERFORM collapse_nodes.

WHEN 'NEWURL'.
CALL FUNCTION 'POPUP_TO_FILL_COMMAND_LINE'
EXPORTING
popuptitle = 'New web site'
text1 = 'Enter the URL Address'
IMPORTING
command_line = url.
IF sy-ucomm = 'WEIT'.
PERFORM display_url.
ENDIF.

WHEN 'PREVIOUS'.
CALL METHOD browser->go_back.

WHEN 'NEXT'.
CALL METHOD browser->go_forward.
ENDCASE.


CLEAR: okcode, url, my_node.

* What action must take place when double-clicking on a node ??
CALL METHOD cl_gui_cfw=>dispatch.

CASE my_node.
WHEN 'OPT1'.
url = 'http://www.computeruniverse.net/imaging.asp'.
PERFORM display_url.

WHEN 'OPT2'.
url = 'http://download.cnet.com/'.
PERFORM display_url.

WHEN 'OPT3'.
url = 'http://javascript.internet.com/'.
PERFORM display_url.

WHEN 'OPT4'.
url = 'http://www.airberlin.com/'.
PERFORM display_url.

WHEN 'OPT5'.
url = 'http://www.thomascook-airlines.de/'.
PERFORM display_url.

WHEN 'OPT6'.
url = 'http://www.abap-fans.de/index3.htm'.
PERFORM display_url.
ENDCASE.

ENDMODULE.


*----------------------------------------------------------------------
* Expand all nodes
*----------------------------------------------------------------------
FORM expand_nodes.

CALL METHOD my_tree->expand_node
EXPORTING
node_key = 'OPT3'.

CALL METHOD my_tree->expand_node
EXPORTING
node_key = 'OPT5'.

CALL METHOD my_tree->expand_node
EXPORTING
node_key = 'OPT6'.

ENDFORM.


*----------------------------------------------------------------------
* Collapse all nodes (except the root node)
*----------------------------------------------------------------------
FORM collapse_nodes.

CALL METHOD my_tree->collapse_all_nodes.
CALL METHOD my_tree->expand_root_nodes.

ENDFORM.



*----------------------------------------------------------------------
* Display the URL of the selected node
*----------------------------------------------------------------------
FORM display_url.

CALL METHOD browser->show_url
EXPORTING
url = url
EXCEPTIONS
cntl_error = 1.

ENDFORM.

Another Type of Nodes Tree


* ----------------------------------------------------------------------
* This program is another type of nodes tree (hierarchical nodes) for
* displaying information on the screen related in some way father-son.
* No need to create a screen nor draw objects on it, but it is desi-
* rable to create a GUI-status (STATUS-100) with some buttons for
* user interaction.
* ----------------------------------------------------------------------
REPORT ztest NO STANDARD PAGE HEADING.

TABLES: sflight, scarr.

DATA: BEGIN OF mytable OCCURS 100,
CARRID like sflight-CARRID,
FLDATE like sflight-FLDATE,
PRICE like sflight-PRICE,
CURRENCY like sflight-CURRENCY,
PLANETYPE like sflight-PLANETYPE,
SEATSMAX like sflight-SEATSMAX,
END OF mytable.


DATA: text1(50), text2(50), text3(50), text4(50),
color1 TYPE i, color2 TYPE i, color3 TYPE i, color4 TYPE i,
level TYPE i,
len1 TYPE i, len2 TYPE i, len3 TYPE i, len4 TYPE i,
mytree TYPE TABLE OF snodetext WITH HEADER LINE.


* ----------------------------------------------------------------------
* Selection screen
* ----------------------------------------------------------------------
SELECTION-SCREEN: BEGIN OF BLOCK marco WITH FRAME TITLE text-001,
SKIP.

SELECT-OPTIONS: pcarrid FOR sflight-carrid,
pfldate FOR sflight-fldate.
SELECTION-SCREEN: END OF BLOCK marco.



* ----------------------------------------------------------------------
* Read data
* ----------------------------------------------------------------------
SELECT carrid fldate price currency planetype SEATSMAX
FROM sflight
INTO CORRESPONDING FIELDS OF TABLE mytable
WHERE carrid IN pcarrid
AND fldate IN pfldate.


* ----------------------------------------------------------------------
* Build the tree
* ----------------------------------------------------------------------
SORT mytable BY carrid fldate.
LOOP AT mytable.
AT FIRST.
text1 = 'Listing of Airlines'.
len1 = 50.
color1 = 2.
level = 1.
PERFORM fill_treetab.
ENDAT.

AT NEW carrid.
select single CARRNAME from scarr
into scarr-CARRNAME
where carrid = mytable-carrid.
CONCATENATE mytable-carrid scarr-CARRNAME
INTO text1 separated by space.
len1 = 25.
color1 = 3.
level = 2.
PERFORM fill_treetab.
ENDAT.

AT NEW fldate.
concatenate 'Flight date: ' mytable-fldate+6(2) mytable-fldate+4(2)
mytable-fldate+0(4) into text1 separated by space.
len1 = 22.
color1 = 4.
level = 3.
PERFORM fill_treetab.
ENDAT.

text1 = mytable-price.
condense text1.
len1 = 12.
color1 = 5.
text2 = mytable-currency.
len2 = 5.
color2 = 5.
text3 = mytable-planetype.
len3 = 10.
color3 = 5.
text4 = mytable-SEATSMAX.
condense text4.
len4 = 7.
color4 = 5.
level = 4.
PERFORM fill_treetab.

ENDLOOP.


*----------------------------------------------------------------------
* Build a hierarchy here
*----------------------------------------------------------------------
CALL FUNCTION 'RS_TREE_CONSTRUCT'
TABLES
nodetab = mytree
EXCEPTIONS
tree_failure = 1.


SET PF-STATUS 'STATUS-100'.

*----------------------------------------------------------------------
* Display the created hierarchy
*----------------------------------------------------------------------
CALL FUNCTION 'RS_TREE_LIST_DISPLAY'
EXPORTING
callback_program = 'ZTEST'
callback_user_command = 'SELECT_NODE'
return_marked_subtree = 'X'
layout_mode = 'X'.



*----------------------------------------------------------------------
* User interaction when working with the nodes tree
*----------------------------------------------------------------------
FORM select_node TABLES knoten STRUCTURE seucomm
USING command
CHANGING exit list_refresh.

exit = ' '.
list_refresh = 'X'.

CASE command.
WHEN 'EXPAND'.
CALL FUNCTION 'RS_TREE_EXPAND'
EXPORTING
node_id = 1
all = 'X'
EXCEPTIONS
not_found = 1
OTHERS = 2.

WHEN 'COLLAPSE'.
CALL FUNCTION 'RS_TREE_COMPRESS'
EXPORTING
node_id = 1
EXCEPTIONS
not_found = 1
OTHERS = 2.

ENDCASE.

ENDFORM.


*----------------------------------------------------------------------
* FORM FILL_TREETAB
*----------------------------------------------------------------------
FORM fill_treetab.

mytree-name = text1.
mytree-color = color1.
mytree-intensiv = '1'.

mytree-text = text1.
mytree-tlength = len1.
mytree-tlevel = level.
mytree-tcolor = color1.


mytree-text1 = text2.
mytree-tlength1 = len2.
mytree-tlevel = level.
mytree-tcolor1 = color2.

mytree-text2 = text3.
mytree-tlength2 = len3.
mytree-tlevel = level.
mytree-tcolor2 = color3.

mytree-text3 = text4.
mytree-tlength3 = len4.
mytree-tlevel = level.
mytree-tcolor3 = color4.

APPEND mytree.

CLEAR: text1, text2, text3, text4,
color1, color2, color3, color4.

ENDFORM.

Report to Create an Options Tree


*---------------------------------------------------------------------
* This report creates an options tree, similar to SAP´s Main Menu.
* Don´t forget to define a PF-STATUS 'STATUS_100' which contains
* a BACK button, a CANC button and 2 Application toolbar buttons:
* EXPAN & COLLA
*---------------------------------------------------------------------
REPORT ztest.


DATA: node TYPE treev_node,
node_table TYPE treev_ntab,
item_table LIKE STANDARD TABLE OF mtreeitm WITH DEFAULT KEY,
item TYPE mtreeitm,
okcode LIKE sy-ucomm,
url(255).

CLASS lcl_application DEFINITION DEFERRED.
CLASS cl_gui_cfw DEFINITION LOAD.

DATA: g_application TYPE REF TO lcl_application,
my_tree TYPE REF TO cl_gui_column_tree,
container_1 TYPE REF TO cl_gui_container,
container_2 TYPE REF TO cl_gui_container,
picture TYPE REF TO cl_gui_picture,
splitter TYPE REF TO cl_gui_splitter_container.


DATA: event TYPE cntl_simple_event,
events TYPE cntl_simple_events,
hierarchy_header TYPE treev_hhdr,
my_node TYPE tv_nodekey.


INCLUDE .

* Necessary Class definition for Tree
CLASS lcl_application DEFINITION.
PUBLIC SECTION.

METHODS:
handle_node_double_click
FOR EVENT node_double_click
OF cl_gui_column_tree
IMPORTING node_key,

handle_item_double_click
FOR EVENT item_double_click
OF cl_gui_column_tree
IMPORTING node_key,

handle_header_click
FOR EVENT header_click
OF cl_gui_column_tree
IMPORTING header_name.
ENDCLASS.



* Necessary Class implementation for Tree
CLASS lcl_application IMPLEMENTATION.

METHOD handle_node_double_click.
my_node = node_key.
ENDMETHOD.

METHOD handle_item_double_click.
my_node = node_key.
ENDMETHOD.

METHOD handle_header_click.
my_node = header_name.
ENDMETHOD.

ENDCLASS.


START-OF-SELECTION.
CALL SCREEN 0100.


*-----------------------------------------------------------------------
* Create the tree
*-----------------------------------------------------------------------
FORM crea_arbol.

* Split the container in 2 parts
CREATE OBJECT splitter
EXPORTING
parent = cl_gui_container=>screen0
rows = 1
columns = 2.

CALL METHOD splitter->set_border
EXPORTING
border = cl_gui_cfw=>true.

CALL METHOD splitter->set_column_mode
EXPORTING
mode = splitter->mode_absolute.

CALL METHOD splitter->set_column_width
EXPORTING
id = 1
width = 330.


* Assign a name to every column
CALL METHOD splitter->get_container
EXPORTING
row = 1
column = 1
RECEIVING
container = container_1.

CALL METHOD splitter->get_container
EXPORTING
row = 1
column = 2
RECEIVING
container = container_2.


* Container´s header
hierarchy_header-heading = 'This can be a subtitle'.
hierarchy_header-width = 50.


* Create the tree
CREATE OBJECT my_tree
EXPORTING
parent = container_1
node_selection_mode = cl_gui_column_tree=>node_sel_mode_single
item_selection = 'X'
hierarchy_column_name = 'Column1'
hierarchy_header = hierarchy_header
EXCEPTIONS
cntl_system_error = 1
create_error = 2
failed = 3
illegal_node_selection_mode = 4
illegal_column_name = 5
lifetime_error = 6.


* Event double click
event-eventid = cl_gui_column_tree=>eventid_node_double_click.
event-appl_event = 'X'.
APPEND event TO events.

* item double click
event-eventid = cl_gui_column_tree=>eventid_item_double_click.
event-appl_event = 'X'.
APPEND event TO events.

* header click
event-eventid = cl_gui_column_tree=>eventid_header_click.
event-appl_event = 'X'.
APPEND event TO events.


CALL METHOD my_tree->set_registered_events
EXPORTING
events = events
EXCEPTIONS
cntl_error = 1
cntl_system_error = 2
illegal_event_combination = 3.


* assign event handlers in the application class to each desired event
CREATE OBJECT g_application.
SET HANDLER g_application->handle_node_double_click FOR my_tree.
SET HANDLER g_application->handle_item_double_click FOR my_tree.
SET HANDLER g_application->handle_header_click FOR my_tree.


* Create the tree´s nodes
PERFORM create_nodes.

* Add nodes to the tree
CALL METHOD my_tree->add_nodes_and_items
EXPORTING
node_table = node_table
item_table = item_table
item_table_structure_name = 'MTREEITM'
EXCEPTIONS
failed = 1
cntl_system_error = 3
error_in_tables = 4
dp_error = 5
table_structure_name_not_found = 6.


* Expand Root node when loading the program
CALL METHOD my_tree->expand_root_nodes.

ENDFORM.


*----------------------------------------------------------------------
* Creation of the image on the right side of the screen. There are
* 5 different options:
* 1- Centers the image in the container
* 2- Normal mode
* 3- Image stretches in the container
* 4- If the size of the container changes, so will change the image
* 5- Centers and stretches the image to adapt to the container
*----------------------------------------------------------------------
FORM create_image.

CREATE OBJECT picture
EXPORTING
parent = container_2
EXCEPTIONS
error = 1.

CLEAR url.
PERFORM load_pic_from_db CHANGING url.

CALL METHOD picture->load_picture_from_url
EXPORTING
url = url.

CALL METHOD picture->set_display_mode
EXPORTING
display_mode = cl_gui_picture=>display_mode_normal_center.
* display_mode = cl_gui_picture=>display_mode_normal.
* display_mode = cl_gui_picture=>display_mode_stretch.
* display_mode = cl_gui_picture=>display_mode_fit.
* display_mode = cl_gui_picture=>display_mode_fit_center.

ENDFORM.


*----------------------------------------------------------------------
* Where´s the location of the image that is going to be displayed
* on the right side of the screen ??
* You can choose pictures in transaction SMW0.
*----------------------------------------------------------------------
FORM load_pic_from_db CHANGING url.

DATA: query_table LIKE w3query OCCURS 1 WITH HEADER LINE,
html_table LIKE w3html OCCURS 1,
pic_data LIKE w3mime OCCURS 0,
return_code LIKE w3param-ret_code,
content_length LIKE w3param-cont_len,
content_type LIKE w3param-cont_type.


REFRESH query_table.
query_table-name = '_OBJECT_ID'.
query_table-value = 'CTS_LOGO.GIF'. "<-- change the name of the
APPEND query_table. "<-- image to display yours


CALL FUNCTION 'WWW_GET_MIME_OBJECT'
TABLES
query_string = query_table
html = html_table
mime = pic_data
CHANGING
return_code = return_code
content_type = content_type
content_length = content_length
EXCEPTIONS
object_not_found = 1
parameter_not_found = 2
OTHERS = 3.


CALL FUNCTION 'DP_CREATE_URL'
EXPORTING
type = 'image'
subtype = cndp_sap_tab_unknown
size = content_length
lifetime = cndp_lifetime_transaction
TABLES
data = pic_data
CHANGING
url = url
EXCEPTIONS
OTHERS = 1.

ENDFORM.


*-----------------------------------------------------------------------
* Let´s create the nodes. Check the logical sequence on concatenation
* between the upper node and the lower node.
* Warning: upper and lower case affect when defining the node´s name
*-----------------------------------------------------------------------
FORM create_nodes.

* This is Root node
node-node_key = 'Root'.
CLEAR node-relatkey.
CLEAR node-relatship.
node-hidden = ' '.
node-disabled = ' '.
node-isfolder = 'X'.
node-n_image = icon_protocol.
node-exp_image = icon_protocol.
APPEND node TO node_table.

CLEAR item.
item-node_key = 'Root'.
item-item_name = 'Column1'.
item-class = cl_gui_column_tree=>item_class_text.
item-text = 'Options tree'.
APPEND item TO item_table.


* This is the first node (Folder 1)
node-node_key = 'NODE1'.
node-relatkey = 'Root'.
node-relatship = cl_gui_column_tree=>relat_last_child.
node-n_image = icon_closed_folder.
node-exp_image = icon_open_folder.
node-expander = 'X'.
APPEND node TO node_table.

item-node_key = 'NODE1'.
item-class = cl_gui_column_tree=>item_class_text.
item-text = 'Folder 1'.
APPEND item TO item_table.

* Option 1 for Folder 1
node-node_key = 'OPT1'.
node-relatkey = 'NODE1'.
node-relatship = cl_gui_column_tree=>relat_last_child.
node-n_image = icon_closed_folder. " close folder
node-exp_image = icon_open_folder. " open folder
node-expander = 'X'.
APPEND node TO node_table.

item-node_key = 'OPT1'.
item-class = cl_gui_column_tree=>item_class_text.
item-text = 'Subfolder'.
APPEND item TO item_table.

* Option 2 for Folder 1
node-node_key = 'OPT2'.
node-relatkey = 'OPT1'.
node-relatship = cl_gui_column_tree=>relat_last_child.
node-n_image = icon_execute_object.
node-expander = 'X'.
APPEND node TO node_table.

item-node_key = 'OPT2'.
item-class = cl_gui_column_tree=>item_class_text.
item-text = 'Item 1'.
APPEND item TO item_table.


* This is the second node (Folder 2)
node-node_key = 'NODE2'.
node-relatkey = 'Root'.
node-relatship = cl_gui_column_tree=>relat_last_child.
node-hidden = ' '.
node-disabled = ' '.
node-n_image = icon_closed_folder.
node-exp_image = icon_open_folder.
APPEND node TO node_table.

item-node_key = 'NODE2'.
item-class = cl_gui_column_tree=>item_class_text.
item-text = 'Folder 2'.
APPEND item TO item_table.

* Option 1 for Folder 2
node-node_key = 'OPT3'.
node-relatkey = 'NODE2'.
node-relatship = cl_gui_column_tree=>relat_last_child.
node-n_image = icon_execute_object.
node-expander = 'X'.
APPEND node TO node_table.

item-node_key = 'OPT3'.
item-class = cl_gui_column_tree=>item_class_text.
item-text = 'Item 2'.
APPEND item TO item_table.

ENDFORM.


*-----------------------------------------------------------------------
* Module STATUS_0100 OUTPUT
*-----------------------------------------------------------------------
MODULE status_0100 OUTPUT.

SET PF-STATUS 'STATUS-100'.
SET TITLEBAR '100'.

IF my_tree IS INITIAL.
PERFORM crea_arbol.
PERFORM create_image.
ENDIF.

ENDMODULE.


*-----------------------------------------------------------------------
* Module USER_COMMAND_0100 INPUT
*-----------------------------------------------------------------------
MODULE user_command_0100 INPUT.

CASE okcode.
WHEN 'CANC' OR 'BACK'.
LEAVE PROGRAM.
WHEN 'EXPAN'.
PERFORM expander_nodos.
WHEN 'COLLA'.
PERFORM contraer_nodos.
ENDCASE.

CLEAR: okcode, my_node.

* What action must take place when double-clicking on a node ??
CALL METHOD cl_gui_cfw=>dispatch.

CASE my_node.
WHEN 'OPT2'.
CALL SCREEN 0200.
WHEN 'OPT3'.
CALL transaction 'SE11'.
WHEN 'HierarchyHea'.
* do nothing
ENDCASE.

ENDMODULE.


*----------------------------------------------------------------------
* Expand all nodes
*----------------------------------------------------------------------
FORM expander_nodos.

CALL METHOD my_tree->expand_node
EXPORTING
node_key = 'OPT2'.

CALL METHOD my_tree->expand_node
EXPORTING
node_key = 'OPT3'.

ENDFORM.


*----------------------------------------------------------------------
* Collapse all nodes
*----------------------------------------------------------------------
FORM contraer_nodos.

CALL METHOD my_tree->collapse_all_nodes.
CALL METHOD my_tree->expand_root_nodes.

ENDFORM.



*-----------------------------------------------------------------------
* Module USER_COMMAND_0200 INPUT
*-----------------------------------------------------------------------
MODULE user_command_0200 INPUT.

LEAVE TO SCREEN 0100.

ENDMODULE.

*-----------------------------------------------------------------------
* Module STATUS_0200 OUTPUT
*-----------------------------------------------------------------------
MODULE status_0200 OUTPUT.

SET PF-STATUS 'STATUS-200'.
SET TITLEBAR '200'.

ENDMODULE.

Report using the ALV Grid Function - 004


*-----------------------------------------------------------------------
* Report using the ALV Grid function.
* All you have to do is create a screen named 0100 and define the
* content of pf-status 'STATUS-100' and titlebar '100'.
* A custom control named CONTAINER is also necessary.
*-----------------------------------------------------------------------
REPORT ztest NO STANDARD PAGE HEADING.

TABLES: sflight.


DATA: BEGIN OF mytable OCCURS 0.
INCLUDE STRUCTURE sflight.
DATA END OF mytable.

DATA:
okcode LIKE sy-ucomm,
record(5) type c,
mygrid TYPE REF TO cl_gui_alv_grid,
mycontainer TYPE scrfname VALUE 'CONTAINER',
custom_container TYPE REF TO cl_gui_custom_container,
field_catal TYPE lvc_t_fcat WITH HEADER LINE.


*-----------------------------------------------------------------------
* Read data from SFLIGHT
*-----------------------------------------------------------------------
START-OF-SELECTION.
SELECT * FROM sflight
UP TO 30 ROWS
INTO CORRESPONDING FIELDS OF TABLE mytable.


CALL SCREEN 0100.


*-----------------------------------------------------------------------
* Module PAI INPUT
*-----------------------------------------------------------------------
MODULE user_command_0100.

* to react on oi_custom_events:
CALL METHOD cl_gui_cfw=>dispatch.

CASE okcode.
WHEN 'CANC' OR 'BACK'.
SET SCREEN 0.
WHEN OTHERS.
* LEAVE PROGRAM.
ENDCASE.

ENDMODULE.


*-----------------------------------------------------------------------
* Module PBO OUTPUT
*-----------------------------------------------------------------------
MODULE status_0100 OUTPUT.

SET PF-STATUS 'STATUS-100'.
SET TITLEBAR '100'.

IF custom_container IS INITIAL.
PERFORM build_field_catal.
describe table mytable lines record.

CREATE OBJECT custom_container
EXPORTING
container_name = mycontainer.

CREATE OBJECT mygrid
EXPORTING
i_appl_events = 'X'
i_parent = custom_container.

CALL METHOD mygrid->set_table_for_first_display
CHANGING
it_outtab = mytable[]
it_fieldcatalog = field_catal[]
EXCEPTIONS
invalid_parameter_combination = 1
program_error = 2
too_many_lines = 3
OTHERS = 4.

IF sy-subrc NE '0'.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
STOP.
ENDIF.

CALL METHOD mygrid->set_toolbar_interactive.

ELSE.
CALL METHOD mygrid->refresh_table_display.
ENDIF.

ENDMODULE.


*-----------------------------------------------------------------------
* Build the field catalogue
*-----------------------------------------------------------------------
FORM build_field_catal.

REFRESH field_catal.
CLEAR field_catal.

field_catal-fieldname = 'CARRID'.
field_catal-ref_field = 'CARRID'.
field_catal-scrtext_l = 'Airline carrier'.
field_catal-fix_column = 'X'.
APPEND field_catal.
CLEAR field_catal.

field_catal-fieldname = 'CONNID'.
field_catal-ref_field = 'CONNID'.
field_catal-scrtext_l = 'Flight connection'.
field_catal-fix_column = 'X'.
APPEND field_catal.
CLEAR field_catal.

field_catal-fieldname = 'FLDATE'.
field_catal-ref_field = 'FLDATE'.
field_catal-scrtext_l = 'Flight date'.
field_catal-fix_column = 'X'.
APPEND field_catal.
CLEAR field_catal.

field_catal-fieldname = 'PRICE'.
field_catal-ref_field = 'PRICE'.
field_catal-scrtext_l = 'Airfare'.
APPEND field_catal.
CLEAR field_catal.

field_catal-fieldname = 'CURRENCY'.
field_catal-ref_field = 'CURRENCY'.
field_catal-scrtext_l = 'Local currency'.
field_catal-outputlen = 10.
APPEND field_catal.
CLEAR field_catal.

field_catal-fieldname = 'PLANETYPE'.
field_catal-ref_field = 'PLANETYPE'.
field_catal-scrtext_l = 'Plane type'.
field_catal-outputlen = 10.
APPEND field_catal.
CLEAR field_catal.

field_catal-fieldname = 'SEATSMAX'.
field_catal-ref_field = 'SEATSMAX'.
field_catal-scrtext_l = 'Maximum capacity'.
field_catal-outputlen = 10.
APPEND field_catal.
CLEAR field_catal.

field_catal-fieldname = 'SEATSOCC'.
field_catal-ref_field = 'SEATSOCC'.
field_catal-scrtext_l = 'Occupied seats'.
field_catal-outputlen = 10.
APPEND field_catal.
CLEAR field_catal.

field_catal-fieldname = 'PAYMENTSUM'.
field_catal-ref_field = 'PAYMENTSUM'.
field_catal-scrtext_l = 'Total current bookings'.
field_catal-outputlen = 15.
APPEND field_catal.
CLEAR field_catal.

ENDFORM.

Report using the ALV Grid Function - 003


*-----------------------------------------------------------------------
* Report using the ALV Grid function.
* All you have to do is create a screen named 0100 and define the
* content of pf-status 'STATUS-100' and titlebar '100'.
* When the grid with the data shows up, press the new button on the
* toolbar and then, make a double click on the grid.
*-----------------------------------------------------------------------
REPORT ztest NO STANDARD PAGE HEADING.

TABLES: sflight.


DATA: BEGIN OF mytable OCCURS 0.
INCLUDE STRUCTURE sflight.
DATA: icon_id TYPE icons-text,
line_color(4) TYPE c,
END OF mytable.

INCLUDE .
DATA:
aux_table LIKE mytable,
okcode LIKE sy-ucomm,
mygrid TYPE REF TO cl_gui_alv_grid,
mycontainer TYPE REF TO cl_gui_custom_container,
mylayout TYPE lvc_s_layo,
field_catal TYPE lvc_t_fcat,
fieldcat_aux TYPE lvc_s_fcat,
new_button TYPE stb_button,
new_button2 TYPE stb_button,
save_button TYPE stb_button,
index_rows TYPE lvc_t_row,
sel_row LIKE lvc_s_row,
line TYPE i.

* Class Definition
CLASS myevent_receiv_001 DEFINITION DEFERRED. "for Toolbar button
CLASS myevent_receiv_002 DEFINITION DEFERRED. "for double click

* Event Declaration
DATA: user_event_receiver TYPE REF TO myevent_receiv_001,
user_event_receiver_002 TYPE REF TO myevent_receiv_002.


INCLUDE ztest.


*-----------------------------------------------------------------------
* Read data from SFLIGHT
*-----------------------------------------------------------------------
START-OF-SELECTION.
SELECT * FROM sflight
INTO TABLE mytable
WHERE carrid IN ('LH', 'AZ', 'AA')
ORDER BY carrid connid.


* Add an Icon to the internal table
LOOP AT mytable.
CALL FUNCTION 'ICON_CREATE'
EXPORTING
name = 'ICON_GREEN_LIGHT'
info = 'Not ready'
IMPORTING
result = mytable-icon_id.
MODIFY mytable TRANSPORTING icon_id.
ENDLOOP.

CALL SCREEN 0100.


*-----------------------------------------------------------------------
* Module PAI INPUT
*-----------------------------------------------------------------------
MODULE user_command_0100.

CASE okcode.
WHEN 'CANC' OR 'BACK'.
CALL METHOD mygrid->free.
LEAVE PROGRAM.
WHEN OTHERS.
CALL METHOD cl_gui_cfw=>dispatch.
ENDCASE.

ENDMODULE.


*-----------------------------------------------------------------------
* Module PBO OUTPUT
*-----------------------------------------------------------------------
MODULE status_0100 OUTPUT.

SET PF-STATUS 'STATUS-100'.
SET TITLEBAR '100'.
PERFORM build_field_catal.

IF mygrid IS INITIAL.

PERFORM build_layout.
PERFORM set_color.

CREATE OBJECT mygrid
EXPORTING i_parent = mycontainer.

CALL METHOD mygrid->set_table_for_first_display
EXPORTING
is_layout = mylayout
CHANGING
it_outtab = mytable[]
it_fieldcatalog = field_catal[]
EXCEPTIONS
invalid_parameter_combination = 1
program_error = 2
too_many_lines = 3
OTHERS = 4.

IF sy-subrc NE '0'.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
STOP.
ENDIF.

* Select a line on the grid:
* APPEND 4 TO index_rows.
* CALL METHOD mygrid->set_selected_rows
* EXPORTING
* it_index_rows = index_rows.

*Event receiver for handling alv toolbar events:
CREATE OBJECT user_event_receiver.
SET HANDLER user_event_receiver->handle_user_command FOR mygrid.
SET HANDLER user_event_receiver->handle_toolbar FOR mygrid.
CALL METHOD mygrid->set_toolbar_interactive.

ELSE.
CALL METHOD mygrid->refresh_table_display.
ENDIF.

* handle double click event:
IF user_event_receiver_002 IS INITIAL.
CREATE OBJECT user_event_receiver_002.
SET HANDLER user_event_receiver_002->handle_double_click FOR mygrid.
ENDIF.

ENDMODULE.


*-----------------------------------------------------------------------
* Build the field catalogue
*-----------------------------------------------------------------------
FORM build_field_catal.

CALL FUNCTION 'LVC_FIELDCATALOG_MERGE'
EXPORTING
i_structure_name = 'SFLIGHT'
CHANGING
ct_fieldcat = field_catal.

LOOP AT field_catal INTO fieldcat_aux.
CASE fieldcat_aux-fieldname.

WHEN 'CARRID'.
fieldcat_aux-scrtext_l = 'Airline carrier'.
fieldcat_aux-fix_column = 'X'.

WHEN 'CONNID'.
fieldcat_aux-scrtext_l = 'Flight connection'.
fieldcat_aux-fix_column = 'X'.

WHEN 'FLDATE'.
fieldcat_aux-scrtext_l = 'Flight date'.
fieldcat_aux-fix_column = 'X'.

WHEN 'PRICE'.
fieldcat_aux-scrtext_l = 'Airfare'.
fieldcat_aux-do_sum = 'X'.

WHEN 'CURRENCY'.
fieldcat_aux-scrtext_l = 'Local currency'.
fieldcat_aux-outputlen = 10.

WHEN 'PLANETYPE'.
fieldcat_aux-scrtext_l = 'Plane type'.
fieldcat_aux-outputlen = 10.

WHEN 'SEATSMAX'.
fieldcat_aux-scrtext_l = 'Maximum capacity'.
fieldcat_aux-do_sum = 'X'.
fieldcat_aux-outputlen = 10.

WHEN 'SEATSOCC'.
fieldcat_aux-scrtext_l = 'Occupied seats'.
fieldcat_aux-do_sum = 'X'.
fieldcat_aux-outputlen = 10.

WHEN 'PAYMENTSUM'.
fieldcat_aux-scrtext_l = 'Total current bookings'.
fieldcat_aux-do_sum = 'X'.
fieldcat_aux-outputlen = 15.

ENDCASE.
MODIFY field_catal FROM fieldcat_aux.
ENDLOOP.

CLEAR fieldcat_aux.
fieldcat_aux-col_pos = 10.
fieldcat_aux-fieldname = 'ICON_ID'.
fieldcat_aux-tabname = 1.
fieldcat_aux-scrtext_l = 'Status'.
fieldcat_aux-outputlen = 5.
APPEND fieldcat_aux TO field_catal.

ENDFORM.


*-----------------------------------------------------------------------
* Build the grid´s layout
* SEL_MODE: determines how rows can be selected.
* A Multiple columns, multiple rows with selection buttons.
* B Simple selection, listbox, Single row/column
* C Multiple rows without buttons
* D Multiple rows with buttons and select all ICON
*-----------------------------------------------------------------------
FORM build_layout.

mylayout-grid_title = 'My Title here'.
mylayout-smalltitle = '3'.
* mylayout-no_hgridln = 'X'.
* mylayout-no_vgridln = 'X'.
* mylayout-no_headers = 'X'.
mylayout-cwidth_opt = 'X'.
* mylayout-no_rowmark = 'X'.
* mylayout-no_toolbar = 'X'.
mylayout-sel_mode = 'A'.
mylayout-edit = 'X'.
mylayout-totals_bef = 'X'.
* mylayout-no_totline = ' '.
* mylayout-keyhot = 'X'.
mylayout-info_fname = 'LINE_COLOR'.

ENDFORM.


*-----------------------------------------------------------------------
* Set color on certain lines:
* Char 1 = C --> This is a color property
* Char 2 --> Color code (1 - 7)
* Char 3 --> Intensified on/off (1 = on)
* Char 4 --> Inverse display (0 = off)
*-----------------------------------------------------------------------
FORM set_color.

LOOP AT mytable.
IF mytable-seatsocc < 60.
mytable-line_color = 'C610'.
MODIFY mytable.
ENDIF.
ENDLOOP.

ENDFORM.

ALV Grid and the Event


*-----------------------------------------------------------------------
* CLASS myevent_receiv_001 DEFINITION
* This class is used to define buttons inside the toolbar of the ALV
* grid and the event when clicking on the toolbar.It defines a user
* command event and variable e_ucomm receives the value of the user
* command.
*-----------------------------------------------------------------------
CLASS myevent_receiv_001 DEFINITION.
PUBLIC SECTION.
METHODS :
handle_toolbar " Create toolbar button
FOR EVENT toolbar OF cl_gui_alv_grid
IMPORTING e_object e_interactive,

handle_user_command
FOR EVENT user_command OF cl_gui_alv_grid " Handling user
IMPORTING e_ucomm. " command for button

ENDCLASS.


*----------------------------------------------------------------------
* CLASS myevent_receiv_002 DEFINITION
* With this definition, we´ll define a double click event on the grid
* and catch the line and column where it happens with help of
* variables e_row and e_column.
*----------------------------------------------------------------------
CLASS myevent_receiv_002 DEFINITION.
PUBLIC SECTION.
METHODS handle_double_click
FOR EVENT double_click OF cl_gui_alv_grid
IMPORTING
e_row
e_column.
ENDCLASS.

*----------------------------------------------------------------------
* CLASS myevent_receiv_001 IMPLEMENTATION
* Implementation of a button inside Toolbar and a SY-UCOMM function
* assigned to it.
* Try to change line MOVE '3' TO new_button-butn_type from 1 to 5
* and see what happens
*----------------------------------------------------------------------
CLASS myevent_receiv_001 IMPLEMENTATION.

METHOD handle_toolbar.
CLEAR: new_button.

* append a separator to normal toolbar to split the
* new buttons from the standard buttons:
MOVE '3' TO new_button-butn_type.
APPEND new_button TO e_object->mt_toolbar.
CLEAR new_button.

* first button:
MOVE: 'Action' TO new_button-function,
'Action button' TO new_button-quickinfo,
'Some action' TO new_button-text,
' ' TO new_button-disabled.
APPEND new_button TO e_object->mt_toolbar.
CLEAR new_button.

* Second button:
MOVE: 'Action 2' TO new_button-function,
'Action 2 button' TO new_button-quickinfo,
'Some action 2' TO new_button-text,
' ' TO new_button-disabled.
APPEND new_button TO e_object->mt_toolbar.
CLEAR new_button.

* Third button:
MOVE: 'Save' TO new_button-function, "<- Save button
'Save data' TO new_button-quickinfo,
icon_system_save TO new_button-icon,
'5' TO new_button-butn_type,
' ' TO new_button-disabled.
APPEND new_button TO e_object->mt_toolbar.
ENDMETHOD.


* What should happen when the user presses any button ??
METHOD handle_user_command.
CASE: e_ucomm.
WHEN 'Save'.
PERFORM save_records.
WHEN 'Action'.
MESSAGE ID 'MJ' TYPE 'I' NUMBER 100 WITH
'Some message or action hier'.
WHEN 'Action 2'.
PERFORM selected_rows.
WHEN OTHERS.
* do nothing
ENDCASE.
ENDMETHOD.

ENDCLASS.

*----------------------------------------------------------------------
* CLASS myevent_receiv_002 IMPLEMENTATION
* We define a method for doble click on the grid. Where did the
* user make double click on the grid, can be determine with
* e_row-index and e_column, which were declared in CLASS
* myevent_receiv_002 DEFINITION
*----------------------------------------------------------------------
CLASS myevent_receiv_002 IMPLEMENTATION.

METHOD handle_double_click.
DATA mymessage(50) TYPE c.
CONCATENATE 'Click on line: ' e_row-index ' and column: ' e_column
INTO mymessage.
MESSAGE i100(mj) WITH mymessage.

READ TABLE mytable INDEX e_row-index INTO aux_table.
aux_table-icon_id = '@0A@'. " <-- Red light
MODIFY mytable FROM aux_table
INDEX e_row-index
TRANSPORTING icon_id.
CALL METHOD mygrid->refresh_table_display.
e_row-index = ' '.
e_column = ' '.
ENDMETHOD.

ENDCLASS.




*-----------------------------------------------------------------------
* Which rows were selected ??
*-----------------------------------------------------------------------
FORM selected_rows.

CLEAR index_rows.
DATA: sel_line(20) TYPE c.

CALL METHOD mygrid->get_selected_rows
IMPORTING
et_index_rows = index_rows.

DESCRIBE TABLE index_rows LINES line.
IF line = 0.
MESSAGE s100(mj) WITH 'Select at least 1 line'.
EXIT.
ENDIF.


LOOP AT index_rows INTO sel_row.
SHIFT sel_row-index LEFT DELETING LEADING '0'.
CONCATENATE sel_row-index sel_line INTO sel_line
SEPARATED BY space.
ENDLOOP.
MESSAGE s100(mj) WITH 'Your selected row is' sel_line.


ENDFORM.


*-----------------------------------------------------------------------
* If any change in the table (Grid is edit capable) takes place,
* the new data is trapped hier and can be saved, processed and
* so on. Contrary to PAI Module, where the original data is still
* untouched, the content of the internal table has changed.
* Depending on your needs, you can save the content of the internal
* table to a database table with instructions like INSERT, UPDATE
* and MODIFY.
*-----------------------------------------------------------------------
FORM save_records.

CLEAR line.
DESCRIBE TABLE mytable LINES line.
IF line = 0.
MESSAGE w100(mj) WITH 'The internal table has no records'.
ENDIF.

* insert
* update
* modify

ENDFORM.

Report using the ALV Grid function - 002


*-----------------------------------------------------------------------
* Report using the ALV Grid function.
* All you have to do is create a screen named 0100 and define the
* content of pf-status 'STATUS-100' and titlebar '100'.
* When the grid with the data shows up, press the new button on the
* toolbar and then, make a double click on the grid.
*-----------------------------------------------------------------------
REPORT ztest NO STANDARD PAGE HEADING.

TABLES: sflight.


DATA: BEGIN OF mytable OCCURS 0.
INCLUDE STRUCTURE sflight.
DATA: icon_id TYPE icons-text,
line_color(4) TYPE c,
END OF mytable.

INCLUDE .

DATA: okcode LIKE sy-ucomm,
mygrid TYPE REF TO cl_gui_alv_grid,
mycontainer TYPE REF TO cl_gui_custom_container,
mylayout TYPE lvc_s_layo,
field_catal TYPE lvc_t_fcat,
fieldcat_aux TYPE lvc_s_fcat.




*-----------------------------------------------------------------------
* Read data from SFLIGHT
*-----------------------------------------------------------------------
START-OF-SELECTION.
SELECT * FROM sflight
INTO TABLE mytable
WHERE carrid IN ('LH', 'AZ', 'AA')
ORDER BY carrid connid.


* Add an Icon to the internal table
LOOP AT mytable.
CALL FUNCTION 'ICON_CREATE'
EXPORTING
name = 'ICON_GREEN_LIGHT'
info = 'Not ready'
IMPORTING
result = mytable-icon_id.
MODIFY mytable TRANSPORTING icon_id.
ENDLOOP.

CALL SCREEN 0100.


*-----------------------------------------------------------------------
* Module PAI INPUT
*-----------------------------------------------------------------------
MODULE user_command_0100.

CASE okcode.
WHEN 'CANC' OR 'BACK'.
leave program.
WHEN OTHERS.
* do nothing
ENDCASE.

ENDMODULE.


*-----------------------------------------------------------------------
* Module PBO OUTPUT
*-----------------------------------------------------------------------
MODULE status_0100 OUTPUT.

SET PF-STATUS 'STATUS-100'.
SET TITLEBAR '100'.
PERFORM build_field_catal.

IF mygrid IS INITIAL.

PERFORM build_layout.
PERFORM set_color.

CREATE OBJECT mygrid
EXPORTING i_parent = mycontainer.

CALL METHOD mygrid->set_table_for_first_display
EXPORTING
is_layout = mylayout
CHANGING
it_outtab = mytable[]
it_fieldcatalog = field_catal[]
EXCEPTIONS
invalid_parameter_combination = 1
program_error = 2
too_many_lines = 3
OTHERS = 4.

IF sy-subrc NE '0'.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
STOP.
ENDIF.


ELSE.
CALL METHOD mygrid->refresh_table_display.
ENDIF.

ENDMODULE.


*-----------------------------------------------------------------------
* Build the field catalogue
*-----------------------------------------------------------------------
FORM build_field_catal.

CALL FUNCTION 'LVC_FIELDCATALOG_MERGE'
EXPORTING
i_structure_name = 'SFLIGHT'
CHANGING
ct_fieldcat = field_catal.

LOOP AT field_catal INTO fieldcat_aux.
CASE fieldcat_aux-fieldname.

WHEN 'CARRID'.
fieldcat_aux-scrtext_l = 'Airline carrier'.
fieldcat_aux-fix_column = 'X'.

WHEN 'CONNID'.
fieldcat_aux-scrtext_l = 'Flight connection'.
fieldcat_aux-fix_column = 'X'.

WHEN 'FLDATE'.
fieldcat_aux-scrtext_l = 'Flight date'.
fieldcat_aux-fix_column = 'X'.

WHEN 'PRICE'.
fieldcat_aux-scrtext_l = 'Airfare'.
fieldcat_aux-do_sum = 'X'.

WHEN 'CURRENCY'.
fieldcat_aux-scrtext_l = 'Local currency'.
fieldcat_aux-outputlen = 10.
fieldcat_aux-emphasize = 'C210'.

WHEN 'PLANETYPE'.
fieldcat_aux-scrtext_l = 'Plane type'.
fieldcat_aux-outputlen = 10.

WHEN 'SEATSMAX'.
fieldcat_aux-scrtext_l = 'Maximum capacity'.
fieldcat_aux-do_sum = 'X'.
fieldcat_aux-outputlen = 10.

WHEN 'SEATSOCC'.
fieldcat_aux-scrtext_l = 'Occupied seats'.
fieldcat_aux-do_sum = 'X'.
fieldcat_aux-outputlen = 10.

WHEN 'PAYMENTSUM'.
fieldcat_aux-scrtext_l = 'Total current bookings'.
fieldcat_aux-do_sum = 'X'.
fieldcat_aux-outputlen = 15.

ENDCASE.
MODIFY field_catal FROM fieldcat_aux.
ENDLOOP.

CLEAR fieldcat_aux.
fieldcat_aux-col_pos = 10.
fieldcat_aux-fieldname = 'ICON_ID'.
fieldcat_aux-tabname = 1.
fieldcat_aux-scrtext_l = 'Status'.
fieldcat_aux-outputlen = 5.
APPEND fieldcat_aux TO field_catal.

ENDFORM.


*-----------------------------------------------------------------------
* Build the grid´s layout
* SEL_MODE: determines how rows can be selected.
* A Multiple columns, multiple rows with selection buttons.
* B Simple selection, listbox, Single row/column
* C Multiple rows without buttons
* D Multiple rows with buttons and select all ICON
*-----------------------------------------------------------------------
FORM build_layout.

mylayout-grid_title = 'My Title here'.
mylayout-smalltitle = '3'.
* mylayout-no_hgridln = 'X'.
* mylayout-no_vgridln = 'X'.
* mylayout-no_headers = 'X'.
mylayout-cwidth_opt = 'X'.
* mylayout-no_rowmark = 'X'.
* mylayout-no_toolbar = 'X'.
mylayout-sel_mode = 'A'.
mylayout-edit = 'X'.
mylayout-totals_bef = 'X'.
* mylayout-no_totline = ' '.
* mylayout-keyhot = 'X'.
mylayout-info_fname = 'LINE_COLOR'.

ENDFORM.


*-----------------------------------------------------------------------
* Set color on certain lines:
* Char 1 = C --> This is a color property
* Char 2 --> Color code (1 - 7)
* Char 3 --> Intensified on/off (1 = on)
* Char 4 --> Inverse display (0 = off)
*-----------------------------------------------------------------------
FORM set_color.

LOOP AT mytable.
IF mytable-seatsocc < 60.
mytable-line_color = 'C610'.
MODIFY mytable.
ENDIF.
ENDLOOP.

ENDFORM.