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 <classname>-><attrbute> 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.

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 visibillity 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 carying out the implementation. Interface components are adresse in the class by <interface name>~<component name>

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 triggerede, only those events handlers that have registred 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 <classname> DEFINITION. EVENTS: <event> EXPORTING VALUE (<ex_par>) TYPE type. CLASS <classname> IMPLEMENTATION. METHOD <m>: RAISE EVENT <event> EXPORTING <ex_par> = <act_par>. Handling events: CLASS <classname> DEFINITION. METHODS: <on_event> FOR <event> OF <classname> ! <interface> IMPORTING <imp_par1>...<imp_parN> SENDER. Setting handler SET HANDLER <ref_handle> <on_event> FOR <ref_sender> ! FOR ALL INSTANCES [ACTIVATION <var>]

• •

name = p_name. TYPE t_name TYPE saplane-planetype. * Counts number of instances n_o_airplanes = n_o_airplanes + 1. ENDMETHOD. * Private static attribute CLASS-DATA n_o_airplanes TYPE i. 'Planetype:'. display_n_o_airplanes. planetype TYPE saplane-planetype. planetype = p_planetype. TYPES: t_name(25) TYPE c. n_o_airplanes. METHOD constructor. ENDMETHOD. WRITE:/ 'Name:'. . METHOD set_attributes.Define. planes:'. ENDMETHOD. *-----------------------------* Private section *-----------------------------PRIVATE SECTION. METHOD display_attributes. *-----------------------------* Public section *-----------------------------PUBLIC SECTION. ****************************************** * Implementation part ****************************************** CLASS lcl_airplane IMPLEMENTATION. name. ENDCLASS. planetype. set_attributes IMPORTING p_name p_planetype display_attributes. implement and use simple class ***INCLUDE ZBC404_HF_LCL_AIRPLANE . WRITE: / 'No. METHODS: constructor. ****************************************** * Definition part ****************************************** CLASS lcl_airplane DEFINITION. METHOD display_n_o_airplanes. * Private attributes DATA: name(25) TYPE c.

.. CALL METHOD: airplane1->display_n_o_airplanes... ENDCLASS. METHODS: constructor importing p2_name type t_name p2_planetype TYPE saplane-planetype. * Create reference to class lcl_airplane DATA: airplane1 TYPE REF TO lcl_airplane.. name = p2_name. START-OF-SELECTION..ENDMETHOD. more code . REPORT zbc404_hf_maintain_airplanes .. METHOD constructor. The resulting report: Maintain airplanes No. * Create instance CREATE OBJECT airplane1. TYPES: t_name(25) TYPE c. planes: No. CLASS lcl_airplane IMPLEMENTATION. END-OF-SELECTION... INCLUDE zbc404_hf_lcl_airplane. PUBLIC SECTION.. airplane2 TYPE REF TO lcl_airplane. * Using methods CALL METHOD: airplane1->display_n_o_airplanes. . planes: Name: Kurt 1 2 Planetype: MD80 Use constructor to create an object with parameters CLASS lcl_airplane DEFINITION. * Setting attributes using a method with parameters CALL METHOD airplane1->set_attributes EXPORTING p_name = 'Kurt' p_planetype = 'MD80'. airplane1->display_attributes.. CREATE OBJECT airplane2.

.. LCL_AIRPLANE has a method display_n_o_airplanes that displays the number of object instances. * Static methods CLASS-METHODS: display_n_o_airplanes. so it also displays n_o_seats. and redefines the superclass method display_attributes.. more code . LCL_PASSENGER_AIRPLANE has the private instance attribute n_o_seats. and redefines the superclass method display_attributes.. TYPES: t_name(25) TYPE c. METHODS: constructor IMPORTING im_name TYPE t_name im_planetype TYPE saplane-planetype.planetype = p2_planetype.. .. *-----------------------------* Public section *-----------------------------PUBLIC SECTION.. LCL_CARGO_PLANE has the private instance attribute cargomax. display_attributes. Superclass LCL_AIRPLANE ***INCLUDE ZBC404_HF_LCL_AIRPLANE . All instance attributes are provided by the cunstructor method.. CREATE OBJECT airplane1 exporting p2_name = 'Hansemand' p2_planetype = 'Boing 747'. . so it also displays cargomax.. ****************************************** * Definition part ****************************************** CLASS lcl_airplane DEFINITION. Subclassing This example uses a superclass LCL_AIRPLANE and subclasses it into LCL_PASSENGER_AIRPLANE and LCL_CARGO_PLANE. START-OF-SELECTION..

PUBLIC SECTION. ******************************************************************* * This is a subclass of class lcl_airplane ******************************************************************* CLASS lcl_passenger_airplane DEFINITION INHERITING FROM lcl_airplane. ENDMETHOD. Sub class LCL_PASSENGER_AIRPLANE ***INCLUDE ZBC404_HF_LCL_PASSENGER_PLANE . * * 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. METHOD constructor. ENDMETHOD. Redefinition of superclass method display_attributes display_attributes REDEFINITION. * . METHOD display_attributes. DATA: n_o_seats TYPE sflight-seatsmax. name = im_name.*-----------------------------* Protected section *-----------------------------PROTECTED SECTION. n_o_airplanes. planetype = im_planetype. WRITE:/ 'Name:'. ENDMETHOD. ENDCLASS. ENDCLASS. planetype TYPE saplane-planetype. name. * Private static attribute CLASS-DATA n_o_airplanes TYPE i. planes:'. * Counts number of instances n_o_airplanes = n_o_airplanes + 1. PRIVATE SECTION. 'Planetype:'. METHOD display_n_o_airplanes. ****************************************** * Implementation part ****************************************** CLASS lcl_airplane IMPLEMENTATION. planetype. WRITE: / 'No. * Private attributes DATA: name(25) TYPE c.

******************************************************************* * This is a subclass of class lcl_airplane ******************************************************************* CLASS lcl_cargo_plane DEFINITION INHERITING FROM lcl_airplane. seats:'. ENDMETHOD. cargomax = im_cargomax. CLASS lcl_passenger_airplane IMPLEMENTATION. WRITE: / 'No. ENDMETHOD. METHOD constructor. * The redefined display_attributes method CALL METHOD super->display_attributes.ENDCLASS. * ENDCLASS. Sub class LCL_CARGO_PLANE ***INCLUDE ZBC404_HF_LCL_CARGO_PLANE . cargomax. . WRITE: / 'Cargo max:'. DATA:cargomax TYPE scplane-cargomax. n_o_seats. ENDMETHOD. * 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. ENDCLASS. METHOD constructor. ENDMETHOD. The redefined display_attributes method METHOD display_attributes. * Redefinition of superclass method display_attributes display_attributes REDEFINITION. 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. ENDCLASS. * 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. CALL METHOD super->display_attributes. PRIVATE SECTION. n_o_seats = im_n_o_seats. PUBLIC SECTION. CLASS lcl_cargo_plane IMPLEMENTATION. METHOD display_attributes.

* When calling a superclass => must be used instead of -> CALL METHOD lcl_airplane=>display_n_o_airplanes. START-OF-SELECTION. o_cargo_plane TYPE REF TO lcl_cargo_plane. * Super class INCLUDE zbc404_hf_lcl_airplane. CREATE OBJECT o_cargo_plane EXPORTING im_name = 'AR13' im_planetype = 'DC 3' im_cargomax = 35. planes: 0 Name: LH505 Planetype: Boing 747 No. differs from the syntax when calling a subclass method. * Call static method display_n_o_airplanes * Note: The syntax for calling a superclass method. DATA: * Type ref to sub classes. * Create objects CREATE OBJECT o_passenger_airplane EXPORTING im_name = 'LH505' im_planetype = 'Boing 747' im_n_o_seats = 350. INCLUDE zbc404_hf_lcl_cargo_plane.0000 No. CALL METHOD o_cargo_plane->display_attributes. Note: It is not necesssary to make typeref to the superclass o_passenger_airplane TYPE REF TO lcl_passenger_airplane. * Sub classes INCLUDE zbc404_hf_lcl_passenger_plane. * Display attributes CALL METHOD o_passenger_airplane->display_attributes. planes: 2 Polymorphism . seats: 350 Name: AR13 Planetype: DC 3 Cargo max: 35.The Main program that uses the classes REPORT zbc404_hf_main . * Display initial number of instances = 0 CALL METHOD lcl_airplane=>display_n_o_airplanes. Result: No.

