SAP ABAP OBJECTS Tutorials

General

Control Framework and enheritance hierarchy

The control framework consists of 2 parts:

CL_GUI_CFW contains methods that provide services for communication with the frontend, and can be used both by control wrapper calsses and by control progtrammers
The CL_GUI_OBJECT encapsulates ActiveX or JavaBeans methods, while CL_GUI_CONTROL is responsible for displaying the control on the screen.

CL_GUI_OBJECT -> CL_GUI_CONTROL -> CL_GUI_* (Wrapper class)

These classes contains methods that are enherited by subsequent classes in the enheritance tree.

Synchronization/Flush

RFC calls is used to synchronize the front and backend. This synchronization takes palce at some predifined points in the program flow. However you should not rely entirely on automatic synchronization, but force synchronization with the Flush method when necessary.

Bear in mind that the synchronization/RFC calls represenmts a bottleneck, so you should keep the not use the Flush method to a minimum.

Syntax:

CALL METHOD cl_gui_cfw=>FLUSH

Set up event handling for controls

There are two types of events:

Application events. T

The flow logic of the screen is processed after the event (The PAI module is processed).

In the events table the event must be registred as an application event by setting then field appl_event to ‘X’:

wa_events-eventid = cl_gui_textedit=>event_double_click.
wa_events-appl_event = ‘X’.
append wa_events to i_events.

Important: The dispatch method of cl_gui_cfw must be called in the PAI module:

CALL METHOD cl_gui_cfw=>dispatch.

System events.

For system events the flow-logic of the screen is not executed. That means that the PAI and PBO modules are not processed.

In the events table the the field appl_event must be set to SAPCE:

wa_events-eventid = cl_gui_textedit=>event_double_click.
wa_events-appl_event = space.
append wa_events to i_events.

The dispatch method of cl_gui_cfw must NOT be called.

Example

It is presumed that a SAP toolbar control named go_toolbar of class cl_gui_toolbar has been defined. To see a complete example of how to handle events, refer to The SAP toolbar control.

Data:

* 1.  Define and instance of the eventhandler class.

*      If the event handler class is defined after the data decalaration

*      the class must be declared as DEFERRED in the top of the program:  CLASS cls_event_handler DEFINITION DEFERRED.

go_event_handler TYPE REF TO cls_event_handler,

* 2. Define table for registration of events.

*     Note that a TYPE REF   to cls_event_handler must be created before you can

*     reference types cntl_simple_events and cntl_simple_event

gi_events TYPE cntl_simple_events,

* Workspace for table gi_events

g_event TYPE cntl_simple_event.

* 3. Define and implement eventhandler class

CLASS cls_event_handler DEFINITION.

PUBLIC SECTION.

METHODS:

*     Syntax:

*     <method name> FOR EVENT <event of control – see the control documentation>

*         OF <class of object> <importing parameters l – see the control documentation>

on_function_selected

FOR EVENT function_selected OF cl_gui_toolbar

IMPORTING fcode,

ENDCLASS.

CLASS cls_event_handler IMPLEMENTATION.

METHOD on_function_selected.

*     Do something when the event is raised

ENDMETHOD.

ENDCLASS.

* 4. Append events to the events table

*     The Event Id  can be found in the control documentation. Note that The event below is registred as an application event

g_event-eventid         = go_toolbar->m_id_function_selected.

g_event-appl_event   = ‘X’.    “This is an application event

APPEND g_event TO gi_events.

…. append more events i necessary…

* 5. Use the events table to register events for the control

CALL METHOD go_toolbar->set_registered_events

EXPORTING

events = gi_events.

* 6. Create event handler

CREATE OBJECT   go_event_handler.

* Syntax:

* SET HANDLER <event handler class> -> <Event handler method>

*   FOR <control>

SET HANDLER go_event_handler->on_function_selected

FOR go_toolbar.

Declare a reference variable before the class has been defined

Scenario:

DATA: o_my_class TYPE REF TO lcl_myclass.

CLASS lcl_myclass…..
ENDCLASS.

This will cause an error because the definition of class lcl_myclass is after the declaration.

Solution:

Define the class in the beginning of the program with DEFINITION DEFERRED:

CLASS lcl_myclass DEFINITION DEFERRED.

DATA: o_my_class TYPE REF TO lcl_myclass.

CLASS lcl_myclass…..
ENDCLASS.

Set focus to a control

Set the focus to a control named go_grid:

CALL METHOD cl_gui_control=>set_focus

EXPORTING control = go_grid.

To Use BADI – Business Add In you need to Understand ABAP OO Interface Concept

Content Author:  Jayanta Narayan Choudhuri
Author Email:      sss@cal.vsnl.net.in
Author Website:  http://www.geocities.com/ojnc

* For Rajat’s OO ans BAdI Education

Report Z_CTRY.

* A Shape Interface “Like a shape” “Behaves like a Shape”

