Object Oriented Programming | Method (Computer Programming) | Inheritance (Object Oriented Programming)

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

• •

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

4. To be able to exit the program. Create screen 100 Place a custom control on the screen by choosing the custom control icon which can be recognized by the letter 'C'. Create a report In the start of selection event add: SET SCREEN '100'.6350 36. 3.2500 14.0000 9. DATA: * Create reference to the custom container custom_container TYPE REF TO cl_gui_custom_container. 5. DATA: ok_code LIKE sy-ucomm.0000 10. and give it the name MYCONTAINER1. CONSTANTS: line_length TYPE i VALUE 254.4159 78. add a pushbutton with the function code EXIT.0000 Example 1: Creating the TextEdit control This is a simple example of how to implement a text edit control. 2. Steps 1.7080 19.** * * * * * * * * * * * * * * * * * * * * * * * 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. 6.7100 16.5398 15. The code REPORT sapmz_hf_controls1 .5200 12. In the elements list enter the name OK_CODE for the element of type OK. .4000 31.0000 24.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

.25 wa_texttable TYPE t_texttable. 53 54 *--------------------------------------------------------------------* 55 * CLASS lcl_event_handler DEFINITION 56 *--------------------------------------------------------------------* 57 * . * 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. 60 PUBLIC SECTION.. 27 28 ********************************************************************** 29 * Data for the protection example 30 ********************************************************************** 31 DATA: 32 from_idx TYPE i. 66 67 *--------------------------------------------------------------------* 68 * CLASS lcl_event_handler IMPLEMENTATION 69 *--------------------------------------------------------------------* .. 26 g_loaded(1) TYPE c. 41 custom_container2 TYPE REF TO cl_gui_custom_container. 64 65 ENDCLASS... 33 to_idx TYPE i. 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. 49 * Internal table for events that should be registred 50 i_events TYPE cntl_simple_events. 51 * Structure for oneline of the table 52 wa_events TYPE cntl_simple_event.

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

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

"This is a system event 210 211 212 APPEND wa_events TO i_events. 175 MESSAGE ID sy-msgid TYPE 'I' NUMBER sy-msgno 176 WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4. 197 MESSAGE ID sy-msgid TYPE 'I' NUMBER sy-msgno 198 WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4. "This is an application event 209 wa_events-appl_event = space. 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 *------------------------------------------------------ . 207 208 * wa_events-appl_event = 'X'. 221 APPEND 'with a text. 199 ENDIF.' TO i_texttable. 223 APPEND 'hallo world !' TO i_texttable. 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.174 IF sy-subrc <> 0. 222 DO 10 TIMES. 225 ENDIF. 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. 196 IF sy-subrc <> 0. 177 ENDIF. 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. 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 . 224 ENDDO.

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

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

" protect .334 335 336 337 ENDFORM.

Sign up to vote on this title
UsefulNot useful