Tuesday, February 19, 2008

SDN Blog Browser


Publiqué este blog SDN Blog Browser en el SDN y ha tenido bastante acogida, así que como no podía ser de otra forma...Lo posteo aquí también -;)

Basicamente, tenemos una carpeta llamada SDN_Blogs en donde tenermos carpetas con los nombre de los bloggers del SDN...Y dentro de esas carpetas tenemos...Blog! Que más ibamos a tener -:P

Simplemente, leemos las carpetas, creamos un ALV Tree para mostrar a los autores y sus blogs y al presionar en cada blog, podemos visualizarlo en un control HTML.




*&----------------------------------------------------------------*
*& Report ZBLOG_BROWSER *
*&----------------------------------------------------------------*
REPORT ZBLOG_BROWSER.

*&----------------------------------------------------------------*
*& CLASS LCL_EVENT_RECEIVER DEFINITION *
*&----------------------------------------------------------------*
CLASS cl_tree_event_receiver DEFINITION.
PUBLIC SECTION.
METHODS: handle_double_click
FOR EVENT node_double_click OF cl_gui_alv_tree
IMPORTING node_key.
ENDCLASS.

*&----------------------------------------------------------------*
*& CLASS lcl_event_receiver IMPLEMENTATION *
*&----------------------------------------------------------------*
CLASS cl_tree_event_receiver IMPLEMENTATION.
METHOD handle_double_click.
CHECK NOT node_key IS INITIAL.
PERFORM cargar_browser USING node_key.
ENDMETHOD.
ENDCLASS.

*&----------------------------------------------------------------*
*& TYPES *
*&----------------------------------------------------------------*
TYPES: BEGIN OF ty_authors,
name(255) TYPE c,
END OF ty_authors.

TYPES: BEGIN OF ty_weblogs,
name(30) TYPE c,
blog(255) TYPE c,
END OF ty_weblogs.

*&----------------------------------------------------------------*
*& DATA *
*&----------------------------------------------------------------*
DATA: tree_event_receiver TYPE REF TO cl_tree_event_receiver,
lt_events TYPE cntl_simple_events,
l_event TYPE cntl_simple_event,
edurl(2048),
directory(100) TYPE c,
author(100) TYPE c.

DATA: file_table TYPE STANDARD TABLE OF sdokpath,
dir_table TYPE STANDARD TABLE OF sdokpath,
t_authors TYPE STANDARD TABLE OF ty_authors,
t_weblogs TYPE STANDARD TABLE OF ty_weblogs,
t_tree TYPE STANDARD TABLE OF ty_weblogs with header line.

DATA: html_control TYPE REF TO cl_gui_html_viewer,
g_alv_tree TYPE REF TO cl_gui_alv_tree,
g_custom_tree_container TYPE REF TO cl_gui_custom_container,
g_custom_html_container TYPE REF TO cl_gui_custom_container,
gs_fieldcat TYPE lvc_s_fcat,
gt_fieldcat_tree TYPE lvc_t_fcat,
l_hierarchy_header TYPE treev_hhdr,
it_node_key TYPE lvc_t_nkey.

FIELD-SYMBOLS: <fs_file_table> LIKE LINE OF file_table,
<fs_dir_table> LIKE LINE OF dir_table,
<fs_authors> LIKE LINE OF t_authors,
<fs_blogs> LIKE LINE OF t_weblogs.

*&----------------------------------------------------------------*
*& START-OF-SELECTION *
*&----------------------------------------------------------------*
START-OF-SELECTION.
PERFORM cargar_datos.
PERFORM init_tree.

CALL SCREEN 0100.

*&----------------------------------------------------------------*
*& Form CARGAR_DATOS *
*&----------------------------------------------------------------*
FORM cargar_datos.

directory = 'C:\SDN_Blogs\'.

CALL FUNCTION 'TMP_GUI_DIRECTORY_LIST_FILES'
EXPORTING
directory = directory
filter = '*.*'
TABLES
file_table = file_table
dir_table = dir_table
EXCEPTIONS
cntl_error = 1
OTHERS = 2.

IF sy-subrc EQ 0.
LOOP AT dir_table ASSIGNING <fs_dir_table>.
append initial line to t_authors
assigning <fs_authors>.
MOVE <fs_dir_table>-pathname TO <fs_authors>-name.
ENDLOOP.

