Dynamic GUI STATUS & TITLE with ABAP code详解编程语言

When you’re used to create buttons in ALV Grid dynamically then you think why SAP doesn’t allow create dynamically buttons on 
GUI STATUS. You can change icon or text if you defined that function has dynamic text but you cannot create buttons at runtime and you always have to create 
GUI STATUS and 
GUI TITLE in SE41, which personally I don’t like, as in most small reports you have to create usually one to five buttons. 
 
So what I want to present to you today is a way to create buttons dynamically at program runtime without a need to create 
GUI STATUS and 
GUI TITLE for each program. In fact whole trick is to create firstly a 
GUI STATUS in empty program which will contain only functions with dynamic texts, and then to fill properly static class attribute.
 
So let’s begin with creating our program to keep 
GUI STATUS and 
GUI TITLE. I will call it ZAB_DYNAMIC_GUI_STATUS. In the source code you do not need to put anything beside report keyword.

“! Dummy program for keeping GUI STATUS and TITLE
“! Used by ZCA_AB_DYNAMIC_GUI
“! Do not delete it!!!
report zab_dynamic_gui_status.

Yes, that’s all from the code point of view in this program. As said it’s only to keep 
GUI STATUS and 
GUI TITLE
Lets create GUI STATUS firstly and call it 
DYNAMIC_STATUS. Items on application toolbar will have the same naming pattern Fxx, so F01, F02 etc. Each of this function code should have dynamic function text with pattern 
ZCA_AB_DYNAMIC_GUI=>BUTTONS-Fxx where Fxx you have to replace by current function code.
Additionally to application toolbar, fill also function keys for save, up, back, exit, print… etc so you can also use it in your programs.
 
Dynamic GUI STATUS & TITLE with ABAP code详解编程语言
Dynamic GUI STATUS & TITLE with ABAP code详解编程语言
 
Save & activate 
GUI STATUS.

 
Now create also 
GUI TITLE and call it 
DYNAMIC_TITLE. As a text put &1 &2 &3 &4 &5. Save & activate. 
Dynamic GUI STATUS & TITLE with ABAP code详解编程语言
 
That’s all for 
ZAB_DYNAMIC_GUI_STATUS program. Now it’s time to create class with static methods to be able to use created STATUS and TITLE everywhere we need.
 
Class is called 
ZCA_AB_DYNAMIC_GUI. Once created please make sure that constant program_name contains name of program which stores 
DYNAMIC_STATUS and 
DYNAMIC_TITLE
 
Definition:

class zca_ab_dynamic_gui definition
  public
  create public .

  public section.
    typesbegin of t_buttons,
             f01 type rsfunc_txt,
             f02 type rsfunc_txt,
             f03 type rsfunc_txt,
             f04 type rsfunc_txt,
             f05 type rsfunc_txt,
             f06 type rsfunc_txt,
             f07 type rsfunc_txt,
             f08 type rsfunc_txt,
             f09 type rsfunc_txt,
             f10 type rsfunc_txt,
             f11 type rsfunc_txt,
             f12 type rsfunc_txt,
             f13 type rsfunc_txt,
             f14 type rsfunc_txt,
             f15 type rsfunc_txt,
             f16 type rsfunc_txt,
             f17 type rsfunc_txt,
             f18 type rsfunc_txt,
             f19 type rsfunc_txt,
             f20 type rsfunc_txt,
             f21 type rsfunc_txt,
             f22 type rsfunc_txt,
             f23 type rsfunc_txt,
             f24 type rsfunc_txt,
             f25 type rsfunc_txt,
             f26 type rsfunc_txt,
             f27 type rsfunc_txt,
             f28 type rsfunc_txt,
             f29 type rsfunc_txt,
             f30 type rsfunc_txt,
             f31 type rsfunc_txt,
             f32 type rsfunc_txt,
             f33 type rsfunc_txt,
             f34 type rsfunc_txt,
             f35 type rsfunc_txt,
           end of t_buttons.
    typesbegin of t_allowed_but,
             function type sy-ucomm,
           end of t_allowed_but.
    typestt_excluded_but type standard table of syucomm.
    typestt_allowed_but type standard table of t_allowed_but.

    constantsb_save          type syucomm value ‘SAVE’,
               b_back           type syucomm value ‘BACK’,
               b_up            type syucomm value ‘UP’,
               b_exit          type syucomm value ‘EXIT’,
               b_print         type syucomm value ‘PRINT’,
               b_find          type syucomm value ‘FIND’,
               b_find_next     type syucomm value ‘FINDNEXT’,
               b_first_page    type syucomm value ‘PGHOME’,
               b_last_page     type syucomm value ‘PGEND’,
               b_previous_page type syucomm value ‘PGUP’,
               b_next_page     type syucomm value ‘PGDOWN’,
               b_01            type syucomm value ‘F01’,
               b_02            type syucomm value ‘F02’,
               b_03            type syucomm value ‘F03’,
               b_04            type syucomm value ‘F04’,
               b_05            type syucomm value ‘F05’,
               b_06            type syucomm value ‘F06’,
               b_07            type syucomm value ‘F07’,
               b_08            type syucomm value ‘F08’,
               b_09            type syucomm value ‘F09’,
               b_10            type syucomm value ‘F10’,
               b_11            type syucomm value ‘F11’,
               b_12            type syucomm value ‘F12’,
               b_13            type syucomm value ‘F13’,
               b_14            type syucomm value ‘F14’,
               b_15            type syucomm value ‘F15’,
               b_16            type syucomm value ‘F16’,
               b_17            type syucomm value ‘F17’,
               b_18            type syucomm value ‘F18’,
               b_19            type syucomm value ‘F19’,
               b_20            type syucomm value ‘F20’,
               b_21            type syucomm value ‘F21’,
               b_22            type syucomm value ‘F22’,
               b_23            type syucomm value ‘F23’,
               b_24            type syucomm value ‘F24’,
               b_25            type syucomm value ‘F25’,
               b_26            type syucomm value ‘F26’,
               b_27            type syucomm value ‘F27’,
               b_28            type syucomm value ‘F28’,
               b_29            type syucomm value ‘F29’,
               b_30            type syucomm value ‘F30’,
               b_31            type syucomm value ‘F31’,
               b_32            type syucomm value ‘F32’,
               b_33            type syucomm value ‘F33’,
               b_34            type syucomm value ‘F34’,
               b_35            type syucomm value ‘F35’,
               program_name    type progname value ‘ZAB_DYNAMIC_GUI_STATUS’.

    class-dataallowed_buttons type tt_allowed_but.
    class-databuttons type t_buttons.
    class-dataexcluded_buttons type tt_excluded_but.
    class-methodsclass_constructor.
    class-methodsadd_button importing  value(iv_button)  type syucomm
                                         value(iv_text)    type smp_dyntxttext optional
                                         value(iv_icon)    type smp_dyntxticon_id optional
                                         value(iv_qinfo)   type smp_dyntxtquickinfo optional
                                         value(iv_allowedtype abap_bool default abap_true
                              exceptions
                                         button_already_filled
                                         button_does_not_exists
                                         icon_and_text_empty.
    class-methodshide_button importing value(iv_buttontype syucomm.
    class-methodsshow_button importing value(iv_buttontype syucomm.
    class-methodsget_toolbar exporting e_toolbar type t_buttons.
    class-methodsadd_separator importing  value(iv_button)  type syucomm.
    class-methodsshow_title  importing value(iv_text1type string
                                         value(iv_text2type string optional
                                         value(iv_text3type string optional
                                         value(iv_text4type string optional
                                         value(iv_text5type string optional.
    class-methodsshow_gui_status.
  protected section.
  private section.
endclass.

 
Implementation:

class zca_ab_dynamic_gui implementation.
  method add_button.
    data button type smp_dyntxt.
    check iv_button is not initial.

    if iv_text is initial and iv_icon is initial.
      raise icon_and_text_empty.
      return.
    endif.

    buttonicon_id iv_icon.
    buttonicon_text iv_text.
    buttontext      iv_text.
    buttonquickinfo iv_qinfo.

    assign component iv_button of structure buttons to fieldsymbol(<bt>).
    if <bt> is assigned.
      if <bt> is initial.
        <bt> button.
        if iv_allowed eq abap_true.
          show_buttoniv_button iv_button ).
        endif.
      else.
        raise button_already_filled.
      endif.
    else.
      raise button_does_not_exists.
    endif.
  endmethod.

  method add_separator.
    add_button(
      exporting
        iv_button              iv_button
        iv_text                |{ cl_abap_char_utilities=>minchar }|
*        iv_icon                = iv_icon
*        iv_qinfo               = iv_qinfo
         iv_allowed             abap_true
      exceptions
        button_already_filled  1
        button_does_not_exists 2
        icon_and_text_empty    3
        others                 4
    ).
    if sysubrc <> 0.
*     message id sy-msgid type sy-msgty number sy-msgno
*                with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
    endif.
  endmethod.

  method class_constructor.
    excluded_buttons value #b_01 b_02 b_03 b_04 b_05 b_06 b_07 b_08 b_09 )
                                b_10 b_11 b_12 b_13 b_14 b_15 b_16 b_17 b_18 b_19 )
                                b_20 b_21 b_22 b_23 b_24 b_25 b_26 b_27 b_28 b_29 )
                                b_30 b_31 b_32 b_33 b_34 b_35 )
                                b_save b_find b_find_next b_first_page b_last_page b_next_page b_previous_page b_print ).
  endmethod.

  method get_toolbar.
    e_toolbar buttons.
  endmethod.

  method hide_button.
    check iv_button is not initial.
    if line_existsallowed_buttons[ function iv_button ] ).
      delete allowed_buttons where function iv_button.
      append iv_button to excluded_buttons.
    endif.
  endmethod.

  method show_button.
    check iv_button is not initial.
    if not line_existsallowed_buttons[ function iv_button ] ).
      data(allowedvalue t_allowed_butfunction iv_button ).
      append allowed to allowed_buttons.
      delete excluded_buttons where table_line eq iv_button.
    endif.
  endmethod.

  method show_gui_status.
    set pf-status ‘DYNAMIC_STATUS’ excluding excluded_buttons[] of program program_name.
  endmethod.

  method show_title.
    set titlebar ‘DYNAMIC_TITLE’ of program program_name with iv_text1 iv_text2 iv_text3 iv_text4 iv_text5.
  endmethod.
endclass.

Once the class is created in dictionary, you can now easily use it in any program to create dynamic STATUS and TITLE. All you need to do is to call in PBO of the screen methods 
show_gui_status and 
show_title. To add new button you have to use methods 
add_button and pass text or icon or both. As I used every possible item to put function code in the 
GUI STATUS then if you need separator then you can use method
 add_separator and pass it’s place. This method will add in fact a button without any text or description which will just looks like separator.  
 
If you want to allow disabled function keys you have to use method 
show_button and pass it’s name to it.
 
Example of use ( create screen 0100 which calls PBO and PAI modules ):

report zab_dynamic_gui_demo.

dataok_code type syucomm.

initialization.

“! Add button at position 01
  zca_ab_dynamic_gui=>add_button(
    exporting
      iv_button              zca_ab_dynamic_gui=>b_01
      iv_text                |Test|
      iv_icon                icon_delete
      iv_qinfo               |Delete|
      iv_allowed             abap_true
    exceptions
      button_already_filled  1
      button_does_not_exists 2
      icon_and_text_empty    3
      others                 4
  ).
  if sysubrc <> 0endif.

“! Add separator ( empty button )  position 02
  zca_ab_dynamic_gui=>add_separatoriv_button zca_ab_dynamic_gui=>b_02 ).

