Test

本文介绍了一个使用 ABAP 编写的 GUI 文本编辑器示例程序,包括如何加载文本、保护选定行、读取和保存文本等功能。通过实例展示了事件处理、双击事件响应等操作。

*&---------------------------------------------------------------------*
*& Report  Z_BARRY_041
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
REPORT z_barry_041 .

TABLES:stxh.
CONSTANTS: line_length TYPE i VALUE 254.
DATA: ok_code LIKE sy-ucomm.
DATA: custom_container TYPE REF TO cl_gui_custom_container,
      editor TYPE REF TO cl_gui_textedit,
      repid LIKE sy-repid.
DATA: tline LIKE tline OCCURS 0 WITH HEADER LINE.
TYPES: BEGIN OF it_txline,
         line(line_length) TYPE c,
       END OF it_txline.
DATA: it_tline TYPE TABLE OF it_txline,
      it_text LIKE j_1ivttxid OCCURS 0 WITH HEADER LINE ,
      BEGIN OF it_thead OCCURS 0 .
        INCLUDE STRUCTURE thead.
DATA:change,
END OF it_thead .

DATA:tmp_tdname LIKE stxh-tdname ,
     tmp_tdid   LIKE stxh-tdid   ,
     tmp_tdobject LIKE stxh-tdobject .

tmp_tdname = '0030000004'.
tmp_tdid   = '0001'.
tmp_tdobject = 'VBBK' .

**********************************************************************
* 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,
  wa_texttable TYPE t_texttable,
  g_loaded(1) TYPE c.

**********************************************************************
* Data for the protection example
**********************************************************************
DATA:
  from_idx TYPE i,
  to_idx   TYPE i,
  index    TYPE i.

**********************************************************************
* Implementing a second Scratch TextEdit control
**********************************************************************
DATA:
  scratch            TYPE REF TO cl_gui_textedit,
  custom_container2  TYPE REF TO cl_gui_custom_container.


**********************************************************************
* Implementing 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.                    "lcl_event_handler DEFINITION

*---------------------------------------------------------------------*
*       CLASS lcl_event_handler IMPLEMENTATION
*---------------------------------------------------------------------*
*       ........                                                      *
*---------------------------------------------------------------------*
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.

* 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.                    "catch_dblclick
ENDCLASS.                    "lcl_event_handler IMPLEMENTATION

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'.
      PERFORM readtext.
    WHEN 'IMP'.
      PERFORM load_texts.
    WHEN 'PROTECT'.
      PERFORM protect.
    WHEN 'SAVE'.
      PERFORM save_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 obejct for custom container
    CREATE OBJECT custom_container
      EXPORTING
        container_name              = 'TEXTEDITCON'
      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.

*------------------------------------------------------
* 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.
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
*&---------------------------------------------------------------------*
*&      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 lines selected
  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 immediately
  CALL METHOD cl_gui_cfw=>flush.
  IF sy-subrc > 0.
* Errormessage: Error in flush
  ENDIF.
ENDFORM.                    " protect

*&---------------------------------------------------------------------*
*&      Form  readtext
*&---------------------------------------------------------------------*
FORM readtext .

  CALL FUNCTION 'READ_TEXT'
    EXPORTING
      client                  = sy-mandt
      id                      = tmp_tdid
      language                = sy-langu
      name                    = tmp_tdname
      object                  = tmp_tdobject
    TABLES
      lines                   = tline
    EXCEPTIONS
      id                      = 1
      language                = 2
      name                    = 3
      not_found               = 4
      object                  = 5
      reference_check         = 6
      wrong_access_to_archive = 7
      OTHERS                  = 8.
  IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
            WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ENDIF.
  LOOP AT tline.
    APPEND tline-tdline TO it_tline .
  ENDLOOP.

  CALL METHOD editor->set_text_as_r3table
    EXPORTING
      table = it_tline.
  CALL METHOD cl_gui_cfw=>flush.
  g_loaded = 'X'.
  REFRESH it_tline.
  CLEAR it_tline.
ENDFORM.                    " readtext
*&---------------------------------------------------------------------*
*&      Form  save_texts
*&---------------------------------------------------------------------*
FORM save_texts .
  CALL METHOD editor->get_text_as_r3table
    IMPORTING
      table = it_tline.
  CHECK NOT it_tline[] IS INITIAL.

ENDFORM.                    " save_texts

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值