adds them to a list in lcl_carrier. LOOP AT plane_list INTO plane. ENDMETHOD. Working example: This example assumes that the classes lcl_airplane. passenger_plane TYPE REF to lcl_passenger_airplane. DATA: plane TYPE REF TO lcl_airplane. re_fuel = re_fuel + plane->estimate_fuel_consumption( distance ). This can be done using enheritance. ENDLOOP. and the processed identically for all the classes. APPEND cargo_plane to plane_list. *&--------------------------------------------------------------------* *& Include ZBC404_HF_LCL_CARRIER * . Object from different classes are stored in an internal table (plane_list) consisting of references to the superclass. 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. that is. What coding for the estimate_fuel_consumption method taht is actually executed. plane_list TYPE TABLE OF REF TO lcl_airplane. and displays the list. CREATE OBJECT passenger_plane APPEND passenger_plane to plane list. DATA: cargo_plane TYPE REF to lcl_cargo_airplane.Polymorphism: When the same method is implemented differently in different classes. depends on which object plane points to. lcl_passnger_airplane and lcl_cargo plane (Se Subcallsing) exists. * Generic method for calucalting required fuel METHOD calculate required_fuel. * Creating the list of references CREATE OBJECT cargo_plane. depends on the dynamic type of the plane reference variable. as it depends on the airplane type. Create objects of type lcl_cargo_plane and lcl_passenger_airplane.

ENDMETHOD. fldate TYPE sflight-fldate. METHOD constructor. connid TYPE sflight-connid. 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. IF im_seatsocc IS INITIAL. PRIVATE SECTION. wa_list_of_flights-fldate = im_fldate. airplane TYPE REF TO lcl_airplane. get_name RETURNING value(ex_name) TYPE string. name = im_name. list_of_airplanes TYPE TABLE OF REF TO lcl_airplane.*& * *&--------------------------------------------------------------------* * CLASS lcl_carrier DEFINITION * *---------------------------------------------------------------------* CLASS lcl_carrier DEFINITION. END OF flight_list_type. ELSE. PUBLIC SECTION. APPEND wa_list_of_flights TO list_of_flights. DATA: name TYPE string. METHOD get_name. list_of_flights TYPE TABLE OF flight_list_type. . seatsocc TYPE sflight-seatsocc. *---------------------------------------------------------------------* * CLASS lcl_carrier IMPLEMENTATION *---------------------------------------------------------------------* CLASS lcl_carrier IMPLEMENTATION. METHODS: constructor IMPORTING im_name TYPE string. ENDMETHOD. display_airplanes. DATA: wa_list_of_flights TYPE flight_list_type. wa_list_of_flights-cargo = im_cargo. wa_list_of_flights-airplane = im_airplane. wa_list_of_flights-seatsocc = im_seatsocc. ex_name = name. TYPES: BEGIN OF flight_list_type. add_a_new_airplane IMPORTING im_airplane TYPE REF TO lcl_airplane. ENDMETHOD. ENDIF. ENDCLASS. wa_list_of_flights-connid = im_connid. cargo(5) TYPE p DECIMALS 3. METHOD create_a_new_flight.

APPEND im_airplane TO list_of_airplanes. CREATE OBJECT o_cargo_plane2 EXPORTING im_name = 'AFL124' im_planetype = 'Iljutsin 2' im_cargomax = 35000. ******************************************************************* * 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. ENDLOOP. o_carrier TYPE REF TO lcl_carrier. CREATE OBJECT o_passenger_airplane2 EXPORTING im_name = 'SK333' im_planetype = 'MD80' im_n_o_seats = 110. LOOP AT list_of_airplanes INTO l_airplane. REPORT zbc404_hf_main . o_cargo_plane TYPE REF TO lcl_cargo_plane. METHOD display_airplanes. o_passenger_airplane2 TYPE REF TO lcl_passenger_airplane. CALL METHOD l_airplane->display_attributes.METHOD add_a_new_airplane. * Create objects CREATE OBJECT o_passenger_airplane EXPORTING im_name = 'LH505' im_planetype = 'Boing 747' im_n_o_seats = 350. DATA: * Type ref to classes o_passenger_airplane TYPE REF TO lcl_passenger_airplane. . * Sub classes for airplanes INCLUDE zbc404_hf_lcl_passenger_plane. INCLUDE zbc404_hf_lcl_cargo_plane. ENDCLASS. CREATE OBJECT o_carrier EXPORTING im_name = 'Spritisch Airways'. CREATE OBJECT o_cargo_plane EXPORTING im_name = 'AR13' im_planetype = 'DC 3' im_cargomax = 35. o_cargo_plane2 TYPE REF TO lcl_cargo_plane. ENDMETHOD. START-OF-SELECTION. ENDMETHOD. * Carrier class INCLUDE zbc404_hf_lcl_carrier. DATA: l_airplane TYPE REF TO lcl_airplane.

IF im_time < 4.000. ENDMETHOD.* Add passenger and cargo planes to the list of airplanes CALL METHOD o_carrier->add_a_new_airplane EXPORTING im_airplane = o_passenger_airplane. PUBLIC SECTION. CALL METHOD o_carrier->add_a_new_airplane EXPORTING im_airplane = o_cargo_plane2. seats: 110 Name: AR13 Planetype: DC 3 Cargo max: 35. * Declare events EVENTS: dog_is_hungry EXPORTING value(ex_time_since_last_meal) TYPE i. REPORT zbc404_hf_events_5. METHOD set_time_since_last_meal. Result: Name: LH505 Planetype: Boing 747 No. CALL METHOD o_carrier->add_a_new_airplane EXPORTING im_airplane = o_cargo_plane. ENDCLASS. set_time_since_last_meal IMPORTING im_time TYPE i. METHODS: constructor IMPORTING im_name TYPE string. METHOD constructor. *---------------------------------------------------------------------* * CLASS lcl_dog DEFINITION *---------------------------------------------------------------------* CLASS lcl_dog DEFINITION. on_dog_is_hungry FOR EVENT dog_is_hungry OF lcl_dog IMPORTING ex_time_since_last_meal. CALL METHOD o_carrier->add_a_new_airplane EXPORTING im_airplane = o_passenger_airplane2. seats: 350 Name: SK333 Planetype: MD80 No.0000 Events Below is a simple example of how to implement an event. WRITE: / 'I am a dog and my name is'. *---------------------------------------------------------------------* * CLASS lcl_dog IMPLEMENTATION *---------------------------------------------------------------------* CLASS lcl_dog IMPLEMENTATION.0000 Name: AFL124 Planetype: Iljutsin 2 Cargo max: 35. im_name. . * Display list of airplanes call method o_carrier->display_airplanes.

* Subsrcribe for event: * set handler <Event handler method> * FOR <ref_sender>!FOR ALL INSTANCES [ACTIVATION <var>] SET HANDLER on_dog_is_hungry FOR ALL INSTANCES ACTIVATION 'X'. ENDMETHOD. ex_time_since_last_meal.SKIP 1. ELSE. CALL METHOD o_dog1->set_time_since_last_meal EXPORTING im_time = 2. ENDIF. * Event method. WRITE: / 'You fool. ENDMETHOD. I have not eaten for more than'. ' hours'. I am not hungry yet You son of a bitch. and CLASS-METHOD dosplay_no_of_employees can be called to . * 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. WRITE: / 'You son of a bitch. The constructor method is used to initialize number and name of thje employee when the object is created. METHOD on_dog_is_hungry. *---------------------------------------------------------------------* * R E P O R T *---------------------------------------------------------------------* DATA: o_dog1 TYPE REF TO lcl_dog. * Raise event RAISE EVENT dog_is_hungry EXPORTING ex_time_since_last_meal = im_time. ENDCLASS. Simple class This example shows how to create a simple employee class. WRITE: / 'Give me something to eat NOW!'. CREATE OBJECT o_dog1 EXPORTING im_name = 'Beefeater'. A display_employee method can be called to show the attributes of the employee. I have not eaten for more than 5 hours Give me something to eat NOW! 1. called when the event dog_is_hungry is raised SKIP 1. START-OF-SELECTION. I am not hungry yet'. Result: I am a dog and my name is Beefeater You fool.

PUBLIC SECTION. END OF t_employee. name TYPE string. WRITE:/ 'Employee'. METHOD display_employee. g_employee-name = im_employee_name. ********************************************************************* * L C L _ E M P L O Y E E ********************************************************************* *---. WRITE: / 'Number of employees is:'.LCL Employee . * Class methods are global for all instances CLASS-METHODS: display_no_of_employees. g_no_of_employees.show the total number of employees (Number of instances of the employee class). *-------------------------------------------------------------------* 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.LCL Employee . PRIVATE SECTION. ENDMETHOD. ENDMETHOD. ENDCLASS. METHODS: constructor IMPORTING im_employee_no TYPE i im_employee_name TYPE string. ENDMETHOD. METHOD constructor. display_employee. *-------------------------------------------------------------------* The public section is accesible from outside *-------------------------------------------------------------------TYPES: BEGIN OF t_employee. g_employee-name. METHOD display_no_of_employees. *--. g_employee-no = im_employee_no. no TYPE i.Definition CLASS lcl_employee DEFINITION. g_employee-no. g_no_of_employees = g_no_of_employees + 1. *-------------------------------------------------------------------* The private section is only accesible from within the classs *-------------------------------------------------------------------DATA: g_employee TYPE t_employee. ENDCLASS.Implementation CLASS lcl_employee IMPLEMENTATION. PROTECTED SECTION. REPORT zbc404_hf_events_1. *********************************************************************** * * R E P O R T .

