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>]

• •

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

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

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

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

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

planes: 0 Name: LH505 Planetype: Boing 747 No.0000 No. START-OF-SELECTION. * When calling a superclass => must be used instead of -> CALL METHOD lcl_airplane=>display_n_o_airplanes. * Super class INCLUDE zbc404_hf_lcl_airplane. 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 initial number of instances = 0 CALL METHOD lcl_airplane=>display_n_o_airplanes. CALL METHOD o_cargo_plane->display_attributes. o_cargo_plane TYPE REF TO lcl_cargo_plane. differs from the syntax when calling a subclass method. * Call static method display_n_o_airplanes * Note: The syntax for calling a superclass method. INCLUDE zbc404_hf_lcl_cargo_plane.The Main program that uses the classes REPORT zbc404_hf_main . seats: 350 Name: AR13 Planetype: DC 3 Cargo max: 35. Result: No. 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. planes: 2 Polymorphism . 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.

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

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

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

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

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

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

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

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

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

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

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

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

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

* 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. ENDMETHOD.monthly_deductions. no = im_no. METHOD lif_employee~add_employee. START-OF-SELECTION. CALL METHOD super->lif_employee~add_employee EXPORTING im_no = no im_name = name im_wage = l_wage. * Calculate wage an call the superclass method add_employee to add * the employee to the employee list DATA: l_wage TYPE i. name = im_name. monthly_deductions = im_monthly_deductions. because the methods are defined * as static (CLASS-METHODS) CALL METHOD o_whitecollar_employee1->display_employee_list. * 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. ******************************************************* * R E P O R T ******************************************************* DATA: * Object references o_bluecollar_employee1 TYPE REF TO lcl_bluecollar_employee. . ENDCLASS. CALL METHOD o_whitecollar_employee1->display_no_of_employees. * Display employee list and number of employees.* * The superclass constructor method must be called from the subclass constructor method CALL METHOD super->constructor. o_whitecollar_employee1 TYPE REF TO lcl_whitecollar_employee. l_wage = monthly_salary . ENDMETHOD. monthly_salary = im_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. * 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.

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

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

because the methods are defined * as static (CLASS-METHODS) CALL METHOD o_whitecollar_employee1->display_employee_list.500 Total number of employees: 2 . * 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. START-OF-SELECTION.850 2 John Dickens 7. CALL METHOD o_whitecollar_employee1->display_no_of_employees. * Register event for o_whitecollar_employee1 SET HANDLER o_whitecollar_employee1->on_employee_added_to_list FOR o_whitecollar_employee1. Note that the result * will be the same when called from o_whitecollar_employee1 or * o_bluecolarcollar_employee1.´ * Add bluecollar employee to employee list CALL METHOD o_whitecollar_employee1->lif_employee~add_employee EXPORTING im_no = 1 im_name = 'Gylle Karen' im_wage = 0. Result: Employee added to list Karen Johnson Employee added to list John Dickens List of Employees 1 Karen Johnson 2. * Display employee list and number of employees.o_bluecollar_employee1 TYPE REF TO lcl_bluecollar_employee. * 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. * Register event for o_bluecollar_employee1 SET HANDLER o_bluecollar_employee1->on_employee_added_to_list FOR o_bluecollar_employee1. * Create bluecollar employee obeject CREATE OBJECT o_bluecollar_employee1 EXPORTING im_no = 1 im_name = 'Karen Johnson' im_hours = 38 im_hourly_payment = 75. o_whitecollar_employee1 TYPE REF TO lcl_whitecollar_employee.

while CL_GUI_CONTROL is responsible for displaying the control on the screen. Synchronization/Flush RFC calls is used to synchronize the front and backend. and can be used both by control wrapper calsses and by control progtrammers The CL_GUI_OBJECT encapsulates ActiveX or JavaBeans methods. However you should not rely entirely on automatic synchronization. T The flow logic of the screen is processed after the event (The PAI module is processed). CL_GUI_OBJECT -> CL_GUI_CONTROL -> CL_GUI_* (Wrapper class) These classes contains methods that are enherited by subsequent classes in the enheritance tree. so you should keep the not use the Flush method to a minimum. This synchronization takes palce at some predifined points in the program flow.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. but force synchronization with the Flush method when necessary. In the events table the event must be registred as an application event by setting then field appl_event to 'X': . Bear in mind that the synchronization/RFC calls represenmts a bottleneck. Syntax: CALL METHOD cl_gui_cfw=>FLUSH Set up event handling for controls There are two types of events: Application events.

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

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

