Sei sulla pagina 1di 5

PROGRAM BCALVC_PRINT.

*&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
* Purpose:
* ~~~~~~~~
* This program illustrates how the events for print processing
* - PRINT_TOP_OF_PAGE
* - PRINT_END_OF_PAGE
* - PRINT_TOP_OF_LIST
* - PRINT_END_OF_LIST
*
* are handled. The corresponding handler methods control the
* appearance of the list printed.
*-----------------------------------------------------------------
* To check program behavior
* ~~~~~~~~~~~~~~~~~~~~~~~~~
* Print the list shown (It has got only three pages).
* Remark: If you choose "Druckansicht" (preview?!) before printing,
* the output for event PRINT_END_OF_PAGE is left out due
* to scrolling.
* Create a spool entry and preview your printout by calling
* TA sp01 to reduce paper output please.
*-----------------------------------------------------------------
* Essential steps (Search for '§')
* ~~~~~~~~~~~~~~~
* 1. Define a (local) class for event handling
* 2. Define a method for each print event you need.
* 3. Implement your event handler methods. Use WRITE to provide output.
* 4. Link used print events and event handler methods.
* 5. In case of PRINT_END_OF_PAGE, you must set 'reservelns' to
* the number of reserved lines at the end of a page.
*&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
&

*********
* Predefine a local class for event handling to allow the
* declaration of a reference variable.
CLASS LCL_EVENT_RECEIVER DEFINITION DEFERRED.
*
*********

DATA: OK_CODE LIKE SY-UCOMM,


g_max type i value 100,
GT_SFLIGHT TYPE TABLE OF SFLIGHT,
G_REPID LIKE SY-REPID,
GS_PRINT TYPE LVC_S_PRNT,
GS_LAYOUT TYPE LVC_S_LAYO,
MYCONTAINER TYPE SCRFNAME VALUE 'BCALVC_EVENT1_CONT1',
* reference to custom container: neccessary to bind ALV Control
CUSTOM_CONTAINER TYPE REF TO CL_GUI_CUSTOM_CONTAINER,
GRID1 TYPE REF TO CL_GUI_ALV_GRID,
EVENT_RECEIVER TYPE REF TO LCL_EVENT_RECEIVER.

* § Step 1. Define a (local) class for event handling


****************************************************************
* LOCAL CLASSES: Definition
****************************************************************
*===============================================================
* class c_event_receiver: local class to handle print events...
* - PRINT_TOP_OF_PAGE (page header)
* - PRINT_END_OF_PAGE (page footer)
* - PRINT_TOP_OF_LIST (list header)
* - PRINT_END_OF_LIST (list footer)
*
* Definition:
* ~~~~~~~~~~~
CLASS LCL_EVENT_RECEIVER DEFINITION.

PUBLIC SECTION.
* § 2. Define a method for each print event you need.
METHODS:
HANDLE_TOP_OF_PAGE
FOR EVENT PRINT_TOP_OF_PAGE OF CL_GUI_ALV_GRID,

HANDLE_END_OF_PAGE
FOR EVENT PRINT_END_OF_PAGE OF CL_GUI_ALV_GRID,

HANDLE_TOP_OF_LIST
FOR EVENT PRINT_TOP_OF_LIST OF CL_GUI_ALV_GRID,

HANDLE_END_OF_LIST
FOR EVENT PRINT_END_OF_LIST OF CL_GUI_ALV_GRID.

PRIVATE SECTION.
DATA: PAGENUM TYPE I.

ENDCLASS.
*
* c_event_receiver (Definition)
*===============================================================

****************************************************************
* LOCAL CLASSES: Implementation
****************************************************************
*===============================================================
* class c_event_receiver (Implementation)
*
CLASS LCL_EVENT_RECEIVER IMPLEMENTATION.
*§ 3. Implement your event handler methods. Use WRITE to provide output.
METHOD HANDLE_TOP_OF_PAGE.
DATA: TABLENAME(30) TYPE C.
PERFORM GET_TABLENAME CHANGING TABLENAME.
WRITE: /,'Event: PRINT_TOP_OF_PAGE'(001),
'Table: '(002),TABLENAME.

ENDMETHOD. "handle_top_of_page
*-------------------------------------------
METHOD HANDLE_END_OF_PAGE.
DATA: TABLENAME(30) TYPE C.

PERFORM GET_TABLENAME CHANGING TABLENAME.


ADD 1 TO PAGENUM.
WRITE: /,'Event: PRINT_END_OF_PAGE'(003),
TEXT-002,TABLENAME,
'Number of pages so far: '(004), PAGENUM.

ENDMETHOD. "handle_end_of_page
*-------------------------------------------
METHOD HANDLE_TOP_OF_LIST.
DATA: TABLENAME(30) TYPE C.
CLEAR PAGENUM.
PERFORM GET_TABLENAME CHANGING TABLENAME.
WRITE: /,'Event: PRINT_TOP_OF_LIST'(005),
TEXT-002,TABLENAME.

ENDMETHOD. "handle_top_of_list
*-------------------------------------------
METHOD HANDLE_END_OF_LIST.
DATA: TABLENAME(30) TYPE C.
PERFORM GET_TABLENAME CHANGING TABLENAME.
WRITE: /,'Event: PRINT_END_OF_LIST'(006),
TEXT-002,TABLENAME.