CALL METHOD g_employee1->display_employee. Create subclasses lcl_bluecollar_employee and lcl_whitecollar_employee. The class has the methods: • • • • Constructor Add_Employee . The wages are calcukated in the method add_employee. g_employee2 TYPE REF TO lcl_employee. except for the redifinition of the add_employee method. the superclass method add_employee is redeifined in the subclasses. CREATE OBJECT g_employee2 EXPORTING im_employee_no = 2 im_employee_name = 'Sally Summer'. The calsses are identical. CALL METHOD g_employee2->display_employee. 2.********************************************************************* DATA: g_employee1 TYPE REF TO lcl_employee. The constructor is used to initialize the attributes of the employee. but as the wages are calculated differently for blue collar employees and white collar emplyees.Adds a new employee to the list of employees Display_Employee_List . CREATE OBJECT g_employee1 EXPORTING im_employee_no = 1 im_employee_name = 'John Jones'. 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. Note that the constructor in the supclasss has to be called from within the constructor of the subclass.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. CALL METHOD g_employee2->display_no_of_employees. START-OF-SELECTION. where the caclculation of wage is different. Principles: Create super class LCL_CompanyEmployees. . Methodes: • Constructor.Displays all employees and there wage Display_no_of_employees .

CLASS LCL_CompanyEmployees IMPLEMENTATION CLASS lcl_company_employees IMPLEMENTATION. ENDLOOP. CLASS-DATA: i_employee_list TYPE TABLE OF t_employee. l_employee-no = im_no. METHODS: constructor. ENDMETHOD. METHOD display_no_of_employees. l_employee-name = im_name. ENDCLASS.• Add_Employee.: The program REPORT zbc404_hf_events_2 . METHOD constructor. In the redefined class the wage is calcuated. wage TYPE i. no_of_employees TYPE i. LOOP AT i_employee_list INTO l_employee. ENDMETHOD. PUBLIC SECTION. and the superclass method is called to add the employees to the emploee list. WRITE: / l_employee-no. l_employee-wage. * Displays all employees and there wage DATA: l_employee TYPE t_employee. add_employee IMPORTING im_no TYPE i im_name TYPE string im_wage TYPE i. . APPEND l_employee TO i_employee_list. WRITE: / 'List of Employees'. ******************************************************* * Super class LCL_CompanyEmployees ******************************************************* CLASS lcl_company_employees DEFINITION. *-. name TYPE string. ENDMETHOD. METHOD display_employee_list. display_no_of_employees. TYPES: BEGIN OF t_employee. no_of_employees = no_of_employees + 1. METHOD add_employee. PRIVATE SECTION. l_employee-name. This is a redinition of the same method in the superclass. display_employee_list. no TYPE i. * Adds a new employee to the list of employees DATA: l_employee TYPE t_employee. END OF t_employee. l_employee-wage = im_wage.

METHOD constructor.* Displays total number of employees SKIP 3. CALL METHOD super->add_employee EXPORTING im_no = no im_name = name im_wage = l_wage. PRIVATE SECTION. hourly_payment = im_hourly_payment. add_employee REDEFINITION. name TYPE string. no = im_no. ENDMETHOD. * The superclass constructor method must be called from the subclass * constructor method CALL METHOD super->constructor. name = im_name. WRITE: / 'Total number of employees:'. METHOD add_employee. *---. ENDCLASS. METHODS: constructor IMPORTING im_no TYPE i im_name TYPE string im_hours TYPE i im_hourly_payment TYPE i. hourly_payment TYPE i. no_of_employees. ******************************************************* * Sub class LCL_BlueCollar_Employee ******************************************************* CLASS lcl_bluecollar_employee DEFINITION INHERITING FROM lcl_company_employees. ENDMETHOD. ENDCLASS. hours TYPE i. PUBLIC SECTION. ENDCLASS.CLASS LCL_BlueCollar_Employee IMPLEMENTATION CLASS lcl_bluecollar_employee IMPLEMENTATION. * Calculate wage an call the superclass method add_employee to add * the employee to the employee list DATA: l_wage TYPE i. DATA:no TYPE i. METHODS: constructor IMPORTING im_no TYPE i . hours = im_hours. PUBLIC SECTION. l_wage = hours * hourly_payment. ******************************************************* * Sub class LCL_WhiteCollar_Employee ******************************************************* CLASS lcl_whitecollar_employee DEFINITION INHERITING FROM lcl_company_employees. ENDMETHOD.

l_wage = monthly_salary . add_employee REDEFINITION.CLASS LCL_WhiteCollar_Employee IMPLEMENTATION CLASS lcl_whitecollar_employee IMPLEMENTATION. monthly_deductions TYPE i. ENDCLASS. name TYPE string. METHOD constructor. ENDCLASS. monthly_deductions = im_monthly_deductions. DATA: no TYPE i. no = im_no. name = im_name. ENDMETHOD.monthly_deductions. METHOD add_employee. * 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 . START-OF-SELECTION. monthly_salary TYPE i. * The superclass constructor method must be called from the subclass * constructor method CALL METHOD super->constructor. * Create bluecollar employee obeject CREATE OBJECT o_bluecollar_employee1 EXPORTING im_no = 1 im_name = 'Gylle Karen' im_hours = 38 im_hourly_payment = 75.im_name TYPE string im_monthly_salary TYPE i im_monthly_deductions TYPE i. CALL METHOD super->add_employee EXPORTING im_no = no im_name = name im_wage = l_wage. monthly_salary = im_monthly_salary. o_whitecollar_employee1 TYPE REF TO lcl_whitecollar_employee. * Calculate wage an call the superclass method add_employee to add * the employee to the employee list DATA: l_wage TYPE i. PRIVATE SECTION. ENDMETHOD. *---. ******************************************************* * R E P O R T ******************************************************* DATA: * Object references o_bluecollar_employee1 TYPE REF TO lcl_bluecollar_employee.

Note that the interface is only implemented in the superclass ( The INTERFACE stament). constants. types and alias names. but an iterface can also contain attrbutes. The interface in the example only contains a method.850 2 John Dickens 7. REPORT zbc404_hf_events_3 . * Add bluecollar employee to employee list CALL METHOD o_whitecollar_employee1->add_employee EXPORTING im_no = 1 im_name = 'Karen Johnson' im_wage = 0.500 Total number of employees: 2 3. The resulting report List of Employees 1 Karen Johnson 2. CALL METHOD o_whitecollar_employee1->display_no_of_employees. but also used in the subclasses. because the methods are defined * as static (CLASS-METHODS) CALL METHOD o_whitecollar_employee1->display_employee_list. All changes in the program compared to example 2 are marked with red. ENDINTERFACE. * Display employee list and number of employees. The output from example 3 is similiar to the output in example 2. Interfaces This example is similiar to th eprevious example.im_monthly_deductions = 2500. Note that the result * will be the same when called from o_whitecollar_employee1 or * o_bluecolarcollar_employee1. however an interface is implemented with the method add_employee. METHODS: add_employee IMPORTING im_no TYPE i im_name TYPE string im_wage TYPE i. *---------------------------------------------------------------------* * INTERFACE lif_employee *---------------------------------------------------------------------* INTERFACE lif_employee. . ******************************************************* * Super class LCL_CompanyEmployees ******************************************************* CLASS lcl_company_employees DEFINITION.

******************************************************* * Sub class LCL_BlueCollar_Employee ******************************************************* CLASS lcl_bluecollar_employee DEFINITION INHERITING FROM lcl_company_employees. PUBLIC SECTION. ENDCLASS. l_employee-name. * Displays all employees and there wage DATA: l_employee TYPE t_employee. WRITE: / l_employee-no. ENDMETHOD. METHOD display_employee_list. name TYPE string. no_of_employees. ENDMETHOD. wage TYPE i. l_employee-wage = im_wage. WRITE: / 'Total number of employees:'. l_employee-name = im_name. display_employee_list. l_employee-wage. no_of_employees = no_of_employees + 1. TYPES: BEGIN OF t_employee. no TYPE i. *-.PUBLIC SECTION. METHOD lif_employee~add_employee. LOOP AT i_employee_list INTO l_employee. no_of_employees TYPE i. ENDMETHOD. display_no_of_employees. ENDMETHOD. * add_employee "Removed IMPORTING im_no TYPE i im_name TYPE string im_wage TYPE i. INTERFACES lif_employee. WRITE: / 'List of Employees'. l_employee-no = im_no.CLASS LCL_CompanyEmployees IMPLEMENTATION CLASS lcl_company_employees IMPLEMENTATION. * Displays total number of employees SKIP 3. METHOD constructor. * Adds a new employee to the list of employees DATA: l_employee TYPE t_employee. END OF t_employee. METHOD display_no_of_employees. METHODS: constructor . ENDLOOP. APPEND l_employee TO i_employee_list. METHODS: constructor. ENDCLASS. PRIVATE SECTION. CLASS-DATA: i_employee_list TYPE TABLE OF t_employee.

