02/08/2010

Report to Creates a Tree


*---------------------------------------------------------------------
* This report creates a tree with the favorite web sites as option
* nodes for viewing these pages in a browser.
* Don´t forget to define a PF-STATUS 'STATUS-100' which contains
* a BACK button, a CANC button and 5 Application toolbar buttons:
* EXPAND, COLLAPSE, NEW URL, PREVIOUS and NEXT site.
*---------------------------------------------------------------------
REPORT ztest NO STANDARD PAGE HEADING.

CLASS lcl_application DEFINITION DEFERRED.
CLASS cl_gui_cfw DEFINITION LOAD.

DATA: node TYPE treev_node,
node_table TYPE treev_ntab,
item_table LIKE STANDARD TABLE OF mtreeitm WITH DEFAULT KEY,
item TYPE mtreeitm,
okcode LIKE sy-ucomm,
url(255).

DATA: g_application TYPE REF TO lcl_application,
my_tree TYPE REF TO cl_gui_column_tree,
container_1 TYPE REF TO cl_gui_container,
container_2 TYPE REF TO cl_gui_container,
browser TYPE REF TO cl_gui_html_viewer,
splitter TYPE REF TO cl_gui_splitter_container.

DATA: event TYPE cntl_simple_event,
events TYPE cntl_simple_events,
hierarchy_header TYPE treev_hhdr,
my_node TYPE tv_nodekey.


INCLUDE .

* Necessary Class definition for object Tree
CLASS lcl_application DEFINITION.
PUBLIC SECTION.

METHODS:
handle_node_double_click
FOR EVENT node_double_click
OF cl_gui_column_tree
IMPORTING node_key,

handle_item_double_click
FOR EVENT item_double_click
OF cl_gui_column_tree
IMPORTING node_key.
ENDCLASS.



* Necessary Class implementation for object Tree
CLASS lcl_application IMPLEMENTATION.

METHOD handle_node_double_click.
my_node = node_key.
ENDMETHOD.

METHOD handle_item_double_click.
my_node = node_key.
ENDMETHOD.

ENDCLASS.


START-OF-SELECTION.
CALL SCREEN 0100.


*-----------------------------------------------------------------------
* Create the tree
*-----------------------------------------------------------------------
FORM crea_arbol.

* Split the container in 2 parts
CREATE OBJECT splitter
EXPORTING
parent = cl_gui_container=>screen0
rows = 1
columns = 2.

CALL METHOD splitter->set_border
EXPORTING
border = cl_gui_cfw=>true.

CALL METHOD splitter->set_column_mode
EXPORTING
mode = splitter->mode_absolute.

CALL METHOD splitter->set_column_width
EXPORTING
id = 1
width = 250.


* Assign a name to every column
CALL METHOD splitter->get_container
EXPORTING
row = 1
column = 1
RECEIVING
container = container_1.

CALL METHOD splitter->get_container
EXPORTING
row = 1
column = 2
RECEIVING
container = container_2.


* Container´s header
hierarchy_header-heading = 'My favorite web sites'.
hierarchy_header-width = 40.


* Create the tree
CREATE OBJECT my_tree
EXPORTING
parent = container_1
node_selection_mode = cl_gui_column_tree=>node_sel_mode_single
item_selection = 'X'
hierarchy_column_name = 'Column1'
hierarchy_header = hierarchy_header
EXCEPTIONS
cntl_system_error = 1
create_error = 2
failed = 3
illegal_node_selection_mode = 4
illegal_column_name = 5
lifetime_error = 6.


* Event double click
event-eventid = cl_gui_column_tree=>eventid_node_double_click.
event-appl_event = 'X'.
APPEND event TO events.

* item double click
event-eventid = cl_gui_column_tree=>eventid_item_double_click.
event-appl_event = 'X'.
APPEND event TO events.

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


* assign event handlers in the application class to each desired event
CREATE OBJECT g_application.
SET HANDLER g_application->handle_node_double_click FOR my_tree.
SET HANDLER g_application->handle_item_double_click FOR my_tree.


* Create the tree´s nodes
PERFORM create_nodes.