Adverb/Adjective

Interface IShape.

Methods:  getArea Returning Value(area) Type F,

getCircumference Returning Value(circumference) Type F.

Endinterface.

* A Circle Class that behaves like a Shape

Class CCircle Definition.

Public Section.

Interfaces IShape.

Aliases:   getArea For IShape~getArea,

getCircumference For IShape~getCircumference.

Methods:   Constructor Importing pRadius Type F,

setRadius   Importing pRadius Type F,

getRadius   Returning Value(pRadius) Type F.

Private Section.

Data radius Type F.

Constants PI Type F Value ‘3.141592365359’.

EndClass.

Class CCircle Implementation.

Method Constructor.

radius = pRadius.

EndMethod.

Method setRadius.

radius = pRadius.

EndMethod.

Method getRadius.

pRadius = radius.

EndMethod.

Method IShape~getArea.

Compute area = 2 * PI * radius.

EndMethod.

Method IShape~getCircumference.

Compute circumference = PI * radius * radius.

EndMethod.

EndClass.

* A Square Class

Class CSquare Definition.

Public Section.

Interfaces IShape.

Aliases:   getArea For IShape~getArea,

getCircumference For IShape~getCircumference.

Methods:   Constructor Importing pSide Type F.

Private Section.

Data side Type F.

EndClass.

Class CSquare Implementation.

Method Constructor.

side = pSide.

EndMethod.

Method IShape~getArea.

Compute area = side * side.

EndMethod.

Method IShape~getCircumference.

Compute circumference = 4 * side.

EndMethod.

EndClass.

* A Rectangle Class

Class CRectangle Definition.

Public Section.

Interfaces IShape.

Aliases:   getArea For IShape~getArea,

getCircumference For IShape~getCircumference.

Methods:   Constructor Importing pHeight Type F

pLength Type F.

Private Section.

Data: height Type F,

length Type F.

EndClass.

Class CRectangle Implementation.

Method Constructor.

height = pHeight.

length = pLength.

EndMethod.

Method IShape~getArea.

Compute area = height * length.

EndMethod.

Method IShape~getCircumference.

Compute circumference = 2 * ( height + length ).

EndMethod.

EndClass.

* START of PROGRAM

* Array of Shapes

Data:  OneShape   Type Ref To IShape,             ” One Object with

Shape Behaviour

ShapeTable Type Table Of Ref To IShape.    ” Array of Objects

with Shape Behaviour

* Concrete Objects with IShape Behaviour!

Data:  C1 Type Ref To CCircle,

S1 Type Ref To CSquare,

R1 Type Ref To CRectangle,

C2 Type Ref To CCircle,

S2 Type Ref To CSquare,

R2 Type Ref To CRectangle.

Data:  descr_ref  TYPE ref to CL_ABAP_TYPEDESCR,

ClassName  Type String,

Serial     Type I.

Data:  myArea          Type F,

myCircumference Type F.

START-OF-SELECTION.

Create Object C1 Exporting pRadius = ‘2.5’.

Create Object C2 Exporting pRadius = ‘5.0’.

Create Object S1 Exporting pSide = ‘3.5’.

Create Object S2 Exporting pSide = ‘6.0’.

Create Object R1 Exporting pHeight = ‘2.8’ pLength = ‘3.4’.

Create Object R2 Exporting pHeight = ‘1.7’ pLength = ‘6.3’.

* Append in any order!

Append S1  to ShapeTable.

Append R2  to ShapeTable.

Append R1  to ShapeTable.

Append C2  to ShapeTable.

Append C1  to ShapeTable.

Append S2  to ShapeTable.

Serial = 0.

Loop At ShapeTable into OneShape.

Call Method OneShape->getArea

Receiving area = myArea.

Call Method OneShape->getCircumference

Receiving circumference = myCircumference.

descr_ref = CL_ABAP_TYPEDESCR=>Describe_By_Object_Ref( OneShape

).

Call Method descr_ref->get_relative_name

Receiving P_RELATIVE_NAME = ClassName.

Add 1 to Serial.

Write: / Serial,  ClassName.

Write: / ‘Area          ‘,  myArea          Decimals 4 Exponent

0.

Write: / ‘Circumference ‘,  myCircumference Decimals 4 Exponent

0.

Write: /.

EndLoop.

** Results

*           1  CSQUARE

*  Area                            12.2500

*  Circumference                   14.0000

*

*           2  CRECTANGLE

*  Area                            10.7100

*  Circumference                   16.0000

*

*           3  CRECTANGLE

*  Area                             9.5200

*  Circumference                   12.4000

*

*           4  CCIRCLE

*  Area                            31.4159

*  Circumference                   78.5398

*

*           5  CCIRCLE

*  Area                            15.7080

*  Circumference                   19.6350

*

*           6  CSQUARE

*  Area                            36.0000

*  Circumference                   24.0000