hours TYPE i. PRIVATE SECTION.. *---. DATA: no TYPE i. . no = im_no.CLASS LCL_BlueCollar_Employee IMPLEMENTATION CLASS lcl_bluecollar_employee IMPLEMENTATION. lif_employee~add_employee REDEFINITION. monthly_deductions TYPE i. name TYPE string. * Calculate wage an call the superclass method add_employee to add * the employee to the employee list DATA: l_wage TYPE i. PRIVATE SECTION. METHOD lif_employee~add_employee. METHOD constructor. lif_employee~add_employee REDEFINITION. METHODS: constructor IMPORTING im_no TYPE i im_name TYPE string im_monthly_salary TYPE i im_monthly_deductions TYPE i. l_wage = hours * hourly_payment.CLASS LCL_WhiteCollar_Employee IMPLEMENTATION CLASS lcl_whitecollar_employee IMPLEMENTATION. name = im_name.IMPORTING im_no TYPE i im_name TYPE string im_hours TYPE i im_hourly_payment TYPE i. ENDMETHOD. ENDCLASS. METHOD constructor. * The superclass constructor method must be called from the subclass * constructor method CALL METHOD super->constructor. hourly_payment = im_hourly_payment. hourly_payment TYPE i. name TYPE string. monthly_salary TYPE i. ENDCLASS. ******************************************************* * Sub class LCL_WhiteCollar_Employee ******************************************************* CLASS lcl_whitecollar_employee DEFINITION INHERITING FROM lcl_company_employees. *---. DATA:no TYPE i. CALL METHOD super->lif_employee~add_employee EXPORTING im_no = no im_name = name im_wage = l_wage. ENDCLASS. PUBLIC SECTION. ENDMETHOD. hours = im_hours.

monthly_deductions = im_monthly_deductions. START-OF-SELECTION. * 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. l_wage = monthly_salary . * 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.* * The superclass constructor method must be called from the subclass constructor method CALL METHOD super->constructor. ENDMETHOD. name = im_name. ENDCLASS. ENDMETHOD.monthly_deductions. . monthly_salary = im_monthly_salary. CALL METHOD o_whitecollar_employee1->display_no_of_employees. METHOD lif_employee~add_employee. * Create bluecollar employee obeject CREATE OBJECT o_bluecollar_employee1 EXPORTING im_no = 1 im_name = 'Gylle Karen' im_hours = 38 im_hourly_payment = 75. Note that the result * will be the same when called from o_whitecollar_employee1 or * o_bluecolarcollar_employee1. no = im_no. * Display employee list and number of employees. because the methods are defined * as static (CLASS-METHODS) CALL METHOD o_whitecollar_employee1->display_employee_list. * 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. o_whitecollar_employee1 TYPE REF TO lcl_whitecollar_employee. * Calculate wage an call the superclass method add_employee to add * the employee to the employee list DATA: l_wage TYPE i. ******************************************************* * R E P O R T ******************************************************* DATA: * Object references o_bluecollar_employee1 TYPE REF TO lcl_bluecollar_employee. CALL METHOD super->lif_employee~add_employee EXPORTING im_no = no im_name = name im_wage = l_wage.

wage TYPE i. There have been no canges to the subclasses. CLASS-DATA: i_employee_list TYPE TABLE OF t_employee. ENDCLASS. . END OF t_employee.4. Events This is the same example as example 4. METHODS: add_employee IMPORTING im_no TYPE i im_name TYPE string im_wage TYPE i. *---------------------------------------------------------------------* * INTERFACE lif_employee *---------------------------------------------------------------------* INTERFACE lif_employee.CLASS LCL_CompanyEmployees IMPLEMENTATION CLASS lcl_company_employees IMPLEMENTATION. * Declare event. name TYPE string. no_of_employees TYPE i. PUBLIC SECTION. ******************************************************* * Super class LCL_CompanyEmployees ******************************************************* CLASS lcl_company_employees DEFINITION. only to the superclass and the report. no TYPE i. All changes are marked with red. * Declare event method on_employee_added_to_list FOR EVENT employee_added_to_list OF lcl_company_employees IMPORTING ex_employee_name sender. METHODS: constructor. *-. REPORT zbc404_hf_events_4 . PRIVATE SECTION. METHOD constructor. display_employee_list. TYPES: BEGIN OF t_employee. * CLASS-EVENTS: Events can also be defined as class-events INTERFACES lif_employee. sp the code for th esubclasses is not shown. display_no_of_employees. For a simple example refer to Events in Examples. Note that declaration could also be placed in the * interface EVENTS: employee_added_to_list EXPORTING value(ex_employee_name) TYPE string. ENDINTERFACE.

******************************************************* * Sub class LCL_WhiteCollar_Employee ******************************************************* CLASS lcl_whitecollar_employee DEFINITION See code in example 3. APPEND l_employee TO i_employee_list. METHOD lif_employee~add_employee. ENDMETHOD. ENDMETHOD. * Raise event employee_added_to_list RAISE EVENT employee_added_to_list EXPORTING ex_employee_name = l_employee-name. ENDCLASS. * Event method WRITE: / 'Employee added to list'.. ******************************************************* * Sub class LCL_BlueCollar_Employee ******************************************************* CLASS lcl_bluecollar_employee DEFINITION INHERITING FROM lcl_company_employees.. ENDMETHOD. l_employee-name = im_name. l_employee-wage.. l_employee-name. ENDCLASS. ex_employee_name. WRITE: / l_employee-no.. * Adds a new employee to the list of employees DATA: l_employee TYPE t_employee. WRITE: / 'List of Employees'.no_of_employees = no_of_employees + 1. ENDCLASS... See code in example 3. CLASS lcl_whitecollar_employee IMPLEMENTATION. ENDLOOP. ENDMETHOD. See code in example 3. METHOD on_employee_added_to_list. WRITE: / 'Total number of employees:'. ENDCLASS. METHOD display_employee_list. ******************************************************* * R E P O R T ******************************************************* DATA: * Object references ... ENDMETHOD. METHOD display_no_of_employees. * Displays all employees and there wage DATA: l_employee TYPE t_employee. ENDCLASS. LOOP AT i_employee_list INTO l_employee. CLASS lcl_bluecollar_employee IMPLEMENTATION. no_of_employees. See code in example 3. l_employee-wage = im_wage. * Displays total number of employees SKIP 3. l_employee-no = im_no.

´ * 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. * Register event for o_bluecollar_employee1 SET HANDLER o_bluecollar_employee1->on_employee_added_to_list FOR o_bluecollar_employee1. START-OF-SELECTION. o_whitecollar_employee1 TYPE REF TO lcl_whitecollar_employee.o_bluecollar_employee1 TYPE REF TO lcl_bluecollar_employee.850 2 John Dickens 7. * Register event for o_whitecollar_employee1 SET HANDLER o_whitecollar_employee1->on_employee_added_to_list FOR o_whitecollar_employee1. * 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.500 Total number of employees: 2 . 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. because the methods are defined * as static (CLASS-METHODS) CALL METHOD o_whitecollar_employee1->display_employee_list. * 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. * Create bluecollar employee obeject CREATE OBJECT o_bluecollar_employee1 EXPORTING im_no = 1 im_name = 'Karen Johnson' im_hours = 38 im_hourly_payment = 75.

Syntax: CALL METHOD cl_gui_cfw=>FLUSH Set up event handling for controls There are two types of events: Application events. However you should not rely entirely on automatic synchronization. Synchronization/Flush RFC calls is used to synchronize the front and backend. T The flow logic of the screen is processed after the event (The PAI module is processed). so you should keep the not use the Flush method to a minimum.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. Bear in mind that the synchronization/RFC calls represenmts a bottleneck. In the events table the event must be registred as an application event by setting then field appl_event to 'X': . This synchronization takes palce at some predifined points in the program flow. 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. but force synchronization with the Flush method when necessary.

* 3. The dispatch method of cl_gui_cfw must NOT be called. Data: * 1. * 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. That means that the PAI and PBO modules are not processed. * Workspace for table gi_events g_event TYPE cntl_simple_event. wa_events-appl_event = 'X'. append wa_events to i_events. append wa_events to i_events. To see a complete example of how to handle events. wa_events-appl_event = space. METHODS: * Syntax: . Define and implement eventhandler class CLASS cls_event_handler DEFINITION. * 2. In the events table the the field appl_event must be set to SAPCE: wa_events-eventid = cl_gui_textedit=>event_double_click. System events. Define table for registration of events.wa_events-eventid = cl_gui_textedit=>event_double_click. PUBLIC SECTION. Important: The dispatch method of cl_gui_cfw must be called in the PAI module: CALL METHOD cl_gui_cfw=>dispatch. For system events the flow-logic of the screen is not executed. * 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. Define and instance of the eventhandler class. go_event_handler TYPE REF TO cls_event_handler. Example It is presumed that a SAP toolbar control named go_toolbar of class cl_gui_toolbar has been defined. refer to The SAP toolbar control.

