02/08/2010

Report to Create an Options Tree


*---------------------------------------------------------------------
* This report creates an options tree, similar to SAP´s Main Menu.
* Don´t forget to define a PF-STATUS 'STATUS_100' which contains
* a BACK button, a CANC button and 2 Application toolbar buttons:
* EXPAN & COLLA
*---------------------------------------------------------------------
REPORT ztest.


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).

CLASS lcl_application DEFINITION DEFERRED.
CLASS cl_gui_cfw DEFINITION LOAD.

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,
picture TYPE REF TO cl_gui_picture,
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 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,

handle_header_click
FOR EVENT header_click
OF cl_gui_column_tree
IMPORTING header_name.
ENDCLASS.



* Necessary Class implementation for 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.

METHOD handle_header_click.
my_node = header_name.
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 = 330.


* 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 = 'This can be a subtitle'.
hierarchy_header-width = 50.


* 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.

* header click
event-eventid = cl_gui_column_tree=>eventid_header_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.
SET HANDLER g_application->handle_header_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 image on the right side of the screen. There are
* 5 different options:
* 1- Centers the image in the container
* 2- Normal mode
* 3- Image stretches in the container
* 4- If the size of the container changes, so will change the image
* 5- Centers and stretches the image to adapt to the container
*----------------------------------------------------------------------
FORM create_image.

CREATE OBJECT picture
EXPORTING
parent = container_2
EXCEPTIONS
error = 1.

CLEAR url.
PERFORM load_pic_from_db CHANGING url.

CALL METHOD picture->load_picture_from_url
EXPORTING
url = url.

CALL METHOD picture->set_display_mode
EXPORTING
display_mode = cl_gui_picture=>display_mode_normal_center.
* display_mode = cl_gui_picture=>display_mode_normal.
* display_mode = cl_gui_picture=>display_mode_stretch.
* display_mode = cl_gui_picture=>display_mode_fit.
* display_mode = cl_gui_picture=>display_mode_fit_center.

ENDFORM.


*----------------------------------------------------------------------
* Where´s the location of the image that is going to be displayed
* on the right side of the screen ??
* You can choose pictures in transaction SMW0.
*----------------------------------------------------------------------
FORM load_pic_from_db CHANGING url.

DATA: query_table LIKE w3query OCCURS 1 WITH HEADER LINE,
html_table LIKE w3html OCCURS 1,
pic_data LIKE w3mime OCCURS 0,
return_code LIKE w3param-ret_code,
content_length LIKE w3param-cont_len,
content_type LIKE w3param-cont_type.


REFRESH query_table.
query_table-name = '_OBJECT_ID'.
query_table-value = 'CTS_LOGO.GIF'. "<-- change the name of the
APPEND query_table. "<-- image to display yours


CALL FUNCTION 'WWW_GET_MIME_OBJECT'
TABLES
query_string = query_table
html = html_table
mime = pic_data
CHANGING
return_code = return_code
content_type = content_type
content_length = content_length
EXCEPTIONS
object_not_found = 1
parameter_not_found = 2
OTHERS = 3.


CALL FUNCTION 'DP_CREATE_URL'
EXPORTING
type = 'image'
subtype = cndp_sap_tab_unknown
size = content_length
lifetime = cndp_lifetime_transaction
TABLES
data = pic_data
CHANGING
url = url
EXCEPTIONS
OTHERS = 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 node (Folder 1)
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 = 'Folder 1'.
APPEND item TO item_table.

* Option 1 for Folder 1
node-node_key = 'OPT1'.
node-relatkey = 'NODE1'.
node-relatship = cl_gui_column_tree=>relat_last_child.
node-n_image = icon_closed_folder. " close folder
node-exp_image = icon_open_folder. " open folder
node-expander = 'X'.
APPEND node TO node_table.

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

* Option 2 for Folder 1
node-node_key = 'OPT2'.
node-relatkey = 'OPT1'.
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 = 'Item 1'.
APPEND item TO item_table.


* This is the second node (Folder 2)
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 = 'Folder 2'.
APPEND item TO item_table.

* Option 1 for Folder 2
node-node_key = 'OPT3'.
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 = 'OPT3'.
item-class = cl_gui_column_tree=>item_class_text.
item-text = 'Item 2'.
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_image.
ENDIF.

ENDMODULE.


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

CASE okcode.
WHEN 'CANC' OR 'BACK'.
LEAVE PROGRAM.
WHEN 'EXPAN'.
PERFORM expander_nodos.
WHEN 'COLLA'.
PERFORM contraer_nodos.
ENDCASE.

CLEAR: okcode, my_node.

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

CASE my_node.
WHEN 'OPT2'.
CALL SCREEN 0200.
WHEN 'OPT3'.
CALL transaction 'SE11'.
WHEN 'HierarchyHea'.
* do nothing
ENDCASE.

ENDMODULE.


*----------------------------------------------------------------------
* Expand all nodes
*----------------------------------------------------------------------
FORM expander_nodos.

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

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

ENDFORM.


*----------------------------------------------------------------------
* Collapse all nodes
*----------------------------------------------------------------------
FORM contraer_nodos.

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

ENDFORM.



*-----------------------------------------------------------------------
* Module USER_COMMAND_0200 INPUT
*-----------------------------------------------------------------------
MODULE user_command_0200 INPUT.

LEAVE TO SCREEN 0100.

ENDMODULE.

*-----------------------------------------------------------------------
* Module STATUS_0200 OUTPUT
*-----------------------------------------------------------------------
MODULE status_0200 OUTPUT.

SET PF-STATUS 'STATUS-200'.
SET TITLEBAR '200'.

ENDMODULE.

Nenhum comentário:

Postar um comentário