IF NOT t_authors[] IS INITIAL.
LOOP AT t_authors ASSIGNING <fs_authors>.
CONCATENATE directory <fs_authors>-name '\'
INTO author.
CALL FUNCTION 'TMP_GUI_DIRECTORY_LIST_FILES'
EXPORTING
directory = author
filter = '*.htm'
TABLES
file_table = file_table
dir_table = dir_table
EXCEPTIONS
cntl_error = 1
OTHERS = 2.
IF sy-subrc EQ 0.
LOOP AT file_table ASSIGNING <fs_file_table>.
append initial line to t_weblogs
assigning <fs_blogs>.
MOVE <fs_authors>-name TO <fs_blogs>-name.
MOVE <fs_file_table>-pathname TO <fs_blogs>-blog.
ENDLOOP.
ENDIF.
ENDLOOP.
ENDIF.
ENDIF.

ENDFORM.

*&----------------------------------------------------------------*
*& FORM init_tree *
*&----------------------------------------------------------------*
FORM init_tree.

DATA: l_tree_container_name(30) TYPE c,
l_html_container_name(30) TYPE c.

l_tree_container_name = 'CUSTOM_TREE'.
l_html_container_name = 'CUSTOM_BROWSER'.

CREATE OBJECT tree_event_receiver.

CREATE OBJECT g_custom_tree_container
EXPORTING
container_name = l_tree_container_name
EXCEPTIONS
cntl_error = 1
cntl_system_error = 2
create_error = 3
lifetime_error = 4
lifetime_dynpro_dynpro_link = 5.

CREATE OBJECT g_custom_html_container
EXPORTING
container_name = l_html_container_name
EXCEPTIONS
cntl_error = 1
cntl_system_error = 2
create_error = 3
lifetime_error = 4
lifetime_dynpro_dynpro_link = 5.

CREATE OBJECT g_alv_tree
EXPORTING
parent = g_custom_tree_container
node_selection_mode =
cl_gui_column_tree=>node_sel_mode_single
item_selection = ''
no_html_header = 'X'
no_toolbar = ''
EXCEPTIONS
cntl_error = 1
cntl_system_error = 2
create_error = 3
lifetime_error = 4
illegal_node_selection_mode = 5
failed = 6
illegal_column_name = 7.

CREATE OBJECT html_control
EXPORTING
parent = g_custom_html_container.

PERFORM fill_catalog_tree.
PERFORM build_hierarchy_header CHANGING l_hierarchy_header.

CALL METHOD g_alv_tree->set_table_for_first_display
EXPORTING
is_hierarchy_header = l_hierarchy_header
CHANGING
it_outtab = t_tree[]
it_fieldcatalog = gt_fieldcat_tree.

PERFORM create_hierarchy.

CLEAR l_event.
l_event-eventid = cl_gui_column_tree=>eventid_node_double_click.
l_event-appl_event = 'X'.
APPEND l_event TO lt_events.
l_event-eventid = cl_gui_column_tree=>eventid_expand_no_children.
APPEND l_event TO lt_events.
CLEAR l_event.

CALL METHOD g_alv_tree->set_registered_events
EXPORTING
events = lt_events
EXCEPTIONS
cntl_error = 1
cntl_system_error = 2
illegal_event_combination = 3.

SET HANDLER tree_event_receiver->handle_double_click FOR g_alv_tree.

CALL METHOD g_alv_tree->frontend_update.

ENDFORM.

*&----------------------------------------------------------------*
*& Form FILL_CATALOG_TREE *
*&----------------------------------------------------------------*
FORM fill_catalog_tree.

DATA: gs_fieldcat TYPE lvc_s_fcat.

CLEAR gs_fieldcat.
gs_fieldcat-col_pos = 1.
gs_fieldcat-fieldname = 'NAME'.
gs_fieldcat-scrtext_s = 'Autor'.
gs_fieldcat-tabname = 'T_WEBLOGS'.
gs_fieldcat-outputlen = '1'.
APPEND gs_fieldcat TO gt_fieldcat_tree.

CLEAR gs_fieldcat.
gs_fieldcat-col_pos = 2.
gs_fieldcat-fieldname = 'BLOG'.
gs_fieldcat-scrtext_s = 'Blog'.
gs_fieldcat-tabname = 'T_WEBLOGS'.
gs_fieldcat-outputlen = '40'.
APPEND gs_fieldcat TO gt_fieldcat_tree.