Create event handler CREATE OBJECT go_event_handler.see the control documentation> * OF <class of object> <importing parameters l . Append events to the events table * The Event Id can be found in the control documentation.. ENDCLASS. * Do something when the event is raised ENDMETHOD. g_event-appl_event = 'X'.. Declare a reference variable before the class has been defined Scenario: DATA: o_my_class TYPE REF TO lcl_myclass. * 6. Use the events table to register events for the control CALL METHOD go_toolbar->set_registered_events EXPORTING events = gi_events.. CLASS lcl_myclass... ENDCLASS.. * 4. METHOD on_function_selected. append more events i necessary. * 5.* <method name> FOR EVENT <event of control . CLASS cls_event_handler IMPLEMENTATION... "This is an application event APPEND g_event TO gi_events. ENDCLASS. . * Syntax: * SET HANDLER <event handler class> -> <Event handler method> * FOR <control> SET HANDLER go_event_handler->on_function_selected FOR go_toolbar.. . Note that The event below is registred as an application event g_event-eventid = go_toolbar->m_id_function_selected.see the control documentation> on_function_selected FOR EVENT function_selected OF cl_gui_toolbar IMPORTING fcode.

Solution: Define the class in the beginning of the program with DEFINITION DEFERRED: CLASS lcl_myclass DEFINITION DEFERRED.This will cause an error because the definition of class lcl_myclass is after the declaration. Methods: getArea Returning Value(area) Type F.. * A Circle Class that behaves like a Shape Class CCircle Definition..geocities.in Author Website: http://www.vsnl. Endinterface.net. To Use BADI . ENDCLASS.Business Add In you need to Understand ABAP OO Interface Concept Content Author: Jayanta Narayan Choudhuri Author Email: sss@cal.com/ojnc * For Rajat's OO ans BAdI Education Report Z_CTRY.. 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. * A Shape Interface "Like a shape" "Behaves like a Shape" Adverb/Adjective Interface IShape. getCircumference Returning Value(circumference) Type F.. . DATA: o_my_class TYPE REF TO lcl_myclass. CLASS lcl_myclass. Public Section.

EndClass. getCircumference For IShape~getCircumference. Class CCircle Implementation. radius = pRadius. Class CSquare Implementation. Compute area = 2 * PI * radius. EndClass. EndMethod. Interfaces IShape. . EndMethod. Method getRadius. Aliases: getArea For IShape~getArea. getCircumference For IShape~getCircumference. pRadius = radius. Data side Type F. setRadius Importing pRadius Type F. EndMethod. getRadius Returning Value(pRadius) Type F.141592365359'. Methods: Private Section. Method IShape~getArea. Aliases: getArea For IShape~getArea. Data radius Type F. radius = pRadius. Constructor Importing pRadius Type F. Method Constructor. EndClass. * A Square Class Class CSquare Definition.Interfaces IShape. Compute circumference = PI * radius * radius. Method setRadius. EndMethod. Method IShape~getCircumference. Constants PI Type F Value '3. Public Section. Constructor Importing pSide Type F. Method Constructor. Methods: Private Section. EndMethod.

length = pLength. Compute circumference = 4 * side. Compute area = side * side. Public Section. Method IShape~getCircumference. getCircumference For IShape~getCircumference. Constructor Importing pHeight Type F pLength Type F. EndMethod. Compute circumference = 2 * ( height + length ). Interfaces IShape. Method Constructor. EndMethod. with Shape Behaviour * Concrete Objects with IShape Behaviour! " One Object with " Array of Objects . EndClass. Compute area = height * length. EndMethod. EndClass.side = pSide. EndMethod. EndClass. height = pHeight. * A Rectangle Class Class CRectangle Definition. Shape Behaviour ShapeTable Type Table Of Ref To IShape. Method IShape~getCircumference. Method IShape~getArea. Methods: Private Section. Class CRectangle Implementation. Method IShape~getArea. Aliases: getArea For IShape~getArea. * START of PROGRAM * Array of Shapes Data: OneShape Type Ref To IShape. EndMethod. Data: height Type F. length Type F. EndMethod.

Create Object S1 Exporting pSide = '3. 0. Write: / Serial. Create Object C1 Exporting pRadius = '2. Append R2 to ShapeTable. Append R1 to ShapeTable. '.5'. Write: / 'Area 0.4'. * Append in any order! Append S1 to ShapeTable. myCircumference Decimals 4 Exponent ClassName. Write: / 'Circumference '. Data: descr_ref ClassName Serial TYPE ref to CL_ABAP_TYPEDESCR. Serial = 0. Create Object R1 Exporting pHeight = '2. Write: /.3'. Append C1 to ShapeTable. START-OF-SELECTION. Loop At ShapeTable into OneShape. Create Object S2 Exporting pSide = '6.0'.5'. CSquare. CRectangle.0'. Data: myArea Type F. Create Object C2 Exporting pRadius = '5. CCircle. Call Method OneShape->getArea Receiving area = myArea. Call Method descr_ref->get_relative_name Receiving P_RELATIVE_NAME = ClassName. Type I. myCircumference Type F. Append C2 to ShapeTable. Append S2 to ShapeTable.8' pLength = '3. Type String. CRectangle. CSquare. myArea Decimals 4 Exponent . Create Object R2 Exporting pHeight = '1. Call Method OneShape->getCircumference Receiving circumference = myCircumference. EndLoop. descr_ref = CL_ABAP_TYPEDESCR=>Describe_By_Object_Ref( OneShape ). Add 1 to Serial.7' pLength = '6.Data: C1 S1 R1 C2 S2 R2 Type Type Type Type Type Type Ref Ref Ref Ref Ref Ref To To To To To To CCircle.

The code REPORT sapmz_hf_controls1 . 3.0000 10. add a pushbutton with the function code EXIT. 5. .** * * * * * * * * * * * * * * * * * * * * * * * Results 1 CSQUARE Area Circumference 2 CRECTANGLE Area Circumference 3 CRECTANGLE Area Circumference 4 CCIRCLE Area Circumference 5 CCIRCLE Area Circumference 6 CSQUARE Area Circumference 12. To be able to exit the program.5398 15.2500 14. DATA: * Create reference to the custom container custom_container TYPE REF TO cl_gui_custom_container. DATA: ok_code LIKE sy-ucomm. CONSTANTS: line_length TYPE i VALUE 254. Create screen 100 Place a custom control on the screen by choosing the custom control icon which can be recognized by the letter 'C'.7100 16.0000 24.5200 12.0000 Example 1: Creating the TextEdit control This is a simple example of how to implement a text edit control. 6.7080 19.6350 36. Create a report In the start of selection event add: SET SCREEN '100'. 4.4159 78. 2.0000 9. In the elements list enter the name OK_CODE for the element of type OK.4000 31. Steps 1. and give it the name MYCONTAINER1.

WHEN 'EXIT'. repid = sy-repid. ENDIF. ENDMODULE. repid LIKE sy-repid. SET SCREEN '100'. *---------------------------------------------------------------------* * MODULE USER_COMMAND_0100 INPUT * *---------------------------------------------------------------------* MODULE user_command_0100 INPUT. " USER_COMMAND_0100 INPUT *&--------------------------------------------------------------------* *& Module STATUS_0100 OUTPUT *&--------------------------------------------------------------------* MODULE status_0100 OUTPUT. CASE ok_code. * The TextEdit control shoul only be initialized the first time the * PBO module executes IF editor IS INITIAL. * Create obejct for custom container CREATE OBJECT custom_container EXPORTING container_name = 'MYCONTAINER1' 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 'I' NUMBER sy-msgno WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.* Create reference to the TextEdit control editor TYPE REF TO cl_gui_textedit. . START-OF-SELECTION. * Create obejct for the TextEditor control CREATE OBJECT editor EXPORTING wordwrap_mode = cl_gui_textedit=>wordwrap_at_fixed_position wordwrap_position = line_length wordwrap_to_linebreak_mode = cl_gui_textedit=>true parent = custom_container EXCEPTIONS error_cntl_create = 1 error_cntl_init = 2 error_cntl_link = 3 error_dp_create = 4 gui_type_not_supported = 5 others = 6 . IF sy-subrc <> 0. ENDCASE. LEAVE TO SCREEN 0.

ENDIF. Application events. . ENDMODULE. The method CL_GUI_CFW=>DISPATCH must be called to initiate event handling In this example an application event is added to the program in example 1. The PAI module is processed after an event. " STATUS_0100 OUTPUT The result Example 2: Event handling . New code is marked with red.MESSAGE ID sy-msgid TYPE 'I' NUMBER sy-msgno WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4. ENDIF.Application event There are 2 types of events: • • System events. These events are triggerede irrespective of the screen flow-logic.