“! Add button at position 03
  zca_ab_dynamic_gui=>add_button(
    exporting
      iv_button              zca_ab_dynamic_gui=>b_03
      iv_text                |Test 2|
      iv_icon                icon_delete
*      iv_qinfo               = ‘Delete’
      iv_allowed             abap_true
    exceptions
      button_already_filled  1
      button_does_not_exists 2
      icon_and_text_empty    3
      others                 4
  ).
  if sysubrc <> 0endif.

“! Acitvate print button ( in default only BACK, UP , EXIT are active )
  zca_ab_dynamic_gui=>show_buttoniv_button zca_ab_dynamic_gui=>b_print ).

  call screen 0100.

module pbo_0100 output.
    zca_ab_dynamic_gui=>show_gui_status).
    zca_ab_dynamic_gui=>show_titleiv_text1 | Test Title | ).
endmodule.

module pai_0100 input.
  case ok_code.
    when zca_ab_dynamic_gui=>b_up or zca_ab_dynamic_gui=>b_back or zca_ab_dynamic_gui=>b_exit.
      leave program.
    when zca_ab_dynamic_gui=>b_01.
      “! Do some action here
    when zca_ab_dynamic_gui=>b_03.
      “! Do some action here
  endcase.
endmodule.

The output of this program is as follows.
Dynamic GUI STATUS & TITLE with ABAP code详解编程语言
 

原创文章,作者:Maggie-Hunter,如若转载,请注明出处:https://blog.ytso.com/tech/pnotes/20177.html

(0)
上一篇 2021年7月19日 23:06
下一篇 2021年7月19日 23:07

相关推荐

发表回复

登录后才能评论