ENDMETHOD. "handle_end_of_list
*-------------------------------------------
ENDCLASS.
*
* c_event_receiver (Implementation)
*===================================================================

START-OF-SELECTION.
SELECT * FROM SFLIGHT INTO TABLE GT_SFLIGHT up to g_max rows.
*
END-OF-SELECTION.
G_REPID = SY-REPID.
CALL SCREEN 100.

*---------------------------------------------------------------------*
* MODULE PBO OUTPUT *
*---------------------------------------------------------------------*
MODULE PBO OUTPUT.
SET PF-STATUS 'MAIN100'.
IF CUSTOM_CONTAINER IS INITIAL.
* create a custom container control for our ALV Control
CREATE OBJECT CUSTOM_CONTAINER
EXPORTING
CONTAINER_NAME = MYCONTAINER
EXCEPTIONS
CNTL_ERROR = 1
CNTL_SYSTEM_ERROR = 2
CREATE_ERROR = 3
LIFETIME_ERROR = 4
LIFETIME_DYNPRO_DYNPRO_LINK = 5.
IF SY-SUBRC NE 0.
* add your handling, for example
CALL FUNCTION 'POPUP_TO_INFORM'
EXPORTING
TITEL = G_REPID
TXT2 = SY-SUBRC
TXT1 = 'The control could not be created'(010).
ENDIF.
* create an instance of alv control
CREATE OBJECT GRID1
EXPORTING I_PARENT = CUSTOM_CONTAINER.
*
* Set a titlebar for the grid control
*
GS_LAYOUT-GRID_TITLE = 'Flights'(100).

* § 5. In case of PRINT_END_OF_PAGE, you must set 'reservelns' to


* the number of reserved lines at the end of a page.
*
* reserve two lines for the PRINT_END_OF_PAGE event
*
GS_PRINT-RESERVELNS = 2.

CALL METHOD GRID1->SET_TABLE_FOR_FIRST_DISPLAY


EXPORTING I_STRUCTURE_NAME = 'SFLIGHT'
IS_PRINT = GS_PRINT
IS_LAYOUT = GS_LAYOUT
CHANGING IT_OUTTAB = GT_SFLIGHT.

********
* ->Create Object to receive events and link them to handler methods.
* When the ALV Control raises the event for the specified instance
* the corresponding method is automatically called.
*
* § 4. Link used print events and event handler methods.
CREATE OBJECT EVENT_RECEIVER.
SET HANDLER EVENT_RECEIVER->HANDLE_TOP_OF_LIST FOR GRID1.
SET HANDLER EVENT_RECEIVER->HANDLE_TOP_OF_PAGE FOR GRID1.
SET HANDLER EVENT_RECEIVER->HANDLE_END_OF_LIST FOR GRID1.
SET HANDLER EVENT_RECEIVER->HANDLE_END_OF_PAGE FOR GRID1.
*
********

ENDIF.
* Controls are not integrated into the TAB-Order
* Call "set_focus" if you want to make sure that 'the cursor'
* is active in your control.
CALL METHOD CL_GUI_CONTROL=>SET_FOCUS EXPORTING CONTROL = GRID1.

* Control Framework flushes at the end of PBO automatically!


ENDMODULE.
*---------------------------------------------------------------------*
* MODULE PAI INPUT *
*---------------------------------------------------------------------*
MODULE PAI INPUT.
CASE OK_CODE.
WHEN 'EXIT'.
PERFORM EXIT_PROGRAM.
ENDCASE.
CLEAR OK_CODE.
ENDMODULE.
*---------------------------------------------------------------------*
* FORM EXIT_PROGRAM *
*---------------------------------------------------------------------*
FORM EXIT_PROGRAM.
CALL METHOD CUSTOM_CONTAINER->FREE.
CALL METHOD CL_GUI_CFW=>FLUSH.
IF SY-SUBRC NE 0.
* add your handling, for example
CALL FUNCTION 'POPUP_TO_INFORM'
EXPORTING
TITEL = G_REPID
TXT2 = SY-SUBRC
TXT1 = 'Error in Flush'(009).
ENDIF.
LEAVE PROGRAM.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form GET_TABLENAME
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* <--P_TABLENAME text
*----------------------------------------------------------------------*
FORM GET_TABLENAME CHANGING P_TABLENAME.
DATA: LT_FIELDCAT TYPE STANDARD TABLE OF LVC_S_FCAT,
LS_FIELDCAT TYPE LVC_S_FCAT.

CALL METHOD GRID1->GET_FRONTEND_FIELDCATALOG


IMPORTING ET_FIELDCATALOG = LT_FIELDCAT.

CALL METHOD CL_GUI_CFW=>FLUSH.


IF SY-SUBRC <> 0.
P_TABLENAME = 'No tablename in fieldcatalog!'(008).
CALL FUNCTION 'POPUP_TO_INFORM'
EXPORTING
TITEL = G_REPID
TXT2 = P_TABLENAME
TXT1 = 'Error in Flush'(011).
ELSE.
READ TABLE LT_FIELDCAT INDEX 1 INTO LS_FIELDCAT.
P_TABLENAME = LS_FIELDCAT-REF_TABLE.
ENDIF.

ENDFORM. " GET_TABLENAME

Potrebbero piacerti anche