* Add nodes to the tree
CALL METHOD my_tree->add_nodes_and_items
EXPORTING
node_table = node_table
item_table = item_table
item_table_structure_name = 'MTREEITM'
EXCEPTIONS
failed = 1
cntl_system_error = 3
error_in_tables = 4
dp_error = 5
table_structure_name_not_found = 6.


* Expand Root node when loading the program
CALL METHOD my_tree->expand_root_nodes.

ENDFORM.


*----------------------------------------------------------------------
* Creation of the browser
*----------------------------------------------------------------------
FORM create_browser.

CREATE OBJECT browser
EXPORTING
parent = container_2.

CALL METHOD browser->show_url
EXPORTING
url = 'http://www.sap.com'
EXCEPTIONS
cntl_error = 1.

ENDFORM.


*-----------------------------------------------------------------------
* Let´s create the nodes. Check the logical sequence on concatenation
* between the upper node and the lower node.
* Warning: upper and lower case affect when defining the node´s name
*-----------------------------------------------------------------------
FORM create_nodes.

* This is Root node
node-node_key = 'Root'.
CLEAR node-relatkey.
CLEAR node-relatship.
node-hidden = ' '.
node-disabled = ' '.
node-isfolder = 'X'.
node-n_image = icon_protocol.
node-exp_image = icon_protocol.
APPEND node TO node_table.

CLEAR item.
item-node_key = 'Root'.
item-item_name = 'Column1'.
item-class = cl_gui_column_tree=>item_class_text.
item-text = 'Options tree'.
APPEND item TO item_table.


* This is the first folder (Software)
node-node_key = 'NODE1'.
node-relatkey = 'Root'.
node-relatship = cl_gui_column_tree=>relat_last_child.
node-n_image = icon_closed_folder.
node-exp_image = icon_open_folder.
node-expander = 'X'.
APPEND node TO node_table.

item-node_key = 'NODE1'.
item-class = cl_gui_column_tree=>item_class_text.
item-text = 'Software'.
APPEND item TO item_table.


* Option 1 for Folder software:
node-node_key = 'OPT1'.
node-relatkey = 'NODE1'.
node-relatship = cl_gui_column_tree=>relat_last_child.
node-n_image = icon_execute_object.
node-expander = 'X'.
APPEND node TO node_table.

item-node_key = 'OPT1'.
item-class = cl_gui_column_tree=>item_class_text.
item-text = 'Computer Universe'.
APPEND item TO item_table.

* Option 2 for Folder software:
node-node_key = 'OPT2'.
node-relatkey = 'NODE1'.
node-relatship = cl_gui_column_tree=>relat_last_child.
node-n_image = icon_execute_object.
node-expander = 'X'.
APPEND node TO node_table.

item-node_key = 'OPT2'.
item-class = cl_gui_column_tree=>item_class_text.
item-text = 'Downloads - CNET'.
APPEND item TO item_table.

* Option 3 for Folder software:
node-node_key = 'OPT3'.
node-relatkey = 'NODE1'.
node-relatship = cl_gui_column_tree=>relat_last_child.
node-n_image = icon_execute_object.
node-expander = 'X'.
APPEND node TO node_table.

item-node_key = 'OPT3'.
item-class = cl_gui_column_tree=>item_class_text.
item-text = 'JavaScript source'.
APPEND item TO item_table.


* This is the second folder (travels)
node-node_key = 'NODE2'.
node-relatkey = 'Root'.
node-relatship = cl_gui_column_tree=>relat_last_child.
node-hidden = ' '.
node-disabled = ' '.
node-n_image = icon_closed_folder.
node-exp_image = icon_open_folder.
APPEND node TO node_table.

item-node_key = 'NODE2'.
item-class = cl_gui_column_tree=>item_class_text.
item-text = 'Travels'.
APPEND item TO item_table.

* Option 1 for Folder travels
node-node_key = 'OPT4'.
node-relatkey = 'NODE2'.
node-relatship = cl_gui_column_tree=>relat_last_child.
node-n_image = icon_execute_object.
node-expander = 'X'.
APPEND node TO node_table.

item-node_key = 'OPT4'.
item-class = cl_gui_column_tree=>item_class_text.
item-text = 'Air Berlin'.
APPEND item TO item_table.

