Showing posts with label OO ABAP.. Show all posts
Showing posts with label OO ABAP.. Show all posts

Friday, November 23, 2007

object oriented abap

Public attributes

Private attributes

Instance attributes

Static attributes

Public methods

Private methods

Constructor method

Static constructor

Protected components

Polymorphism

Public attributes
Public attributes are defined in the PUBLIC section and can be viewed and changed from outside the class. There is direct access to public attributes. As a general rule, as few public attributes should be defined as possible.

PUBLIC SECTION.
DATA: Counter type i.
Private attributes
Private attributes are defined in the PRIVATE section. The can only be viewes and changed from within the class. There is no direct access from outside the class.

PRIVATE SECTION.
DATA: name(25) TYPE c,
planetype LIKE saplane-planetyp,
Instance attributes
There exist one instance attribute for each instance of the class, thus they exist seperately for each object. Instance attributes are declared with the DATA keyword.

Static attributes
Static attributes exist only once for each class. The data are the same for all instances of the class, and can be used e.g. for instance counters. Static attributes are defined with the keyword CLASS-DATA.

PRIVATE SECTION.


CLASS-DATA: counter type i,
Public methods
Can called from outside the class

PUBLIC SECTION.


METHODS: set_attributes IMPORTING p_name(25) TYPE c,


p_planetype LIKE saplane-planetyp,


Private methods
Can only be called from inside the class. They are placed in the PRIVATE section of the class.

Constructor method
Implicitly, each class has an instance constructor method with the reserved name constructor and a static constructor method with the reserved name class_constructor.

The instance constructor is executed each time you create an object (instance) with the CREATE OBJECT statement, while the class constructor is executed exactly once before you first access a class.

The constructors are always present. However, to implement a constructor you must declare it explicitly with the METHODS or CLASS-METHODS statements. An instance constructor can have IMPORTING parameters and exceptions. You must pass all non-optional parameters when creating an object. Static constructors have no parameters.

Static constructor
The static constructor is always called CLASS_CONSTRUCTER, and is called autmatically before the clas is first accessed, that is before any of the following actions are executed:

Creating an instance using CREATE_OBJECT
Adressing a static attribute using ->
Calling a ststic attribute using CALL METHOD
Registering a static event handler
Registering an evetm handler method for a static event
The static constructor cannot be called explicitly.

Protected components
When we are talking subclassing and enheritance there is one more component than Public and Private, the Protected component. Protected components can be used by the superclass and all of the subclasses. Note that Subclasses cannot access Private components.

Polymorphism
Polymorphism: When the same method is implemented differently in different classes. This can be done using enheritance, by redefining a method from the superclass in subclasses and implement it differently.

ABAP SYNTAX FOR OOP(OBJECT ORIENTED)

Template for making a class | Template for calling a class | Subclass | Template for calling a class | Using a class as a parameter for a method | Interfaces | Events

Template for making a class
Delete the parts that should not be used

******************************************
* Definition part
******************************************
CLASS xxx DEFINITION.
*------------------------------
* Public section
*------------------------------
PUBLIC SECTION.
TYPES:
DATA:
* Static data
CLASS-DATA:
* Methods
METHODS:
* Using the constructor to initialize parameters
constructor IMPORTING xxx type yyy,

* Method with parameters
mm1 IMPORTING iii TYPE ttt.
* Method without parameters
mm2.
* Static methods
CLASS-METHODS:

*---------------------------------------------------*
* Protected section. Also accessable by subclasses
*---------------------------------------------------
PROTECTED SECTION.
*---------------------------------------------------
* Private section. Not accessable by subclasses
*---------------------------------------------------
PRIVATE SECTION.
ENDCLASS.
******************************************
* Implementation part
******************************************
CLASS lcl_airplane IMPLEMENTATION.
METHOD constructor.
ENDMETHOD.

METHOD mm1.
ENDMETHOD.

