SAP R/3 форум ABAP консультантов
Russian ABAP Developer's Club

Home - FAQ - Search - Memberlist - Usergroups - Profile - Log in to check your private messages - Register - Log in - English
Blogs - Weblogs News

Column Tree and Docking Template



 
Post new topic   Reply to topic    Russian ABAP Developer's Club Forum Index -> ALV Grid / ALV Tree / ALV List
View previous topic :: View next topic  
Author Message
admin
Администратор
Администратор



Joined: 01 Sep 2007
Posts: 1640

PostPosted: Wed Mar 04, 2009 5:05 pm    Post subject: Column Tree and Docking Template Reply with quote

Version 2.0
Author WATTO
Source from: http: /www.watto.org/program/abap/download/Z_TREE_DOCK_TEMPLATE.abap

Description: A template program for building a report that has a docked window on the left which contains a tree with columns. Includes the following functionality...

Features:
- Create a docking window
- Create a tree with columns
- Adding tree nodes with images
- Adding tree columns as text or checkboxes
- Handling of double-clicks and checkbox clicks of the tree

Code:
REPORT z_tree_dock_template.

*** CLASSES ***
CLASS:
  cl_tree_events DEFINITION DEFERRED,
  cl_gui_cfw     DEFINITION LOAD.


*** INTERNAL TABLES ***
DATA:
  v_nodes TYPE treev_node,
  i_nodes TYPE treev_ntab,
  i_items TYPE STANDARD TABLE OF mtreeitm,
  v_items TYPE mtreeitm.


*** OBJECTS ***
DATA:
  o_tree_listener TYPE REF TO cl_tree_events,
  o_tree          TYPE REF TO cl_gui_column_tree.


*** DATA ***
DATA:
  v_ok_code TYPE sy-ucomm.



************************************************************************
***
*** CLASSES
***
************************************************************************



*----------------------------------------------------------------------
*       CLASS CL_TREE_EVENTS DEFINITION
*----------------------------------------------------------------------
* [+] Class for handling events on o_tree
*----------------------------------------------------------------------
CLASS cl_tree_events DEFINITION.

  PUBLIC SECTION.
    METHODS:

      handle_node_double_click
        FOR EVENT node_double_click
        OF cl_gui_column_tree
        IMPORTING node_key,

      handle_checkbox_change
        FOR EVENT checkbox_change
        OF cl_gui_column_tree
        IMPORTING node_key item_name checked.

ENDCLASS.                    "cl_tree_events DEFINITION


*----------------------------------------------------------------------
*       CLASS CL_TREE_EVENTS IMPLEMENTATION
*----------------------------------------------------------------------
* [+] Class for handling events on o_tree
*----------------------------------------------------------------------
CLASS cl_tree_events IMPLEMENTATION.


*---------------------------------------------------------------------*
*       METHOD HANDLE_NODE_DOUBLE_CLICK
*---------------------------------------------------------------------*
* [+] Processes a double-click on a node
*---------------------------------------------------------------------*
  METHOD  handle_node_double_click.
    WRITE: / node_key.
  ENDMETHOD.                    "handle_node_double_click


*---------------------------------------------------------------------*
*       METHOD HANDLE_CHECKBOX_CHANGE
*---------------------------------------------------------------------*
* [+] Processes a double-click on a node
*---------------------------------------------------------------------*
  METHOD  handle_checkbox_change.
    WRITE: / node_key.
    WRITE: / item_name.
    WRITE: / checked.
  ENDMETHOD.                    "handle_checkbox_change


ENDCLASS.                    "cl_tree_events IMPLEMENTATION



************************************************************************
***
*** MAIN PROCESSING
***
************************************************************************



*&---------------------------------------------------------------------*
*&      START_OF_SELECTION
*&---------------------------------------------------------------------*
START-OF-SELECTION.
* Create the event listener object
  CREATE OBJECT o_tree_listener.

  SET SCREEN 100.



************************************************************************
***
*** DOCKING AND TREE OBJECT METHODS
***
************************************************************************



*----------------------------------------------------------------------
*       FORM BUILD_DOCKED_WINDOW
*----------------------------------------------------------------------
* [+] Constructs and loads the data into the docked window...
*     [+] Builds the dock
*     [+] Builds the tree
*     [+] Loads the tree nodes
*----------------------------------------------------------------------
FORM build_docked_window.

