Vous êtes sur la page 1sur 7

*&====================================================================*

* Z_TEXT_EDIT_CONTROL_TO_DB *
*=====================================================================*
* This small program is to demonstrate a rudimentary way to read and *
* save entries from the CL_GUI_TEXTEDIT control to a text-object. *
* The requirement was a simple possibility of text entry with no text *
* formatting (except line breaks) which could be displayed in an *
* embedded dynpro. *
* *
* I found many examples of the EDIT_TEXT functions and how to use the *
* CL_GUI_TEXTEDIT class but non in combination. *
*---------------------------------------------------------------------*
* AUTHOR: Harry Fumey *
* DATE: 29.07.2009 *
* *
*=====================================================================*
* COMMENTS: *
* Create a dynpro 0100 and three push buttons named "PB_EXIT" *
* "PB_READ" and "PB_SAVE" as well as a custom container named *
* "CONTAINER". *
* The push buttons have function code "EXIT", "SAVE" and "READ". *
* Create your own text objects and id’s using SE75 to play around *
*PROGRAM name: *
REPORT z_text_edit_control_to_db.

CONSTANTS length TYPE i VALUE 132.

DATA x_head LIKE thead. " Header from DB


DATA xnote TYPE TABLE OF tline. " Line from DB
DATA wa_xnote LIKE tline. " Work area
DATA g_subrc TYPE subrc.
DATA is_modified TYPE i.

DATA: ok_code LIKE sy-ucomm.


DATA: g_init TYPE char1.
* Create ref for custom container
DATA: ccontainer TYPE REF TO cl_gui_custom_container.
* Create ref for text edit control
DATA: editor TYPE REF TO cl_gui_textedit.

**********************************************************************
* Transport table for Control to internal table
**********************************************************************
TYPES:
BEGIN OF ty_ttable,
line(length) TYPE c,
END OF ty_ttable.
DATA:
wa_ttable TYPE ty_ttable,
i_ttable TYPE TABLE OF ty_ttable.
SELECTION-SCREEN BEGIN OF BLOCK t1 WITH FRAME.

PARAMETERS:
p_object LIKE stxh-tdobject DEFAULT 'ZADM',
p_name LIKE stxh-tdname,
p_id LIKE stxh-tdid DEFAULT 'ZAD1',
p_spras LIKE stxh-tdspras DEFAULT sy-langu.

SELECTION-SCREEN END OF BLOCK t1.

START-OF-SELECTION.

PERFORM read_text_from_db.

*&---------------------------------------------------------------------*
*& Form READ_TEXT_FROM_DB
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM read_text_from_db.

* Read the text object from DB into data table


CALL FUNCTION 'READ_TEXT'
EXPORTING
client = sy-mandt
id = p_id
language = p_spras
name = p_name
object = p_object
IMPORTING
header = x_head
TABLES
lines = xnote
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.
* Loop over data table and fill internal table for the text edit control
* The field for formatting is omitted.
LOOP AT xnote INTO wa_xnote.
wa_ttable-line = wa_xnote-tdline.
APPEND wa_ttable TO i_ttable.
CLEAR wa_ttable.
ENDLOOP.
g_init = 'X'.
ENDIF.
SET SCREEN '100'.
ENDFORM. " read_text_from_db

*---------------------------------------------------------------------*
* MODULE USER_COMMAND_0100 INPUT *
*---------------------------------------------------------------------*
MODULE user_command_0100 INPUT.
CASE ok_code.
WHEN 'READ'.
PERFORM read_text_into_control.
WHEN 'SAVE'.
PERFORM save_text_to_db.
WHEN 'EXIT'.
LEAVE TO SCREEN 0.
WHEN OTHERS.

ENDCASE.
ENDMODULE. " USER_COMMAND_0100 INPUT
*&---------------------------------------------------------------------*
*& Module STATUS_0100 OUTPUT
*&---------------------------------------------------------------------*
MODULE status_0100 OUTPUT.
* Check if control is present
IF editor IS INITIAL.