METHOD mm2.
ENDMETHOD.

ENDCLASS.
Template for calling a class*
Create reference to class lcl_airplane
DATA: airplane1 TYPE REF TO lcl_airplane.

START-OF-SELECTION.
* Create instance using parameters in the cosntructor method
CREATE OBJECT airplane1 exporting im_name = 'Hansemand'
im_planetype = 'Boing 747'.
* Calling a method with parameters
CALL METHOD: airplane1->display_n_o_airplanes,
airplane1->display_attributes.
Subclass
CLASS xxx DEFINITION INHERITING FROM yyy.

Using af class as a parameter for a method
The class LCL_AIRPLANE is used as a parameter for method add_a_new_airplane:

METHODS:

add_a_new_airplane importing im_airplane TYPE REF to lcl_airplane.


Interfaces
In ABAP interfaces are implemented in addition to, and independently of classes. An interface only has a declaration part,
and do not have visibility sections. Components (Attributes, methods, constants, types) can be defined the same way as in classes.

· Interfaces are listed in the definition part lof the class, and must always be in the PUBLIC SECTION.
· Operations defined in the interface atre impemented as methods of the class. All methods of the interface
must be present in the implementation part of the class.
· Attributes, events, constants and types defined in the interface are automatically available to the class
carrying out the implementation.
· Interface components are addressed in the class by ~


Example of how to implement an interface:
INTERFACE lif_document

DATA: author type ref to lcl_author.
METHODS: print,

display.

ENDINTERFACE.
CLASS lcl_text_document DEFINITION.
PUBLIC SECTION.

INTERFACES lif_document.

METHODS display.

ENDCLASS.
CLASS lcl_text_document IMPLEMENTTION.

METHOD lif_document~print.

ENDMETHOD.
METHOD lif_document~display

ENDMETHOD.

METHOD display.

ENDMETHOD.

ENDCLASS.
REPORT zzz.
DATA: text_doc TYPE REF TO lcl_document.
Start-of-selection.

CREATE OBJECT text_doc.
CALL METHOD text_doc->lif_document~print.

CALL METHOD text_doc->lif_document~display.

CALL METHOD text_doc->display.

Events
For events of controls, refer to How to use controls.

· Events can only have EXPORTING parameters
· When an event is triggered, only those events handlers that have registered themselves
using SET HANDLER by this point of runtime are executed. You can register an event using
ACTIVATION 'X' and derigert it by using ACTIVATION 'SPACE'.
Defining events:
CLASS DEFINITION.
EVENTS: EXPORTING VALUE () TYPE type.
CLASS IMPLEMENTATION.

METHOD :

RAISE EVENT EXPORTING = .
Handling events:
CLASS DEFINITION.

METHODS: FOR OF ! IMPORTING ... SENDER.
Setting handler
SET HANDLER FOR ! FOR ALL INSTANCES

[ACTIVATION ]

EXAMPLES OF OBJECT ORIENTED ABAP PROGRAMMING

Define, implement and use simple class

***INCLUDE ZBC404_HF_LCL_AIRPLANE .
******************************************
* Definition part
******************************************
CLASS lcl_airplane DEFINITION.
*------------------------------
* Public section
*------------------------------
PUBLIC SECTION.
TYPES: t_name(25) TYPE c.
METHODS:
constructor,
set_attributes IMPORTING p_name TYPE t_name
p_planetype TYPE saplane-planetype,
display_attributes,
display_n_o_airplanes.
*------------------------------
* Private section
*------------------------------
PRIVATE SECTION.
* Private attributes
DATA: name(25) TYPE c,
planetype TYPE saplane-planetype.
* Private static attribute
CLASS-DATA n_o_airplanes TYPE i.
ENDCLASS.
******************************************
* Implementation part
******************************************
CLASS lcl_airplane IMPLEMENTATION.
METHOD constructor.
* Counts number of instances
n_o_airplanes = n_o_airplanes + 1.
ENDMETHOD.