* Local variables
  DATA: li_events TYPE cntl_simple_events, "events
        lv_events TYPE cntl_simple_event,  "events
        lo_dock   TYPE REF TO cl_gui_docking_container, "docked window
        lv_repid  TYPE syrepid, "report ID
        lv_tree_header TYPE treev_hhdr.


* Setting variables
  lv_repid = sy-repid.


* Create the dock window object
  CREATE OBJECT lo_dock
    EXPORTING
      repid     = lv_repid
      dynnr     = sy-dynnr
      side      = lo_dock->dock_at_left
      extension = 400.


* header of the tree
  lv_tree_header-heading = 'My Tree :)'.
  lv_tree_header-width = 30.


* Create the tree object
  CREATE OBJECT o_tree
    EXPORTING
      parent                = lo_dock
      node_selection_mode   = cl_gui_column_tree=>node_sel_mode_single
      item_selection        = 'X'
      hierarchy_column_name = 'TreeColumn'    " the column that contains the node names
      hierarchy_header      = lv_tree_header.


* Define the columns (excluding the column with the node names 'TreeColumn')
*                               code   size  name
  PERFORM define_column USING 'Column1' 5  'Check'.
  PERFORM define_column USING 'Column2' 15 'My Column 2'.


* Define the events to listen for
  lv_events-eventid = cl_gui_column_tree=>eventid_node_double_click.
  lv_events-appl_event = 'X'. " process PAI if event occurs
  APPEND lv_events TO li_events.

  lv_events-eventid = cl_gui_column_tree=>eventid_checkbox_change.
  lv_events-appl_event = 'X'.
  APPEND lv_events TO li_events.

  CALL METHOD o_tree->set_registered_events
    EXPORTING
      events = li_events.


* Set the event listeners
  SET HANDLER o_tree_listener->handle_node_double_click FOR o_tree.
  SET HANDLER o_tree_listener->handle_checkbox_change   FOR o_tree.


* Build the tree
  PERFORM build_tree.


* expand the node with key 'Root'
  CALL METHOD o_tree->expand_node
    EXPORTING
      node_key = 'Root'.

ENDFORM.                    "build_docked_window



*----------------------------------------------------------------------
*       FORM BUILD_TREE
*----------------------------------------------------------------------
* [+] Loads the tree nodes
*----------------------------------------------------------------------
FORM build_tree.

* NODES ARE INSERTED IN THE ORDER SHOWN.
* DO NOT DEFINE A CHILD BEFORE ITS PARENT NODE!

  CLEAR:
    i_nodes,
    v_nodes,
    i_items,
    v_items.

  REFRESH:
    i_nodes,
    i_items.


** CREATE ROOT NODE ***

  CLEAR v_nodes.
  v_nodes-node_key = 'Root'.
  v_nodes-isfolder = 'X'.
  APPEND v_nodes TO i_nodes.

  PERFORM add_text_column USING 'Root' 'TreeColumn' 'Root Node'.


** CREATE BRANCHES **

*                      folder  code    parent    image      name
  PERFORM add_node USING 'X' 'Branch1' 'Root'    ''     'Branch Node 1'.
  PERFORM add_node USING ' ' 'Leaf1'   'Branch1' ''     'Leaf Node 1'.
  PERFORM add_node USING ' ' 'Leaf2'   'Branch1' '@10@' 'Leaf Node 2'.

  PERFORM add_node USING 'X' 'Branch2' 'Root'    '' 'Branch Node 2'.
  PERFORM add_node USING ' ' 'Leaf3'   'Branch2' '' 'Leaf Node 3'.


** CREATE COLUMNS **

*                                    node    column       value
  PERFORM add_checkbox_column USING 'Leaf1' 'Column1'.

  PERFORM add_checkbox_column USING 'Leaf2' 'Column1'.
  PERFORM add_text_column     USING 'Leaf2' 'Column2' 'Hello World!'.

  PERFORM add_checkbox_column USING 'Leaf3' 'Column1'.
  PERFORM add_text_column     USING 'Leaf3' 'Column2' 'Guess What?'.



* Adds the nodes to the tree.
  CALL METHOD o_tree->add_nodes_and_items
    EXPORTING
      node_table                = i_nodes
      item_table                = i_items
      item_table_structure_name = 'MTREEITM'.


ENDFORM.                    "build_tree



*----------------------------------------------------------------------
*       FORM DEFINE_COLUMN
*----------------------------------------------------------------------
* [+] Defines a column
*----------------------------------------------------------------------
FORM define_column
  USING
    p_code TYPE c
    p_size TYPE i
    p_name TYPE c.

  CALL METHOD o_tree->add_column
    EXPORTING
      name        = p_code
      width       = p_size
      header_text = p_name.