********************************************************************** * Impmenting events ********************************************************************** DATA: event_type(20) TYPE c. ENDMETHOD. Name it EVENT_TYPE The code: REPORT sapmz_hf_controls1 . refresh i_events. repid LIKE sy-repid. CLASS lcl_event_handler IMPLEMENTATION. LEAVE TO SCREEN 0. . PUBLIC SECTION. DATA: ok_code LIKE sy-ucomm. *---------------------------------------------------------------------* * CLASS lcl_event_handler DEFINITION *---------------------------------------------------------------------* CLASS lcl_event_handler DEFINITION. * Create reference to the TextEdit control editor TYPE REF TO cl_gui_textedit.Steps: 1. CLEAR wa_events. WHEN OTHERS. START-OF-SELECTION. event_type = 'Event DBLCLICK raised'. ENDCLASS. * Structure for oneline of the table wa_events TYPE cntl_simple_event. * Internal table for events that should be registred i_events TYPE cntl_simple_events. ENDCLASS. CASE ok_code. *---------------------------------------------------------------------* * MODULE USER_COMMAND_0100 INPUT * *---------------------------------------------------------------------* MODULE user_command_0100 INPUT. CLASS-METHODS: catch_dblclick FOR EVENT dblclick OF cl_gui_textedit IMPORTING sender. DATA: * Create reference to the custom container custom_container TYPE REF TO cl_gui_custom_container. CONSTANTS: line_length TYPE i VALUE 254. METHOD catch_dblclick. Create an input/output field on screen 100. SET SCREEN '100'. where the event type can be output. WHEN 'EXIT'.

ENDMODULE. IF sy-subrc <> 0.* Call the Dispacth method to initiate application event handling call method cl_gui_cfw=>Dispatch. * The TextEdit control shoul only be initialized the first time the * PBO module executes IF editor IS INITIAL. wa_events-appl_event = 'X'. ENDIF. MESSAGE ID sy-msgid TYPE 'I' NUMBER sy-msgno WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4. "This is an application event . " USER_COMMAND_0100 INPUT *&--------------------------------------------------------------------* *& Module STATUS_0100 OUTPUT *&--------------------------------------------------------------------* MODULE status_0100 OUTPUT. * Create obejct for the TextEditor control CREATE OBJECT editor EXPORTING wordwrap_mode = cl_gui_textedit=>wordwrap_at_fixed_position wordwrap_position = line_length wordwrap_to_linebreak_mode = cl_gui_textedit=>true parent = custom_container EXCEPTIONS error_cntl_create = 1 error_cntl_init = 2 error_cntl_link = 3 error_dp_create = 4 gui_type_not_supported = 5 others = 6 . repid = sy-repid. ENDCASE. IF sy-subrc <> 0. MESSAGE ID sy-msgid TYPE 'I' NUMBER sy-msgno WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4. ENDIF. * Register the event in the internal table i_events wa_events-eventid = cl_gui_textedit=>event_double_click. * Create obejct for custom container CREATE OBJECT custom_container EXPORTING container_name = 'MYCONTAINER1' EXCEPTIONS cntl_error = 1 cntl_system_error = 2 create_error = 3 lifetime_error = 4 lifetime_dynpro_dynpro_link = 5 others = 6 . * Link the event handler method to the event and the * TextEdit control SET HANDLER lcl_event_handler=>catch_dblclick FOR editor.

System event System events are passed irrespective of the flow-logic of the screen..... the input/ouput field should show the text: Event DBLCLICK Example 3: Event handling . since the flowlogic of the screen an dthe fielde transport is ignore. * Reacting to the system event call method cl_gui_cfw=>set_new_ok_code exporting new_code = 'SHOW'...append wa_events to i_events. CASE ok_code. *---call method cl_gui_cfw=>Dispatch... ENDIF. "This is an application event wa_events-appl_event = space.. METHOD catch_dblclick. " STATUS_0100 OUTPUT Result: When you double clicks on the TextEdit control nothing happens... *--wa_events-appl_event = 'X'..event_type = 'Event DBLCLICK raised'. . " STATUS_0100 OUTPUT * * Result: When you double click on the TextEdit control.. ENDMODULE. event_type = 'System dblclick'. ENDCASE. "This is a system event ENDIF.... code. ENDMODULE.. Pass the table to the TextEdit control using method set_registred_events call method editor->set_registered_events exporting events = i_events. Code .... WHEN 'SHOW'... To implement a system event change the code from example 2 as follows: Code: CLASS lcl_event_handler IMPLEMENTATION. ENDMODULE. " USER_COMMAND_0100 INPUT MODULE status_0100 OUTPUT. *--.. WHEN OTHERS. MODULE user_command_0100 INPUT.

APPEND 'hallo world !' TO i_texttable. DO 10 TIMES. * Create internal table with texts APPEND 'This a method that fills the TextEdit control' TO i_texttable. TYPES: BEGIN OF t_texttable. Give itname PUSHBUTTON_IMPORT and function code IMP. " USER_COMMAND_0100 INPUT *&--------------------------------------------------------------------* *& Form load_texts *&--------------------------------------------------------------------* * This form creates an internal table with texts. * Load TextEdit control with texts CALL METHOD editor->set_text_as_r3table EXPORTING table = i_texttable. CASE ok_code.. DATA i_texttable TYPE TABLE OF t_texttable. line(line_length) TYPE c. perform load_texts.Example 4: Calling methods of the control In this exercise a function that loads the texts of an internal table into the text window. ENDCASE. code... IF sy-subrc > 0.' TO i_texttable... Steps: Define anoterh pushbutton on the screen. Define a form CREATE_TEXTS that carries out the text import. * Display an error message . WHEN 'IMP'. ENDMODULE. APPEND 'with a text. Code: MODULE user_command_0100 INPUT. The the contents of * the table is instered into the TextEdit control using method * set_text_as_r3table *---------------------------------------------------------------------* FORM load_texts. ENDDO. that activates the method that fills the TextEdit control... END OF t_texttable. Only changes to the code in example 2 is show. is implemented..

" create_texts Example 5: Responding to an event When you double click on a text line in the TextEdit control. The load of the table should be moved to the PBO module. * Display an error message ENDIF.EXIT. * All methods that operates on controls are transferred to the frontend * by a RFC calls. The line number of the TextEdit control that is double clicked. and the entry is prefixed with '*' and loaded into the TextEdit control again. and a global flag g_loaded added. The whole program now looks like this: Code REPORT sapmz_hf_controls1 . The changes in thje code are marked with red. ********************************************************************** * Utillity table to load texts ********************************************************************** TYPES: BEGIN OF t_texttable. is retreived using method GET_SELECTION_POS. CALL METHOD cl_gui_cfw=>flush. The position of the double click in the TextEdit control is used to find the entry in the table. repid LIKE sy-repid. DATA: * Create reference to the custom container custom_container TYPE REF TO cl_gui_custom_container. DATA: ok_code LIKE sy-ucomm. IF sy-subrc > 0. END OF t_texttable. CONSTANTS: line_length TYPE i VALUE 254. * Create reference to the TextEdit control editor TYPE REF TO cl_gui_textedit. the method FLUSH is used to determine when this is done. The internal text table is reloaded froim the TextEdit control with method GET_TEXT_AS_R3TABLE. DATA: i_texttable TYPE TABLE OF t_texttable. you want it to be prefixed with a '*'. ENDIF. . The program should be changed so that the internal table i_texttable is global. line(line_length) TYPE c. ENDFORM. g_loaded(1) TYPE c.

EXIT. * Reload texts from h einternal table perform load_texts. SHIFT wa_texttable RIGHT. * Used for the sytem event call method cl_gui_cfw=>set_new_ok_code exporting new_code = 'SHOW'. * Texts in the TextEdit control can have been changed. ENDIF.********************************************************************** * Impmenting events ********************************************************************** DATA: event_type(20) TYPE c. . ENDCLASS. * Read the position of the double click CALL METHOD sender->get_selection_pos IMPORTING from_line = from_line from_pos = from_pos to_line = to_line to_pos = to_pos. from_pos TYPE i. CLASS-METHODS: catch_dblclick FOR EVENT dblclick OF cl_gui_textedit IMPORTING sender. METHOD catch_dblclick. * Internal table for events that should be registred i_events TYPE cntl_simple_events. CLASS lcl_event_handler IMPLEMENTATION. * Read the line of the internal table that was clicked READ TABLE i_texttable INDEX from_line INTO wa_texttable. ELSEIF wa_texttable+0(1) NS '*'. modify i_texttable from wa_texttable index from_line. wa_texttable+0(1) = '*'. to_line TYPE i. CALL METHOD sender->get_text_as_r3table IMPORTING table = i_texttable. to_pos TYPE i. IF sy-subrc <> 0. so * first reload text from the control into the internal * table that contains text IF NOT g_loaded IS INITIAL. ENDIF. *---------------------------------------------------------------------* * CLASS lcl_event_handler DEFINITION *---------------------------------------------------------------------* CLASS lcl_event_handler DEFINITION. * Structure for oneline of the table wa_events TYPE cntl_simple_event. DATA: from_line TYPE i. SHIFT wa_texttable. IF wa_texttable+0(1) CS '*'. ENDIF. PUBLIC SECTION. wa_texttable TYPE t_texttable.