METHOD set_attributes.
name = p_name.
planetype = p_planetype.
ENDMETHOD.

METHOD display_attributes.
WRITE:/ 'Name:', name, 'Planetype:', planetype.
ENDMETHOD.

METHOD display_n_o_airplanes.
WRITE: / 'No. planes:', n_o_airplanes.
ENDMETHOD.

ENDCLASS.
REPORT zbc404_hf_maintain_airplanes .

INCLUDE zbc404_hf_lcl_airplane.

* Create reference to class lcl_airplane
DATA: airplane1 TYPE REF TO lcl_airplane,
airplane2 TYPE REF TO lcl_airplane.

START-OF-SELECTION.

* Create instance
CREATE OBJECT airplane1.
CALL METHOD: airplane1->display_n_o_airplanes.

CREATE OBJECT airplane2.

* Setting attributes using a method with parameters
CALL METHOD airplane1->set_attributes EXPORTING p_name = 'Kurt'
p_planetype = 'MD80'.
END-OF-SELECTION.
* Using methods
CALL METHOD: airplane1->display_n_o_airplanes,
airplane1->display_attributes.

The resulting report:
Maintain airplanes
No. planes: 1
No. planes: 2
Name: Kurt Planetype: MD80
Use constructor to create an object with parameters
CLASS lcl_airplane DEFINITION.
PUBLIC SECTION.
TYPES: t_name(25) TYPE c.
METHODS:
constructor importing p2_name type t_name
p2_planetype TYPE saplane-planetype,
..... more code .......
CLASS lcl_airplane IMPLEMENTATION.
METHOD constructor.
name = p2_name.
planetype = p2_planetype.
..... more code .......
START-OF-SELECTION.
CREATE OBJECT airplane1 exporting p2_name = 'Hansemand'
p2_planetype = 'Boing 747'.

Subclassing
This example uses a superclass LCL_AIRPLANE and subclasses it into LCL_PASSENGER_AIRPLANE and LCL_CARGO_PLANE.

LCL_AIRPLANE has a method display_n_o_airplanes that displays the number of object instances.

LCL_PASSENGER_AIRPLANE has the private instance attribute n_o_seats, and redefines the superclass method display_attributes,
so it also displays n_o_seats.

LCL_CARGO_PLANE has the private instance attribute cargomax, and redefines the superclass method
display_attributes, so it also displays cargomax.

All instance attributes are provided by the cunstructor method.

Superclass LCL_AIRPLANE

***INCLUDE ZBC404_HF_LCL_AIRPLANE .
******************************************
* Definition part
******************************************
CLASS lcl_airplane DEFINITION.
*------------------------------
* Public section
*------------------------------
PUBLIC SECTION.
TYPES: t_name(25) TYPE c.
METHODS:
constructor IMPORTING im_name TYPE t_name
im_planetype TYPE saplane-planetype,
display_attributes.
* Static methods
CLASS-METHODS:
display_n_o_airplanes.
*------------------------------
* Protected section
*------------------------------
PROTECTED SECTION.
* Private attributes
DATA: name(25) TYPE c,
planetype TYPE saplane-planetype.
* Private static attribute
CLASS-DATA n_o_airplanes TYPE i.
ENDCLASS.
******************************************
* Implementation part
******************************************
CLASS lcl_airplane IMPLEMENTATION.
METHOD constructor.
name = im_name.
planetype = im_planetype.
* Counts number of instances
n_o_airplanes = n_o_airplanes + 1.
ENDMETHOD.
METHOD display_attributes.
WRITE:/ 'Name:', name, 'Planetype:', planetype.
ENDMETHOD.
METHOD display_n_o_airplanes.
WRITE: / 'No. planes:', n_o_airplanes.
ENDMETHOD.
ENDCLASS.
Sub class LCL_PASSENGER_AIRPLANE

