07-20-2007 9:59 AM
07-20-2007 10:03 AM
see this is a sample program using OO concept.
<b>better you refer the ABAPDOCU(transaction code) and find more examples on OO concept.</b>
REPORT demo_abap_objects_events NO STANDARD PAGE HEADING.
INCLUDE <list>.
************************************************************************
Declarations
************************************************************************
INTERFACE i_vehicle.
DATA max_speed TYPE i.
EVENTS: speed_change EXPORTING value(new_speed) TYPE i.
METHODS: drive,
stop.
ENDINTERFACE.
*----
CLASS c_ship DEFINITION.
PUBLIC SECTION.
METHODS constructor.
INTERFACES i_vehicle.
PRIVATE SECTION.
ALIASES max FOR i_vehicle~max_speed.
DATA ship_speed TYPE i.
ENDCLASS.
*----
CLASS c_truck DEFINITION.
PUBLIC SECTION.
METHODS constructor.
INTERFACES i_vehicle.
PRIVATE SECTION.
ALIASES max FOR i_vehicle~max_speed.
DATA truck_speed TYPE i.
ENDCLASS.
*----
CLASS status DEFINITION.
PUBLIC SECTION.
CLASS-EVENTS button_clicked EXPORTING value(fcode) TYPE sy-ucomm.
CLASS-METHODS: class_constructor,
user_action.
ENDCLASS.
*----
CLASS c_list DEFINITION.
PUBLIC SECTION.
METHODS: fcode_handler FOR EVENT button_clicked OF status
IMPORTING fcode,
list_change FOR EVENT speed_change OF i_vehicle
IMPORTING new_speed,
list_output.
PRIVATE SECTION.
DATA: id TYPE i,
ref_ship TYPE REF TO c_ship,
ref_truck TYPE REF TO c_truck,
BEGIN OF line,
id TYPE i,
flag(1) TYPE c,
iref TYPE REF TO i_vehicle,
speed TYPE i,
END OF line,
list LIKE SORTED TABLE OF line WITH UNIQUE KEY id.
ENDCLASS.
************************************************************************
Implementations
************************************************************************
CLASS c_ship IMPLEMENTATION.
METHOD constructor.
max = 30.
ENDMETHOD.
METHOD i_vehicle~drive.
CHECK ship_speed < max.
ship_speed = ship_speed + 10.
RAISE EVENT i_vehicle~speed_change
EXPORTING new_speed = ship_speed.
ENDMETHOD.
METHOD i_vehicle~stop.
CHECK ship_speed > 0.
ship_speed = 0.
RAISE EVENT i_vehicle~speed_change
EXPORTING new_speed = ship_speed.
ENDMETHOD.
ENDCLASS.
*----
CLASS c_truck IMPLEMENTATION.
METHOD constructor.
max = 150.
ENDMETHOD.
METHOD i_vehicle~drive.
CHECK truck_speed < max.
truck_speed = truck_speed + 50.
RAISE EVENT i_vehicle~speed_change
EXPORTING new_speed = truck_speed.
ENDMETHOD.
METHOD i_vehicle~stop.
CHECK truck_speed > 0.
truck_speed = 0.
RAISE EVENT i_vehicle~speed_change
EXPORTING new_speed = truck_speed.
ENDMETHOD.
ENDCLASS.
*----
CLASS status IMPLEMENTATION.
METHOD class_constructor.
SET PF-STATUS 'VEHICLE'.
WRITE 'Click a button!'.
ENDMETHOD.
METHOD user_action.
RAISE EVENT button_clicked EXPORTING fcode = sy-ucomm.
ENDMETHOD.
ENDCLASS.
*----
CLASS c_list IMPLEMENTATION.
METHOD fcode_handler.
CLEAR line.
CASE fcode.
WHEN 'CREA_SHIP'.
id = id + 1.
CREATE OBJECT ref_ship.
line-id = id.
line-flag = 'C'.
line-iref = ref_ship.
APPEND line TO list.
WHEN 'CREA_TRUCK'.
id = id + 1.
CREATE OBJECT ref_truck.
line-id = id.
line-flag = 'T'.
line-iref = ref_truck.
APPEND line TO list.
WHEN 'DRIVE'.
CHECK sy-lilli > 0.
READ TABLE list INDEX sy-lilli INTO line.
CALL METHOD line-iref->drive.
WHEN 'STOP'.
LOOP AT list INTO line.
CALL METHOD line-iref->stop.
ENDLOOP.
WHEN 'CANCEL'.
LEAVE PROGRAM.
ENDCASE.
CALL METHOD list_output.
ENDMETHOD.
METHOD list_change.
line-speed = new_speed.
MODIFY TABLE list FROM line.
ENDMETHOD.
METHOD list_output.
sy-lsind = 0.
SET TITLEBAR 'TIT'.
LOOP AT list INTO line.
IF line-flag = 'C'.
WRITE / icon_ws_ship AS ICON.
ELSEIF line-flag = 'T'.
WRITE / icon_ws_truck AS ICON .
ENDIF.
WRITE: 'Speed = ', line-speed.
ENDLOOP.
ENDMETHOD.
ENDCLASS.
************************************************************************
Global data of program
************************************************************************
DATA list TYPE REF TO c_list.
************************************************************************
Program events
************************************************************************
START-OF-SELECTION.
CREATE OBJECT list.
SET HANDLER: list->fcode_handler,
list->list_change FOR ALL INSTANCES.
*----
AT USER-COMMAND.
CALL METHOD status=>user_action.
************************************************************************
regards,
srinivas
<b>*reward for useful answers*</b>
07-20-2007 10:05 AM
Hi,
Refer the link:
http://help.sap.com/saphelp_nw2004s/helpdata/en/ce/b518b6513611d194a50000e8353423/frameset.htm
<b>Reward points</b>
Regards
07-20-2007 10:06 AM
Hi
OOPs ABAP uses Classes and Interfaces which uses Methods and events.
If you have Java skills it is advantage for you.
There are Local classes as well as Global Classes.
Local classes we can work in SE38 straight away.
But mostly it is better to use the Global classes.
Global Classes or Interfaces are to be created in SE24.
SAP already given some predefined classes and Interfaces.
This OOPS concepts very useful for writing BADI's also.
So first create a class in SE 24.
Define attributes, Methods for that class.
Define parameters for that Method.
You can define event handlers also to handle the messages.
After creation in each method write the code.
Methods are similar to ABAP PERFORM -FORM statements.
After the creation of CLass and methods come to SE38 and create the program.
In the program create a object type ref to that class and with the help of that Object call the methods of that Class and display the data.
Example:
REPORT sapmz_hf_alv_grid .
Type pool for icons - used in the toolbar
TYPE-POOLS: icon.
TABLES: zsflight.
To allow the declaration of o_event_receiver before the
lcl_event_receiver class is defined, decale it as deferred in the
start of the program
CLASS lcl_event_receiver DEFINITION DEFERRED.
*----
G L O B A L I N T E R N A L T A B L E S
*----
*DATA: gi_sflight TYPE STANDARD TABLE OF sflight.
To include a traffic light and/or color a line the structure of the
table must include fields for the traffic light and/or the color
TYPES: BEGIN OF st_sflight.
INCLUDE STRUCTURE zsflight.
Field for traffic light
TYPES: traffic_light TYPE c.
Field for line color
types: line_color(4) type c.
TYPES: END OF st_sflight.
TYPES: tt_sflight TYPE STANDARD TABLE OF st_sflight.
DATA: gi_sflight TYPE tt_sflight.
*----
G L O B A L D A T A
*----
DATA: ok_code LIKE sy-ucomm,
Work area for internal table
g_wa_sflight TYPE st_sflight,
ALV control: Layout structure
gs_layout TYPE lvc_s_layo.
Declare reference variables to the ALV grid and the container
DATA:
go_grid TYPE REF TO cl_gui_alv_grid,
go_custom_container TYPE REF TO cl_gui_custom_container,
o_event_receiver TYPE REF TO lcl_event_receiver.
DATA:
Work area for screen 200
g_screen200 LIKE zsflight.
Data for storing information about selected rows in the grid
DATA:
Internal table
gi_index_rows TYPE lvc_t_row,
Information about 1 row
g_selected_row LIKE lvc_s_row.
*----
C L A S S E S
*----
CLASS lcl_event_receiver DEFINITION.
PUBLIC SECTION.
METHODS:
handle_toolbar 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
IMPORTING e_ucomm.
ENDCLASS.
----
CLASS lcl_event_receiver IMPLEMENTATION
----
CLASS lcl_event_receiver IMPLEMENTATION.
METHOD handle_toolbar.
Event handler method for event toolbar.
CONSTANTS:
Constants for button type
c_button_normal TYPE i VALUE 0,
c_menu_and_default_button TYPE i VALUE 1,
c_menu TYPE i VALUE 2,
c_separator TYPE i VALUE 3,
c_radio_button TYPE i VALUE 4,
c_checkbox TYPE i VALUE 5,
c_menu_entry TYPE i VALUE 6.
DATA:
ls_toolbar TYPE stb_button.
Append seperator to the normal toolbar
CLEAR ls_toolbar.
MOVE c_separator TO ls_toolbar-butn_type..
APPEND ls_toolbar TO e_object->mt_toolbar.
Append a new button that to the toolbar. Use E_OBJECT of
event toolbar. E_OBJECT is of type CL_ALV_EVENT_TOOLBAR_SET.
This class has one attribute MT_TOOLBAR which is of table type
TTB_BUTTON. The structure is STB_BUTTON
CLEAR ls_toolbar.
MOVE 'CHANGE' TO ls_toolbar-function.
MOVE icon_change TO ls_toolbar-icon.
MOVE 'Change flight' TO ls_toolbar-quickinfo.
MOVE 'Change' TO ls_toolbar-text.
MOVE ' ' TO ls_toolbar-disabled.
APPEND ls_toolbar TO e_object->mt_toolbar.
ENDMETHOD.
METHOD handle_user_command.
Handle own functions defined in the toolbar
CASE e_ucomm.
WHEN 'CHANGE'.
PERFORM change_flight.
LEAVE TO SCREEN 0.
ENDCASE.
ENDMETHOD.
ENDCLASS.
*----
S T A R T - O F - S E L E C T I O N.
*----
START-OF-SELECTION.
SET SCREEN '100'.
&----
*& Module USER_COMMAND_0100 INPUT
&----
MODULE user_command_0100 INPUT.
CASE ok_code.
WHEN 'EXIT'.
LEAVE TO SCREEN 0.
ENDCASE.
ENDMODULE. " USER_COMMAND_0100 INPUT
&----
*& Module STATUS_0100 OUTPUT
&----
MODULE status_0100 OUTPUT.
DATA:
For parameter IS_VARIANT that is sued to set up options for storing
the grid layout as a variant in method set_table_for_first_display
l_layout TYPE disvariant,
Utillity field
l_lines TYPE i.
After returning from screen 200 the line that was selected before
going to screen 200, should be selected again. The table gi_index_rows
was the output table from the GET_SELECTED_ROWS method in form
CHANGE_FLIGHT
DESCRIBE TABLE gi_index_rows LINES l_lines.
IF l_lines > 0.
CALL METHOD go_grid->set_selected_rows
EXPORTING
it_index_rows = gi_index_rows.
CALL METHOD cl_gui_cfw=>flush.
REFRESH gi_index_rows.
ENDIF.
Read data and create objects
IF go_custom_container IS INITIAL.
Read data from datbase table
PERFORM get_data.
Create objects for container and ALV grid
CREATE OBJECT go_custom_container
EXPORTING container_name = 'ALV_CONTAINER'.
CREATE OBJECT go_grid
EXPORTING
i_parent = go_custom_container.
Create object for event_receiver class
and set handlers
CREATE OBJECT o_event_receiver.
SET HANDLER o_event_receiver->handle_user_command FOR go_grid.
SET HANDLER o_event_receiver->handle_toolbar FOR go_grid.
Layout (Variant) for ALV grid
l_layout-report = sy-repid. "Layout fo report
*----
Setup the grid layout using a variable of structure lvc_s_layo
*----
Set grid title
gs_layout-grid_title = 'Flights'.
Selection mode - Single row without buttons
(This is the default mode
gs_layout-sel_mode = 'B'.
Name of the exception field (Traffic light field) and the color
field + set the exception and color field of the table
gs_layout-excp_fname = 'TRAFFIC_LIGHT'.
gs_layout-info_fname = 'LINE_COLOR'.
LOOP AT gi_sflight INTO g_wa_sflight.
IF g_wa_sflight-paymentsum < 100000.
Value of traffic light field
g_wa_sflight-traffic_light = '1'.
Value of color field:
C = Color, 6=Color 1=Intesified on, 0: Inverse display off
g_wa_sflight-line_color = 'C610'.
ELSEIF g_wa_sflight-paymentsum => 100000 AND
g_wa_sflight-paymentsum < 1000000.
g_wa_sflight-traffic_light = '2'.
ELSE.
g_wa_sflight-traffic_light = '3'.
ENDIF.
MODIFY gi_sflight FROM g_wa_sflight.
ENDLOOP.
Grid setup for first display
CALL METHOD go_grid->set_table_for_first_display
EXPORTING i_structure_name = 'SFLIGHT'
is_variant = l_layout
i_save = 'A'
is_layout = gs_layout
CHANGING it_outtab = gi_sflight.
*-- End of grid setup -
Raise event toolbar to show the modified toolbar
CALL METHOD go_grid->set_toolbar_interactive.
Set focus to the grid. This is not necessary in this
example as there is only one control on the screen
CALL METHOD cl_gui_control=>set_focus EXPORTING control = go_grid.
ENDIF.
ENDMODULE. " STATUS_0100 OUTPUT
&----
*& Module USER_COMMAND_0200 INPUT
&----
MODULE user_command_0200 INPUT.
CASE ok_code.
WHEN 'EXIT200'.
LEAVE TO SCREEN 100.
WHEN'SAVE'.
PERFORM save_changes.
ENDCASE.
ENDMODULE. " USER_COMMAND_0200 INPUT
&----
*& Form get_data
&----
FORM get_data.
Read data from table SFLIGHT
SELECT *
FROM zsflight
INTO TABLE gi_sflight.
ENDFORM. " load_data_into_grid
&----
*& Form change_flight
&----
Reads the contents of the selected row in the grid, ans transfers
the data to screen 200, where it can be changed and saved.
----
FORM change_flight.
DATA:l_lines TYPE i.
REFRESH gi_index_rows.
CLEAR g_selected_row.
Read index of selected rows
CALL METHOD go_grid->get_selected_rows
IMPORTING
et_index_rows = gi_index_rows.
Check if any row are selected at all. If not
table gi_index_rows will be empty
DESCRIBE TABLE gi_index_rows LINES l_lines.
IF l_lines = 0.
CALL FUNCTION 'POPUP_TO_DISPLAY_TEXT'
EXPORTING
textline1 = 'You must choose a line'.
EXIT.
ENDIF.
Read indexes of selected rows. In this example only one
row can be selected as we are using gs_layout-sel_mode = 'B',
so it is only ncessary to read the first entry in
table gi_index_rows
LOOP AT gi_index_rows INTO g_selected_row.
IF sy-tabix = 1.
READ TABLE gi_sflight INDEX g_selected_row-index INTO g_wa_sflight.
ENDIF.
ENDLOOP.
Transfer data from the selected row to screenm 200 and show
screen 200
CLEAR g_screen200.
MOVE-CORRESPONDING g_wa_sflight TO g_screen200.
LEAVE TO SCREEN '200'.
ENDFORM. " change_flight
&----
*& Form save_changes
&----
Changes made in screen 200 are written to the datbase table
zsflight, and to the grid table gi_sflight, and the grid is
updated with method refresh_table_display to display the changes
----
FORM save_changes.
DATA: l_traffic_light TYPE c.
Update traffic light field
Update database table
MODIFY zsflight FROM g_screen200.
Update grid table , traffic light field and color field.
Note that it is necessary to use structure g_wa_sflight
for the update, as the screen structure does not have a
traffic light field
MOVE-CORRESPONDING g_screen200 TO g_wa_sflight.
IF g_wa_sflight-paymentsum < 100000.
g_wa_sflight-traffic_light = '1'.
C = Color, 6=Color 1=Intesified on, 0: Inverse display off
g_wa_sflight-line_color = 'C610'.
ELSEIF g_wa_sflight-paymentsum => 100000 AND
g_wa_sflight-paymentsum < 1000000.
g_wa_sflight-traffic_light = '2'.
clear g_wa_sflight-line_color.
ELSE.
g_wa_sflight-traffic_light = '3'.
clear g_wa_sflight-line_color.
ENDIF.
MODIFY gi_sflight INDEX g_selected_row-index FROM g_wa_sflight.
Refresh grid
CALL METHOD go_grid->refresh_table_display.
CALL METHOD cl_gui_cfw=>flush.
LEAVE TO SCREEN '100'.
ENDFORM. " save_changes
chk this blog
/people/vijaybabu.dudla/blog/2006/07/21/topofpage-in-alv-using-clguialvgrid
chk out the links below:
General Tutorial for OOPS
Have a look at these links for OO ABAP.
http://www.sapgenie.com/abap/OO/
http://help.sap.com/saphelp_nw04/helpdata/en/c3/225b5654f411d194a60000e8353423/content.htm
http://www.esnips.com/doc/375fff1b-5a62-444d-8ec1-55508c308b17/prefinalppt.ppt
http://www.esnips.com/doc/2c76dc57-e74a-4539-a20e-29383317e804/OO-abap.pdf
http://www.esnips.com/doc/5c65b0dd-eddf-4512-8e32-ecd26735f0f2/prefinalppt.ppt
http://www.sapgenie.com/abap/controls/index.htm
http://www.esnips.com/doc/2c76dc57-e74a-4539-a20e-29383317e804/OO-abap.pdf
http://www.esnips.com/doc/0ef39d4b-586a-4637-abbb-e4f69d2d9307/SAP-CONTROLS-WORKSHOP.pdf
http://help.sap.com/saphelp_erp2005/helpdata/en/ce/b518b6513611d194a50000e8353423/frameset.htm
SDN Series:
https://www.sdn.sap.com/irj/sdn/developerareas/abap?rid=/webcontent/uuid/35eaef9c-0b01-0010-dd8b-e3b... [original link is broken]
http://help.sap.com/printdocu/core/Print46c/en/data/pdf/BCABA/BCABA.pdf
Basic concepts of OOPS
http://www.henrikfrank.dk/abapuk.html
http://www.erpgenie.com/abap/OO/
<b>Reward points for useful Answers</b>
Regards
Anji
07-20-2007 10:06 AM
Hi, here i am forwarding some important links regarding OOPS.
http://esnips.com/doc/39fdc647-1aed-4b40-a476-4d3042b6ec28/class_builder.ppt
http://esnips.com/doc/5c65b0dd-eddf-4512-8e32-ecd26735f0f2/prefinalppt.ppt
http://esnips.com/doc/448e8302-68b1-4046-9fef-8fa8808caee0/abap-objects-by-helen.pdf
http://esnips.com/doc/92be4457-1b6e-4061-92e5-8e4b3a6e3239/Object-Oriented-ABAP.ppt
http://esnips.com/doc/2c76dc57-e74a-4539-a20e-29383317e804/OO-abap.pdf
http://esnips.com/doc/0ef39d4b-586a-4637-abbb-e4f69d2d9307/SAP-CONTROLS-WORKSHOP.pdf
http://help.sap.com/saphelp_nw2004s/helpdata/en/ce/b518b6513611d194a50000e8353423/frameset.htm
reward poins if it is useful
07-20-2007 10:07 AM
This document uses Splitter concept to split the container and then displays top-of-page in top container and output in bottom container by placing the grid inside the container.
Procedure
Step 1: Create a Docking container. Then create a splitter by taking the docking container as parent.
Data : o_docking type ref to cl_gui_docking_container,"Docking Container
o_split type ref to cl_gui_easy_splitter_container."Splitter
CREATE OBJECT o_docking
EXPORTING
RATIO = '95'.
IF sy-subrc eq 0.
Splitting the Docking container
CREATE OBJECT o_split
EXPORTING
PARENT = o_docking
sash_position = 14 "Position of Splitter Bar (in Percent)
with_border = 0. "With Border = 1 Without Border = 0
ENDIF.
Step 2: Make the two containers so that one will be placed in top of split and the other in bottom. Create the grid so that it should be inside the bottom container. Create a object for class cl_dd_document so that text, url and images can be placed inside top of page.
Data : o_top_container type ref to cl_gui_container, "Top Container
o_bottom_container type ref to cl_gui_container,"Bottom Container
o_document type ref to cl_dd_document. "Document
Placing the containers in the splitter
o_top_container = o_split->top_left_container .
o_bottom_container = o_split->bottom_right_container .
Creating Grid
CREATE OBJECT o_grid
EXPORTING
i_parent = o_bottom_container.
Creating the document
CREATE OBJECT o_document
EXPORTING
style = 'ALV_GRID'.
Step 3: Add the text, url and images by using o_document.
Calling the methods for dynamic text
CALL METHOD o_document->add_text
EXPORTING
text = 'No. of Materials:'.
Adding GAP
CALL METHOD o_document->add_gap
EXPORTING
width = 10.
Adding Text
CALL METHOD o_document->add_text
EXPORTING
text = v_cln."V_cln is the variable holding no. of materials
Adding Line
CALL METHOD o_document->new_line.
To add picture to the top of page, we need to find the picture id. We can find this by using tcode OAOR. Give Class name as PICTURES and Class TYPE as OT in selection.
Choose some picture inside that and in bottom click Details tab to get the ID.
Adding Picture
CALL METHOD o_document->add_picture
EXPORTING
picture_id = 'TRVPICTURE01'
width = '100'.
Adding Line
CALL METHOD o_document->new_line.
Adding Links
CALL METHOD o_document->add_link
EXPORTING
name = 'OOPS Link'
url =
tooltip = 'ABAP Objects Forum'
text = 'ABAP Objects Forum'.
Display the data
CALL METHOD o_document->display_document
EXPORTING
parent = o_top_container.
Calling the method of ALV to process top of page
CALL METHOD o_grid->list_processing_events
EXPORTING
i_event_name = 'TOP_OF_PAGE'
i_dyndoc_id = o_document.
Complete Code
Screen 9000, GUI Status ZSTATUS and GUI Title ZTITLE should be created and in Flow logic of the screen, PBO and PAI should be uncommented.
Data Declaration
data : itab type standard table of MARA,"Output Internal table
i_fieldcat type standard table of lvc_s_fcat,"Field catalog
wa type MARA,
w_variant type disvariant,
o_docking type ref to cl_gui_docking_container,"Docking Container
o_grid type ref to cl_gui_alv_grid,"Grid
o_split type ref to cl_gui_easy_splitter_container,"Splitter
o_top_container type ref to cl_gui_container, "Top Container
o_bottom_container type ref to cl_gui_container,"Bottom Container
o_document type ref to cl_dd_document, "Document
Variable Declaration
v_ln TYPE i, "No. of lines
v_cln(255) TYPE c. "No. of lines
select * from mara into corresponding fields of table itab up to 10 rows.
Finding no. of lines to display in top-of-page
DESCRIBE TABLE itab LINES v_ln.
MOVE v_ln TO v_cln.
call screen 9000.
&----
*& Module STATUS_9000 OUTPUT
&----
text
----
module STATUS_9000 output.
if o_docking is initial.
SET PF-STATUS 'ZSTATUS'. "GUI Status
SET TITLEBAR 'ZTITLE'. "Title
Creating Objects
perform create_objects.
Filling top of page
perform fill_top_of_page.
Filling the fieldcatalog table
perform build_fieldcat.
Displaying the output
perform display_output.
endif.
endmodule. " STATUS_9000 OUTPUT
&----
*& Module USER_COMMAND_9000 INPUT
&----
PAI
----
module USER_COMMAND_9000 input.
data lv_ucomm type sy-ucomm.
lv_ucomm = sy-ucomm.
CASE lv_ucomm.
WHEN 'CANCEl' OR 'EXIT'.
perform free_objects.
LEAVE PROGRAM.
when 'BACK'.
perform free_objects.
set screen '0'.
leave screen.
ENDCASE.
endmodule. " USER_COMMAND_9000 INPUT
&----
*& Form create_objects
&----
Creating Container,Splitter and Grid
----
form create_objects .
Creating Docking Container
CREATE OBJECT o_docking
EXPORTING
RATIO = '95'.
IF sy-subrc eq 0.
Splitting the Docking container
CREATE OBJECT o_split
EXPORTING
PARENT = o_docking
sash_position = 20 "Position of Splitter Bar (in Percent)
with_border = 0. "With Border = 1 Without Border = 0
Placing the containers in the splitter
o_top_container = o_split->top_left_container .
o_bottom_container = o_split->bottom_right_container .
Creating Grid
CREATE OBJECT o_grid
EXPORTING
i_parent = o_bottom_container.
Creating the document
CREATE OBJECT o_document
EXPORTING
style = 'ALV_GRID'.
ENDIF.
endform. " create_objects
&----
*& Form fill_top_of_page
&----
Filling the top of page
----
form fill_top_of_page .
Calling the methods for dynamic text
CALL METHOD o_document->add_text
EXPORTING
text = 'No. of Materials:'.
Adding GAP
CALL METHOD o_document->add_gap
EXPORTING
width = 10.
Adding Text
CALL METHOD o_document->add_text
EXPORTING
text = v_cln.
Adding Line
CALL METHOD o_document->new_line.
Adding Picture
CALL METHOD o_document->add_picture
EXPORTING
picture_id = 'TRVPICTURE01'
width = '100'.
Adding Line
CALL METHOD o_document->new_line.
Adding Links
CALL METHOD o_document->add_link
EXPORTING
name = 'OOPS Link'
url =
tooltip = 'ABAP Objects Forum'
text = 'ABAP Objects Forum'.
Display the data
CALL METHOD o_document->display_document
EXPORTING
parent = o_top_container.
Calling the method of ALV to process top of page
CALL METHOD o_grid->list_processing_events
EXPORTING
i_event_name = 'TOP_OF_PAGE'
i_dyndoc_id = o_document.
endform. " fill_top_of_page
&----
*& Form build_fieldcat
&----
Creating Field catalog
----
form build_fieldcat .
CALL FUNCTION 'LVC_FIELDCATALOG_MERGE'
EXPORTING
I_STRUCTURE_NAME = 'MARA'
CHANGING
ct_fieldcat = i_fieldcat
EXCEPTIONS
INCONSISTENT_INTERFACE = 1
PROGRAM_ERROR = 2
OTHERS = 3.
endform. " build_fieldcat
&----
*& Form display_output
&----
Displaying the Output
----
form display_output .
w_variant-report = sy-repid.
CALL METHOD o_grid->set_table_for_first_display
EXPORTING
IS_VARIANT = w_variant
I_SAVE = 'A'
CHANGING
it_outtab = itab
IT_FIELDCATALOG = i_fieldcat
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.
endform. " display_output
&----
*& Form free_objects
&----
Free Objects
----
form free_objects .
CALL METHOD o_grid->free
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 o_top_container->free
EXCEPTIONS
CNTL_ERROR = 1
CNTL_SYSTEM_ERROR = 2
others = 3.
CALL METHOD o_bottom_container->free
EXCEPTIONS
CNTL_ERROR = 1
CNTL_SYSTEM_ERROR = 2
others = 3.
CALL METHOD o_docking->free
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.
endform. " free_objects
Regards,
Pavan
07-20-2007 10:08 AM
hi,
This example is a step by step example, moving from a simple class to more sophisticated classes, and covers inheritance, interfaces and events.
Simple class
Inheritance and ploymorphism
Interfaces
Events
1. Simple class
This example shows how to create a simple employee class. The constructor method is used to initialize number and name of thje employee when the object is created. A display_employee method can be called to show the attributes of the employee, and CLASS-METHOD dosplay_no_of_employees can be called to show the total number of employees (Number of instances of the employee class).
REPORT zbc404_hf_events_1.
*********************************************************************
L C L _ E M P L O Y E E
*********************************************************************
*---- LCL Employee - Definition
CLASS lcl_employee DEFINITION.
PUBLIC SECTION.
*----
-
The public section is accesible from outside
*----
-
TYPES:
BEGIN OF t_employee,
no TYPE i,
name TYPE string,
END OF t_employee.
METHODS:
constructor
IMPORTING im_employee_no TYPE i
im_employee_name TYPE string,
display_employee.
Class methods are global for all instances
CLASS-METHODS: display_no_of_employees.
PROTECTED SECTION.
*----
-
The protecetd section is accesible from the class and its subclasses
*----
-
Class data are global for all instances
CLASS-DATA: g_no_of_employees TYPE i.
PRIVATE SECTION.
*----
-
The private section is only accesible from within the classs
*----
-
DATA: g_employee TYPE t_employee.
ENDCLASS.
*--- LCL Employee - Implementation
CLASS lcl_employee IMPLEMENTATION.
METHOD constructor.
g_employee-no = im_employee_no.
g_employee-name = im_employee_name.
g_no_of_employees = g_no_of_employees + 1.
ENDMETHOD.
METHOD display_employee.
WRITE:/ 'Employee', g_employee-no, g_employee-name.
ENDMETHOD.
METHOD display_no_of_employees.
WRITE: / 'Number of employees is:', g_no_of_employees.
ENDMETHOD.
ENDCLASS.
************************************************************************
R E P O R T
*********************************************************************
DATA: g_employee1 TYPE REF TO lcl_employee,
g_employee2 TYPE REF TO lcl_employee.
START-OF-SELECTION.
CREATE OBJECT g_employee1
EXPORTING im_employee_no = 1
im_employee_name = 'John Jones'.
CREATE OBJECT g_employee2
EXPORTING im_employee_no = 2
im_employee_name = 'Sally Summer'.
CALL METHOD g_employee1->display_employee.
CALL METHOD g_employee2->display_employee.
CALL METHOD g_employee2->display_no_of_employees.
2. Inheritance and polymorphism
This example uses a superclass lcl_company_employees and two subclasses lcl_bluecollar_employee and lcl_whitecollar_employee to add employees to a list and then display a list of employees and there wages. The wages are calcukated in the method add_employee, but as the wages are calculated differently for blue collar employees and white collar emplyees, the superclass method add_employee is redeifined in the subclasses.
Principles:
Create super class LCL_CompanyEmployees.
The class has the methods:
Constructor
Add_Employee - Adds a new employee to the list of employees
Display_Employee_List - Displays all employees and there wage
Display_no_of_employees - Displays total number of employees
Note the use of CLASS-DATA to keep the list of employees and number of employees the same from instance to instance.
Create subclasses lcl_bluecollar_employee and lcl_whitecollar_employee. The calsses are identical, except for the redifinition of the add_employee method, where the caclculation of wage is different.
Methodes:
Constructor. The constructor is used to initialize the attributes of the employee. Note that the constructor in the supclasss has to be called from within the constructor of the subclass.
Add_Employee. This is a redinition of the same method in the superclass. In the redefined class the wage is calcuated, and the superclass method is called to add the employees to the emploee list.:
The program
REPORT zbc404_hf_events_2 .
*******************************************************
Super class LCL_CompanyEmployees
*******************************************************
CLASS lcl_company_employees DEFINITION.
PUBLIC SECTION.
TYPES:
BEGIN OF t_employee,
no TYPE i,
name TYPE string,
wage TYPE i,
END OF t_employee.
METHODS:
constructor,
add_employee
IMPORTING im_no TYPE i
im_name TYPE string
im_wage TYPE i,
display_employee_list,
display_no_of_employees.
PRIVATE SECTION.
CLASS-DATA: i_employee_list TYPE TABLE OF t_employee,
no_of_employees TYPE i.
ENDCLASS.
*-- CLASS LCL_CompanyEmployees IMPLEMENTATION
CLASS lcl_company_employees IMPLEMENTATION.
METHOD constructor.
no_of_employees = no_of_employees + 1.
ENDMETHOD.
METHOD add_employee.
Adds a new employee to the list of employees
DATA: l_employee TYPE t_employee.
l_employee-no = im_no.
l_employee-name = im_name.
l_employee-wage = im_wage.
APPEND l_employee TO i_employee_list.
ENDMETHOD.
METHOD display_employee_list.
Displays all employees and there wage
DATA: l_employee TYPE t_employee.
WRITE: / 'List of Employees'.
LOOP AT i_employee_list INTO l_employee.
WRITE: / l_employee-no, l_employee-name, l_employee-wage.
ENDLOOP.
ENDMETHOD.
METHOD display_no_of_employees.
Displays total number of employees
SKIP 3.
WRITE: / 'Total number of employees:', no_of_employees.
ENDMETHOD.
ENDCLASS.
*******************************************************
Sub class LCL_BlueCollar_Employee
*******************************************************
CLASS lcl_bluecollar_employee DEFINITION
INHERITING FROM lcl_company_employees.
PUBLIC SECTION.
METHODS:
constructor
IMPORTING im_no TYPE i
im_name TYPE string
im_hours TYPE i
im_hourly_payment TYPE i,
add_employee REDEFINITION.
PRIVATE SECTION.
DATA:no TYPE i,
name TYPE string,
hours TYPE i,
hourly_payment TYPE i.
ENDCLASS.
*---- CLASS LCL_BlueCollar_Employee IMPLEMENTATION
CLASS lcl_bluecollar_employee IMPLEMENTATION.
METHOD constructor.
The superclass constructor method must be called from the subclass
constructor method
CALL METHOD super->constructor.
no = im_no.
name = im_name.
hours = im_hours.
hourly_payment = im_hourly_payment.
ENDMETHOD.
METHOD add_employee.
Calculate wage an call the superclass method add_employee to add
the employee to the employee list
DATA: l_wage TYPE i.
l_wage = hours * hourly_payment.
CALL METHOD super->add_employee
EXPORTING im_no = no
im_name = name
im_wage = l_wage.
ENDMETHOD.
ENDCLASS.
*******************************************************
Sub class LCL_WhiteCollar_Employee
*******************************************************
CLASS lcl_whitecollar_employee DEFINITION
INHERITING FROM lcl_company_employees.
PUBLIC SECTION.
METHODS:
constructor
IMPORTING im_no TYPE i
im_name TYPE string
im_monthly_salary TYPE i
im_monthly_deductions TYPE i,
add_employee REDEFINITION.
PRIVATE SECTION.
DATA:
no TYPE i,
name TYPE string,
monthly_salary TYPE i,
monthly_deductions TYPE i.
ENDCLASS.
*---- CLASS LCL_WhiteCollar_Employee IMPLEMENTATION
CLASS lcl_whitecollar_employee IMPLEMENTATION.
METHOD constructor.
The superclass constructor method must be called from the subclass
constructor method
CALL METHOD super->constructor.
no = im_no.
name = im_name.
monthly_salary = im_monthly_salary.
monthly_deductions = im_monthly_deductions.
ENDMETHOD.
METHOD add_employee.
Calculate wage an call the superclass method add_employee to add
the employee to the employee list
DATA: l_wage TYPE i.
l_wage = monthly_salary - monthly_deductions.
CALL METHOD super->add_employee
EXPORTING im_no = no
im_name = name
im_wage = l_wage.
ENDMETHOD.
ENDCLASS.
*******************************************************
R E P O R T
*******************************************************
DATA:
Object references
o_bluecollar_employee1 TYPE REF TO lcl_bluecollar_employee,
o_whitecollar_employee1 TYPE REF TO lcl_whitecollar_employee.
START-OF-SELECTION.
Create bluecollar employee obeject
CREATE OBJECT o_bluecollar_employee1
EXPORTING im_no = 1
im_name = 'Gylle Karen'
im_hours = 38
im_hourly_payment = 75.
Add bluecollar employee to employee list
CALL METHOD o_bluecollar_employee1->add_employee
EXPORTING im_no = 1
im_name = 'Gylle Karen'
im_wage = 0.
Create whitecollar employee obeject
CREATE OBJECT o_whitecollar_employee1
EXPORTING im_no = 2
im_name = 'John Dickens'
im_monthly_salary = 10000
im_monthly_deductions = 2500.
Add bluecollar employee to employee list
CALL METHOD o_whitecollar_employee1->add_employee
EXPORTING im_no = 1
im_name = 'Karen Johnson'
im_wage = 0.
Display employee list and number of employees. Note that the result
will be the same when called from o_whitecollar_employee1 or
o_bluecolarcollar_employee1, because the methods are defined
as static (CLASS-METHODS)
CALL METHOD o_whitecollar_employee1->display_employee_list.
CALL METHOD o_whitecollar_employee1->display_no_of_employees.
The resulting report
List of Employees
1 Karen Johnson 2.850
2 John Dickens 7.500
Total number of employees: 2
3. Interfaces
This example is similiar to th eprevious example, however an interface is implemented with the method add_employee. Note that the interface is only implemented in the superclass ( The INTERFACE stament), but also used in the subclasses.
The interface in the example only contains a method, but an iterface can also contain attrbutes, constants, types and alias names.
The output from example 3 is similiar to the output in example 2.
All changes in the program compared to example 2 are marked with red.
REPORT zbc404_hf_events_3 .
*----
*
INTERFACE lif_employee
*----
*
INTERFACE lif_employee.
METHODS:
add_employee
IMPORTING im_no TYPE i
im_name TYPE string
im_wage TYPE i.
ENDINTERFACE.
*******************************************************
Super class LCL_CompanyEmployees
*******************************************************
CLASS lcl_company_employees DEFINITION.
PUBLIC SECTION.
INTERFACES lif_employee.
TYPES:
BEGIN OF t_employee,
no TYPE i,
name TYPE string,
wage TYPE i,
END OF t_employee.
METHODS:
constructor,
add_employee "Removed
IMPORTING im_no TYPE i
im_name TYPE string
im_wage TYPE i,
display_employee_list,
display_no_of_employees.
PRIVATE SECTION.
CLASS-DATA: i_employee_list TYPE TABLE OF t_employee,
no_of_employees TYPE i.
ENDCLASS.
*-- CLASS LCL_CompanyEmployees IMPLEMENTATION
CLASS lcl_company_employees IMPLEMENTATION.
METHOD constructor.
no_of_employees = no_of_employees + 1.
ENDMETHOD.
METHOD lif_employee~add_employee.
Adds a new employee to the list of employees
DATA: l_employee TYPE t_employee.
l_employee-no = im_no.
l_employee-name = im_name.
l_employee-wage = im_wage.
APPEND l_employee TO i_employee_list.
ENDMETHOD.
METHOD display_employee_list.
Displays all employees and there wage
DATA: l_employee TYPE t_employee.
WRITE: / 'List of Employees'.
LOOP AT i_employee_list INTO l_employee.
WRITE: / l_employee-no, l_employee-name, l_employee-wage.
ENDLOOP.
ENDMETHOD.
METHOD display_no_of_employees.
Displays total number of employees
SKIP 3.
WRITE: / 'Total number of employees:', no_of_employees.
ENDMETHOD.
ENDCLASS.
*******************************************************
Sub class LCL_BlueCollar_Employee
*******************************************************
CLASS lcl_bluecollar_employee DEFINITION
INHERITING FROM lcl_company_employees.
PUBLIC SECTION.
METHODS:
constructor
IMPORTING im_no TYPE i
im_name TYPE string
im_hours TYPE i
im_hourly_payment TYPE i,
lif_employee~add_employee REDEFINITION..
PRIVATE SECTION.
DATA:no TYPE i,
name TYPE string,
hours TYPE i,
hourly_payment TYPE i.
ENDCLASS.
*---- CLASS LCL_BlueCollar_Employee IMPLEMENTATION
CLASS lcl_bluecollar_employee IMPLEMENTATION.
METHOD constructor.
The superclass constructor method must be called from the subclass
constructor method
CALL METHOD super->constructor.
no = im_no.
name = im_name.
hours = im_hours.
hourly_payment = im_hourly_payment.
ENDMETHOD.
METHOD lif_employee~add_employee.
Calculate wage an call the superclass method add_employee to add
the employee to the employee list
DATA: l_wage TYPE i.
l_wage = hours * hourly_payment.
CALL METHOD super->lif_employee~add_employee
EXPORTING im_no = no
im_name = name
im_wage = l_wage.
ENDMETHOD.
ENDCLASS.
*******************************************************
Sub class LCL_WhiteCollar_Employee
*******************************************************
CLASS lcl_whitecollar_employee DEFINITION
INHERITING FROM lcl_company_employees.
PUBLIC SECTION.
METHODS:
constructor
IMPORTING im_no TYPE i
im_name TYPE string
im_monthly_salary TYPE i
im_monthly_deductions TYPE i,
lif_employee~add_employee REDEFINITION.
PRIVATE SECTION.
DATA:
no TYPE i,
name TYPE string,
monthly_salary TYPE i,
monthly_deductions TYPE i.
ENDCLASS.
*---- CLASS LCL_WhiteCollar_Employee IMPLEMENTATION
CLASS lcl_whitecollar_employee IMPLEMENTATION.
METHOD constructor.
The superclass constructor method must be called from the subclass
constructor method
CALL METHOD super->constructor.
no = im_no.
name = im_name.
monthly_salary = im_monthly_salary.
monthly_deductions = im_monthly_deductions.
ENDMETHOD.
METHOD lif_employee~add_employee.
Calculate wage an call the superclass method add_employee to add
the employee to the employee list
DATA: l_wage TYPE i.
l_wage = monthly_salary - monthly_deductions.
CALL METHOD super->lif_employee~add_employee
EXPORTING im_no = no
im_name = name
im_wage = l_wage.
ENDMETHOD.
ENDCLASS.
*******************************************************
R E P O R T
*******************************************************
DATA:
Object references
o_bluecollar_employee1 TYPE REF TO lcl_bluecollar_employee,
o_whitecollar_employee1 TYPE REF TO lcl_whitecollar_employee.
START-OF-SELECTION.
Create bluecollar employee obeject
CREATE OBJECT o_bluecollar_employee1
EXPORTING im_no = 1
im_name = 'Gylle Karen'
im_hours = 38
im_hourly_payment = 75.
Add bluecollar employee to employee list
CALL METHOD o_bluecollar_employee1->lif_employee~add_employee
EXPORTING im_no = 1
im_name = 'Karen Johnson'
im_wage = 0.
Create whitecollar employee obeject
CREATE OBJECT o_whitecollar_employee1
EXPORTING im_no = 2
im_name = 'John Dickens'
im_monthly_salary = 10000
im_monthly_deductions = 2500.
Add bluecollar employee to employee list
CALL METHOD o_whitecollar_employee1->lif_employee~add_employee
EXPORTING im_no = 1
im_name = 'Gylle Karen'
im_wage = 0.
Display employee list and number of employees. Note that the result
will be the same when called from o_whitecollar_employee1 or
o_bluecolarcollar_employee1, because the methods are defined
as static (CLASS-METHODS)
CALL METHOD o_whitecollar_employee1->display_employee_list.
CALL METHOD o_whitecollar_employee1->display_no_of_employees.
4. Events
This is the same example as example 4. All changes are marked with red. There have been no canges to the subclasses, only to the superclass and the report, sp the code for th esubclasses is not shown.
For a simple example refer to Events in Examples.
REPORT zbc404_hf_events_4 .
*----
*
INTERFACE lif_employee
*----
*
INTERFACE lif_employee.
METHODS:
add_employee
IMPORTING im_no TYPE i
im_name TYPE string
im_wage TYPE i.
ENDINTERFACE.
*******************************************************
Super class LCL_CompanyEmployees
*******************************************************
CLASS lcl_company_employees DEFINITION.
PUBLIC SECTION.
TYPES:
BEGIN OF t_employee,
no TYPE i,
name TYPE string,
wage TYPE i,
END OF t_employee.
Declare event. Note that declaration could also be placed in the
interface
EVENTS: employee_added_to_list
EXPORTING value(ex_employee_name) TYPE string.
CLASS-EVENTS: Events can also be defined as class-events
INTERFACES lif_employee.
METHODS:
constructor,
display_employee_list,
display_no_of_employees,
Declare event method
on_employee_added_to_list FOR EVENT employee_added_to_list OF lcl_company_employees
IMPORTING ex_employee_name sender.
PRIVATE SECTION.
CLASS-DATA:
i_employee_list TYPE TABLE OF t_employee,
no_of_employees TYPE i.
ENDCLASS.
*-- CLASS LCL_CompanyEmployees IMPLEMENTATION
CLASS lcl_company_employees IMPLEMENTATION.
METHOD constructor.
no_of_employees = no_of_employees + 1.
ENDMETHOD.
METHOD lif_employee~add_employee.
Adds a new employee to the list of employees
DATA: l_employee TYPE t_employee.
l_employee-no = im_no.
l_employee-name = im_name.
l_employee-wage = im_wage.
APPEND l_employee TO i_employee_list.
Raise event employee_added_to_list
RAISE EVENT employee_added_to_list
EXPORTING ex_employee_name = l_employee-name.
ENDMETHOD.
METHOD display_employee_list.
Displays all employees and there wage
DATA: l_employee TYPE t_employee.
WRITE: / 'List of Employees'.
LOOP AT i_employee_list INTO l_employee.
WRITE: / l_employee-no, l_employee-name, l_employee-wage.
ENDLOOP.
ENDMETHOD.
METHOD display_no_of_employees.
Displays total number of employees
SKIP 3.
WRITE: / 'Total number of employees:', no_of_employees.
ENDMETHOD.
METHOD on_employee_added_to_list.
Event method
WRITE: / 'Employee added to list', ex_employee_name.
ENDMETHOD.
ENDCLASS.
*******************************************************
Sub class LCL_BlueCollar_Employee
*******************************************************
CLASS lcl_bluecollar_employee DEFINITION
INHERITING FROM lcl_company_employees.
See code in example 3...
ENDCLASS.
CLASS lcl_bluecollar_employee IMPLEMENTATION.
See code in example 3...
ENDCLASS.
*******************************************************
Sub class LCL_WhiteCollar_Employee
*******************************************************
CLASS lcl_whitecollar_employee DEFINITION
See code in example 3...
ENDCLASS.
CLASS lcl_whitecollar_employee IMPLEMENTATION.
See code in example 3...
ENDCLASS.
*******************************************************
R E P O R T
*******************************************************
DATA:
Object references
o_bluecollar_employee1 TYPE REF TO lcl_bluecollar_employee,
o_whitecollar_employee1 TYPE REF TO lcl_whitecollar_employee.
START-OF-SELECTION.
Create bluecollar employee obeject
CREATE OBJECT o_bluecollar_employee1
EXPORTING im_no = 1
im_name = 'Karen Johnson'
im_hours = 38
im_hourly_payment = 75.
Register event for o_bluecollar_employee1
SET HANDLER o_bluecollar_employee1->on_employee_added_to_list
FOR o_bluecollar_employee1.
Add bluecollar employee to employee list
CALL METHOD o_bluecollar_employee1->lif_employee~add_employee
EXPORTING im_no = 1
im_name = 'Gylle Karen'
im_wage = 0.
Create whitecollar employee obeject
CREATE OBJECT o_whitecollar_employee1
EXPORTING im_no = 2
im_name = 'John Dickens'
im_monthly_salary = 10000
im_monthly_deductions = 2500.
Register event for o_whitecollar_employee1
SET HANDLER o_whitecollar_employee1->on_employee_added_to_list
FOR o_whitecollar_employee1.´
Add bluecollar employee to employee list
CALL METHOD o_whitecollar_employee1->lif_employee~add_employee
EXPORTING im_no = 1
im_name = 'Gylle Karen'
im_wage = 0.
Display employee list and number of employees. Note that the result
will be the same when called from o_whitecollar_employee1 or
o_bluecolarcollar_employee1, because the methods are defined
as static (CLASS-METHODS)
CALL METHOD o_whitecollar_employee1->display_employee_list.
CALL METHOD o_whitecollar_employee1->display_no_of_employees.
Result:
Employee added to list Karen Johnson
Employee added to list John Dickens
List of Employees
1 Karen Johnson 2.850
2 John Dickens 7.500
Total number of employees: 2