Tuesday, March 1, 2011

Set Top of page in ALV using OOPS

This program is to demonstrate how to display text in ALV Header using Top Of Page Event while using Objects.

*&---------------------------------------------------------------------*
*& Report  ZALV_CL_TOPOFPAGE
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

REPORT  ZALV_CL_TOPOFPAGE.


*----------------------------------------------------------------------*
*       CLASS cl_main DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cl_main DEFINITION.
  PUBLIC SECTION.
    METHODS:
    constructor,
    get_data,
    populate_fc_layout,
    top_of_page FOR EVENT top_of_page OF cl_gui_alv_grid
    IMPORTING
     e_dyndoc_id
     table_index,
    print_top_of_page FOR EVENT print_top_of_page OF  cl_gui_alv_grid
     IMPORTING table_index,
    display.


  PROTECTED SECTION.

  PRIVATE SECTION.
    TYPE-POOLS:sdydo.
    DATA:i_sbook TYPE STANDARD TABLE OF sbook INITIAL SIZE 0.
    DATA:oref_splitter TYPE REF TO cl_gui_splitter_container,
         oref_container TYPE REF TO cl_gui_custom_container,
         oref_alv TYPE REF TO cl_gui_alv_grid,
         container1 TYPE REF TO cl_gui_container,
         container2 TYPE REF TO cl_gui_container,
         oref_doc TYPE REF TO cl_dd_document,
         oref_html TYPE REF TO cl_gui_html_viewer,
         wa_layout TYPE lvc_s_layo,
         i_fieldcatalog TYPE lvc_t_fcat.

ENDCLASS.                    "cl_main DEFINITION
*----------------------------------------------------------------------*
*       CLASS cl_main IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cl_main IMPLEMENTATION.
  METHOD constructor.

    CREATE OBJECT oref_container
      EXPORTING
        container_name              = 'CONTAINER'
        repid                       = 'ZALV_CL_TOPOFPAGE'
        dynnr                       = '0100'
      EXCEPTIONS
        cntl_error                  = 1
        cntl_system_error           = 2
        create_error                = 3
        lifetime_error              = 4
        lifetime_dynpro_dynpro_link = 5
        OTHERS                      = 6.
    IF sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*            WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
    ENDIF.


    CREATE OBJECT oref_splitter
      EXPORTING
        parent            = oref_container
        rows              = 2
        columns           = 1
      EXCEPTIONS
        cntl_error        = 1
        cntl_system_error = 2
        OTHERS            = 3.
    IF sy-subrc <> 0.
*     MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*                WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
    ENDIF.

    CALL METHOD oref_splitter->get_container
      EXPORTING
        row       = 1
        column    = 1
      RECEIVING
        container = container1.

    CALL METHOD oref_splitter->set_row_height
      EXPORTING
        id                = 1
        height            = 20
      EXCEPTIONS
        cntl_error        = 1
        cntl_system_error = 2
        OTHERS            = 3.
    IF sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*            WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
    ENDIF.


    CALL METHOD oref_splitter->get_container
      EXPORTING
        row       = 2
        column    = 1
      RECEIVING
        container = container2.


    CREATE OBJECT oref_doc
      EXPORTING
        style = 'ALV_GRID'.


    CREATE OBJECT oref_alv
      EXPORTING
        i_parent          = container2
      EXCEPTIONS
        error_cntl_create = 1
        error_cntl_init   = 2
        error_cntl_link   = 3
        error_dp_create   = 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.


  ENDMETHOD.                    "constructor
  METHOD get_data.
    SELECT * FROM sbook INTO TABLE i_sbook UP TO 500 ROWS.
    IF sy-subrc <> 0.

    ENDIF.

  ENDMETHOD.                    "get_data
  METHOD populate_fc_layout.
    CALL FUNCTION 'LVC_FIELDCATALOG_MERGE'
      EXPORTING
        i_structure_name       = 'SBOOK'
        i_client_never_display = 'X'
        i_internal_tabname     = 'I_SBOOK'
      CHANGING
        ct_fieldcat            = i_fieldcatalog
      EXCEPTIONS
        inconsistent_interface = 1
        program_error          = 2
        OTHERS                 = 3.
    IF sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*         WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
    ENDIF.

    wa_layout-cwidth_opt = 'X'.

  ENDMETHOD.                    "populate_fc_layout
  METHOD top_of_page.

    DATA: text TYPE sdydo_text_element,
          background_id TYPE sdydo_key VALUE 'ALV_BACKGROUND'.

    text = 'Welcome'.

    CALL METHOD oref_doc->add_text
      EXPORTING
        text         = text