***INCLUDE ZBC404_HF_LCL_PASSENGER_PLANE .
*******************************************************************
* This is a subclass of class lcl_airplane
*******************************************************************
CLASS lcl_passenger_airplane DEFINITION INHERITING FROM lcl_airplane.
PUBLIC SECTION.
* The constructor contains the parameters from the superclass
* plus the parameters from the subclass
METHODS:
constructor IMPORTING im_name TYPE t_name
im_planetype TYPE saplane-planetype
im_n_o_seats TYPE sflight-seatsmax,
* Redefinition of superclass method display_attributes
display_attributes REDEFINITION.
PRIVATE SECTION.
DATA: n_o_seats TYPE sflight-seatsmax.
ENDCLASS.
CLASS lcl_passenger_airplane IMPLEMENTATION.
METHOD constructor.
* The constructor method of the superclass MUST be called withing the
* construtor
CALL METHOD super->constructor
EXPORTING im_name = im_name
im_planetype = im_planetype.
n_o_seats = im_n_o_seats.
ENDMETHOD.
* The redefined display_attributes method
METHOD display_attributes.
CALL METHOD super->display_attributes.
WRITE: / 'No. seats:', n_o_seats.
ENDMETHOD.
ENDCLASS.
Sub class LCL_CARGO_PLANE
***INCLUDE ZBC404_HF_LCL_CARGO_PLANE .
*******************************************************************
* This is a subclass of class lcl_airplane
*******************************************************************
CLASS lcl_cargo_plane DEFINITION INHERITING FROM lcl_airplane.
PUBLIC SECTION.
METHODS:
* The constructor contains the parameters from the superclass
* plus the parameters from the subclass
constructor IMPORTING im_name TYPE t_name
im_planetype TYPE saplane-planetype
im_cargomax type scplane-cargomax,
* Redefinition of superclass method display_attributes
display_attributes REDEFINITION.
PRIVATE SECTION.
DATA:cargomax TYPE scplane-cargomax.
ENDCLASS.
CLASS lcl_cargo_plane IMPLEMENTATION.
METHOD constructor.
* The constructor method of the superclass MUST be called withing the


* constructor
CALL METHOD super->constructor
EXPORTING im_name = im_name
im_planetype = im_planetype.
cargomax = im_cargomax.
ENDMETHOD.
METHOD display_attributes.
* The redefined display_attributes method
CALL METHOD super->display_attributes.
WRITE: / 'Cargo max:', cargomax.
ENDMETHOD.
ENDCLASS.


The Main program that uses the classes

REPORT zbc404_hf_main .
* Super class
INCLUDE zbc404_hf_lcl_airplane.
* Sub classes
INCLUDE zbc404_hf_lcl_passenger_plane.
INCLUDE zbc404_hf_lcl_cargo_plane.
DATA:
* Type ref to sub classes. Note: It is not necesssary to make typeref to the superclass
o_passenger_airplane TYPE REF TO lcl_passenger_airplane,
o_cargo_plane TYPE REF TO lcl_cargo_plane.
START-OF-SELECTION.
* Display initial number of instances = 0


CALL METHOD lcl_airplane=>display_n_o_airplanes.
* Create objects
CREATE OBJECT o_passenger_airplane
EXPORTING
im_name = 'LH505'
im_planetype = 'Boing 747'
im_n_o_seats = 350.
CREATE OBJECT o_cargo_plane
EXPORTING
im_name = 'AR13'
im_planetype = 'DC 3'
im_cargomax = 35.
* Display attributes
CALL METHOD o_passenger_airplane->display_attributes.
CALL METHOD o_cargo_plane->display_attributes.
* Call static method display_n_o_airplanes
* Note: The syntax for calling a superclass method, differs from the syntax when calling a
* subclass method.
* When calling a superclass => must be used instead of ->


CALL METHOD lcl_airplane=>display_n_o_airplanes.
Result:

No. planes: 0
Name: LH505 Planetype: Boing 747
No. seats: 350
Name: AR13 Planetype: DC 3
Cargo max: 35,0000
No. planes: 2

Polymorphism
Polymorphism: When the same method is implemented differently in different classes. This can be done u
sing enheritance, by redefining a method from the superclass in subclasses and implement it differently.

Classes:

lcl_airplane Superclass
lcl_cargo_airplane Subclass
lcl_passenger_airplane Subclass
The method estimate_fuel_consumption is implemented differently in the 3 classes, as it depends on the airplane type.

Object from different classes are stored in an internal table (plane_list) consisting of references to the
superclass, and the processed identically for all the classes.
What coding for the estimate_fuel_consumption method taht is actually executed, depends on the dynamic
type of the plane reference variable, that is, depends on which object plane points to.
DATA: cargo_plane TYPE REF to lcl_cargo_airplane,
passenger_plane TYPE REF to lcl_passenger_airplane,
plane_list TYPE TABLE OF REF TO lcl_airplane.
* Creating the list of references
CREATE OBJECT cargo_plane.
APPEND cargo_plane to plane_list.
CREATE OBJECT passenger_plane
APPEND passenger_plane to plane list.
* Generic method for calucalting required fuel
METHOD calculate required_fuel.
DATA: plane TYPE REF TO lcl_airplane.
LOOP AT plane_list INTO plane.
re_fuel = re_fuel + plane->estimate_fuel_consumption( distance ).
ENDLOOP.
ENDMETHOD.
Working example:
This example assumes that the classes lcl_airplane, lcl_passnger_airplane and lcl_cargo plane exists.
Create objects of type lcl_cargo_plane and lcl_passenger_airplane, adds them to a list in lcl_carrier,
and displays the list.
*&---------------------------------------------------------------------*
*& Include ZBC404_HF_LCL_CARRIER *
*& *
*&---------------------------------------------------------------------*
* CLASS lcl_carrier DEFINITION *
*----------------------------------------------------------------------*
CLASS lcl_carrier DEFINITION.
PUBLIC SECTION.
TYPES: BEGIN OF flight_list_type,
connid TYPE sflight-connid,
fldate TYPE sflight-fldate,
airplane TYPE REF TO lcl_airplane,
seatsocc TYPE sflight-seatsocc,
cargo(5) TYPE p DECIMALS 3,
END OF flight_list_type.
METHODS: constructor IMPORTING im_name TYPE string,
get_name RETURNING value(ex_name) TYPE string,
add_a_new_airplane IMPORTING
im_airplane TYPE REF TO lcl_airplane,
create_a_new_flight importing
im_connid type sflight-connid
im_fldate type sflight-fldate
im_airplane type ref to lcl_airplane
im_seatsocc type sflight-seatsocc
optional
im_cargo type p optional,
display_airplanes.
PRIVATE SECTION.
DATA: name TYPE string,
list_of_airplanes TYPE TABLE OF REF TO lcl_airplane,
list_of_flights TYPE TABLE OF flight_list_type.
ENDCLASS.
*---------------------------------------------------------------------*


* CLASS lcl_carrier IMPLEMENTATION


*---------------------------------------------------------------------*


CLASS lcl_carrier IMPLEMENTATION.
METHOD constructor.
name = im_name.
ENDMETHOD.
METHOD get_name.
ex_name = name.
ENDMETHOD.
METHOD create_a_new_flight.
DATA: wa_list_of_flights TYPE flight_list_type.
wa_list_of_flights-connid = im_connid.
wa_list_of_flights-fldate = im_fldate.
wa_list_of_flights-airplane = im_airplane.
IF im_seatsocc IS INITIAL.
wa_list_of_flights-cargo = im_cargo.
ELSE.
wa_list_of_flights-seatsocc = im_seatsocc.
ENDIF.
APPEND wa_list_of_flights TO list_of_flights.
ENDMETHOD.
METHOD add_a_new_airplane.
APPEND im_airplane TO list_of_airplanes.
ENDMETHOD.
METHOD display_airplanes.
DATA: l_airplane TYPE REF TO lcl_airplane.
LOOP AT list_of_airplanes INTO l_airplane.
CALL METHOD l_airplane->display_attributes.
ENDLOOP.
ENDMETHOD.
ENDCLASS.