* Option 2 for Folder travels
node-node_key = 'OPT5'.
node-relatkey = 'NODE2'.
node-relatship = cl_gui_column_tree=>relat_last_child.
node-n_image = icon_execute_object.
node-expander = 'X'.
APPEND node TO node_table.

item-node_key = 'OPT5'.
item-class = cl_gui_column_tree=>item_class_text.
item-text = 'Condor'.
APPEND item TO item_table.

* This is the third Folder (SAP help)
node-node_key = 'NODE3'.
node-relatkey = 'Root'.
node-relatship = cl_gui_column_tree=>relat_last_child.
node-hidden = ' '.
node-disabled = ' '.
node-n_image = icon_closed_folder.
node-exp_image = icon_open_folder.
APPEND node TO node_table.

item-node_key = 'NODE3'.
item-class = cl_gui_column_tree=>item_class_text.
item-text = 'SAP help'.
APPEND item TO item_table.

* Option 1 for Folder SAP help
node-node_key = 'OPT6'.
node-relatkey = 'NODE3'.
node-relatship = cl_gui_column_tree=>relat_last_child.
node-n_image = icon_execute_object.
node-expander = 'X'.
APPEND node TO node_table.

item-node_key = 'OPT6'.
item-class = cl_gui_column_tree=>item_class_text.
item-text = 'ABAP-Fans'.
APPEND item TO item_table.

ENDFORM.


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

SET PF-STATUS 'STATUS-100'.
SET TITLEBAR '100'.

IF my_tree IS INITIAL.
PERFORM crea_arbol.
PERFORM create_browser.
ENDIF.

ENDMODULE.


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

CASE okcode.
WHEN 'CANC' OR 'BACK'.
LEAVE PROGRAM.

WHEN 'EXPAND'.
PERFORM expand_nodes.

WHEN 'COLLAPSE'.
PERFORM collapse_nodes.

WHEN 'NEWURL'.
CALL FUNCTION 'POPUP_TO_FILL_COMMAND_LINE'
EXPORTING
popuptitle = 'New web site'
text1 = 'Enter the URL Address'
IMPORTING
command_line = url.
IF sy-ucomm = 'WEIT'.
PERFORM display_url.
ENDIF.

WHEN 'PREVIOUS'.
CALL METHOD browser->go_back.

WHEN 'NEXT'.
CALL METHOD browser->go_forward.
ENDCASE.


CLEAR: okcode, url, my_node.

* What action must take place when double-clicking on a node ??
CALL METHOD cl_gui_cfw=>dispatch.

CASE my_node.
WHEN 'OPT1'.
url = 'http://www.computeruniverse.net/imaging.asp'.
PERFORM display_url.

WHEN 'OPT2'.
url = 'http://download.cnet.com/'.
PERFORM display_url.

WHEN 'OPT3'.
url = 'http://javascript.internet.com/'.
PERFORM display_url.

WHEN 'OPT4'.
url = 'http://www.airberlin.com/'.
PERFORM display_url.

WHEN 'OPT5'.
url = 'http://www.thomascook-airlines.de/'.
PERFORM display_url.

WHEN 'OPT6'.
url = 'http://www.abap-fans.de/index3.htm'.
PERFORM display_url.
ENDCASE.

ENDMODULE.


*----------------------------------------------------------------------
* Expand all nodes
*----------------------------------------------------------------------
FORM expand_nodes.

CALL METHOD my_tree->expand_node
EXPORTING
node_key = 'OPT3'.

CALL METHOD my_tree->expand_node
EXPORTING
node_key = 'OPT5'.

CALL METHOD my_tree->expand_node
EXPORTING
node_key = 'OPT6'.

ENDFORM.


*----------------------------------------------------------------------
* Collapse all nodes (except the root node)
*----------------------------------------------------------------------
FORM collapse_nodes.

CALL METHOD my_tree->collapse_all_nodes.
CALL METHOD my_tree->expand_root_nodes.

ENDFORM.



*----------------------------------------------------------------------
* Display the URL of the selected node
*----------------------------------------------------------------------
FORM display_url.

CALL METHOD browser->show_url
EXPORTING
url = url
EXCEPTIONS
cntl_error = 1.

ENDFORM.

Nenhum comentário:

Postar um comentário