* Make object for the custom container


CREATE OBJECT ccontainer
EXPORTING
container_name = 'CONTAINER'
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.
* If the container is initial we need to create the control too
CREATE OBJECT editor
EXPORTING
wordwrap_mode =
cl_gui_textedit=>wordwrap_at_fixed_position
wordwrap_position = length
wordwrap_to_linebreak_mode = cl_gui_textedit=>true
parent = ccontainer
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.

*Move internal table into control if text-object was found.


IF g_init = 'X'.
PERFORM read_text_into_control.
ELSE.
PERFORM read_text_from_db.
PERFORM read_text_into_control.
ENDIF.
ENDMODULE. " STATUS_0100 OUTPUT
*&---------------------------------------------------------------------*
*& Form READ_TEXT_INTO_CONTROL
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM read_text_into_control.

* Transport the internal table to the control


CALL METHOD editor->set_text_as_r3table
EXPORTING
table = i_ttable
EXCEPTIONS
error_dp = 1
error_dp_create = 2
OTHERS = 3.
IF sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
* WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.

* Transport to frontend using flush method

CALL METHOD cl_gui_cfw=>flush


EXCEPTIONS
cntl_system_error = 1
cntl_error = 2
OTHERS = 3.
IF sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
* WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.

ENDFORM. " read_text_into_control


*&---------------------------------------------------------------------*
*& Form SAVE_TEXT_TO_DB
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM save_text_to_db .

*Read data from control (front end) to internal table and change flag "is_m
odified"

CALL METHOD editor->get_text_as_r3table


EXPORTING
only_when_modified = cl_gui_textedit=>false
IMPORTING
table = i_ttable
is_modified = is_modified
EXCEPTIONS
error_dp = 1
error_cntl_call_method = 2
error_dp_create = 3
potential_data_loss = 4
OTHERS = 5.
IF sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
* WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.

*Check if the text object is already in the database


CALL FUNCTION 'READ_TEXT'
EXPORTING
* CLIENT = SY-MANDT
id = p_id
language = p_spras
name = p_name
object = p_object
IMPORTING
header = x_head
TABLES
lines = xnote
EXCEPTIONS
id = 1
language = 2
name = 3
not_found = 4
object = 5
reference_check = 6
wrong_access_to_archive = 7
OTHERS = 8
.

g_subrc = sy-subrc.
*Empty the data table of old entries
IF g_subrc = 0.
CLEAR xnote.
REFRESH xnote.
ENDIF.

*In case the entry does not exist initialize a new one
*Note that table XNOTE will be emptied

IF g_subrc <> 0.
CALL FUNCTION 'INIT_TEXT'
EXPORTING
id = p_id
language = p_spras
name = p_name
object = p_object
IMPORTING
header = x_head
TABLES
lines = xnote
EXCEPTIONS
id = 1
language = 2
name = 3
object = 4
OTHERS = 5.
IF sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
* WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.
ENDIF.

*Fill the data table with new values from internal table coming from the te
xt edit control
LOOP AT i_ttable INTO wa_ttable.
wa_xnote-tdline = wa_ttable-line.
APPEND wa_xnote TO xnote.
CLEAR wa_xnote.
ENDLOOP.
CLEAR i_ttable.
FREE i_ttable.

*If the entry already existed on the data base the new text will be saved t
o the data base.
*Otherwise the data will be saved using the newly created x_head entry into
the data base.

CALL FUNCTION 'SAVE_TEXT'


EXPORTING
* CLIENT = SY-MANDT
header = x_head
* INSERT = ' '
savemode_direct = 'X'
* OWNER_SPECIFIED = ' '
* LOCAL_CAT = ' '
* IMPORTING
* FUNCTION =
* NEWHEADER =
TABLES
lines = xnote
EXCEPTIONS
id = 1
language = 2
name = 3
object = 4
OTHERS = 5
.
IF sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
* WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.
CLEAR g_init.

ENDFORM. " SAVE_TEXT_TO_DB

Vous aimerez peut-être aussi