REPORT zbc404_hf_main .
*******************************************************************


* This reprort uses class LCL_AIRPLNAE and subclasses


* LCL_CARGO_PLANE and LCL_PASSENGER_AIRPLANE and class LCL_CARRIER


*******************************************************************
* Super class for airplanes
INCLUDE zbc404_hf_lcl_airplane.
* Sub classes for airplanes
INCLUDE zbc404_hf_lcl_passenger_plane.
INCLUDE zbc404_hf_lcl_cargo_plane.
* Carrier class
INCLUDE zbc404_hf_lcl_carrier.
DATA:
* Type ref to classes
o_passenger_airplane TYPE REF TO lcl_passenger_airplane,
o_passenger_airplane2 TYPE REF TO lcl_passenger_airplane,
o_cargo_plane TYPE REF TO lcl_cargo_plane,
o_cargo_plane2 TYPE REF TO lcl_cargo_plane,
o_carrier TYPE REF TO lcl_carrier.
START-OF-SELECTION.
* Create objects
CREATE OBJECT o_passenger_airplane
EXPORTING
im_name = 'LH505'
im_planetype = 'Boing 747'
im_n_o_seats = 350.
CREATE OBJECT o_passenger_airplane2
EXPORTING
im_name = 'SK333'
im_planetype = 'MD80'
im_n_o_seats = 110.
CREATE OBJECT o_cargo_plane
EXPORTING
im_name = 'AR13'
im_planetype = 'DC 3'
im_cargomax = 35.
CREATE OBJECT o_cargo_plane2
EXPORTING
im_name = 'AFL124'
im_planetype = 'Iljutsin 2'
im_cargomax = 35000.
CREATE OBJECT o_carrier
EXPORTING im_name = 'Spritisch Airways'.
* Add passenger and cargo planes to the list of airplanes
CALL METHOD o_carrier->add_a_new_airplane
EXPORTING im_airplane = o_passenger_airplane.
CALL METHOD o_carrier->add_a_new_airplane
EXPORTING im_airplane = o_passenger_airplane2.
CALL METHOD o_carrier->add_a_new_airplane
EXPORTING im_airplane = o_cargo_plane.
CALL METHOD o_carrier->add_a_new_airplane
EXPORTING im_airplane = o_cargo_plane2.
* Display list of airplanes


call method o_carrier->display_airplanes.
Result:
Name: LH505 Planetype: Boing 747
No. seats: 350
Name: SK333 Planetype: MD80
No. seats: 110
Name: AR13 Planetype: DC 3
Cargo max: 35,0000
Name: AFL124 Planetype: Iljutsin 2
Cargo max: 35.000,0000



Events
Below is a simple example of how to implement an event.



REPORT zbc404_hf_events_5.
*---------------------------------------------------------------------*


* CLASS lcl_dog DEFINITION


*---------------------------------------------------------------------*


CLASS lcl_dog DEFINITION.
PUBLIC SECTION.
* Declare events
EVENTS:
dog_is_hungry
EXPORTING value(ex_time_since_last_meal) TYPE i.
METHODS:
constructor
IMPORTING im_name TYPE string,
set_time_since_last_meal
IMPORTING im_time TYPE i,
on_dog_is_hungry FOR EVENT dog_is_hungry OF lcl_dog
IMPORTING ex_time_since_last_meal.
ENDCLASS.



*---------------------------------------------------------------------*


* CLASS lcl_dog IMPLEMENTATION


