miércoles, julio 25, 2007

DYNPRO - Textos

La primera idea que nos viene a la cabeza cuando queremos guardar texto es crear un campo de char255 en una tabla Z. Pero no se mantendría el formato (negrita, cursiva, salta de línea). Para resolver este problema se pueden utilizar los textos de sap.

Los diferentes id de textos se guardan en la tabla TTXID.

1) Configurar Texto nuevo Tx. SE75.

Si vamos a la sm30 hay un botón donde para buscar pasos en el Customizing que trabajen con esta tabla. Debemos buscar una descripción parecida a 'Definir ID de texto'. Aquí crearemos nuestros objetos y clases nuevas. Previamente hay que buscar un tipo de objeto al que queremos añadir los nuevos ID's.

2) Inicializar variables
IF NOT cc_org IS INITIAL.
CALL METHOD cc_org->free.
ENDIF.

IF NOT txtedit_nota IS INITIAL.
CALL METHOD txtedit_nota->free.
ENDIF.

3) Recuperar Texto.

Programa Control =>
DATA: cc_nota TYPE REF TO cl_gui_custom_container.
DATA: txtedit_nota TYPE REF TO cl_gui_textedit.
************************************************************************************
PBO
************************************************************************************
* Crear CUSTOM-CONTROL
IF cc_nota IS INITIAL.

CREATE OBJECT cc_nota
EXPORTING
container_name = 'CC_TEXTO' "Nombre del custom control del Dynpro
EXCEPTIONS
cntl_error = 1
cntl_system_error = 2
create_error = 3
lifetime_error = 4
lifetime_dynpro_dynpro_link = 5
others = 6.

ENDIF.

* Asignarle edición de texto
IF txtedit_nota IS INITIAL.

CREATE OBJECT txtedit_nota
EXPORTING
wordwrap_mode = 2
wordwrap_position = 72
parent = cc_nota
EXCEPTIONS
error_cntl_create = 1
error_cntl_init = 2
error_cntl_link = 3
error_dp_create = 4
gui_type_not_supported = 5
others = 6.

* Quitar botones
CALL METHOD txtedit_nota->set_toolbar_mode.
CALL METHOD txtedit_nota->set_statusbar_mode.
PERFORM llenar_texto.
ENDIF.

* Visualizar/Modificar
IF action EQ 'DISP'.
CALL METHOD txtedit_nota->set_readonly_mode
EXPORTING
readonly_mode = '1'.
ELSE.
CALL METHOD txtedit_nota->set_readonly_mode
EXPORTING
readonly_mode = '0'.
ENDIF.

****************************************************************************
Rutina=> PERFORM llenar_texto.
****************************************************************************
DATA: w_header TYPE thead,
t_tline TYPE TABLE OF tline,
w_tline TYPE tline,
t_texto TYPE TABLE OF char255.

w_header-tdobject = 'FORMMAT'.
w_header-tdname = zwf_mat_alta_dg-formnumber.
w_header-tdspras = sy-langu.

CALL FUNCTION 'READ_TEXT'
EXPORTING
id = w_header-tdid "Tipo de texto
language = w_header-tdspras "Idioma
name = w_header-tdname "Id del texto
object = w_header-tdobject "Nombre del objeto
TABLES
lines = t_tline
EXCEPTIONS
id = 1
language = 2
name = 3
not_found = 4
object = 5
reference_check = 6
wrong_access_to_archive = 7
OTHERS = 8.

*Copiar texto con formato a sólo texto
LOOP AT t_tline INTO w_tline.
APPEND w_tline-tdline TO t_texto.
ENDLOOP.

* Guardar texto en objeto
CALL METHOD txtedit_nota->set_text_as_r3table
EXPORTING
table = t_texto
EXCEPTIONS
error_dp = 1
error_dp_create = 2
OTHERS = 3.

3) Guardar texto.
DATA: w_header TYPE thead,
t_tline TYPE TABLE OF tline,
w_tline TYPE tline,
t_texto TYPE TABLE OF char255,
w_texto TYPE char255.

CLEAR w_header.
w_header-tdobject = 'FORMMAT'.
w_header-tdname = zwf_mat_alta_dg-formnumber.
w_header-tdspras = sy-langu.
w_header-tdid = 'DG'. " texto datos generales

* Eliminar texto anterior
CALL FUNCTION 'DELETE_TEXT'
EXPORTING
id = w_header-tdid
language = w_header-tdspras
name = w_header-tdname
object = w_header-tdobject
EXCEPTIONS
not_found = 1
OTHERS = 2.

* Recuperar texto de la pantalla
CALL METHOD txtedit_nota->get_text_as_r3table
IMPORTING
table = t_texto.

LOOP AT t_texto INTO w_texto.
w_tline-tdformat = '*'.
w_tline-tdline = w_texto.
APPEND w_tline TO t_tline .
ENDLOOP.

* Guardar texto nuevo
CALL FUNCTION 'SAVE_TEXT'
EXPORTING
header = w_header
TABLES
lines = t_tline
EXCEPTIONS
id = 1
language = 2
name = 3
object = 4
OTHERS = 5.

CALL FUNCTION 'COMMIT_TEXT'.

* Para deshabilitar el scroll horitzontal al realizar el CREATE OBJECT informar         wordwrap_mode              = cl_gui_textedit=>wordwrap_off