Posted: Wed Sep 05, 2007 3:58 pm Post subject: Progress bar with estimate time to finish
Author: Emmanuele Prudenzano (Syn)
This sample code can give you the ability of display a progress bar on sapgui or job log with estimate time to finish.
Code:
* Author : Emmanuele Prudenzano
* Syn
* Object : Sample progress bar with sapgui
* Method : set_limit( <number of item to be shown> )
* set_message( <message> )
* show_message( ) ==> show message on status bar
* increment( \[<number of item processed>\] )
* get_estimate( ) ==> get seconds estimate to end
* get_percentage( ) ==> get percentage of work
* start_timer( ) ==> start the timer
* reset( ) ==> reset timer.
*---------------------------------------------------------------------*
* CLASS gui_update DEFINITION
*---------------------------------------------------------------------* CLASS gui_update DEFINITION.
PUBLIC SECTION.
METHODS: constructor IMPORTING value(i_limit) TYPE i OPTIONAL
value(i_msg) TYPE string OPTIONAL,
set_limit IMPORTING value(i_limit) TYPE i,
set_message IMPORTING value(i_msg) TYPE string,
show_message IMPORTING value(i_msg) TYPE string OPTIONAL,
increment IMPORTING value(i_step) TYPE i DEFAULT 1,
get_estimate_as_text RETURNING value(e_end_txt)
TYPE string,
get_estimate RETURNING value(e_end_sec) TYPE i,
get_percentage RETURNING value(e_perc) TYPE i,
start_timer,
reset.
PRIVATE SECTION.
DATA max TYPE i.
DATA cur TYPE i.
DATA step TYPE i.
DATA next TYPE i.
DATA start TYPE i.
DATA msg TYPE string.
DATA started.
ENDCLASS.CLASS gui_update IMPLEMENTATION.
METHOD constructor.
CALL METHOD reset.
IF i_limit IS SUPPLIED.
CALL METHOD set_limit( i_limit ).
ENDIF.
IF i_msg IS SUPPLIED.
msg = i_msg.
ELSE.
msg = '&perc% (&cur/&max). End in &end'.
ENDIF.
ENDMETHOD.
METHOD reset.
cur = 0.
step = 0.
next = 0.
start = 0.
max = 1.
started = space.
ENDMETHOD.
METHOD set_limit.
max = i_limit.
IF max < 1. max = 1. ENDIF.
step = max / 10.
IF step GT 100.
step = 100.
ELSEIF step LT 1.
step = 1.
ENDIF.
IF started EQ space.
CALL METHOD start_timer( ).
ENDIF.
ENDMETHOD.
METHOD set_message.
msg = i_msg.
ENDMETHOD.
METHOD show_message.
DATA: txt(100).
DATA: txt_msg(100).
DATA: e_txt TYPE string.
DATA: perc TYPE i.
DATA l TYPE i.
perc = me->get_percentage( ).
e_txt = me->get_estimate_as_text( ).
IF i_msg IS SUPPLIED.
txt_msg = i_msg.
ELSE.
txt_msg = msg.
ENDIF.
WRITE perc TO txt LEFT-JUSTIFIED DECIMALS 0 EXPONENT 0.
l = strlen( txt ).
REPLACE '&perc' WITH txt(l) INTO txt_msg.
WRITE cur TO txt LEFT-JUSTIFIED DECIMALS 0 EXPONENT 0.
l = strlen( txt ).
REPLACE '&cur' WITH txt(l) INTO txt_msg.
WRITE max TO txt LEFT-JUSTIFIED DECIMALS 0 EXPONENT 0.
l = strlen( txt ).
REPLACE '&max' WITH txt(l) INTO txt_msg.
l = strlen( e_txt ).
REPLACE '&end' WITH e_txt(l) INTO txt_msg.
IF sy-batch = 'X'.
MESSAGE i398(00) WITH txt_msg.
ELSE.
CALL FUNCTION 'SAPGUI_PROGRESS_INDICATOR'
EXPORTING
percentage = perc
text = txt_msg.
ENDIF.
ENDMETHOD.
METHOD increment.
ADD i_step TO cur.
IF next = 0 OR cur GT next.
next = next + step.
CALL METHOD show_message.
ENDIF.
ENDMETHOD.
METHOD get_percentage. " exporting value(e_perc) type p.
e_perc = cur * 100 / max.
ENDMETHOD.
METHOD get_estimate.
DATA: now TYPE i.
DATA: perc TYPE p.
DATA: end TYPE p.
GET RUN TIME FIELD now.
perc = me->get_percentage( ).
end = ( ( now - start ) * ( 100 - perc ) ) / perc.
IF end LT 0.
end = \-1.
ELSE.
end = ( end / 1000000 ). " end in seco.
end = floor( end ).
ENDIF.
e_end_sec = end.
ENDMETHOD.
METHOD get_estimate_as_text.
DATA um(3).
DATA: end TYPE i.
end = me->get_estimate( ).
IF end LT 0.
e_end_txt = '[Not Avaible]'.
ELSE.
um = 'sec'.
IF end > 60.
end = floor( end / 60 ).
um = 'min'.
IF end > 60.
end = floor( end / 60 ).
um = 'ore'.
ENDIF.
ENDIF.
DATA: txt(100) TYPE c.
WRITE end TO txt LEFT-JUSTIFIED DECIMALS 0 EXPONENT 0.
CONCATENATE txt um INTO e_end_txt SEPARATED BY space.
ENDIF.
ENDMETHOD.
METHOD start_timer.
GET RUN TIME FIELD start.
cur = 0.
next = 0.
started = 'X'.
ENDMETHOD.
ENDCLASS.
And here there are two sample of using:
Code:
FORM test0.
* Easy way
DATA: myguiupdate TYPE REF TO gui_update.
CREATE OBJECT myguiupdate EXPORTING i_limit = 10.
DO 10 TIMES.
... put your code here...
CALL METHOD myguiupdate->increment( ).
ENDDO.
ENDFORM.
Or more complicate possibility:
Code:
FORM test1.
DATA: myguiupdate TYPE REF TO gui_update.
CREATE OBJECT myguiupdate.
CALL METHOD myguiupdate->set_message(
'Reading data. &perc% (&cur/&max). End in &end' ). LOOP AT ...
.... you code here
CALL METHOD myguiupdate->increment( ).
ENDLOOP
CALL METHOD myguiupdate->reset( ).
CALL METHOD myguiupdate->set_message(
'Process Data. &perc% (&cur/&max). End in &end' ). LOOP AT ...
.... you code here
CALL METHOD myguiupdate->increment( ).
ENDLOOP
ENDFORM.
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You cannot attach files in this forum You can download files in this forum
All product names are trademarks of their respective companies. SAPNET.RU websites are in no way affiliated with SAP AG. SAP, SAP R/3, R/3 software, mySAP, ABAP, BAPI, xApps, SAP NetWeaver and any other are registered trademarks of SAP AG. Every effort is made to ensure content integrity. Use information on this site at your own risk.