PERFORM Load_texts. WHEN 'IMP'. " USER_COMMAND_0100 INPUT *&--------------------------------------------------------------------* *& Module STATUS_0100 OUTPUT *&--------------------------------------------------------------------* MODULE status_0100 OUTPUT. * Create obejct for the TextEditor control CREATE OBJECT editor EXPORTING wordwrap_mode = cl_gui_textedit=>wordwrap_at_fixed_position wordwrap_position = line_length wordwrap_to_linebreak_mode = cl_gui_textedit=>true parent = custom_container EXCEPTIONS . event_type = 'System dblclick'. repid = sy-repid. ENDIF.ENDMETHOD. REFRESH: i_events. MESSAGE ID sy-msgid TYPE 'I' NUMBER sy-msgno WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4. IF sy-subrc <> 0. WHEN 'SHOW'. *---------------------------------------------------------------------* * MODULE USER_COMMAND_0100 INPUT * *---------------------------------------------------------------------* MODULE user_command_0100 INPUT. * CALL METHOD cl_gui_cfw=>dispatch. WHEN 'EXIT'. ENDCLASS. LEAVE TO SCREEN 0. ENDMODULE. START-OF-SELECTION. CASE ok_code. SET SCREEN '100'. WHEN OTHERS. CLEAR wa_events. "Not used for system events ENDCASE. * The TextEdit control shoul only be initialized the first time the * PBO module executes IF editor IS INITIAL. * Create object for custom container CREATE OBJECT custom_container EXPORTING container_name = 'MYCONTAINER1' EXCEPTIONS cntl_error = 1 cntl_system_error = 2 create_error = 3 lifetime_error = 4 lifetime_dynpro_dynpro_link = 5 others = 6 .

* Pass the table to the TextEdit control uding method * set_registred_events CALL METHOD editor->set_registered_events EXPORTING events = i_events. the method FLUSH is used to determine when this is * done. DO 10 TIMES. IF sy-subrc <> 0. IF sy-subrc > 0. "This is a system event APPEND wa_events TO i_events. ENDIF. ENDDO. CALL METHOD cl_gui_cfw=>flush. APPEND 'with a text. * All methods that operates on controls are transferred to the frontend * by a RFC calls. IF sy-subrc > 0. " STATUS_0100 OUTPUT *&--------------------------------------------------------------------* *& Form Load_texts *&--------------------------------------------------------------------* * This form loads the lines of the internal table i_texttable into * the TextEdit control *---------------------------------------------------------------------* FORM Load_texts. ENDIF. MESSAGE ID sy-msgid TYPE 'I' NUMBER sy-msgno WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4. APPEND 'hallo world !' TO i_texttable. "This is an application event wa_events-appl_event = space. * Create internal table with texts taht can be uploaded to * the TextEdit control APPEND 'This a method that fills the TextEdit control' TO i_texttable. * Link the event handler method to the event and the * TextEdit control SET HANDLER lcl_event_handler=>catch_dblclick FOR editor. * Load TextEdit control with texts CALL METHOD editor->set_text_as_r3table EXPORTING table = i_texttable. * Display an error message EXIT.error_cntl_create = 1 error_cntl_init = 2 error_cntl_link = 3 error_dp_create = 4 gui_type_not_supported = 5 others = 6 .' TO i_texttable. * wa_events-appl_event = 'X'. * Display an error message . ENDMODULE. * Register the event in the internal table i_events wa_events-eventid = cl_gui_textedit=>event_double_click. ENDIF.

This method does not need line numbers or a FLUSH statement Steps • Add a new pushbutton to the screen with the function code PROTECT...... This is very important when working e. ENDFORM.. PERFORM protect. Note: Instead of using method PROTECT_LINES. " create_texts Example 6: Protect a line in the TextEdit control and the importance of FLUSH All methods that operates on controls are transfered to the fronend by RFC calls.. g_loaded = 'X'... with export parameters from a method. as the parmeters will not be correct before the FLUSH method has been called...... . *&--------------------------------------------------------------------* *& Form protect *&--------------------------------------------------------------------* .. The example below portects selected lines in the TextEdit and uses FLUSH to ensure that the correct parameters are returned from method GET_SELECTION_POS..ENDIF.. ENDCASE.... The FLUSH method is used to synchronize control execution and the frontend. the method PROTECT_SELECTION could be used..g...... MODULE user_command_0100 INPUT. Code Add the following code to the example: * Global variables DATA: from_idx TYPE i.... to_idx TYPE i... code... index TYPE i. ......... CASE ok_code... WHEN 'PROTECT'..

CALL METHOD editor->protect_lines EXPORTING from_line = from_idx to_line = to_idx. * Errormessage: Error in flush ENDIF. * Without this synchronization the variables from_idx and * to_idx will have obsolutete values (The initial value for * both. The new TextEdit control will be designed to act as a clipboard for short texts.* Protects marked lines in a TextEdit control *---------------------------------------------------------------------* FORM protect. Steps: • Add a new container to the screen and name it MYCONTAINER2. * Protect the selected lines IF to_idx > from_idx. * Flush again to protect immidiately CALL METHOD cl_gui_cfw=>flush. * The PROTECT_SELECTION method could be used instead. are 0) CALL METHOD cl_gui_cfw=>flush. " protect Example 7: Using multiple controls In this example a second TextEdit control will be added to the screen. Code: . * Synchronize execution in the control with the ABAP program. * Errormessage: Error in flush ENDIF. eliminating the * need of line numbers and the last FLUSH * call method editor->protect_selection. to_idx = to_idx .1. ENDIF. IF sy-subrc > 0. IF sy-subrc > 0. ENDFORM. * Determine the area selected by the user CALL METHOD editor->get_selection_pos IMPORTING from_line = from_idx to_line = to_idx EXCEPTIONS error_cntl_call_method = 1.

Result: . MESSAGE ID sy-msgid TYPE 'I' NUMBER sy-msgno WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4. Insert the following code in the PBO module: *-----------------------------------------------------* The SCRATCH TextEdit control *-----------------------------------------------------IF scratch IS INITIAL. * Remove the staus bar CALL METHOD scratch->set_statusbar_mode EXPORTING statusbar_mode = cl_gui_textedit=>false. ENDIF. * Create obejct for the SCRATCH TextEditor control CREATE OBJECT scratch EXPORTING parent = custom_container2 wordwrap_mode = cl_gui_textedit=>wordwrap_at_windowborder wordwrap_to_linebreak_mode = cl_gui_textedit=>true.Insert global datadeclaration: ********************************************************************** * Implementing a second Scratch TextEdit control ********************************************************************** DATA: scratch TYPE REF TO cl_gui_textedit. ENDIF. * Create obejct for custom container2 CREATE OBJECT custom_container2 EXPORTING container_name = 'MYCONTAINER2' EXCEPTIONS cntl_error = 1 cntl_system_error = 2 create_error = 3 lifetime_error = 4 lifetime_dynpro_dynpro_link = 5 others = 6 . custom_container2 TYPE REF TO cl_gui_custom_container. IF sy-subrc <> 0.

21 END OF t_texttable. 13 repid LIKE sy-repid.1 REPORT sapmz_hf_controls1 . 20 line(line_length) TYPE c. 11 * Create reference to the TextEdit control 12 editor TYPE REF TO cl_gui_textedit. 14 15 ********************************************************************** 16 * Utillity table to load texts 17 ********************************************************************** 18 TYPES: 19 BEGIN OF t_texttable. 7 8 DATA: 9 * Create reference to the custom container 10 custom_container TYPE REF TO cl_gui_custom_container. . 2 3 CONSTANTS: 4 line_length TYPE i VALUE 254. 22 23 DATA: 24 i_texttable TYPE TABLE OF t_texttable. 5 6 DATA: ok_code LIKE sy-ucomm.

41 custom_container2 TYPE REF TO cl_gui_custom_container. 33 to_idx TYPE i. 64 65 ENDCLASS. * 58 *--------------------------------------------------------------------* 59 CLASS lcl_event_handler DEFINITION.... 35 36 ********************************************************************** 37 * Implementing a second Scratch TextEdit control 38 ********************************************************************** 39 DATA: 40 scratch TYPE REF TO cl_gui_textedit... 66 67 *--------------------------------------------------------------------* 68 * CLASS lcl_event_handler IMPLEMENTATION 69 *--------------------------------------------------------------------* . 26 g_loaded(1) TYPE c.. 51 * Structure for oneline of the table 52 wa_events TYPE cntl_simple_event. 49 * Internal table for events that should be registred 50 i_events TYPE cntl_simple_events. 60 PUBLIC SECTION.. 53 54 *--------------------------------------------------------------------* 55 * CLASS lcl_event_handler DEFINITION 56 *--------------------------------------------------------------------* 57 * . 42 43 44 ********************************************************************** 45 * Implementing events 46 ********************************************************************** 47 DATA: 48 event_type(20) TYPE c. 61 CLASS-METHODS: 62 catch_dblclick FOR EVENT dblclick 63 OF cl_gui_textedit IMPORTING sender. 34 index TYPE i.25 wa_texttable TYPE t_texttable. 27 28 ********************************************************************** 29 * Data for the protection example 30 ********************************************************************** 31 DATA: 32 from_idx TYPE i.

