ABAP Objects in Action - Screen Programming With The Control Framework
ABAP Objects in Action - Screen Programming With The Control Framework
This article demonstrates how to use controls with ABAP Objects to program single-screen user interfaces. It
also takes you step-by-step through the development process and provides sample code that serves as a
starting point for creating more inviting user interfaces.
If you have attended a recent training class or SAP demonstration where SAP's "flight
model" was used as a database, you should recognize the abbreviations AA, AZ, and so
on, shown on the flight information screen in Figure 1. But compared to classical training
examples, the screen in Figure 1 is much fancier. The upper left portion shows an
airplane graphic that has no functionality - it is for visual purposes only. On the lower left
is a tree structure, from which users can select carriers or flight connections from SAP's
flight model. The third pane displays the Web site of the selected carrier or the detail lists
for flight connections.
This is just one example of the kinds of easy-to-use, easy-to-navigate interfaces that
you can provide users when you combine the strengths of GUI control technology with
ABAP Objects.
In this article, we show you how to use controls with ABAP Objects to program singlescreen user interfaces. After providing an overview of the control framework, we will take
you step by step through the development process and provide some sample code which will run in R/3 Release 4.6 - that you can use as a starting point for creating more
inviting user interfaces in your own applications.
Controls in General
Controls are independent software components that are shipped with the SAP GUI,
beginning with Release 4.5. You can place controls on screens to either replace or work
alongside classical components - both are supported in parallel. SAP currently supports
ActiveX controls for Microsoft Windows platforms and JavaBeans for the SAP GUI in the
Java environment.
Note that controls provide a great deal of functionality and can keep a lot of data at
the presentation server without putting weight on the application server (for example,
during scrolling and editing of texts). On the other hand, frequent data exchange between
the frontend and backend may increase the network load. Therefore, controls are
appropriate when most of the work can be done at the frontend and when exchange with
the backend does not occur too often.
Control Framework
In order to facilitate programming with controls, starting with Release 4.6, the SAP Basis
system provides a control framework (CFW). The CFW encapsulates all available
controls in global classes of ABAP Objects. To work with controls on your screen, simply
create objects of these global classes, call their methods, and react on their events.
Since the CFW classes are part of an inheritance hierarchy, these classes provide a
standardized interface for most of the common functionality you will find in the controls.
The CFW distinguishes between container controls and application controls. Application
controls are the components you want to use on the screen. Container controls provide a
software layer that standardizes all layout-related issues and the implementation of
application controls. All application controls must be linked to a container control while
the container control is linked to an area of the screen (see Figure 2).
CL_GUI_CUSTOM_CONTAINER:
Application Controls
After creating objects of container controls, you can create objects of application controls
- the components you want to use onscreen. You must link each application control to an
object of a container control. You can do this by simply passing the respective reference
to the parameter parent of the application control's constructor. Important application
controls and their respective global classes are:
Control Methods
You use the methods defined in the control's classes to work with the controls displayed
on the screen. In general, you work with the methods of the application controls. You
would use these, for example, to fill text from an internal table into the text editor of the
textedit control. But sometimes you will also call methods of the CFW. In order to
minimize the network load between backend and frontend, method calls are buffered in
an automation queue before being sent to the frontend at defined synchronization points,
such as at the end of PBO (Process Before Output) processing. To force a
synchronization point in your program, you can call the static method cl_gui_cfw=> flush.
event, you must register an event handler method in your ABAP program with the
SET HANDLER statement. This method is then executed on the application server.
Pros and cons: The benefits of using this default technique is that the event handler
method is executed automatically. There also are no conflicts with the classical
automatic input checks associated with the screen. The disadvantage is that the
contents of the classical screen fields that might exist alongside controls are not
automatically transported to the program.
to the application server beforehand. The disadvantage is that this kind of event
handling can lead to conflicts with the automatic input checks on the screen, which
can cause events to be lost.
Why two types of event handling? You have these options because controls may be
displayed on classical ABAP screens, and there may be classical screen elements
alongside controls. The flexibility of the two types of event registration and the
methods of the CFW, mentioned above, allow you to steer the sequence of data
transports between frontend and backend. If you do not use any classical screen
components at all, you can simply work with system events (the default mode).
we have chosen the program type 1 (executable). Note that we could have taken one of
the other program types that support screens, namely a module pool or a function pool.
Choosing an executable just simplifies the program execution, because you can start the
program directly from the ABAP Editor.
The program does not contain any global data declarations, and it does not make use of
the classical reporting events such as START-OF-SELECTION. It is simply a container
for the three processing blocks of the classical screen framework, shown in Figure 5, and
our two local classes.
Figure 6 shows which objects the program uses to create the screen display of the
demonstration program, along with the references between these objects.
During screen display, there is no instance of the local class screen_init because
there is no global reference variable in the program that can point to such an instance.
An object of screen_init will be created temporarily during each PBO processing of the
screen, where it is used as a factory object to create the control objects from the global
classes cl_gui_splitter_container, cl_gui_picture, and cl_gui_ simple_tree. After each
PBO processing, the object of screen_init will be deleted by the garbage collector. The
objects of the global classes are kept alive by pointers from the CFW because they are
bound to screen areas. An instance of screen_handler is registered as an event handler
for events of object cl_gui_simple_tree. This instance itself holds references to objects of
the global classes cl_gui_ html_viewer and cl_gui_alv_grid. The instances of the global
classes are linked to controls on the screen via the CFW layer. The two objects of cl_gui_
splitter_container split the screen in the three areas shown in Figure 1. The objects of the
application controls are linked to these areas. The objects of cl_gui_html_viewer and
cl_gui_alv_grid are both linked to the same area.
respectively, the latter with the addition AT EXIT-COMMAND. We did not use the
graphical Screen Painter for our example because we have no classical elements on our
screen, and we will use an implicit method to create a custom control. The PBO module
sets a GUI status. Within that status, the three function codes BACK, EXIT, and CANCEL
are defined with function type E (Exit Command) and are activated in the symbol bar.
Therefore, the PAI module is called only when the user wants to leave the program. The
most important action during PBO is calling the static method init_screen of class
screen_handler. All actual screen handling is encapsulated in the two local classes.
LOAD-OF-PROGRAM.
CALL SCREEN 100.
MODULE status_0100 OUTPUT.
SET PF-STATUS 'SCREEN_100'.
SET TITLEBAR 'TIT_100'.
CALL METHOD screen_init=>init_screen.
ENDMODULE.
MODULE cancel INPUT.
LEAVE PROGRAM.
ENDMODULE.
Class screen_init has a public static method, init_screen, that can be called
without creating an object of that class.
METHOD init_screen.
DATA screen TYPE REF TO screen_init.
CREATE OBJECT screen.
ENDMETHOD.
METHOD constructor.
DATA: events TYPE cntl_simple_events,
event LIKE LINE OF events,
event_handler TYPE REF TO screen_handler,
Alternatively, we could create a custom control on Screen 100 with the graphical
Screen Painter and pass its name instead of screen0. Then, only this area would
be used for displaying the controls.
TIP: When you write more complex programs that work with
more than one screen, you must create custom controls with
the graphical Screen Painter in order to distinguish between
3.
the screens.
Split the screen into two columns (columns = 2). We fill the remaining
IMPORTING parameters with appropriate values to split Screen 100 into two
columns. For each column, a new container control is created. References to
each new container are stored in a system table of the CFW. You cannot access
this table from the ABAP program. Nevertheless, the table lines point to container
control objects of the program that are currently linked to a screen and prevent
these objects from being deleted by the garbage collector, even if the container
control objects are not referenced in the program itself (see Figure 6).
TIP: To remove a control object from the program, you must
use its method free before initializing all respective reference
variables. This method deletes the respective entry from the
4.
5.
Creating the Application Controls By going through the steps listed above, we have
created all the container controls that we need. Now we can go on to create our
application controls. The process is shown in the code in Listing 5, and involves these
steps:
1.
Create application controls for the left column (CREATE OBJECT picture
and CREATE OBJECT tree). With the instance constructor of class screen_init,
we create the two application controls for the containers in the left column. In the
upper container, we create an object of cl_gui_picture. In the lower container, we
create an object of cl_gui_simple_tree. We link the objects to the respective
container controls by passing the references in container_top and container_
bottom to their constructor's IMPORTING parameter parent.
2.
2.
By moving a blank character ' ' (default value) and not an 'X' to the
component appl_event, we define that the event is handled as a system event
(default) and not as an application event.
Send initial data to the application controls (call method: me->fill picture, me->fill
tree). Finally, the constructor calls the methods fill_picture and fill_tree to initialize
the contents of our application controls on the screen.
Method fill_picture
Listing 6 shows the instance method fill_picture of class screen_init. The method
fill_picture imports a picture in GIF format from an indx type database table abtree into a
local internal table pict_tab. The function module dp_create_url creates a URL for that
internal table and passes it to the local variable url. By passing that URL to method
load_picture_from_url of our picture control object, we send the picture to the picture
control at the screen. The method set_display_mode allows us to set the attributes of the
picture displayed in the control.
Instead of working with an internal table, you can also use a local picture file from
your presentation server and pass its name directly to the parameter url of method
load_picture_from_url. Our program has the advantage of working for all users.
METHOD fill_picture.
TYPES pict_line(256) TYPE c.
DATA pict_tab TYPE TABLE OF pict_line.
DATA url(255) TYPE c.
IMPORT pict_tab = pict_tab FROM DATABASE abtree(pi) ID 'FLIGHTS'.
CALL FUNCTION 'DP_CREATE_URL'
EXPORTING
type = 'IMAGE'
subtype = 'GIF'
TABLES
data = pict_tab
CHANGING
url = url.
CALL METHOD picture->load_picture_from_url EXPORTING url = url.
CALL METHOD picture->set_display_mode
EXPORTING display_mode = picture->display_mode_fit_center.
ENDMETHOD.
TIP:How can you save pictures in an indx type database table? Listing 7 shows a little
helper routine that fulfills that purpose. Provide your favorite picture in a folder (for
example, C:\TEMP\ of your presentation server) and simply run this program. The
program loads the picture in binary format into an internal table and exports it to an indx
type database table. (We strongly recommend that you create your own indx type table
instead of using abtree or indx itself!)
REPORT picture_save.
PARAMETERS file TYPE rlgrap-filename DEFAULT 'C:\TEMP\.GIF'.
PARAMETERS id(20) TYPE c.
DATA pict_line(256) TYPE c.
DATA pict_tab LIKE TABLE OF pict_line.
CALL FUNCTION 'WS_UPLOAD'
EXPORTING
filename = file
filetype = 'BIN'
TABLES
data_tab = pict_tab.
EXPORT pict_tab = pict_tab TO DATABASE abtree(pi) ID id.
Method fill_tree
Listing 8 shows the instance method fill_tree of class screen_init. To create a tree-like
structure within the tree control, you must call method add_nodes and pass an internal
table of special structure and contents to that method. The addition TYPE TABLE OF in
the declaration of node_table in Listing 8 shows that the line type of that internal table is
abdemonode, defined in the ABAP Dictionary. You can create such types by copying the
global template structure mtreesnode.
The internal table contains one line for each node of the tree. Each node must have a
unique key node_key. The columns relatkey and relatship describe the relations between
the nodes. You can also add description texts, change the standard icons, and so on. In
our example, we create a node table from the contents of database table spfli. The
database table spfli is the well-known table of flight connections from SAP's training and
demonstration flight model.
We select all data from spfli into the sorted internal table spfli_tab and process this
table in a loop. There, we fill the internal table node_table with lines that represent a tree
structure of two hierarchy levels. The attributes of the nodes are set by assigning flags to
some columns of node_table. Note that we replace the standard folder icon with an
airplane icon for the subnodes by assigning the internal code "@AV@" to the image
columns.
METHOD fill_tree.
DATA: node_table TYPE TABLE OF abdemonode,
node TYPE abdemonode,
spfli_wa TYPE spfli,
spfli_tab TYPE SORTED TABLE OF spfli
WITH UNIQUE KEY carrid connid.
SELECT carrid connid
FROM spfli
INTO CORRESPONDING FIELDS OF TABLE spfli_tab.
node-hidden = ' '.
node-disabled = ' '.
node-isfolder = 'X'.
node-expander = ' '.
LOOP AT spfli_tab INTO spfli_wa.
AT NEW carrid.
node-node_key = spfli_wa-carrid.
CLEAR node-relatkey.
CLEAR node-relatship.
node-text = spfli_wa-carrid.
node-n_image = ' '.
node-exp_image = ' '.
APPEND node TO node_table.
ENDAT.
AT NEW connid.
CONCATENATE spfli_wa-carrid spfli_wa-connid
INTO node-node_key.
node-relatkey = spfli_wa-carrid.
node-relatship =
cl_gui_simple_tree=>relat_last_child.
node-text = spfli_wa-connid.
node-n_image = '@AV@'.
node-exp_image = '@AV@'.
ENDAT.
APPEND node TO node_table.
ENDLOOP.
CALL METHOD tree->add_nodes
EXPORTING table_structure_name = 'ABDEMONODE'
node_table = node_table.
ENDMETHOD.
METHOD constructor.
CREATE OBJECT: html_viewer EXPORTING parent = container,
list_viewer EXPORTING i_parent = container.
ENDMETHOD.
Method handle_node_double_click
Listing 10 shows the instance method handle_node_double_click of class
screen_handler. This method is declared and registered as an event handler for the
event node_double_click of our tree control object. Therefore, each time a user doubleclicks on a node of the tree, it triggers that method. The method imports the event's
EXPORTING parameter node_key, which contains the key of the selected node.
Depending on the key's contents, the event handler either calls method fill_html or
fill_list. It also sets the visibility of the two application controls referenced by html_viewer
and list_viewer. Remember that both controls are linked to the right column of our vertical
splitter control. The event handler steers their alternative display in that container.
As stated above, method calls to the frontend are buffered in an automation queue.
But since there is no automatic synchronization point after handling a system event on
the backend, we must force the system to process the preceding method calls. To do
this, we must call the static method cl_gui_cfw=>flush. Note that we do not have any
regular PAI processing in our program except when the user leaves the screen. The
communication between frontend and backend is handled by the CFW. The frontend-tobackend communication is triggered by events, and the backend-to-frontend
communication is triggered by flushes.
METHOD handle_node_double_click.
DATA: carrid TYPE spfli-carrid,
connid TYPE spfli-connid.
carrid = node_key(2).
connid = node_key+2(4).
IF connid IS INITIAL.
CALL METHOD: fill_html EXPORTING
html_viewer->set_visible EXPORTING visible = 'X',
list_viewer->set_visible EXPORTING visible = ' '.
ELSE.
CALL METHOD: fill_list EXPORTING
connid = connid,
list_viewer->set_visible EXPORTING visible
html_viewer->set_visible EXPORTING visible
ENDIF.
CALL METHOD cl_gui_cfw=>flush.
ENDMETHOD.
Listing
10
carrid = carrid,
carrid = carrid
= 'X',
= ' '.
Method fill_html
Listing 11 shows the instance method fill_html of class screen_handler. When a user
selects a node, this method reads the value of column URL from the database table
SCARR according to the node selected. The selected URL is simply passed to the
method show_url of our HTML control, which displays it in its area on the screen.
Beginning with Release 4.6C, the new column URL of the carrier database table
SCARR will contain the addresses of the carrier's Web sites. To add, change, or update
the addresses, you can provide your own data and change the program accordingly.
METHOD fill_html.
DATA url TYPE scarr-url.
SELECT SINGLE url
FROM
scarr
INTO
url
WHERE carrid = carrid.
CALL METHOD html_viewer->show_url EXPORTING url = url.
ENDMETHOD.
Method fill_list
Listing 12 shows the instance method fill_list of class screen_handler. This method
gives you an impression of how to work with the new SAP List Viewer, which will replace
classical ABAP list processing. As with the tree control, you have to prepare data in an
internal table and pass this table to a method of the List Viewer object. In our case, we
read detail information from database table sflight into an internal table flight_tab when
the user has double-clicked a flight connection in the tree. In addition to the actual data,
we prepare and pass additional information - such as the list's title and some of its
attributes - to the method. Figure 7 shows the screen after selecting a flight connection
in the tree.
METHOD fill_list.
DATA: flight_tab TYPE TABLE OF demofli,
BEGIN OF flight_title,
carrname TYPE scarr-carrname,
cityfrom TYPE spfli-cityfrom,
cityto TYPE spfli-cityto,
END OF flight_title,
list_layout TYPE lvc_s_layo.
SELECT SINGLE c~carrname p~cityfrom p~cityto
INTO
CORRESPONDING FIELDS OF flight_title
FROM
( scarr AS c
INNER JOIN spfli AS p ON
c~carrid = p~carrid )
WHERE
p~carrid = carrid AND
p~connid = connid.
SELECT fldate seatsmax seatsocc
INTO
CORRESPONDING FIELDS OF TABLE
flight_tab
FROM
sflight
WHERE
carrid = carrid AND connid = connid
ORDER BY fldate.
CONCATENATE flight_title-carrname
connid
flight_title-cityfrom
flight_title-cityto
INTO list_layout-grid_title
SEPARATED BY space.
list_layout-smalltitle = 'X'.
list_layout-cwidth_opt = 'X'.
list_layout-no_toolbar = 'X'.
CALL METHOD list_viewer->set_table_for_first_display
EXPORTING i_structure_name = 'DEMOFLI'
is_layout = list_layout
CHANGING
it_outtab = flight_tab.
ENDMETHOD.
Listing
12
Conclusion
In this article, we showed you the principal concepts of programming screens with the
control framework using a detailed demonstration program. This procedure can be
condensed into the following steps:
1.
Create container control objects and link them to areas at the screen.
2.
Create application control objects and link them to the container controls.
3.
Provide data for the controls in data objects, which, in most cases, are
events - in great detail. We simply wanted to provide an overview for the ABAP Objects
programmer who wants to use global classes of the CFW and who defines his or her own
local classes to handle the control objects on the screen.
Using this article as a starting point, you can learn more about the features of the
control framework by referring to the documentation of the control classes or to their
definition in the class builder of the ABAP Workbench. With that information, you can
then continue to explore the CFW on your own. For example, you might create classes
that react when a user clicks on a picture. Or you could look further into tree controls, so
that when a user selects a node in a tree control, you could introduce context menus for
those nodes. Users would then find these when they right-click on a particular node.
Finally, there is much to discover about the ALV, which we covered only briefly. ALV is the
best choice for presenting tabular data on the screen. It replaces classical table controls
as well as classical lists, and it provides a complete environment for interactive reporting.
If you wish to design and improve GUI features that benefit your users, the
opportunities are there, and the development process for these interfaces is more
streamlined than ever before. Whichever features you choose to take advantage of, this
article can be your springboard to creating an interface that is an effective tool for your
end users.
Horst Keller is information developer in the SAP ABAP & GUI Group. He documents the
ABAP language with an emphasis on ABAP Objects. He also develops and teaches
classes on ABAP programming and gives workshops at technical education conferences.
You can reach him at horst.keller@sap.com.
Gisbert Loff is product manager in the SAP ABAP & GUI Group, where he handles all
GUI-related issues. He has given various lecture presentations and workshops at
technical education conferences and SAPPHIREs. You can reach him
atgisbert.loff@sap.com.