*---------------------------------------------------------------------*
CLASS lcl_dog IMPLEMENTATION.
METHOD constructor.
WRITE: / 'I am a dog and my name is', im_name.
ENDMETHOD.
METHOD set_time_since_last_meal.
IF im_time < 4.
SKIP 1.
WRITE: / 'You fool, I am not hungry yet'.
ELSE.
* Subsrcribe for event:


* set handler


* FOR !FOR ALL INSTANCES [ACTIVATION ]
SET HANDLER on_dog_is_hungry FOR ALL INSTANCES ACTIVATION 'X'.
* Raise event
RAISE EVENT dog_is_hungry
EXPORTING ex_time_since_last_meal = im_time.
ENDIF.
ENDMETHOD.
METHOD on_dog_is_hungry.
* Event method, called when the event dog_is_hungry is raised
SKIP 1.
WRITE: / 'You son of a LLLL. I have not eaten for more than',
ex_time_since_last_meal, ' hours'.
WRITE: / 'Give me something to eat NOW!'.
ENDMETHOD.
ENDCLASS.
*---------------------------------------------------------------------*


* R E P O R T


*---------------------------------------------------------------------*
DATA: o_dog1 TYPE REF TO lcl_dog.
START-OF-SELECTION.
CREATE OBJECT o_dog1 EXPORTING im_name = 'Beefeater'.
CALL METHOD o_dog1->set_time_since_last_meal
EXPORTING im_time = 2.
* This method call will raise the event dog_is_hungy


* because time > 3


CALL METHOD o_dog1->set_time_since_last_meal
EXPORTING im_time = 5.
Result:

I am a dog and my name is Beefeater

You fool, I am not hungry yet

You son of a LLLL. I have not eaten for more than 5 hours
Give me something to eat NOW!

generation of languages(types)

OOP languages can also be seen as Sixth Generation Languages (
1.GL: machine 40 code;
2.GL: mnemonic languages like assembler, BASIC, FORTRAN and C;
3.GL: Typed procedural languages like PASCAL, Visual Basic and PL/1;
4:GLQuery languages like SQL;
5.GL: Artificial Intelligence descriptive modeling languages like LISP, PROLOG, VRML;
6.GL: Distributed communication

languages like SmallTalk, DELPHI, ADA, Java, VB.NET (maybe), C++).

oops by multi language support

oops by multilanguage support

How to implement F4 search help in OO ALV Grid

I have searched SDN myself for the answer for sometime. All the friends that answered were helpful but there were only example links. So here is a simple code to implement F4 search help for OO ALV grid cells:

First we have to define the method and sub programs. In the definition part of our events class, include a code as the following:

CLASS lcl_event_handler DEFINITION .
PUBLIC SECTION .
METHODS:
...
....

on_f4 FOR EVENT onf4 OF cl_gui_alv_grid
IMPORTING e_fieldname
es_row_no
er_event_data
et_bad_cells

e_display.

....

ENDCLASS.

For the implementation part, code can be as follows:
CLASS lcl_event_handler IMPLEMENTATION .
.....
....
METHOD on_f4.
PERFORM on_f4 USING e_fieldname
es_row_no-row_id
er_event_data
et_bad_cells
e_display
er_data_changed.
er_event_data->m_event_handled = 'X'.
ENDMETHOD. "on_f4
.....
.....
ENDCLASS .

Within our main program after displaying the table (by method set_table_for_first_display), we have to create the event handler object and set the handlers for these events.