CLASS lcl_myclass.. * A Shape Interface "Like a shape" "Behaves like a Shape" Adverb/Adjective Interface IShape.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.geocities. getCircumference Returning Value(circumference) Type F...This will cause an error because the definition of class lcl_myclass is after the declaration. Public Section.vsnl.in Author Website: http://www. To Use BADI . Endinterface.net. 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. DATA: o_my_class TYPE REF TO lcl_myclass. . ENDCLASS. Solution: Define the class in the beginning of the program with DEFINITION DEFERRED: CLASS lcl_myclass DEFINITION DEFERRED. * A Circle Class that behaves like a Shape Class CCircle Definition.. Methods: getArea Returning Value(area) Type F.

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

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

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

DATA: * Create reference to the custom container custom_container TYPE REF TO cl_gui_custom_container. CONSTANTS: line_length TYPE i VALUE 254. 6. 5.0000 24. . Steps 1.6350 36. To be able to exit the program.7100 16. The code REPORT sapmz_hf_controls1 . In the elements list enter the name OK_CODE for the element of type OK.** * * * * * * * * * * * * * * * * * * * * * * * 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. Create a report In the start of selection event add: SET SCREEN '100'.7080 19.4159 78.5200 12. Create screen 100 Place a custom control on the screen by choosing the custom control icon which can be recognized by the letter 'C'. add a pushbutton with the function code EXIT. and give it the name MYCONTAINER1. 3.0000 9. 2. DATA: ok_code LIKE sy-ucomm.0000 10.2500 14. 4.5398 15.0000 Example 1: Creating the TextEdit control This is a simple example of how to implement a text edit control.4000 31.

repid LIKE sy-repid. *---------------------------------------------------------------------* * MODULE USER_COMMAND_0100 INPUT * *---------------------------------------------------------------------* MODULE user_command_0100 INPUT. ENDIF. . repid = sy-repid. MESSAGE ID sy-msgid TYPE 'I' NUMBER sy-msgno WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4. * 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.* Create reference to the TextEdit control editor TYPE REF TO cl_gui_textedit. LEAVE TO SCREEN 0. IF sy-subrc <> 0. * 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 . " USER_COMMAND_0100 INPUT *&--------------------------------------------------------------------* *& Module STATUS_0100 OUTPUT *&--------------------------------------------------------------------* MODULE status_0100 OUTPUT. * The TextEdit control shoul only be initialized the first time the * PBO module executes IF editor IS INITIAL. ENDMODULE. SET SCREEN '100'. WHEN 'EXIT'. START-OF-SELECTION. ENDCASE. CASE ok_code.

These events are triggerede irrespective of the screen flow-logic.Application event There are 2 types of events: • • System events. The PAI module is processed after an event. ENDMODULE. " STATUS_0100 OUTPUT The result Example 2: Event handling . Application events. ENDIF. ENDIF. .MESSAGE ID sy-msgid TYPE 'I' NUMBER sy-msgno WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4. 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. New code is marked with red.

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

ENDCASE. IF sy-subrc <> 0. ENDIF. * Link the event handler method to the event and the * TextEdit control SET HANDLER lcl_event_handler=>catch_dblclick FOR editor. * Register the event in the internal table i_events wa_events-eventid = cl_gui_textedit=>event_double_click. * The TextEdit control shoul only be initialized the first time the * PBO module executes IF editor IS INITIAL. ENDMODULE. * 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. MESSAGE ID sy-msgid TYPE 'I' NUMBER sy-msgno WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4. IF sy-subrc <> 0. "This is an application event . " USER_COMMAND_0100 INPUT *&--------------------------------------------------------------------* *& Module STATUS_0100 OUTPUT *&--------------------------------------------------------------------* MODULE status_0100 OUTPUT. * 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 . ENDIF. MESSAGE ID sy-msgid TYPE 'I' NUMBER sy-msgno WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.* Call the Dispacth method to initiate application event handling call method cl_gui_cfw=>Dispatch. wa_events-appl_event = 'X'.

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

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

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

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

REFRESH: i_events. WHEN OTHERS. CASE ok_code. ENDCLASS.ENDMETHOD. CLEAR wa_events. " USER_COMMAND_0100 INPUT *&--------------------------------------------------------------------* *& Module STATUS_0100 OUTPUT *&--------------------------------------------------------------------* MODULE status_0100 OUTPUT. WHEN 'IMP'. * 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 . LEAVE TO SCREEN 0. ENDIF. START-OF-SELECTION. * CALL METHOD cl_gui_cfw=>dispatch. ENDMODULE. repid = sy-repid. SET SCREEN '100'. IF sy-subrc <> 0. * The TextEdit control shoul only be initialized the first time the * PBO module executes IF editor IS INITIAL. WHEN 'SHOW'. event_type = 'System dblclick'. MESSAGE ID sy-msgid TYPE 'I' NUMBER sy-msgno WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4. *---------------------------------------------------------------------* * MODULE USER_COMMAND_0100 INPUT * *---------------------------------------------------------------------* MODULE user_command_0100 INPUT. * 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 . PERFORM Load_texts. "Not used for system events ENDCASE. WHEN 'EXIT'.

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