ENDFORM.

*&----------------------------------------------------------------*
*& FORM build_hierarchy_header *
*&----------------------------------------------------------------*
FORM build_hierarchy_header CHANGING
p_hierarchy_header TYPE treev_hhdr.

CLEAR p_hierarchy_header.
p_hierarchy_header-heading = 'Autor'.
p_hierarchy_header-width = 30.
p_hierarchy_header-width_pix = ' '.

ENDFORM. "BUILD_HIERARCHY_HEADER

*&----------------------------------------------------------------*
*& FORM create_hierarchy *
*&----------------------------------------------------------------*
FORM create_hierarchy.

DATA: l_root_key TYPE lvc_nkey,
l_next_key TYPE lvc_nkey,
l_last_key TYPE lvc_nkey,
header TYPE string,
w_menge_text(13) TYPE c.

CLEAR l_root_key.

LOOP AT t_authors ASSIGNING <fs_authors>.
READ TABLE t_weblogs WITH KEY name = <fs_authors>-name
ASSIGNING <fs_blogs>.
IF sy-subrc EQ 0.
header = <fs_blogs>-name.
ENDIF.
CLEAR l_root_key.
CLEAR l_next_key.
PERFORM add_node USING header l_root_key
CHANGING l_next_key.
LOOP AT t_weblogs ASSIGNING <fs_blogs>
WHERE name EQ <fs_authors>-name.

MOVE-CORRESPONDING <fs_blogs> TO t_tree.

PERFORM add_leaf USING t_tree l_next_key
CHANGING l_last_key.
ENDLOOP.
ENDLOOP.

ENDFORM.

*&----------------------------------------------------------------*
*& FORM ADD_NODE *
*&----------------------------------------------------------------*
FORM add_node USING l_name l_root_key
CHANGING l_next_key.

DATA: l_node_text TYPE lvc_value,
ls_tree TYPE ty_weblogs.

l_node_text = l_name.
CALL METHOD g_alv_tree->add_node
EXPORTING
i_relat_node_key = l_root_key
i_relationship = cl_gui_column_tree=>relat_last_child
i_node_text = l_node_text
is_outtab_line = ls_tree
IMPORTING
e_new_node_key = l_next_key.

ENDFORM. "ADD_NODE

*&----------------------------------------------------------------*
*& FORM ADD_LEAF *
*&----------------------------------------------------------------*
FORM add_leaf USING l_tree TYPE ty_weblogs
l_next_key
CHANGING l_last_key.

CALL METHOD g_alv_tree->add_node
EXPORTING
i_relat_node_key = l_next_key
i_relationship = cl_gui_column_tree=>relat_last_child
is_outtab_line = t_tree
IMPORTING
e_new_node_key = l_last_key.

ENDFORM. "ADD_LEAF

*---------------------------------------------------------------------*
* FORM cargar_browser *
*---------------------------------------------------------------------*
FORM cargar_browser USING l_index.

READ TABLE t_tree INDEX l_index.

CONCATENATE 'FILE:' directory t_tree-name '\' t_tree-blog
INTO edurl.

CALL METHOD html_control->show_url
EXPORTING
url = edurl
EXCEPTIONS
cnht_error_parameter = 1
OTHERS = 2.

ENDFORM.

*---------------------------------------------------------------------*
*& Module STATUS_0100 OUTPUT *
*---------------------------------------------------------------------*
MODULE status_0100 OUTPUT.

SET PF-STATUS 'BLOGS'.
SET TITLEBAR 'TITLE'.

ENDMODULE. " STATUS_0100 OUTPUT

*&--------------------------------------------------------------------*
*& Module USER_COMMAND_0100 INPUT *
*&--------------------------------------------------------------------*
MODULE user_command_0100 INPUT.

IF sy-ucomm EQ 'BACK'.
LEAVE PROGRAM.
ENDIF.

ENDMODULE. " USER_COMMAND_0100 INPUT


Espero que les guste -:D

Saludos,

Blag.

1 comment:

Iceman said...

me toco verlo en los Blogs de SDN, y tambien como les agrado el programa... muy bueno =P