• SAP Drag or drop tree


      1 *&---------------------------------------------------------------------*
      2 *& Report  RSDEMO_DRAG_DROP_EDIT_TREE                                  *
      3 *&                                                                     *
      4 *&---------------------------------------------------------------------*
      5 *&                                                                     *
      6 *&                                                                     *
      7 *&---------------------------------------------------------------------*
      8 
      9 REPORT  rsdemo_drag_drop_edit_tree    .
     10 DATA ok_code TYPE sy-ucomm.
     11 DATA node_itab_left LIKE node_str OCCURS 0.
     12 DATA node_itab_right LIKE node_str OCCURS 0.
     13 DATA node LIKE node_str.
     14 
     15 DATA container TYPE REF TO cl_gui_custom_container.
     16 DATA splitter TYPE REF TO cl_gui_easy_splitter_container.
     17 DATA right TYPE REF TO cl_gui_container.
     18 DATA left  TYPE REF TO cl_gui_container.
     19 
     20 DATA tree_right TYPE REF TO cl_gui_simple_tree.
     21 DATA tree_left TYPE REF TO cl_gui_simple_tree.
     22 
     23 DATA behaviour_left TYPE REF TO cl_dragdrop.
     24 DATA behaviour_right TYPE REF TO cl_dragdrop.
     25 
     26 DATA handle_tree_left TYPE i.
     27 DATA handle_tree_right TYPE i.
     28 
     29 *---------------------------------------------------------------------*
     30 *       CLASS lcl_treeobject DEFINITION
     31 *---------------------------------------------------------------------*
     32 *       Definition of Data Container                                  *
     33 *---------------------------------------------------------------------*
     34 CLASS lcl_drag_object DEFINITION.
     35   PUBLIC SECTION.
     36     DATA text TYPE STANDARD TABLE OF mtreesnode-text.
     37 ENDCLASS.
     38 
     39 
     40 *---------------------------------------------------------------------*
     41 *       CLASS dragdrop_receiver DEFINITION
     42 *---------------------------------------------------------------------*
     43 *       ........                                                      *
     44 *---------------------------------------------------------------------*
     45 CLASS lcl_dragdrop_receiver DEFINITION.
     46   PUBLIC SECTION.
     47     METHODS:
     48        flavor_select FOR EVENT on_drop_get_flavor OF cl_gui_simple_tree
     49                       IMPORTING node_key flavors drag_drop_object,
     50        left_drag FOR EVENT on_drag_multiple OF cl_gui_simple_tree
     51                       IMPORTING node_key_table drag_drop_object,
     52        right_drop FOR EVENT on_drop OF cl_gui_simple_tree
     53                       IMPORTING node_key drag_drop_object,
     54        drop_complete FOR EVENT on_drop_complete_multiple OF
     55                       cl_gui_simple_tree
     56                       IMPORTING node_key_table drag_drop_object.
     57 
     58 ENDCLASS.
     59 
     60 ************************************************************************
     61 
     62 
     63 ************************************************************************
     64 
     65 START-OF-SELECTION.
     66   CALL SCREEN 100.
     67 *&---------------------------------------------------------------------*
     68 *&      Module  START  OUTPUT
     69 *&---------------------------------------------------------------------*
     70 *       text
     71 *----------------------------------------------------------------------*
     72 MODULE start OUTPUT.
     73   SET PF-STATUS 'BASE'.
     74   IF container is initial.
     75     CREATE OBJECT container
     76         EXPORTING container_name = 'CONTAINER'.
     77     CREATE OBJECT splitter
     78         EXPORTING parent = container
     79                   orientation = 1.
     80     left = splitter->top_left_container.
     81     right = splitter->bottom_right_container.
     82     CREATE OBJECT tree_right
     83      EXPORTING parent = right
     84                node_selection_mode = tree_right->node_sel_mode_multiple.
     85     CREATE OBJECT tree_left
     86       EXPORTING parent = left
     87                 node_selection_mode = tree_left->node_sel_mode_multiple.
     88 
     89 * Definition of drag drop behaviour
     90     CREATE OBJECT behaviour_left.
     91     CALL METHOD behaviour_left->add
     92         EXPORTING
     93               flavor = 'Tree_move'
     94               dragsrc = 'X'
     95               droptarget = ' '
     96               effect = cl_dragdrop=>copy.
     97     CALL METHOD behaviour_left->add
     98          EXPORTING
     99                flavor = 'Tree_copy'
    100                dragsrc = 'X'
    101                droptarget = ' '
    102                effect = cl_dragdrop=>copy.
    103 
    104     CALL METHOD behaviour_left->get_handle
    105          IMPORTING handle = handle_tree_left.
    106 
    107 * Drag Drop behaviour of tree control nodes are defined in the node
    108 * structure
    109     PERFORM fill_tree CHANGING handle_tree_left node_itab_left.
    110     CALL METHOD tree_left->add_nodes
    111          EXPORTING node_table = node_itab_left
    112                    table_structure_name = 'NODE_STR'.
    113 
    114     CREATE OBJECT behaviour_right.
    115 
    116     CALL METHOD behaviour_right->add
    117            EXPORTING
    118                  flavor = 'Tree_copy'
    119                  dragsrc = ' '
    120                  droptarget = 'X'
    121                  effect = cl_dragdrop=>copy.
    122     CALL METHOD behaviour_right->add
    123            EXPORTING
    124                  flavor = 'Tree_move'
    125                  dragsrc = ' '
    126                  droptarget = 'X'
    127                  effect = cl_dragdrop=>copy.
    128     CALL METHOD behaviour_right->get_handle
    129          IMPORTING handle = handle_tree_right.
    130 
    131     PERFORM fill_tree CHANGING handle_tree_right node_itab_right.
    132     CALL METHOD tree_right->add_nodes
    133          EXPORTING node_table = node_itab_right
    134                    table_structure_name = 'NODE_STR'.
    135 
    136 * registration of drag and drop events
    137     DATA dragdrop TYPE REF TO lcl_dragdrop_receiver.
    138     CREATE OBJECT dragdrop.
    139     SET HANDLER dragdrop->flavor_select FOR tree_right.
    140     SET HANDLER dragdrop->left_drag FOR tree_left.
    141     SET HANDLER dragdrop->right_drop FOR tree_right.
    142     SET HANDLER dragdrop->drop_complete FOR tree_left.
    143 
    144 
    145     CALL METHOD tree_left->expand_node
    146          EXPORTING node_key = 'Root'.
    147     CALL METHOD tree_right->expand_node
    148          EXPORTING node_key = 'Root'.
    149 
    150   ENDIF.
    151 ENDMODULE.                             " START  OUTPUT
    152 *&---------------------------------------------------------------------*
    153 *&      Module  EXIT  INPUT
    154 *&---------------------------------------------------------------------*
    155 *       text
    156 *----------------------------------------------------------------------*
    157 MODULE exit INPUT.
    158   LEAVE PROGRAM.
    159 ENDMODULE.                             " EXIT  INPUT
    160 *&---------------------------------------------------------------------*
    161 *&      Form  fill_tree
    162 *&---------------------------------------------------------------------*
    163 *       text
    164 *----------------------------------------------------------------------*
    165 *  -->  p1        text
    166 *  <--  p2        text
    167 *----------------------------------------------------------------------*
    168 FORM fill_tree CHANGING handle_tree TYPE i
    169                         node_itab LIKE node_itab_left.
    170   DATA: node LIKE mtreesnode.
    171 
    172 * node table of the left tree
    173   CLEAR node.
    174   node-node_key = 'Root'.
    175   node-isfolder = 'X'.
    176   node-text = 'Texte'.
    177   node-dragdropid = ' '.
    178   APPEND node TO node_itab.
    179 
    180   CLEAR node.
    181   node-node_key = 'Child1'.
    182   node-relatkey = 'Root'.
    183     node-isfolder = 'X'.
    184   node-relatship = cl_gui_simple_tree=>relat_last_child.
    185   node-text = 'DragDrop Text 1'.
    186   node-dragdropid = handle_tree.       " handle of behaviour
    187   APPEND node TO node_itab.
    188 
    189   CLEAR node.
    190   node-node_key = 'Child2'.
    191   node-relatkey = 'Root'.
    192     node-isfolder = 'X'.
    193     node-relatship = cl_gui_simple_tree=>relat_last_child.
    194   node-text = 'DragDrop Text 2'.
    195   node-dragdropid = handle_tree.       " handle of behaviour
    196   APPEND node TO node_itab.
    197 
    198 
    199 ENDFORM.                               " fill_tree
    200 *&---------------------------------------------------------------------*
    201 *&      Module  USER_COMMAND_0100  INPUT
    202 *&---------------------------------------------------------------------*
    203 *       text
    204 *----------------------------------------------------------------------*
    205 MODULE user_command_0100 INPUT.
    206   CALL METHOD cl_gui_cfw=>dispatch.
    207 ENDMODULE.                             " USER_COMMAND_0100  INPUT
    208 
    209 
    210 *---------------------------------------------------------------------*
    211 *       CLASS DRAGDROP_RECEIVER IMPLEMENTATION
    212 *---------------------------------------------------------------------*
    213 *       ........                                                      *
    214 *---------------------------------------------------------------------*
    215 CLASS lcl_dragdrop_receiver IMPLEMENTATION.
    216   METHOD flavor_select.
    217     IF node_key <> 'Root'.
    218       SEARCH flavors FOR 'Tree_move'.
    219       IF sy-subrc = 0.
    220         CALL METHOD drag_drop_object->set_flavor
    221              EXPORTING newflavor = 'Tree_move'.
    222       ELSE.
    223         CALL METHOD drag_drop_object->abort.
    224       ENDIF.
    225     ELSE.
    226       SEARCH flavors FOR 'Tree_copy'.
    227       IF sy-subrc = 0.
    228         CALL METHOD drag_drop_object->set_flavor
    229              EXPORTING newflavor = 'Tree_copy'.
    230       ELSE.
    231         CALL METHOD drag_drop_object->abort.
    232       ENDIF.
    233     ENDIF.
    234 
    235   ENDMETHOD.
    236   METHOD left_drag.
    237     DATA drag_object TYPE REF TO lcl_drag_object.
    238     DATA node_key TYPE node_str-node_key.
    239     CREATE OBJECT drag_object.
    240     LOOP AT node_key_table INTO node_key.
    241       READ TABLE node_itab_left WITH KEY node_key = node_key
    242                            INTO node.
    243 
    244       APPEND node-text TO drag_object->text.
    245     ENDLOOP.
    246     drag_drop_object->object = drag_object.
    247   ENDMETHOD.
    248 
    249   METHOD right_drop.
    250     DATA drag_object TYPE REF TO lcl_drag_object.
    251     data new_nodes like node_itab_right.
    252     data new_node type node_str.
    253     CATCH SYSTEM-EXCEPTIONS move_cast_error = 1.
    254       drag_object ?= drag_drop_object->object.
    255     ENDCATCH.
    256     IF sy-subrc = 1.
    257       " data object has unexpected class
    258                                        " => cancel Drag & Drop operation
    259       CALL METHOD drag_drop_object->abort.
    260       EXIT.
    261      ENDIF.
    262    loop at drag_object->text into new_node-text.
    263      perform add_node changing node_key new_node new_nodes
    264                                node_itab_left.
    265     endloop.
    266      call method tree_right->add_nodes
    267           exporting node_table           = new_nodes
    268                     table_structure_name = 'NODE_STR'.
    269   ENDMETHOD.
    270   METHOD drop_complete.
    271     DATA node_key TYPE node_str-node_key.
    272     IF drag_drop_object->flavor = 'Tree_move_to_Edit'.
    273       LOOP AT node_key_table INTO node_key.
    274         CALL METHOD tree_left->delete_node
    275                EXPORTING node_key = node_key.
    276         DELETE node_itab_left WHERE node_key = node_key.
    277       ENDLOOP.
    278     ENDIF.
    279 
    280   ENDMETHOD.
    281 ENDCLASS.
    282 
    283 *&---------------------------------------------------------------------*
    284 *&      Form  ADD_NODE
    285 *&---------------------------------------------------------------------*
    286 *       text
    287 *----------------------------------------------------------------------*
    288 *      <--P_NEW_NODE  text
    289 *      <--P_NEW_NODES  text
    290 *      <--P_NODE_ITAB_LEFT  text
    291 *----------------------------------------------------------------------*
    292 FORM ADD_NODE CHANGING node_key
    293                        NEW_NODE type node_str
    294                        NEW_NODES like node_itab_left
    295                        NODE_ITAB_LEFT like node_itab_left.
    296 data lines type i.
    297 describe table node_itab_left lines lines.
    298   new_node-node_key = lines.
    299   new_node-isfolder = 'X'.
    300   new_node-dragdropid = handle_tree_right.
    301   new_node-relatkey = node_key.
    302 
    303   APPEND new_node TO new_nodes.
    304   append new_node to node_itab_left.
    305 
    306 ENDFORM.                    " ADD_NODE
  • 相关阅读:
    190822——喜欢
    190821——彼岸无岸
    190820——随笔
    BLE——协议层次结构
    190817——肖申克的救赎
    190818——人
    190819——皖北部分村落的变迁史
    【转】vfork 和 fork的区别
    C语言文件操作
    【makefile】symbol <函数> : can't resolve symbol 问题分析
  • 原文地址:https://www.cnblogs.com/JackeyLove/p/13650070.html
Copyright © 2020-2023  润新知