ENDCASE... This method does not need line numbers or a FLUSH statement Steps • Add a new pushbutton to the screen with the function code PROTECT. WHEN 'PROTECT'. index TYPE i.. Note: Instead of using method PROTECT_LINES... CASE ok_code... with export parameters from a method..... Code Add the following code to the example: * Global variables DATA: from_idx TYPE i.... " 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....... 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.. The FLUSH method is used to synchronize control execution and the frontend.... .... .. as the parmeters will not be correct before the FLUSH method has been called.g.. g_loaded = 'X'...... *&--------------------------------------------------------------------* *& Form protect *&--------------------------------------------------------------------* . This is very important when working e.....ENDIF. to_idx TYPE i. MODULE user_command_0100 INPUT.. PERFORM protect...... code. ENDFORM. the method PROTECT_SELECTION could be used.

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

MESSAGE ID sy-msgid TYPE 'I' NUMBER sy-msgno WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4. * 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 the following code in the PBO module: *-----------------------------------------------------* The SCRATCH TextEdit control *-----------------------------------------------------IF scratch IS INITIAL. ENDIF. Result: .Insert global datadeclaration: ********************************************************************** * Implementing a second Scratch TextEdit control ********************************************************************** DATA: scratch TYPE REF TO cl_gui_textedit. IF sy-subrc <> 0. * Remove the staus bar CALL METHOD scratch->set_statusbar_mode EXPORTING statusbar_mode = cl_gui_textedit=>false. custom_container2 TYPE REF TO cl_gui_custom_container. * 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 . ENDIF.

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

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

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

137 138 CASE ok_code. 140 LEAVE TO SCREEN 0. 143 WHEN 'IMP'. " USER_COMMAND_0100 INPUT 154 *&--------------------------------------------------------------------* 155 *& Module STATUS_0100 OUTPUT 156 *&--------------------------------------------------------------------* 157 MODULE status_0100 OUTPUT. 149 * CALL METHOD cl_gui_cfw=>dispatch.124 125 START-OF-SELECTION. 142 event_type = 'System dblclick'. 126 CLEAR wa_events. 151 152 153 ENDMODULE. 127 REFRESH: i_events. 146 PERFORM protect. 144 PERFORM load_texts. 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 . 145 WHEN 'PROTECT'. 141 WHEN 'SHOW'. 161 repid = sy-repid. 158 * The TextEdit control shoul only be initialized the first time the 159 * PBO module executes 160 IF editor IS INITIAL. 130 131 132 133 *--------------------------------------------------------------------* 134 * MODULE USER_COMMAND_0100 INPUT * 135 *--------------------------------------------------------------------* 136 MODULE user_command_0100 INPUT. "Not used for system events 150 ENDCASE. 139 WHEN 'EXIT'. 128 129 SET SCREEN '100'. 147 148 WHEN OTHERS. .

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. 207 208 * wa_events-appl_event = 'X'.174 IF sy-subrc <> 0. "This is an application event 209 wa_events-appl_event = space. "This is a system event 210 211 212 APPEND wa_events TO i_events. 221 APPEND 'with a text. 225 ENDIF. 199 ENDIF. 177 ENDIF. 226 227 *-----------------------------------------------------228 * The SCRATCH TextEdit control 229 *------------------------------------------------------ . 197 MESSAGE ID sy-msgid TYPE 'I' NUMBER sy-msgno 198 WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4. 223 APPEND 'hallo world !' TO i_texttable.' TO i_texttable. 196 IF sy-subrc <> 0. 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. 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 . 175 MESSAGE ID sy-msgid TYPE 'I' NUMBER sy-msgno 176 WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4. 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. 224 ENDDO. 204 205 * Register the event in the internal table i_events 206 wa_events-eventid = cl_gui_textedit=>event_double_click. 222 DO 10 TIMES.

255 256 * Remove the staus bar 257 CALL METHOD scratch->set_statusbar_mode 258 EXPORTING statusbar_mode = cl_gui_textedit=>false. 261 262 263 ENDMODULE. 244 MESSAGE ID sy-msgid TYPE 'I' NUMBER sy-msgno 245 WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4. 279 ENDIF.230 IF scratch IS INITIAL. 243 IF sy-subrc <> 0. 280 281 * All methods that operates on controls are transferred to the frontend . 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. 277 * Display an error message 278 EXIT. 272 273 * Load TextEdit control with texts 274 CALL METHOD editor->set_text_as_r3table 275 EXPORTING table = i_texttable. 246 ENDIF. 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 . 259 260 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. 276 IF sy-subrc > 0.

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

334 335 336 337 ENDFORM. " protect .