CREATE OBJECT gr_event_handler .
*--Registering handler methods to handle ALV Grid events
.....
.....
SET HANDLER gr_event_handler->on_f4 FOR gr_alvgrid .
PERFORM register_f4_fields. "set cells with search help
*§3.Optionally register ENTER to raise event DATA_CHANGED.
call method gr_alvgrid->register_edit_event
EXPORTING
i_event_id = cl_gui_alv_grid=>mc_evt_modified.
...... ....... ......
FORM register_f4_fields. "which fields will have F4 search help DATA: lt_f4 TYPE lvc_t_f4 WITH HEADER LINE .
DATA: lt_f4_data TYPE lvc_s_f4.
lt_f4_data-fieldname = 'CHARG'.
lt_f4_data-register = 'X' .
* lt_f4_data-getbefore = 'X' .
lt_f4_data-CHNGEAFTER ='X'.
INSERT lt_f4_data INTO TABLE lt_f4.
lt_f4_data-fieldname = 'LGORT'.
lt_f4_data-register = 'X' .
* lt_f4_data-getbefore = 'X' .
lt_f4_data-CHNGEAFTER ='X'.
INSERT lt_f4_data INTO TABLE lt_f4.
CALL METHOD gr_alvgrid->register_f4_for_fields
EXPORTING
it_f4 = lt_f4[].
ENDFORM. "register_f4_fields

The main sub program where we handle the changed cell value is as follows:
FORM on_f4 USING P_E_FIELDNAME
ROW_ID
P_ER_EVENT_DATA
TYPE REF TO CL_ALV_EVENT_DATA
P_ET_BAD_CELLS
P_E_DISPLAY
IR_DATA_CHANGED
TYPE REF TO cl_alv_changed_data_protocol.
DATA: BEGIN OF value_charg OCCURS 0, "the value table that is passed to F4 fm
charg like zpp_kpduzelt-charg,
lgort like zpp_kpduzelt-lgort,
clabs like mchb-clabs,
END OF value_charg.
DATA : ls_mod_cell TYPE lvc_s_modi ,
ls_del_cell TYPE lvc_s_moce ,
ls_good_cell TYPE lvc_s_modi,
lv_value TYPE lvc_value .
DATA : ls_mod_row like line of gt_list.

.....,

.... *§5 define fields and field-symbols for data-update
field-symbols: type lvc_t_modi.
data: ls_modi type lvc_s_modi.
create object ir_data_changed.
SORT ir_data_changed->mt_mod_cells BY row_id .
LOOP AT ir_data_changed->mt_mod_cells
INTO ls_mod_cell.
ENDLOOP.
case p_e_fieldname. "read changed cell
when 'CHARG'.
......
* here must be the code to fill in the possible values table
* and the call to fm F4IF_INT_TABLE_VALUE_REQUEST
......
CALL FUNCTION 'F4IF_INT_TABLE_VALUE_REQUEST'
EXPORTING
retfield = 'CHARG'
value_org = 'S'
* DYNPPROG = SY-REPID
* DYNPNR = SY-DYNNR
* DYNPROFIELD = 'PRUEFLOS'
TABLES
value_tab = value_charg
* field_tab = field_tab
return_tab = return_tab
EXCEPTIONS
parameter_error = 1
no_values_found = 2
OTHERS = 3.
IF sy-subrc = 0 and return_tab-fieldval <> ''.
move return_tab-fieldval to charg.
ls_mod_cell-row_id = row_id.
ls_mod_cell-fieldname = 'CHARG'.
move charg to lv_value.
ls_mod_cell-value = lv_value.
append ls_mod_cell to ir_data_changed->mt_mod_cells.
*******************
call method gr_alvgrid->get_frontend_fieldcatalog
IMPORTING
et_fieldcatalog = lt_fcat.
read table gt_list index row_id into wa_tab.
create data lp_wa like line of gt_list.
assign lp_wa->* to .
= wa_tab.
read table lt_fcat
with key fieldname = ls_mod_cell-fieldname into ls_fieldcat.
move ls_fieldcat-ref_table to l_tabname.
move ls_fieldcat-fieldname to l_fieldname.
assign component ls_fieldcat-fieldname
of structure wa_tab
to .
**§6 assign the cell table fieldsymbol to the dereferenced data table
*and
* fill the table.
assign p_er_event_data->m_data->* to .
append ls_mod_cell to .
....
....
ENDIF.
....
.....
endcase.

Blog Archive