*        sap_color    = cl_dd_document=>list_positive
        sap_fontsize = cl_dd_document=>large.

    oref_doc->new_line( ).
    text = 'Vino'.

    CALL METHOD oref_doc->add_text
      EXPORTING
        text = text.

    CREATE OBJECT oref_html
      EXPORTING
        parent             = container1
      EXCEPTIONS
        cntl_error         = 1
        cntl_install_error = 2
        dp_install_error   = 3
        dp_error           = 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.

    oref_doc->html_control = oref_html.

    CALL FUNCTION 'REUSE_ALV_GRID_COMMENTARY_SET'
      EXPORTING
        document = oref_doc
        bottom   = space.

    CALL METHOD oref_doc->merge_document.

    CALL METHOD oref_doc->set_document_background
      EXPORTING
        picture_id = background_id.

    CALL METHOD oref_doc->display_document
      EXPORTING
        reuse_control      = 'X'
        parent             = container1
      EXCEPTIONS
        html_display_error = 1
        OTHERS             = 2.
    IF sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*            WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
    ENDIF.
  ENDMETHOD.                    "create_top_of_page
  METHOD display.

    SET HANDLER me->top_of_page FOR oref_alv.
    SET HANDLER me->print_top_of_page FOR oref_alv.

    CALL METHOD oref_alv->set_table_for_first_display
      EXPORTING
        is_layout                     = wa_layout
      CHANGING
        it_outtab                     = i_sbook
        it_fieldcatalog               = i_fieldcatalog
      EXCEPTIONS
        invalid_parameter_combination = 1
        program_error                 = 2
        too_many_lines                = 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.

    CALL METHOD oref_doc->initialize_document
      .


    CALL METHOD oref_alv->list_processing_events
      EXPORTING
        i_event_name = 'TOP_OF_PAGE'
        i_dyndoc_id  = oref_doc.


    CALL SCREEN 0100.
  ENDMETHOD.                    "display
  METHOD print_top_of_page.
    WRITE:/5 'Print top of page' COLOR 5 INTENSIFIED OFF.
    WRITE:/5 'Line1'.
  ENDMETHOD.                    "print_top_of_page
ENDCLASS.                    "cl_main IMPLEMENTATION

DATA: oref_main TYPE REF TO cl_main.
DATA:ok_code TYPE syucomm.

START-OF-SELECTION.

  CREATE OBJECT oref_main.

  oref_main->get_data( ).
  oref_main->populate_fc_layout( ).

END-OF-SELECTION.

  oref_main->display( ).
*&---------------------------------------------------------------------*
*&      Module  STATUS_0100  OUTPUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
MODULE status_0100 OUTPUT.
  SET PF-STATUS '0100'.
*  SET TITLEBAR 'xxx'.
ENDMODULE.                 " STATUS_0100  OUTPUT
*&---------------------------------------------------------------------*
*&      Module  USER_COMMAND_0100  INPUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
MODULE user_command_0100 INPUT.
  CASE ok_code.
    WHEN 'BACK'.
      SET SCREEN 00.
      LEAVE SCREEN.
  ENDCASE.
ENDMODULE.                 " USER_COMMAND_0100  INPUT

1 comment: