Powered By

Free XML Skins for Blogger

Powered by Blogger

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

Wednesday, February 4, 2009

sap abap/4 sample codes

abap/4 sample codes used in real time implementation

Sunday, January 18, 2009

SAP ABAP SYNTAX FOR OOP-OBJECT ORIENTED

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

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

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

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

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

METHOD mm1.
ENDMETHOD.

METHOD mm2.
ENDMETHOD.

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

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

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

METHODS:

add_a_new_airplane importing im_airplane TYPE REF to lcl_airplane.


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

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


Example of how to implement an interface:
INTERFACE lif_document

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

display.

ENDINTERFACE.
CLASS lcl_text_document DEFINITION.
PUBLIC SECTION.

INTERFACES lif_document.

METHODS display.

ENDCLASS.
CLASS lcl_text_document IMPLEMENTTION.

METHOD lif_document~print.

ENDMETHOD.
METHOD lif_document~display

ENDMETHOD.

METHOD display.

ENDMETHOD.

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

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

CALL METHOD text_doc->lif_document~display.

CALL METHOD text_doc->display.

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

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

METHOD :

RAISE EVENT EXPORTING = .
Handling events:
CLASS DEFINITION.

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

[ACTIVATION ]

Friday, October 10, 2008

SAP ABAP Pop a Date in ABAP Report Selection Screens

*
* Pop a Date in selection screens for the users to choose the month and
* year
*
* Written by : SAP Basis, ABAP Programming and Other IMG Stuff
*
*

REPORT ZPOPDATE.

DATA: V_CODE LIKE SY-SUBRC.

PARAMETER: V_MONTH LIKE ISELLIST-MONTH.

AT SELECTION-SCREEN ON VALUE-REQUEST FOR V_MONTH.

CALL FUNCTION 'POPUP_TO_SELECT_MONTH'
EXPORTING
ACTUAL_MONTH = '200205'
LANGUAGE = SY-LANGU
START_COLUMN = 8
START_ROW = 5
IMPORTING
SELECTED_MONTH = V_MONTH
RETURN_CODE = V_CODE
EXCEPTIONS
FACTORY_CALENDAR_NOT_FOUND = 1
HOLIDAY_CALENDAR_NOT_FOUND = 2
MONTH_NOT_FOUND = 3
OTHERS = 4.

Tips: Date Formats with popups
With Compliments from: Shweta Sahay
Email: shwetasahay20032003@yahoo.co.in

*--------Display different date formats with popups-----------------*

REPORT zdate .

DATA: l_code LIKE sy-subrc.
DATA: lv_date(10) TYPE c.
DATA: BEGIN OF lwa_date OCCURS 0,
lv_d(2) TYPE c VALUE '1',
END OF lwa_date.

PARAMETER: p_month LIKE isellist-month.
PARAMETER: p_date(2) TYPE c.

AT SELECTION-SCREEN.

INITIALIZATION.
DO 31 TIMES.
APPEND lwa_date TO lwa_date.
lwa_date-lv_d = lwa_date-lv_d + 1.
WRITE lwa_date-lv_d.
ENDDO.

AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_month.

CALL FUNCTION 'POPUP_TO_SELECT_MONTH'
EXPORTING
actual_month = '200505'
language = sy-langu
start_column = 8
start_row = 5
IMPORTING
selected_month = p_month
return_code = l_code
EXCEPTIONS
factory_calendar_not_found = 1
holiday_calendar_not_found = 2
month_not_found = 3
OTHERS = 4.




AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_date.

CHECK NOT lwa_date[] IS INITIAL.
CALL FUNCTION 'POPUP_WITH_TABLE'
EXPORTING
endpos_col = 5
endpos_row = 10
startpos_col = 2
startpos_row = 1
titletext = 'DATE'
IMPORTING
choice = lwa_date
TABLES
valuetab = lwa_date
EXCEPTIONS
break_off = 1
OTHERS = 2.
p_date = lwa_date-lv_d.


END-OF-SELECTION.

CONCATENATE p_date '/' p_month+4(2) '/' p_month+0(4) INTO lv_date.
WRITE:/ 'IN DD/MM/YYYY', lv_date.
CONCATENATE p_month+4(2) '/' p_date '/' p_month+0(4) INTO lv_date.
WRITE:/ 'IN MM/DD/YYYY', lv_date.
CONCATENATE p_month+0(4) '/' p_month+4(2) '/' p_date INTO lv_date.
WRITE:/ 'IN YYYY/MM/DD', lv_date.
*end of program.

IN SAP ABAP Database Access

This graphic is explained in the accompanying text

This section describes how ABAP programs communicate with the central database in the R/3 System.

Accessing the Database in the R/3 System

Open SQL

Native SQL

Logical Databases

Contexts

Programming Database Changes

SAP ABAP Code in eCATT Editor

Description

ABAP code can be inserted in the eCATT editor. Here the interaction of eCATT import/export parameters with ABAP code happens only via eCATT Variable parameters. So before the ABAP-ENDABAP block, have the required values in the Variable parameters. And then use these eCATT Variable parameters in ABAP block. During the ABAP-ENDABAP block, the eCATT Variable parameters can interact with local variable of ABAP block. After the ENDABAP command, these eCATT Variable parameters having values from ABAP code can be passed to next Import parameters.
Transaction is SECATT.
The scenario taken here is of SU01 transaction code. For the given user ID, the initials are added via recording. So Initials is import parameters. Once the recording is done, the same initial value is passed to ADRP table to get the first name of the person via ABAP code.

/* ABAP Code in eCATT Editor */
*------------------------------------------------*.* Step 1:* Record SU01 transactionSAPGUI ( SU01_50_STEP_3 ).* Parameterize the 'Initials' as Import Parameter.SAPGUI ( SU01_100_STEP_4 ).SAPGUI ( SU01_50_STEP_4 ).SAPGUI ( S000_40_STEP_2 ).*------------------------------------------------*.* Step 2:* Pass Import Parameter value into Variable ParameterL_IV_INI = P_IV_INITIALS.*------------------------------------------------*.* Step 3:* ABAP-ENDABAP block from 'Pattern' button of application* toolbar to get the first name from the ADRP table by* passing the Initials as inputABAP. TABLES: ADRP. DATA: WA_ADRP TYPE ADRP. SELECT SINGLE * INTO WA_ADRP FROM ADRP WHERE INITIALS = L_IV_INI.* Pass the local variable of ABAP block to Variable Parameter of eCATT L_IV_NAME_FIRST = WA_ADRP-NAME_FIRST.ENDABAP.*------------------------------------------------*.* Step 4:LOG ( L_IV_NAME_FIRST ).*------------------------------------------------*./* ABAP Code in eCATT Editor */
*------------------------------------------------*.*

Step 1:* Record SU01 transactionSAPGUI ( SU01_50_STEP_3 ).* Parameterize the 'Initials' as Import Parameter.SAPGUI ( SU01_100_STEP_4 ).SAPGUI ( SU01_50_STEP_4 ).
SAPGUI ( S000_40_STEP_2 ).*------------------------------------------------*.*

Step 2:* Pass Import Parameter value into Variable ParameterL_IV_INI = P_IV_INITIALS.*------------------------------------------------*.*

Step 3:* ABAP-ENDABAP block from 'Pattern' button of application* toolbar to get the first name from the ADRP table by* passing the Initials as inputABAP. TABLES: ADRP. DATA: WA_ADRP TYPE ADRP. SELECT SINGLE * INTO WA_ADRP FROM ADRP WHERE INITIALS = L_IV_INI.* Pass the local variable of ABAP block to Variable Parameter of eCATT L_IV_NAME_FIRST = WA_ADRP-NAME_FIRST.ENDABAP.*------------------------------------------------*.*
Step 4:LOG ( L_IV_NAME_FIRST ).*------------------------------------------------*.

SAP ABAP CODE FOR TEXT EDIT CONTROL

TIP: Use SE75 to create your own custom text ID for SAVE_TEXT object


Example 1: Creating the TextEdit control

Example 2: Event handling - Application event

Example 3: Event handling - System event

Example 4: Calling a methods of the control

Example 5: Responding to an event

Example 6: Protect a line in the TextEdit control and the importance of FLUSH

Example 7: Using multiple controls

See the whole program code

Example 1: Creating the TextEdit control
This is a simple example of how to implement a text edit control.

Steps

Create a report
In the start of selection event add: SET SCREEN '100'.
Create screen 100
Place a custom control on the screen by choosing the custom control icon which can be recognized by the letter 'C', and give it the name MYCONTAINER1.
To be able to exit the program, add a pushbutton with the function code EXIT.
In the elements list enter the name OK_CODE for the element of type OK.
The code

REPORT sapmz_hf_controls1 .
CONSTANTS:
line_length TYPE i VALUE 254.
DATA: ok_code LIKE sy-ucomm.
DATA:
* Create reference to the custom container
custom_container TYPE REF TO cl_gui_custom_container,
* Create reference to the TextEdit control
editor TYPE REF TO cl_gui_textedit,
repid LIKE sy-repid.
START-OF-SELECTION.
SET SCREEN '100'.
*---------------------------------------------------------------------*


* MODULE USER_COMMAND_0100 INPUT *


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


MODULE user_command_0100 INPUT.
CASE ok_code.
WHEN 'EXIT'.
LEAVE TO SCREEN 0.
ENDCASE.
ENDMODULE. " USER_COMMAND_0100 INPUT
*&---------------------------------------------------------------------*


*& Module STATUS_0100 OUTPUT


*&---------------------------------------------------------------------*


MODULE status_0100 OUTPUT.
* The TextEdit control should only be initialized the first time the


* PBO module executes


IF editor IS INITIAL.
repid = sy-repid.
* Create obejct for custom container
CREATE OBJECT custom_container
EXPORTING
container_name = 'MYCONTAINER1'
EXCEPTIONS
cntl_error = 1
cntl_system_error = 2
create_error = 3
lifetime_error = 4
lifetime_dynpro_dynpro_link = 5
others = 6
.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE 'I' NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
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
.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE 'I' NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
ENDIF.
ENDMODULE. " STATUS_0100 OUTPUT


THE RESULT





Example 2: Event handling - Application event
There are 2 types of events:

System events. These events are triggerede irrespective of the screen flow-logic.
Application events. The PAI module is processed after an event. 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.

Steps:

Create an input/output field on screen 100, where the event type can be output. Name it EVENT_TYPE
The code:



REPORT sapmz_hf_controls1 .
CONSTANTS:
line_length TYPE i VALUE 254.
DATA: ok_code LIKE sy-ucomm.
DATA:
* Create reference to the custom container
custom_container TYPE REF TO cl_gui_custom_container,
* Create reference to the TextEdit control
editor TYPE REF TO cl_gui_textedit,
repid LIKE sy-repid.
**********************************************************************


* Impmenting events


**********************************************************************
DATA:


event_type(20) TYPE c,


* Internal table for events that should be registred


i_events TYPE cntl_simple_events,


* Structure for oneline of the table


wa_events TYPE cntl_simple_event.
*---------------------------------------------------------------------*


* CLASS lcl_event_handler DEFINITION


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


CLASS lcl_event_handler DEFINITION.


PUBLIC SECTION.


CLASS-METHODS:


catch_dblclick FOR EVENT dblclick


OF cl_gui_textedit IMPORTING sender.
ENDCLASS.
CLASS lcl_event_handler IMPLEMENTATION.


METHOD catch_dblclick.


event_type = 'Event DBLCLICK raised'.


ENDMETHOD.


ENDCLASS.





START-OF-SELECTION.
CLEAR wa_events. refresh i_events.
SET SCREEN '100'.



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


* MODULE USER_COMMAND_0100 INPUT *


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


MODULE user_command_0100 INPUT.
CASE ok_code.
WHEN 'EXIT'.
LEAVE TO SCREEN 0.
WHEN OTHERS.
* Call the Dispacth method to initiate application event handling


call method cl_gui_cfw=>Dispatch.



ENDCASE.
ENDMODULE. " 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.
repid = sy-repid.
* Create obejct for custom container
CREATE OBJECT custom_container
EXPORTING
container_name = 'MYCONTAINER1'
EXCEPTIONS
cntl_error = 1
cntl_system_error = 2
create_error = 3
lifetime_error = 4
lifetime_dynpro_dynpro_link = 5
others = 6
.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE 'I' NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
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
.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE 'I' NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
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.


wa_events-appl_event = 'X'. "This is an application event


append wa_events to i_events.
* Pass the table to the TextEdit control using method


* set_registred_events


call method editor->set_registered_events


exporting events = i_events.
ENDIF.
ENDMODULE. " STATUS_0100 OUTPUT
Result:

When you double click on the TextEdit control, the input/ouput field should show the text: Event DBLCLICK

Example 3: Event handling - System event
System events are passed irrespective of the flow-logic of the screen. To implement a system event change the code from example 2 as follows:

Code:
CLASS lcl_event_handler IMPLEMENTATION.
METHOD catch_dblclick.
*--- event_type = 'Event DBLCLICK raised'.
* Reacting to the system event
call method cl_gui_cfw=>set_new_ok_code


exporting new_code = 'SHOW'.







MODULE user_command_0100 INPUT.
CASE ok_code.
code.........
WHEN 'SHOW'.
event_type = 'System dblclick'.
WHEN OTHERS.
*---- call method cl_gui_cfw=>Dispatch.
ENDCASE.
ENDMODULE. " USER_COMMAND_0100 INPUT


MODULE status_0100 OUTPUT.
Code ................
*--- wa_events-appl_event = 'X'. "This is an application event


wa_events-appl_event = space. "This is a system event


ENDIF.
ENDMODULE. " STATUS_0100 OUTPUT
Result:

When you double clicks on the TextEdit control nothing happens, since the flow-logic of the screen an dthe fielde transport is ignore.

Example 4: Calling methods of the control
In this exercise a function that loads the texts of an internal table into the text window, is implemented.

Steps:

Define anoterh pushbutton on the screen, that activates the method that fills the TextEdit control. Give itname PUSHBUTTON_IMPORT and function code IMP.

Define a form CREATE_TEXTS that carries out the text import.

Only changes to the code in example 2 is show.

Code:

MODULE user_command_0100 INPUT.
CASE ok_code.


code.........


WHEN 'IMP'.


perform load_texts.


ENDCASE.


ENDMODULE. " 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.
TYPES:
BEGIN OF t_texttable,
line(line_length) TYPE c,
END OF t_texttable.
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.
APPEND 'with a text.' TO i_texttable.
DO 10 TIMES.
APPEND 'hallo world !' TO i_texttable.
ENDDO.
* Load TextEdit control with texts
CALL METHOD editor->set_text_as_r3table
EXPORTING table = i_texttable.
IF sy-subrc > 0.
* Display an error message
EXIT.
ENDIF.
* All methods that operates on controls are transferred to the frontend


* by a RFC calls. the method FLUSH is used to determine when this is done.
CALL METHOD cl_gui_cfw=>flush.
IF sy-subrc > 0.
* Display an error message
ENDIF.
ENDFORM. " create_texts


Example 5: Responding to an event
When you double click on a text line in the TextEdit control, you want it to be prefixed with a '*'.

The line number of the TextEdit control that is double clicked, is retreived using method GET_SELECTION_POS. The internal text table is reloaded froim the TextEdit control with method GET_TEXT_AS_R3TABLE. The position of the double click in the TextEdit control is used to find the entry in the table, and the entry is prefixed with '*' and loaded into the TextEdit control again.

The program should be changed so that the internal table i_texttable is global, and a global flag g_loaded added. The load of the table should be moved to the PBO module. The changes in thje code are marked with red. The whole program now looks like this:

Code

REPORT sapmz_hf_controls1 .
CONSTANTS:
line_length TYPE i VALUE 254.
DATA: ok_code LIKE sy-ucomm.
DATA:
* Create reference to the custom container
custom_container TYPE REF TO cl_gui_custom_container,
* Create reference to the TextEdit control
editor TYPE REF TO cl_gui_textedit,
repid LIKE sy-repid.
**********************************************************************


* Utillity table to load texts


**********************************************************************
TYPES:


BEGIN OF t_texttable,


line(line_length) TYPE c,


END OF t_texttable.
DATA:


i_texttable TYPE TABLE OF t_texttable,


g_loaded(1) TYPE c.



**********************************************************************


* Impmenting events


**********************************************************************
DATA:
event_type(20) TYPE c,
* Internal table for events that should be registred
i_events TYPE cntl_simple_events,
* Structure for oneline of the table
wa_events TYPE cntl_simple_event.
*---------------------------------------------------------------------*


* CLASS lcl_event_handler DEFINITION


*---------------------------------------------------------------------*
CLASS lcl_event_handler DEFINITION.
PUBLIC SECTION.
CLASS-METHODS:
catch_dblclick FOR EVENT dblclick
OF cl_gui_textedit IMPORTING sender.
ENDCLASS.
CLASS lcl_event_handler IMPLEMENTATION.
METHOD catch_dblclick.
DATA:
from_line TYPE i,
from_pos TYPE i,
to_line TYPE i,
to_pos TYPE i,
wa_texttable TYPE t_texttable.
* Used for the sytem event
call method cl_gui_cfw=>set_new_ok_code
exporting new_code = 'SHOW'.
* 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.
* Texts in the TextEdit control can have been changed, so


* first reload text from the control into the internal


* table that contains text
IF NOT g_loaded IS INITIAL.


CALL METHOD sender->get_text_as_r3table


IMPORTING table = i_texttable.


* Read the line of the internal table that was clicked
READ TABLE i_texttable INDEX from_line INTO wa_texttable.


IF sy-subrc <> 0.


EXIT.


ENDIF.
IF wa_texttable+0(1) CS '*'.


SHIFT wa_texttable.


ELSEIF wa_texttable+0(1) NS '*'.


SHIFT wa_texttable RIGHT.


wa_texttable+0(1) = '*'.


ENDIF.


modify i_texttable from wa_texttable index from_line.
* Reload texts from h einternal table
perform load_texts.
ENDIF.
ENDMETHOD.
ENDCLASS.



START-OF-SELECTION.
CLEAR wa_events.
REFRESH: i_events.
SET SCREEN '100'.
*---------------------------------------------------------------------*


* MODULE USER_COMMAND_0100 INPUT *


*---------------------------------------------------------------------*
MODULE user_command_0100 INPUT.
CASE ok_code.
WHEN 'EXIT'.
LEAVE TO SCREEN 0.
WHEN 'SHOW'.
event_type = 'System dblclick'.
WHEN 'IMP'.
PERFORM Load_texts.
WHEN OTHERS.
* CALL METHOD cl_gui_cfw=>dispatch. "Not used for system events
ENDCASE.
ENDMODULE. " 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.
repid = sy-repid.
* 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
.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE 'I' NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
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
.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE 'I' NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
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.
* wa_events-appl_event = 'X'. "This is an application event
wa_events-appl_event = space. "This is a system event
APPEND wa_events TO i_events.
* Pass the table to the TextEdit control uding method


* set_registred_events
CALL METHOD editor->set_registered_events
EXPORTING events = i_events.
* 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.
APPEND 'with a text.' TO i_texttable.
DO 10 TIMES.
APPEND 'hallo world !' TO i_texttable.
ENDDO.



ENDIF.
ENDMODULE. " 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.
IF sy-subrc > 0.
* Display an error message
EXIT.
ENDIF.
* All methods that operates on controls are transferred to the frontend


* by a RFC calls. the method FLUSH is used to determine when this is


* done.
CALL METHOD cl_gui_cfw=>flush.
IF sy-subrc > 0.
* Display an error message
ENDIF.
g_loaded = 'X'.
ENDFORM. " 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 FLUSH method is used to synchronize control execution and the frontend. This is very important when working e.g. with export parameters from a method, as the parmeters will not be correct before the FLUSH method has been called.

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.

Note: Instead of using method PROTECT_LINES, the method PROTECT_SELECTION could be used. This method does not need line numbers or a FLUSH statement

Steps

Add a new pushbutton to the screen with the function code PROTECT.
Code

Add the following code to the example:

* Global variables


DATA:


from_idx TYPE i,


to_idx TYPE i,


index TYPE i.


MODULE user_command_0100 INPUT.


CASE ok_code.


code.......................


WHEN 'PROTECT'.


PERFORM protect.





. .......................


ENDCASE.


*&---------------------------------------------------------------------*


*& Form protect


*&---------------------------------------------------------------------*


* Protects marked lines in a TextEdit control


*----------------------------------------------------------------------*
FORM protect.
* 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.
* Synchronize execution in the control with the ABAP program.


* Without this synchronization the variables from_idx and


* to_idx will have obsolutete values (The initial value for


* both, are 0)
CALL METHOD cl_gui_cfw=>flush.
IF sy-subrc > 0.
* Errormessage: Error in flush
ENDIF.
* Protect the selected lines
IF to_idx > from_idx.
to_idx = to_idx - 1.
ENDIF.
CALL METHOD editor->protect_lines
EXPORTING
from_line = from_idx
to_line = to_idx.
* The PROTECT_SELECTION method could be used instead, eliminating the


* need of line numbers and the last FLUSH





* call method editor->protect_selection.
* Flush again to protect immidiately


CALL METHOD cl_gui_cfw=>flush.
IF sy-subrc > 0.
* Errormessage: Error in flush
ENDIF.



ENDFORM. " protect


Example 7: Using multiple controls
In this example a second TextEdit control will be added to the screen. The new TextEdit control will be designed to act as a clipboard for short texts.

Steps:

Add a new container to the screen and name it MYCONTAINER2.


Code:

Insert global datadeclaration:

**********************************************************************


* Implementing a second Scratch TextEdit control


**********************************************************************


DATA:


scratch TYPE REF TO cl_gui_textedit,


custom_container2 TYPE REF TO cl_gui_custom_container.


Insert the following code in the PBO module:

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


* The SCRATCH TextEdit control


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


IF scratch IS INITIAL.
* 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
.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE 'I' NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
* Create obejct for the SCRATCH TextEditor control
CREATE OBJECT scratch
EXPORTING
parent = custom_container2
wordwrap_mode =
cl_gui_textedit=>wordwrap_at_windowborder
wordwrap_to_linebreak_mode = cl_gui_textedit=>true.
* Remove the staus bar
CALL METHOD scratch->set_statusbar_mode
EXPORTING statusbar_mode = cl_gui_textedit=>false.
ENDIF.



Tuesday, October 7, 2008

SAP ABAP CODE FOR PICTURE CONTROL

Steps:
Create a screen
Place a custom container for the picture on the screen. Name the container GO_PICTURE_CONTAINER.


* Type pool for using SAP icons

TYPE-POOLS: icon.

* Declarations

DATA:

go_picture TYPE REF TO cl_gui_picture,

go_picture_container TYPE REF TO cl_gui_custom_container.

MODULE status_0100 OUTPUT.
IF go_picture_container IS INITIAL.

* Create obejcts for picture and container and
* setup picture control
CREATE OBJECT go_picture_container

EXPORTING

container_name = 'PICTURE_CONTAINER'.

CREATE OBJECT go_picture

EXPORTING
parent = go_picture_container.


* Set display mode (Stretching, original size etc.)

CALL METHOD go_picture->set_display_mode

EXPORTING

DISPLAY_MODE = CL_GUI_PICTURE=>display_mode_fit_center

EXCEPTIONS = 1.


* Load picture from SAP Icons. To oad a picture from an URL use method
* load_picture_from_url

CALL METHOD go_picture->load_picture_from_sap_icons

EXPORTING

icon = icon_delete

EXCEPTIONS error = 1.

ENDIF.

ENDMODULE.

SAP ABAP CODE FOR HTML VIEWER

Note that the SAP HTML viewer uses internet Explorer 4.0 or higher.

This example uses the SAP HTML viewer to browse the internet. The navigation buttons are placed on a SAP Toolbar control. The Goto URL button and field are normal dynpro elements, and so is the Show URL field.

Steps:

Create a screen and place a container named go_html_container for the HTML viewer.
Create a dynpro button with ethe text GGoto url and functioncode GOTOURL.
Create a dynpro input/output field named G_SCREEN100_URL_TEXT. This field is used to key in the URLl.
Create a dynpro input/output field named G_SCREEN100_DISPLAY_URL. This field is used to show the current URL

The screen:





The code



SAPMZ_HF_HTML_CONTROL
REPORT sapmz_hf_html_control .



TYPE-POOLS: icon.



CLASS cls_event_handler DEFINITION DEFERRED.



*-------------------------------------------------------------------
* G L O B A L V A R I A B L E S
*-------------------------------------------------------------------
DATA:
ok_code LIKE sy-ucomm,
* Container for html vieaer
go_html_container TYPE REF TO cl_gui_custom_container,
* HTML viewer
go_htmlviewer TYPE REF TO cl_gui_html_viewer,
* Container for toolbar
go_toolbar_container TYPE REF TO cl_gui_custom_container,
* SAP Toolbar
go_toolbar TYPE REF TO cl_gui_toolbar,
* Event handler for toolbar
go_event_handler TYPE REF TO cls_event_handler,
* Variable for URL text field on screen 100
g_screen100_url_text(255) TYPE c,
g_screen100_display_url(255) TYPE c.






*-------------------------------------------------------------------
* I N T E R N A L T A B L E S
*-------------------------------------------------------------------
DATA:
* Table for button group
gi_button_group TYPE ttb_button,
* Table for registration of events. Note that a TYPE REF
* to cls_event_handler must be created before you can
* reference types cntl_simple_events and cntl_simple_event.
gi_events TYPE cntl_simple_events,
* Workspace for table gi_events
g_event TYPE cntl_simple_event.






START-OF-SELECTION.
SET SCREEN '100'.



*---------------------------------------------------------------------*
* CLASS cls_event_handler DEFINITION
*---------------------------------------------------------------------
* Handles events for the toolbar an the HTML viewer
*---------------------------------------------------------------------*
CLASS cls_event_handler DEFINITION.
PUBLIC SECTION.
METHODS:
* Handles method function_selected for the toolbar control
on_function_selected
FOR EVENT function_selected OF cl_gui_toolbar
IMPORTING fcode,
* Handles method navigate_complete for the HTML viewer control
on_navigate_complete
FOR EVENT navigate_complete OF cl_gui_html_viewer
IMPORTING url.
ENDCLASS.



CLASS cls_event_handler IMPLEMENTATION.



* Handles method function_selected for the toolbar control
METHOD on_function_selected.
CASE fcode.
WHEN 'BACK'.
CALL METHOD go_htmlviewer->go_back
EXCEPTIONS cntl_error = 1.
WHEN 'FORWARD'.
CALL METHOD go_htmlviewer->go_forward
EXCEPTIONS cntl_error = 1.
WHEN 'STOP'.
CALL METHOD go_htmlviewer->stop
EXCEPTIONS cntl_error = 1.



WHEN 'REFRESH'.
CALL METHOD go_htmlviewer->do_refresh
EXCEPTIONS cntl_error = 1.



WHEN 'HOME'.
CALL METHOD go_htmlviewer->go_home
EXCEPTIONS cntl_error = 1.



WHEN 'EXIT'.
LEAVE TO SCREEN 0.
ENDCASE.
ENDMETHOD.



* Handles method navigate_complete for the HTML viewer control
METHOD on_navigate_complete.
* Display current URL in a textfield on the screen
g_screen100_display_url = url.
ENDMETHOD.



ENDCLASS.









*&---------------------------------------------------------------------*
*& Module STATUS_0100 OUTPUT
*&---------------------------------------------------------------------*
MODULE status_0100 OUTPUT.



IF go_html_container IS INITIAL.
* Create container for HTML viewer
CREATE OBJECT go_html_container
EXPORTING
container_name = 'HTML_CONTAINER'.



* Create HTML viewer
CREATE OBJECT go_htmlviewer
EXPORTING parent = go_html_container.



* Create container for toolbar
CREATE OBJECT go_toolbar_container
EXPORTING
container_name = 'TOOLBAR_CONTAINER'.



* Create toolbar
CREATE OBJECT go_toolbar
EXPORTING
parent = go_toolbar_container.



* Add buttons to the toolbar
PERFORM add_button_group.



* Create event table. The event ID must be found in the
* documentation of the specific control
CLEAR g_event.
REFRESH gi_events.
g_event-eventid = go_toolbar->m_id_function_selected.
g_event-appl_event = 'X'. "This is an application event
APPEND g_event TO gi_events.



g_event-eventid = go_htmlviewer->m_id_navigate_complete.
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.



CALL METHOD go_htmlviewer->set_registered_events
EXPORTING
events = gi_events.



* Create event handlers
CREATE OBJECT go_event_handler.



SET HANDLER go_event_handler->on_function_selected
FOR go_toolbar.



SET HANDLER go_event_handler->on_navigate_complete
FOR go_htmlviewer.



ENDIF.



ENDMODULE. " STATUS_0100 OUTPUT
*&---------------------------------------------------------------------*
*& Module USER_COMMAND_0100 INPUT
*&---------------------------------------------------------------------*
MODULE user_command_0100 INPUT.
* Handles the pushbutton for goto url
CASE ok_code.
WHEN 'GOTOURL'.
PERFORM goto_url.
ENDCASE.
ENDMODULE. " USER_COMMAND_0100 INPUT



*&---------------------------------------------------------------------*
*& Form add_button_group
*&---------------------------------------------------------------------*
* Adds a button group to the toolbar


*-----------------------------------------------------------------------
FORM add_button_group.



* BACK botton
CALL METHOD cl_gui_toolbar=>fill_buttons_data_table
EXPORTING
fcode = 'BACK'
icon = icon_arrow_left
butn_type = cntb_btype_button
text = ''
quickinfo = 'Go back'
CHANGING
data_table = gi_button_group.



* FORWARD botton
CALL METHOD cl_gui_toolbar=>fill_buttons_data_table
EXPORTING
fcode = 'FORWARD'
icon = icon_arrow_right
butn_type = cntb_btype_button
text = ''
quickinfo = 'Go forward'
CHANGING
data_table = gi_button_group.



* STOP button
CALL METHOD cl_gui_toolbar=>fill_buttons_data_table
EXPORTING
fcode = 'STOP'
icon = icon_breakpoint
butn_type = cntb_btype_button
text = ''
quickinfo = 'Stop'
CHANGING
data_table = gi_button_group.



* REFRESH button
CALL METHOD cl_gui_toolbar=>fill_buttons_data_table
EXPORTING
fcode = 'REFRESH'
icon = icon_refresh
butn_type = cntb_btype_button
text = ''
quickinfo = 'Refresh'
CHANGING
data_table = gi_button_group.



* Home button
CALL METHOD cl_gui_toolbar=>fill_buttons_data_table
EXPORTING
fcode = 'HOME'
icon = ''
butn_type = cntb_btype_button
text = 'Home'
quickinfo = 'Home'
CHANGING
data_table = gi_button_group.



* Separator
CALL METHOD cl_gui_toolbar=>fill_buttons_data_table
EXPORTING
fcode = 'SEP1'
icon = ' '
butn_type = cntb_btype_sep
CHANGING
data_table = gi_button_group.



* EXIT button
CALL METHOD cl_gui_toolbar=>fill_buttons_data_table
EXPORTING
fcode = 'EXIT'
icon = icon_close
butn_type = cntb_btype_button
text = ''
quickinfo = 'Close porgram'
CHANGING
data_table = gi_button_group.



* Add button group to toolbar
CALL METHOD go_toolbar->add_button_group
EXPORTING data_table = gi_button_group.



ENDFORM. " add_button_group
*&---------------------------------------------------------------------*
*& Form goto_url
*&---------------------------------------------------------------------*
* Calls method SHOW_URL to navigate to an URL
*----------------------------------------------------------------------
FORM goto_url.



CALL METHOD go_htmlviewer->show_url
EXPORTING url = g_screen100_url_text.



ENDFORM. " goto_url

SAP ABAP CODE FOR DIALOG BOX AND SPILTTER CONTAINERS

This example shows how to use the dialog box container and the splitter container.

A dialog box container is created, and a splitter container with 2 rows and 1 column is placed on it.

To keep the example simple row 1 of the splitter container is not used, but you can use it to place a control.

Row 2 is used to place a toolbar control in the button of the dialog box. The toolbar has an OK and a Cancel button. To keep the example simple, these 2 buttons only closes the dialog screen. The dialog box can also be closed by using the close button in the top right corner of the dialog box. This button is standard functionality, however you have to do the coding for closing the dialog box yourself.

Steps:

Create a screen
Place dynpro button on the screen that opens the dialog box. Name: DIALOG_BUTTON. Caption: Show dialog box. Function code: DIALOG
Place another dynpro on the screen to exit the program. Name: EXIT_BUTTON. Caption: Exit. Function code: EXIT.
Place a custom control on the screen for the dialog box. Name: DIALOG_CONTAINER
Place a custom control on the screen for the toolbar control. Name: TOOLBAR_CONTAINER
The screen:



The code:
REPORT sapmz_hf_dialogbox_cont.

* Icons for the toolbar
TYPE-POOLS:icon.

* Predefinition of the event handler class. Necessary if
* you want to make references to the class before it is defined
CLASS lcl_event_handler DEFINITION DEFERRED.

*----------------------------------------------------------------------
* G L O B A L D A T A
*----------------------------------------------------------------------
DATA:
ok_code LIKE sy-ucomm,
* Dialog container
go_dialog_container TYPE REF TO cl_gui_dialogbox_container,
* Splitter container
go_splitter_container TYPE REF TO cl_gui_splitter_container,
* Event handler class
go_event_handler TYPE REF TO lcl_event_handler,
* SAP Toolbar
go_toolbar TYPE REF TO cl_gui_toolbar.



*----------------------------------------------------------------------
* Table and workarea 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.
*----------------------------------------------------------------------
DATA:
gi_events TYPE cntl_simple_events,
g_event TYPE cntl_simple_event.



*---------------------------------------------------------------------*
* CLASS lcl_event_handler
*---------------------------------------------------------------------*
* This class is used to handle events from the dialobox
* and the toolbar
*---------------------------------------------------------------------*
CLASS lcl_event_handler DEFINITION.
PUBLIC SECTION.
METHODS:
* Close event of the dialogbox
on_close
FOR EVENT close OF cl_gui_dialogbox_container
IMPORTING sender,
* Select event of the toolbar
on_function_selected
FOR EVENT function_selected OF cl_gui_toolbar
IMPORTING fcode.
ENDCLASS.

CLASS lcl_event_handler IMPLEMENTATION.
*----------------------------------------------------------------------
* Handles the Close event of the dialogbox. The colse event is
* triggered when the close button in the top right corner of the
* dialogbox is pushed. Closes the dialog box
*----------------------------------------------------------------------
METHOD on_close.
IF NOT sender IS INITIAL.
CALL METHOD sender->free
EXCEPTIONS
OTHERS = 1.
IF sy-subrc <> 0.
* Error handling
ENDIF.
FREE go_dialog_container.
CLEAR go_dialog_container.
ENDIF.
ENDMETHOD.

*----------------------------------------------------------------------
* Handles the Function Selected event of the toolbar, which is
* triggered when one of the buttons on the toolbar is pushed.
* Both pushbuttons closes the dialogbox
*----------------------------------------------------------------------
METHOD on_function_selected.
CASE fcode.
WHEN 'OK'.
CALL METHOD go_dialog_container->free.
FREE go_dialog_container.
CLEAR go_dialog_container.

WHEN 'CANCEL'.
CALL METHOD go_dialog_container->free.
FREE go_dialog_container.
CLEAR go_dialog_container.
ENDCASE.

ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.
* Necessary to get access to certain predefined constants
CLASS cl_gui_cfw DEFINITION LOAD.
SET SCREEN '100'.



*&---------------------------------------------------------------------*
*& Module USER_COMMAND_0100 INPUT
*&---------------------------------------------------------------------*
* Handles the 2 dynpro pushbuttons on screen
*----------------------------------------------------------------------
MODULE user_command_0100 INPUT.
CASE ok_code.
WHEN 'EXIT'.
LEAVE TO SCREEN 0.
WHEN 'DIALOG'.
PERFORM show_dialog_box.
ENDCASE.
ENDMODULE. " USER_COMMAND_0100 INPUT



*&---------------------------------------------------------------------*
*& Form show_dialog_box
*&---------------------------------------------------------------------*
* Performed when the Show dialog box button is pushed.
*----------------------------------------------------------------------*
FORM show_dialog_box.



IF go_dialog_container IS INITIAL.
*----------------------------------------------------------------------
* Create Dialogbox
*----------------------------------------------------------------------
CREATE OBJECT go_dialog_container
EXPORTING
* PARENT =
width = 400
height = 150
style = cl_gui_control=>ws_sysmenu
* REPID =
* dynnr = '100'
* LIFETIME = lifetime_default
top = 100
left = 350
caption = 'My dialog box'
* NO_AUTODEF_PROGID_DYNNR =
* METRIC = 0
* NAME =
EXCEPTIONS
CNTL_ERROR = 1
CNTL_SYSTEM_ERROR = 2
CREATE_ERROR = 3
LIFETIME_ERROR = 4
LIFETIME_DYNPRO_DYNPRO_LINK = 5
EVENT_ALREADY_REGISTERED = 6
ERROR_REGIST_EVENT = 7
others = 8.
IF sy-subrc <> 0.
* Do some error handling..............
ENDIF.



*----------------------------------------------------------------------
* Create Splitter container and configure
*----------------------------------------------------------------------
CREATE OBJECT go_splitter_container
EXPORTING
parent = go_dialog_container
rows = 2
columns = 1
EXCEPTIONS
others = 1.
IF sy-subrc <> 0.
* Do some error handling..............
ENDIF.



* Set row height for row 1
CALL METHOD go_splitter_container->set_row_height
EXPORTING
id = 1
height = 90.


* The splitter container should not have a border
CALL METHOD go_splitter_container->set_border
EXPORTING
border = cl_gui_cfw=>false.



* Configure the splitter bar. The splitterbar is configures * so that it can't be moved
CALL METHOD go_splitter_container->set_row_sash
EXPORTING
id = 1 "Configure splitter bar no. 1
type = 0 "Type_movable
value = 0. "False



*----------------------------------------------------------------------
* Create Toolbar and set parent to the Splitter container
*----------------------------------------------------------------------
CREATE OBJECT go_toolbar
EXPORTING
parent = go_splitter_container
EXCEPTIONS
others = 1.
IF sy-subrc <> 0.

ENDIF.

* Add a buttons CALL METHOD go_toolbar->add_button
EXPORTING
fcode = 'OK' "Function Code
icon = icon_okay "ICON name
is_disabled = ' ' "Disabled = X
butn_type = cntb_btype_button "Type of button
text = 'OK' "Text on button
is_checked = ' ' "Button selected
EXCEPTIONS
OTHERS = 1.
IF sy-subrc <> 0.
* Do some error handling..............
ENDIF.



CALL METHOD go_toolbar->add_button
EXPORTING
fcode = 'CANCEL' "Function Code
icon = icon_cancel "ICON name
is_disabled = ' ' "Disabled = X
butn_type = cntb_btype_button "Type of button
text = 'Cancel' "Text on button
is_checked = ' ' "Button selected
EXCEPTIONS
OTHERS = 1.
IF sy-subrc <> 0.
* Do some error handling..............
ENDIF.



*----------------------------------------------------------------------
* Add Toolbar control to row 2 of the Splitter container
*----------------------------------------------------------------------
CALL METHOD go_splitter_container->add_control
EXPORTING row = 2
column = 1
control = go_toolbar.

*----------------------------------------------------------------------
* Create object for the event handler class
*----------------------------------------------------------------------
CREATE OBJECT go_event_handler.

*----------------------------------------------------------------------
* Set Handler for the dialog container. Note that you don't have to
* register the events for this class
*----------------------------------------------------------------------
SET HANDLER go_event_handler->on_close
FOR go_dialog_container.

*----------------------------------------------------------------------
* Create event handler table for the toolbar control, and register
* the events
*----------------------------------------------------------------------

* Create event table. The event ID must be found in the
* documentation of the specific control
CLEAR g_event.
REFRESH gi_events.
g_event-eventid = go_toolbar->m_id_function_selected.
g_event-appl_event = 'X'. "This is an application event
APPEND g_event TO gi_events.

* Use the events table to register events for the control
CALL METHOD go_toolbar->set_registered_events
EXPORTING
events = gi_events.

* Set handler
SET HANDLER go_event_handler->on_function_selected
FOR go_toolbar.
* Synchronization
CALL METHOD cl_gui_cfw=>flush.
ENDIF.
ENDFORM. " show_dialog_box

SAP ABAP CODE FOR CREATING TOOLBAR CONTROLS

General

Add method

Add_button_group method

Set_button state method

Simple example

Advanced example

General
See also Set up event handling for controls for a general example of event handling

Note: To get a list of all icons, use program SHOWICON.

Add method
Adds a new button to the toolbar
CALL METHOD go_toolbar->add_button

EXPORTING fcode = 'EXIT' "Function Code for button

icon = icon_system_end "ICON name, You can use type pool ICON

is_disabled = ' ' "Disabled = X

butn_type = gc_button_normal "Type of button, see below


text 'Exit' "Text on button

quickinfo = 'Exit program' "Quick info

is_checked = ' '. "Button selected

Toolbar button types used in method ADD_BUTTON:
Value
Constant
Meaning

0
cntb_btype_button
Button (normal)

1
cntb_btype_dropdown
Pushbutton with menu

2
cntb_btype_menu
Menu

3
cntb_btype_sep
Seperator

4
cntb_btype_group
Pushbutton group

5
cntb_btype_check
Checkbox

6

Menu entry


Add_button_group method
This method is used to add a list of buttons to the toolbar. The buttons are defined in a table of type TTB_BUTTON, and it can be filled witha button definitions using method fill_buttons_data_table of the cl_gui_toolbar class. The button group is added to the toolbar using method add_button_group of the toolbar object.



* 1. Declare a table for buttons

DATA: gi_button_group TYPE ttb_button.
* 2. Create buttons in button table

CALL METHOD cl_gui_toolbar=>fill_buttons_data_table


EXPORTING


fcode = 'Disable'


* icon =


* DISABLED =


butn_type = cntb_btype_group


* TEXT =


* QUICKINFO =


* CHECKED =


changing


data_table = gi_button_group


* EXCEPTIONS


* CNTB_BTYPE_ERROR = 1


* others = 2


.
CALL METHOD cl_gui_toolbar=>fill_buttons_data_table..... add more buttons to the table
*3. Add button group to toolbar


CALL METHOD go_toolbar->add_button_group


EXPORTING data_table = gi_button_group.


Set_button state method
Used to change the state of individual buttons at runtime. If the button should be removed, use the delete_button method.

CALL METHOD go_toolbar->set_button_state


EXPORTING


* ENABLED = 'X'


* CHECKED = ' '


fcode = "Note: This is the function code of the button that should be changed


* EXCEPTIONS


* CNTL_ERROR = 1


* CNTB_ERROR_FCODE = 2


* others = 3


.


Simple example
This example shows how to create a toolbar with a single Exit button, used to exit the program.

Steps:

Create a screen and add a custom container named TOOLBAR_CONTAINER
Code:

REPORT sapmz_hf_toolbar .
TYPE-POOLS: icon.
CLASS cls_event_handler DEFINITION DEFERRED.


* G L O B A L D A T A
DATA:
ok_code LIKE sy-ucomm,
* Reference for conatiner
go_toolbar_container TYPE REF TO cl_gui_custom_container,
* Reference for SAP Toolbar
go_toolbar TYPE REF TO cl_gui_toolbar,
* Event handler
go_event_handler TYPE REF TO cls_event_handler.


* G L O B A L T A B L E S
DATA:
* Table for registration of events. Note that a TYPE REF
* to cls_event_handler must be created before you can
* reference types cntl_simple_events and cntl_simple_event.
gi_events TYPE cntl_simple_events,
* Workspace for table gi_events
g_event TYPE cntl_simple_event.


*---------------------------------------------------------------------*
* CLASS cls_event_handler DEFINITION
*---------------------------------------------------------------------*
* ........ *
*---------------------------------------------------------------------*
CLASS cls_event_handler DEFINITION.
PUBLIC SECTION.
METHODS:
on_function_selected
FOR EVENT function_selected OF cl_gui_toolbar
IMPORTING fcode,
on_dropdown_clicked
FOR EVENT dropdown_clicked OF cl_gui_toolbar
IMPORTING fcode posx posy.

ENDCLASS.

*---------------------------------------------------------------------*
* CLASS cls_event_handler IMPLEMENTATION
*---------------------------------------------------------------------*
* ........ *
*---------------------------------------------------------------------*
CLASS cls_event_handler IMPLEMENTATION.
METHOD on_function_selected.
CASE fcode.
WHEN 'EXIT'.
LEAVE TO SCREEN 0.
ENDCASE.
ENDMETHOD.



METHOD on_dropdown_clicked.
* Not implented yet
ENDMETHOD.
ENDCLASS.

START-OF-SELECTION.
SET SCREEN '100'.

*&---------------------------------------------------------------------*
*& Module STATUS_0100 OUTPUT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
MODULE status_0100 OUTPUT.
IF go_toolbar_container IS INITIAL.
* Create container
CREATE OBJECT go_toolbar_container
EXPORTING
container_name = 'TOOLBAR_CONTAINER'.

* Create toolbar
CREATE OBJECT go_toolbar
EXPORTING
parent = go_toolbar_container.

* Add a button
CALL METHOD go_toolbar->add_button
EXPORTING fcode = 'EXIT' "Function Code
icon = icon_system_end "ICON name
is_disabled = ' ' "Disabled = X
butn_type = cntb_btype_button "Type of button
text = 'Exit' "Text on button
quickinfo = 'Exit program' "Quick info
is_checked = ' '. "Button selected


* Create event table. The event ID must be found in the
* documentation of the specific control
CLEAR g_event.
REFRESH gi_events.
g_event-eventid = go_toolbar->m_id_function_selected.
g_event-appl_event = 'X'. "This is an application event
APPEND g_event TO gi_events.

g_event-eventid = go_toolbar->m_id_dropdown_clicked.
g_event-appl_event = 'X'.
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.

* Create event handlers
CREATE OBJECT go_event_handler.
SET HANDLER go_event_handler->on_function_selected
FOR go_toolbar.
SET HANDLER go_event_handler->on_dropdown_clicked
FOR go_toolbar.
ENDIF.
ENDMODULE. " STATUS_0100 OUTPUT

Advanced example

The toolbar in this example contains an Exit button, two buttons that enables/and disables a Print button, and a Menu button with a context menu.

The Disable/Enable buttons are of type Pushbutton group which makes them act together, so when one of the buttons are selected (Down) the other is deselected (Up).

Note that the context menu for the Menu button, must be created as a GUI status. You can create an empty GUI status of type context, and then manually add menus. The context menu instance is created with type reference to class cl_ctmenu.

Steps:
Create a screen and add a custom container named TOOLBAR_CONTAINER
Create a GUI status of type Context and name it TOOLBAR. Note that you can not add any buttons to the GUI sttaus at design time.

The screen:

In this screen shot the Disable button is activated which makes the Print button disabled:





The code:

REPORT sapmz_hf_toolbar .

TYPE-POOLS: icon.

CLASS cls_event_handler DEFINITION DEFERRED.

*--- G L O B A L D A T A
DATA:
ok_code LIKE sy-ucomm,
* Global varables for position of context menu
g_posx TYPE i,
g_posy TYPE i,
* Reference for conatiner
go_toolbar_container TYPE REF TO cl_gui_custom_container,
* Reference for SAP Toolbar
go_toolbar TYPE REF TO cl_gui_toolbar,
* Event handler
go_event_handler TYPE REF TO cls_event_handler,
* Context menu
go_context_menu TYPE REF TO cl_ctmenu.


*--- G L O B A L T A B L E S
DATA:
* Table for registration of events. Note that a TYPE REF
* to cls_event_handler must be created before you can
* reference types cntl_simple_events and cntl_simple_event
gi_events TYPE cntl_simple_events,
* Workspace for table gi_events
g_event TYPE cntl_simple_event,
* Table for button group
gi_button_group TYPE ttb_button.


*---------------------------------------------------------------------*
* CLASS CLS_EVENT_HANDLER
*---------------------------------------------------------------------*
* This class handles the function_selected and dropdow_clicked events
* from the toolbar
*---------------------------------------------------------------------*
CLASS cls_event_handler DEFINITION.
PUBLIC SECTION.
METHODS:
on_function_selected
FOR EVENT function_selected OF cl_gui_toolbar
IMPORTING fcode,
on_dropdown_clicked
FOR EVENT dropdown_clicked OF cl_gui_toolbar
IMPORTING fcode posx posy.

ENDCLASS.


CLASS cls_event_handler IMPLEMENTATION.
METHOD on_function_selected.
*-- Actions for buttons and context menus
CASE fcode.
WHEN 'EXIT'.
LEAVE TO SCREEN 0.
WHEN 'ENABLE'.
* Enable the PRINT button
CALL METHOD go_toolbar->set_button_state
EXPORTING
enabled = 'X'
fcode = 'PRINT'.
WHEN 'DISABLE'.
* Disable the PRINT button
CALL METHOD go_toolbar->set_button_state
EXPORTING
enabled = ' '
fcode = 'PRINT'.
* Other menus and context menus
WHEN 'PRINT'.
CALL FUNCTION 'POPUP_TO_DISPLAY_TEXT'
EXPORTING
textline1 = 'Printing'.
WHEN 'CONTEXT1'.
CALL FUNCTION 'POPUP_TO_DISPLAY_TEXT'
EXPORTING
textline1 = 'Menu: Do something funny'.
WHEN 'CONTEXT2'.
CALL FUNCTION 'POPUP_TO_DISPLAY_TEXT'
EXPORTING
textline1 = 'Menu: Do something crazy'.
WHEN 'SUB1'.
CALL FUNCTION 'POPUP_TO_DISPLAY_TEXT'
EXPORTING
textline1 = 'Submenu: Do something boring'.
WHEN 'SUB2'.
CALL FUNCTION 'POPUP_TO_DISPLAY_TEXT'
EXPORTING
textline1 = 'Submenu: Do something good'.

ENDCASE.
ENDMETHOD.

METHOD on_dropdown_clicked.
*-- Fires when a dropdown menu is clicked. After it has been
*-- clicked a context menu is shown beside the button.

* Save x and y position of button for use with context menu
CLEAR: g_posx, g_posy.
g_posx = posx.
g_posy = posy.


* Create context menu for menu button
PERFORM create_context_menu.

ENDMETHOD.
ENDCLASS.

START-OF-SELECTION.
SET SCREEN '100'.


*&---------------------------------------------------------------------*
*& Module STATUS_0100 OUTPUT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
MODULE status_0100 OUTPUT.



IF go_toolbar_container IS INITIAL.
* Create container
CREATE OBJECT go_toolbar_container
EXPORTING
container_name = 'TOOLBAR_CONTAINER'.



* Create toolbar
CREATE OBJECT go_toolbar
EXPORTING
parent = go_toolbar_container.


* Add a button to the toolbar
PERFORM add_button.

* Add a button group to the toolbar
PERFORM add_button_group.

* Create event table. Note that the event ID must be found in the
* documentation of the specific control
CLEAR g_event. REFRESH gi_events.
g_event-eventid = go_toolbar->m_id_function_selected.
g_event-appl_event = 'X'. "This is an application event
APPEND g_event TO gi_events.

CLEAR g_event.
g_event-eventid = go_toolbar->m_id_dropdown_clicked.
g_event-appl_event = 'X'.
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.

* Create event handlers
CREATE OBJECT go_event_handler.

SET HANDLER go_event_handler->on_function_selected
FOR go_toolbar.


SET HANDLER go_event_handler->on_dropdown_clicked
FOR go_toolbar.

ENDIF.
ENDMODULE. " STATUS_0100 OUTPUT

*&---------------------------------------------------------------------*
*& Form add_button
*&---------------------------------------------------------------------*
* Adds one pushbutton to the toolbar
*----------------------------------------------------------------------*
FORM add_button.
CALL METHOD go_toolbar->add_button
EXPORTING fcode = 'EXIT' "Function Code
icon = icon_system_end "ICON name
is_disabled = ' ' "Disabled = X
butn_type = cntb_btype_button "Type of button
text = 'Exit' "Text on button
quickinfo = 'Exit program' "Quick info
is_checked = ' '. "Button selected

ENDFORM. " add_button


*&---------------------------------------------------------------------*
*& Form add_button_group
*&---------------------------------------------------------------------*
* Adds a button group to the toolbar.
* The buttons are added to table gi_button_group, and the table is used
* as input to the Add_button_group method. Note that method Fill_buttons
* is used to fill the table.
*----------------------------------------------------------------------*
FORM add_button_group.

* Add a seperator
CALL METHOD cl_gui_toolbar=>fill_buttons_data_table
EXPORTING
fcode = 'SEP1'
icon = ' '
butn_type = cntb_btype_sep
CHANGING
data_table = gi_button_group.
.
* Add an Enable button
CALL METHOD cl_gui_toolbar=>fill_buttons_data_table
EXPORTING
fcode = 'ENABLE'
icon = ' '
butn_type = cntb_btype_group
text = 'Enable'
quickinfo = 'Enable a print button'
checked = 'X'
CHANGING
data_table = gi_button_group.
.



* Add a Disable button
CALL METHOD cl_gui_toolbar=>fill_buttons_data_table
EXPORTING
fcode = 'DISABLE'
icon = ''
butn_type = cntb_btype_group
text = 'Disable'
quickinfo = 'Disable print button'
checked = ' '
CHANGING
data_table = gi_button_group.



* Add a seperator
CALL METHOD cl_gui_toolbar=>fill_buttons_data_table
EXPORTING
fcode = 'SEP2'
icon = ' '
butn_type = cntb_btype_sep
CHANGING
data_table = gi_button_group.



* Add print button
CALL METHOD cl_gui_toolbar=>fill_buttons_data_table
EXPORTING
fcode = 'PRINT'
icon = icon_print
butn_type = cntb_btype_button
text = 'Print'
quickinfo = 'Print something'
CHANGING
data_table = gi_button_group.


* Add a menu button
CALL METHOD cl_gui_toolbar=>fill_buttons_data_table
EXPORTING
fcode = 'MENU'
icon = ' '
butn_type = cntb_btype_menu
text = 'Menu'
quickinfo = 'A menu buttonz'
CHANGING
data_table = gi_button_group.

* Add button group to toolbar
CALL METHOD go_toolbar->add_button_group
EXPORTING data_table = gi_button_group.

ENDFORM. " add_button_group
*&---------------------------------------------------------------------*
*& Form create_context_menu
*&---------------------------------------------------------------------*
* This form creates a context menu and a submenu for the menu button.
*----------------------------------------------------------------------*
FORM create_context_menu.
DATA: lo_submenu TYPE REF TO cl_ctmenu.



IF go_context_menu IS INITIAL.

*-- Create context menu
CREATE OBJECT go_context_menu.

CALL METHOD go_context_menu->add_function
EXPORTING
fcode = 'CONTEXT1'
text = 'Do something funny'.

CALL METHOD go_context_menu->add_function
EXPORTING
fcode = 'CONTEXT2'
text = 'Do something crazy'.



CALL METHOD go_context_menu->add_separator.

* Create sub menu for the context menu
CREATE OBJECT lo_submenu.

CALL METHOD lo_submenu->add_function
EXPORTING
fcode = 'SUB1'
text = 'Do something boring'.

CALL METHOD lo_submenu->add_function
EXPORTING
fcode = 'SUB2'
text = 'Do something good'.

*-- Add sub menu to the context menu
CALL METHOD go_context_menu->add_submenu
EXPORTING
menu = lo_submenu
text = 'Do something else.....'.
ENDIF.

* Link menu to toolbar button. To position the context menu the
* x and y positions of the menu button is used.
* These values was retrieved in the On_dropdown_clicked
* method of cls_event_handler
CALL METHOD go_toolbar->track_context_menu
EXPORTING
context_menu = go_context_menu
posx = g_posx
posy = g_posy.

ENDFORM. " create_context_menu

SAP Abap Code for Create Listbox

Description:
Here is a simple program that create Listbox and display the selected value. I create this program to help Dennis Staiger. And because this tutorial very simple it is more suitable for newbie.

First, we create Type and Data declaration, and get data for listbox value.

REPORT  Z_LISTBOX .
TABLES: T554T.
DATA : BEGIN OF WA_T554T,
AWART TYPE AWART,
ATEXT TYPE ABWTXT,
END OF WA_T554T,
IT_T554T LIKE STANDARD TABLE OF WA_T554T,
OK_CODE LIKE sy-ucomm.

START-OF-SELECTION.

* Get Listbox Item
SELECT AWART ATEXT
INTO TABLE IT_T554T
FROM T554T
WHERE SPRSL = 'EN'.

SORT IT_T554T BY AWART.
DELETE ADJACENT DUPLICATES FROM IT_T554T COMPARING AWART.

CALL SCREEN 9100.

Lets create the screen with Listbox on it.
Screen name : '9100'.
Label name : 'Label1'.
Label text : 'Please Select:'.
Lisbox name : 'T554T-AWART'.

Tips for creating Lisbox:

  • Create input field
  • Double click on your input field to display Screen Painter Attributes
  • Select Dropdown as Lisbox.

Don't forget to input FctCode value: 'SELECTED'.

Don't forget to add OK_CODE in screen element list.

Now let add code for the Flow Logic on Screen 9100.

PROCESS BEFORE OUTPUT.
MODULE STATUS_9100.
*
PROCESS AFTER INPUT.
MODULE USER_COMMAND_9100.

PROCESS ON VALUE-REQUEST.
FIELD T554T-AWART MODULE create_dropdown_box.

Here we create GUI-Status and GUI-Title.
MODULE STATUS_9100.

MODULE STATUS_9100 OUTPUT.
SET PF-STATUS 'ST_9100'.
SET TITLEBAR 'TB_9100'.
ENDMODULE. " STATUS_9100 OUTPUT




Here we add Listbox value.
FIELD T554T-AWART MODULE create_dropdown_box.

MODULE create_dropdown_box INPUT.
CALL FUNCTION 'F4IF_INT_TABLE_VALUE_REQUEST'
EXPORTING
retfield = 'AWART'
value_org = 'S'
TABLES
value_tab = IT_T554T
EXCEPTIONS
parameter_error = 1
no_values_found = 2
OTHERS = 3.
IF sy-subrc <> 0.
...
ENDIF.
ENDMODULE. " create_dropdown_box INPUT

Here we display the Selected Lisbox Value using message box.
MODULE USER_COMMAND_9100.

MODULE USER_COMMAND_9100 INPUT.
CASE OK_CODE.
WHEN 'BACK'. LEAVE TO SCREEN 0.
WHEN 'EXIT'. LEAVE TO SCREEN 0.
WHEN 'CANC'. LEAVE TO SCREEN 0.
WHEN 'SELECTED'.
READ TABLE IT_T554T INTO WA_T554T BINARY SEARCH
WITh KEY AWART = T554T-AWART.
IF SY-SUBRC = 0.
MESSAGE I398(00) WITH 'Key: ' WA_T554T-AWART
'Value: ' WA_T554T-ATEXT.

ENDIF.
ENDCASE.
ENDMODULE. " USER_COMMAND_9100 INPUT

Archives