100 * Read the line of the internal table that was clicked 101 READ TABLE i_texttable INDEX from_line INTO wa_texttable.. 84 85 86 * Read the position of the double click 87 CALL METHOD sender->get_selection_pos 88 IMPORTING 89 from_line = from_line 90 from_pos = from_pos 91 to_line = to_line 92 to_pos = to_pos. 111 ENDIF.. 122 123 . 104 ENDIF. 93 94 * Texts in the TextEdit control can have been changed. 77 to_line TYPE i.. 98 CALL METHOD sender->get_text_as_r3table 99 IMPORTING table = i_texttable. 76 from_pos TYPE i.. 108 ELSEIF wa_texttable+0(1) NS '*'. 73 METHOD catch_dblclick.. 103 EXIT. 102 IF sy-subrc <> 0. 112 MODIFY i_texttable FROM wa_texttable INDEX from_line. 78 to_pos TYPE i.70 * * . 105 106 IF wa_texttable+0(1) CS '*'. 74 DATA: 75 from_line TYPE i. 71 *--------------------------------------------------------------------* 72 CLASS lcl_event_handler IMPLEMENTATION... 109 SHIFT wa_texttable RIGHT. 110 wa_texttable+0(1) = '*'. 113 * Reload texts from h einternal table 114 PERFORM load_texts. 121 ENDCLASS. so 95 * first reload text from the control into the internal 96 * table that contains text 97 IF NOT g_loaded IS INITIAL. 115 116 117 ENDIF. 79 80 81 * Used for the sytem event 82 CALL METHOD cl_gui_cfw=>set_new_ok_code 83 EXPORTING new_code = 'SHOW'. 118 119 120 ENDMETHOD. 107 SHIFT wa_texttable.

. " USER_COMMAND_0100 INPUT 154 *&--------------------------------------------------------------------* 155 *& Module STATUS_0100 OUTPUT 156 *&--------------------------------------------------------------------* 157 MODULE status_0100 OUTPUT. 137 138 CASE ok_code. 149 * CALL METHOD cl_gui_cfw=>dispatch.124 125 START-OF-SELECTION. 146 PERFORM protect. 126 CLEAR wa_events. 142 event_type = 'System dblclick'. 139 WHEN 'EXIT'. 145 WHEN 'PROTECT'. 130 131 132 133 *--------------------------------------------------------------------* 134 * MODULE USER_COMMAND_0100 INPUT * 135 *--------------------------------------------------------------------* 136 MODULE user_command_0100 INPUT. 144 PERFORM load_texts. 151 152 153 ENDMODULE. "Not used for system events 150 ENDCASE. 127 REFRESH: i_events. 162 * Create obejct for custom container 163 CREATE OBJECT custom_container 164 EXPORTING 165 container_name = 'MYCONTAINER1' 166 EXCEPTIONS 167 cntl_error = 1 168 cntl_system_error = 2 169 create_error = 3 170 lifetime_error = 4 171 lifetime_dynpro_dynpro_link = 5 172 others = 6 173 . 161 repid = sy-repid. 128 129 SET SCREEN '100'. 147 148 WHEN OTHERS. 140 LEAVE TO SCREEN 0. 158 * The TextEdit control shoul only be initialized the first time the 159 * PBO module executes 160 IF editor IS INITIAL. 143 WHEN 'IMP'. 141 WHEN 'SHOW'.

199 ENDIF. 223 APPEND 'hallo world !' TO i_texttable. 175 MESSAGE ID sy-msgid TYPE 'I' NUMBER sy-msgno 176 WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4. 178 179 * Create obejct for the TextEditor control 180 CREATE OBJECT editor 181 EXPORTING 182 183 wordwrap_mode = 184 cl_gui_textedit=>wordwrap_at_fixed_position 185 wordwrap_position = line_length 186 wordwrap_to_linebreak_mode = cl_gui_textedit=>true 187 parent = custom_container 188 EXCEPTIONS 189 error_cntl_create = 1 190 error_cntl_init = 2 191 error_cntl_link = 3 192 error_dp_create = 4 193 gui_type_not_supported = 5 194 others = 6 195 . 177 ENDIF. 207 208 * wa_events-appl_event = 'X'.174 IF sy-subrc <> 0. "This is an application event 209 wa_events-appl_event = space. 217 218 * Create internal table with texts taht can be uploaded to 219 * the TextEdit control 220 APPEND 'This a method that fills the TextEdit control' TO i_texttable. 200 201 * Link the event handler method to the event and the 202 * TextEdit control 203 SET HANDLER lcl_event_handler=>catch_dblclick FOR editor. 222 DO 10 TIMES. 224 ENDDO. 213 * Pass the table to the TextEdit control uding method 214 * set_registred_events 215 CALL METHOD editor->set_registered_events 216 EXPORTING events = i_events.' TO i_texttable. 197 MESSAGE ID sy-msgid TYPE 'I' NUMBER sy-msgno 198 WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4. 204 205 * Register the event in the internal table i_events 206 wa_events-eventid = cl_gui_textedit=>event_double_click. 226 227 *-----------------------------------------------------228 * The SCRATCH TextEdit control 229 *------------------------------------------------------ . 225 ENDIF. 221 APPEND 'with a text. "This is a system event 210 211 212 APPEND wa_events TO i_events. 196 IF sy-subrc <> 0.

279 ENDIF. 259 260 ENDIF. 247 248 * Create obejct for the SCRATCH TextEditor control 249 CREATE OBJECT scratch 250 EXPORTING 251 parent = custom_container2 252 wordwrap_mode = 253 cl_gui_textedit=>wordwrap_at_windowborder 254 wordwrap_to_linebreak_mode = cl_gui_textedit=>true. 246 ENDIF. " STATUS_0100 OUTPUT 264 265 *&--------------------------------------------------------------------* 266 *& Form Load_texts 267 *&--------------------------------------------------------------------* 268 * This form loads the lines of the internal table i_texttable into 269 * the TextEdit control 270 *---------------------------------------------------------------------* 271 FORM load_texts. 272 273 * Load TextEdit control with texts 274 CALL METHOD editor->set_text_as_r3table 275 EXPORTING table = i_texttable. 276 IF sy-subrc > 0. 255 256 * Remove the staus bar 257 CALL METHOD scratch->set_statusbar_mode 258 EXPORTING statusbar_mode = cl_gui_textedit=>false. 280 281 * All methods that operates on controls are transferred to the frontend . 243 IF sy-subrc <> 0. 231 * Create obejct for custom container2 232 CREATE OBJECT custom_container2 233 EXPORTING 234 container_name = 'MYCONTAINER2' 235 EXCEPTIONS 236 cntl_error = 1 237 cntl_system_error = 2 238 create_error = 3 239 lifetime_error = 4 240 lifetime_dynpro_dynpro_link = 5 241 others = 6 242 .230 IF scratch IS INITIAL. 244 MESSAGE ID sy-msgid TYPE 'I' NUMBER sy-msgno 245 WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4. 277 * Display an error message 278 EXIT. 261 262 263 ENDMODULE.

309 IF sy-subrc > 0. eliminating the 324 * need of line numbers and the last FLUSH 325 326 * call method editor->protect_selection. 316 ENDIF. 286 * Display an error message 287 ENDIF. 321 322 323 * The PROTECT_SELECTION method could be used instead. 288 g_loaded = 'X'. are 0) 308 CALL METHOD cl_gui_cfw=>flush.1. 289 ENDFORM. 310 * Errormessage: Error in flush 311 ENDIF. 303 304 * Synchronize execution in the control with the ABAP program. 284 CALL METHOD cl_gui_cfw=>flush. 296 * Determine the area selected by the user 297 CALL METHOD editor->get_selection_pos 298 IMPORTING 299 from_line = from_idx 300 to_line = to_idx 301 EXCEPTIONS 302 error_cntl_call_method = 1. 327 328 329 * Flush again to protect immidately 330 CALL METHOD cl_gui_cfw=>flush. 312 313 * Protect the lines selected 314 IF to_idx > from_idx. . the method FLUSH is used to determine when this is 283 * done. 331 IF sy-subrc > 0. 315 to_idx = to_idx . 305 * Without this synchronization the variables from_idx and 306 * to_idx will have obsolutete values (The initial value for 307 * both. 332 * Errormessage: Error in flush 333 ENDIF. " create_texts 290 *&--------------------------------------------------------------------* 291 *& Form protect 292 *&--------------------------------------------------------------------* 293 * Protects marked lines in a TextEdit control 294 *---------------------------------------------------------------------* 295 FORM protect.282 * by a RFC calls. 285 IF sy-subrc > 0. 317 CALL METHOD editor->protect_lines 318 EXPORTING 319 from_line = from_idx 320 to_line = to_idx.

" protect .334 335 336 337 ENDFORM.

Sign up to vote on this title
UsefulNot useful