ENDFORM.                    "define_column



*----------------------------------------------------------------------
*       FORM ADD_CHECKBOX_COLUMN
*----------------------------------------------------------------------
* [+] Adds a checkbox column to a node
*----------------------------------------------------------------------
FORM add_checkbox_column
  USING
    p_node_name   TYPE c
    p_column_name TYPE c.

  CLEAR v_items.
  v_items-node_key  = p_node_name.
  v_items-item_name = p_column_name.
  v_items-class     = cl_gui_column_tree=>item_class_checkbox.
  v_items-editable  = 'X'.
  APPEND v_items TO i_items.

ENDFORM.                    "add_checkbox_column



*----------------------------------------------------------------------
*       FORM ADD_TEXT_COLUMN
*----------------------------------------------------------------------
* [+] Adds a text column to a node
*----------------------------------------------------------------------
FORM add_text_column
  USING
    p_node_name   TYPE c
    p_column_name TYPE c
    p_value       TYPE c.

  CLEAR v_items.
  v_items-node_key  = p_node_name.
  v_items-item_name = p_column_name.
  v_items-text      = p_value.
  v_items-class     = cl_gui_column_tree=>item_class_text.
  APPEND v_items TO i_items.

ENDFORM.                    "add_text_column



*----------------------------------------------------------------------
*       FORM ADD_NODE
*----------------------------------------------------------------------
* [+] Adds a node to the tree
* [+] Adds a single column for the node
*----------------------------------------------------------------------
FORM add_node
  USING
    p_folder TYPE c
    p_code   TYPE c
    p_parent TYPE c
    p_image  TYPE c
    p_name   TYPE c.

  CLEAR:
    v_nodes,
    v_items.

* add the node
  v_nodes-node_key  = p_code.
  v_nodes-relatkey  = p_parent.
  v_nodes-relatship = cl_gui_column_tree=>relat_last_child.
  v_nodes-isfolder  = p_folder.
  v_nodes-n_image   = p_image.

  APPEND v_nodes TO i_nodes.

* add the column that displays the node name
  v_items-node_key  = p_code.
  v_items-item_name = 'TreeColumn'.
  v_items-text      = p_name.
  v_items-class     = cl_gui_column_tree=>item_class_text.
  APPEND v_items TO i_items.


ENDFORM.                    "add_node



************************************************************************
***
*** MODULES
***
************************************************************************



*----------------------------------------------------------------------
*       MODULE LOAD_DOCK
*----------------------------------------------------------------------
* [+] Loads the docked window and tree
*----------------------------------------------------------------------
MODULE load_dock OUTPUT.
  SET PF-STATUS 'MAIN'.

  IF o_tree IS INITIAL.
*   build the tree/dock
    PERFORM build_docked_window.
  ENDIF.

ENDMODULE.                    "load_dock OUTPUT



*----------------------------------------------------------------------
*       MODULE PROCESS_EVENTS_0100
*----------------------------------------------------------------------
* [+] Processes the events from the dock/tree
*----------------------------------------------------------------------
MODULE process_events_0100 INPUT.

  DATA: lv_tree_return_code TYPE i.

* If the tree made an event that affects PAI,
* then dont process the event here.
  CALL METHOD cl_gui_cfw=>dispatch
    IMPORTING
      return_code = lv_tree_return_code.

  IF lv_tree_return_code <> cl_gui_cfw=>rc_noevent.
    CLEAR v_ok_code.
    EXIT.
  ENDIF.

* Otherwise, the user didn't click the tree, they clicked something
* else (such as a button), so process as normal.
  CASE v_ok_code.
    WHEN 'BACK'.
      CLEAR o_tree.
      LEAVE TO SCREEN 0.
  ENDCASE.

  CLEAR v_ok_code.
ENDMODULE.                    "process_events_0100 INPUT
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Russian ABAP Developer's Club Forum Index -> ALV Grid / ALV Tree / ALV List All times are GMT + 4 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


All product names are trademarks of their respective companies. SAPNET.RU websites are in no way affiliated with SAP AG.
SAP, SAP R/3, R/3 software, mySAP, ABAP, BAPI, xApps, SAP NetWeaver and any other are registered trademarks of SAP AG.
Every effort is made to ensure content integrity. Use information on this site at your own risk.