Drag’n’Drop im TreeControl

Dieses Programm erzeugt einen TreeControl, in dem die Länder nach Anfangsbuchstabe gruppiert werden. Für jeden Buchstaben wird eine Gruppe erzeugt. Die Länder können dann in andere Gruppen verschoben oder kopiert werden.

Um das Beispiel nutzen zu können, müssen Sie noch den Screen 100 anlegen. Definieren sie auf dem Dynpro 100 einen CustomControl über die gesamte Größe und benennen ihn mit TREE_CONTAINER.

Tragen Sie bitte in der Elementliste die Variable “G_OK_CODE” in das Feld “OK_CODE” ein.

Ausserdem müssen Sie noch das PBO-Modul “STATUS_0100” im PBO und das PAI-Modul “USER_COMMAND_0100” im PAI einbinden.

Zusätzlich müssen Sie noch den GUI-Status “MAIN” erzeugen und der F3-Taste den Ok-Code “BACK” zuordnen.

Weitere Hilfe unter: SAP-Print-Doku

So sieht’s aus:

Code

REPORT zzTreeDragAndDrop MESSAGE-ID zs.

*** Bekanntmachung Klassen
CLASS cl_gui_cfw      DEFINITION LOAD.

*** Types
TYPES:
      item_table_type LIKE STANDARD TABLE OF mtreeitm
      WITH DEFAULT KEY.

*** Datendefinition
DATA: g_custom_container TYPE REF TO cl_gui_custom_container,
      g_tree             TYPE REF TO cl_gui_list_tree,
      g_ok_code          TYPE sy-ucomm.

*-- Drag and Drop
DATA: behaviour_drag     TYPE REF TO cl_dragdrop,
      behaviour_drop     TYPE REF TO cl_dragdrop,
      handle_drag        TYPE i,
      handle_drop        TYPE i,
      node               TYPE treev_node,
      item               TYPE mtreeitm,
      node_key_dragged   TYPE mtreesnode-node_key,
      node_key_dropped   TYPE mtreesnode-node_key.

*-- Baumzweige
DATA: node_table         TYPE treev_ntab,
      item_table         TYPE item_table_type,
      node_table_add     TYPE treev_ntab,
      item_table_add     TYPE item_table_type,
      nodekey(12)        TYPE n,
      nodekeyx(12)       TYPE c.

*-- Variablen zum Ändern des Baumes
DATA: node_table_upd     TYPE STANDARD TABLE OF treev_unod,
      node_upd           TYPE treev_unod,
      item_table_upd     TYPE STANDARD TABLE OF treemuctit,
      item_upd           TYPE treemuctit.


*---------------------------------------------------------------------*
*       CLASS lcl_drag_object DEFINITION
*---------------------------------------------------------------------*
CLASS lcl_drag_object DEFINITION.
  PUBLIC SECTION.
    DATA: text      TYPE mtreesnode-text,
          node_key  TYPE mtreesnode-node_key.
ENDCLASS.


*---------------------------------------------------------------------*
*       CLASS lcl_dragdrop_receiver DEFINITION
*---------------------------------------------------------------------*
CLASS lcl_dragdrop_receiver DEFINITION.
  PUBLIC SECTION.
    METHODS:

*-- Drag'n'Drop: DRAG
     tree_drag
        FOR EVENT on_drag
        OF cl_gui_list_tree
        IMPORTING node_key item_name drag_drop_object,
*-- Drag'n'Drop: DROP
     tree_drop
        FOR EVENT on_drop
        OF cl_gui_list_tree
        IMPORTING node_key drag_drop_object,
*-- Drag'n'Drop: DROP COMPLETE
     drop_complete
        FOR EVENT on_drop_complete
        OF cl_gui_list_tree
        IMPORTING node_key drag_drop_object.

ENDCLASS.


*---------------------------------------------------------------------*
*       CLASS lcl_dragdrop_receiver IMPLEMENTATION
*---------------------------------------------------------------------*
CLASS lcl_dragdrop_receiver IMPLEMENTATION.

*_____________________________________________________*
*                                                     *
* TREE_DRAG
*_____________________________________________________*

  METHOD tree_drag.
    DATA drag_object TYPE REF TO lcl_drag_object.
    CREATE OBJECT drag_object.
    drag_object->node_key    = node_key.
    node_key_dragged         = node_key.
    drag_drop_object->object = drag_object.
  ENDMETHOD.

*_____________________________________________________*
*                                                     *
* TREE_DROP
*_____________________________________________________*

  METHOD tree_drop.
    DATA drag_object TYPE REF TO lcl_drag_object.
    CATCH SYSTEM-EXCEPTIONS move_cast_error = 1.
      drag_object ?= drag_drop_object->object.
    ENDCATCH.
    IF sy-subrc = 1.
      CALL METHOD drag_drop_object->abort.
      EXIT.
    ENDIF.
* Synchronize Automation Queue after Get Methods
    CALL METHOD cl_gui_cfw=>flush.
    node_key_dropped = node_key.
  ENDMETHOD.

*_____________________________________________________*
*                                                     *
* DROP_COMPLETE
*_____________________________________________________*

  METHOD drop_complete.

    IF drag_drop_object->flavor = 'DragAndDrop'.

      CASE drag_drop_object->effect.
*-- Knoten kopieren
        WHEN cl_dragdrop=>copy.
*-- Tabellen zum Hinzufügen eines neuen Knotens löschen
          CLEAR node_table_add.
          CLEAR item_table_add.
*-- selektierten Knoten kopieren:
          LOOP AT node_table
               INTO node WHERE node_key  = node_key_dragged.
            node-relatkey   = node_key_dropped.
            ADD 1 TO nodekey.
            node-node_key = nodekey.
            APPEND node TO: node_table_add,
                            node_table.
*-- Items zum Knoten kopieren
            LOOP AT item_table INTO item
                 WHERE node_key = node_key_dragged.
              item-node_key = node-node_key.
              APPEND item TO: item_table_add,
                              item_table.
            ENDLOOP. "Node
          ENDLOOP.   "Item

*-- Eintrag zu Drop-Zone hinzufügen
          CALL METHOD g_tree->add_nodes_and_items
            EXPORTING
              node_table                     = node_table_add
              item_table                     = item_table_add
              item_table_structure_name      = 'MTREEITM'.

        WHEN cl_dragdrop=>move.
*-- Knoten verschieben
          CALL METHOD g_tree->move_node
            EXPORTING
              node_key           = node_key_dragged
              relatkey           = node_key_dropped
              relatship          = cl_gui_list_tree=>relat_last_child
            EXCEPTIONS
              OTHERS             = 8.

      ENDCASE.
    ENDIF.

  ENDMETHOD.

ENDCLASS.


START-OF-SELECTION.

  SET SCREEN 100.

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

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

  IF g_tree IS INITIAL.
    PERFORM create_and_init_dragdrop.
    PERFORM create_and_init_tree.
  ENDIF.

ENDMODULE.                 " STATUS_0100  OUTPUT

*---------------------------------------------------------------------*
*       FORM create_and_init_dragdrop                                 *
*---------------------------------------------------------------------*
FORM create_and_init_dragdrop.

  DATA: effect_lv TYPE i.
  effect_lv = cl_dragdrop=>move + cl_dragdrop=>copy.

*-- Definition Drag'n'Drop-Verhalten: Quelle
  CREATE OBJECT behaviour_drag.
  CALL METHOD behaviour_drag->add
    EXPORTING
      flavor     = 'DragAndDrop'
      dragsrc    = 'X'
      droptarget = ' '
      effect     = effect_lv.

  CALL METHOD behaviour_drag->get_handle
    IMPORTING handle = handle_drag.

*-- Definition Drag'n'Drop-Verhalten: Ziel
  CREATE OBJECT behaviour_drop.
  CALL METHOD behaviour_drop->add
    EXPORTING
      flavor     = 'DragAndDrop'
      dragsrc    = ' '
      droptarget = 'X'
      effect     = effect_lv.

  CALL METHOD behaviour_drop->get_handle
    IMPORTING handle = handle_drop.

ENDFORM.

*---------------------------------------------------------------------*
*       FORM create_and_init_tree                                     *
*---------------------------------------------------------------------*
FORM create_and_init_tree.

  DATA: events TYPE cntl_simple_events,
        event  TYPE cntl_simple_event.

* create a container for the tree control
  CREATE OBJECT g_custom_container
    EXPORTING
      container_name = 'TREE_CONTAINER'.

* Tree control erzeugen
  CREATE OBJECT g_tree
    EXPORTING
      parent                = g_custom_container
      node_selection_mode   = cl_gui_list_tree=>node_sel_mode_single
      item_selection        = 'X'
      with_headers          = ' '.

*-- Baum aus der Ländertabelle erzeugen
  PERFORM build_node_and_item_table.

*-- Knoten hinzufügen
  CALL METHOD g_tree->add_nodes_and_items
    EXPORTING
      node_table                     = node_table
      item_table                     = item_table
      item_table_structure_name      = 'MTREEITM'.

*-- Knoten "Root" aufklappen
    CALL METHOD g_tree->expand_node
         EXPORTING node_key = 'Root'.
  ENDIF.

*-- Drag'n'Drop-Ereignisse erzeugen
  DATA dragdrop TYPE REF TO lcl_dragdrop_receiver.
  CREATE OBJECT dragdrop.
  SET HANDLER dragdrop->tree_drag     FOR g_tree.
  SET HANDLER dragdrop->tree_drop     FOR g_tree.
  SET HANDLER dragdrop->drop_complete FOR g_tree.

ENDFORM.

*---------------------------------------------------------------------*
*       FORM build_node_and_item_table                                *
*---------------------------------------------------------------------*
FORM build_node_and_item_table.

  DATA:
    first_letter,
    t_t005t LIKE t005t OCCURS 0 WITH HEADER LINE.

  nodekey = 1.
*-- Node with key 'Root'
  CLEAR node.
  node-node_key  = 'Root'.
  node-hidden    = ' '.
  node-disabled  = ' '.
  node-isfolder  = 'X'.
  APPEND node TO node_table.

*-- Item to key 'Root'
  CLEAR item.
  item-node_key  = 'Root'.
  item-item_name = '1'.
  item-class     = cl_gui_list_tree=>item_class_text.
  item-alignment = cl_gui_list_tree=>align_auto.
  item-font      = cl_gui_list_tree=>item_font_prop.
  item-text      = 'Root'.
  APPEND item TO item_table.

*-- Selektion der Länder
  SELECT * FROM t005t INTO TABLE t_t005t WHERE spras = 'D'.

*-- Sortieren
  sort t_t005t by land1.

  CLEAR first_letter.

  LOOP AT t_t005t.
    IF first_letter <> t_t005t-land1(1).
*-- Verzeichnis "Länder mit Buchstabe (x)" erstellen
      first_letter = t_t005t-land1(1).
      CLEAR node.
      ADD 1 TO nodekey.
      node-node_key   = nodekey.
      nodekeyx        = nodekey.
      node-relatkey   = 'Root'.
      node-relatship  = cl_gui_list_tree=>relat_last_child.
      node-dragdropid = handle_drop.
      node-hidden     = ' '.
      node-disabled   = ' '.
      node-isfolder   = 'X'.
      APPEND node TO node_table.

*-- Item zum Verzeichnis
      CLEAR item.
      item-node_key  = node-node_key.
      item-item_name = '1'.
      item-class     = cl_gui_list_tree=>item_class_text.
      item-alignment = cl_gui_list_tree=>align_auto.
      item-font      = cl_gui_list_tree=>item_font_prop.
      CONCATENATE 'Länder mit' first_letter
             INTO item-text separated by space.
      APPEND item TO item_table.
    ENDIF.

*-- Nun die Länder anhängen und dem jeweiligen Verzeichnis zuordnen
    CLEAR node.
    ADD 1 TO nodekey.
    node-node_key   = nodekey.
    node-relatkey   = nodekeyx.
    node-relatship  = cl_gui_list_tree=>relat_last_child.
    node-dragdropid = handle_drag.
    node-hidden     = ' '.
    node-disabled   = ' '.
    node-isfolder   = ' '.
    APPEND node TO node_table.

*-- Item "Land"
    CLEAR item.
    item-node_key  = node-node_key.
    item-item_name = '1'.
    item-style     = cl_gui_list_tree=>style_intensified.
    item-class     = cl_gui_list_tree=>item_class_text.
    item-alignment = cl_gui_list_tree=>align_auto.
    item-font      = cl_gui_list_tree=>item_font_fixed.
    item-text      = t_t005t-land1.
    APPEND item TO item_table.

*-- Item "Länderbezeichnung"
    CLEAR item.
    item-node_key  = node-node_key.
    item-item_name = '2'.
    item-class     = cl_gui_list_tree=>item_class_text.
    item-alignment = cl_gui_list_tree=>align_auto.
    item-font      = cl_gui_list_tree=>item_font_prop.
    item-text      = t_t005t-landx.
    APPEND item TO item_table.

  ENDLOOP.

ENDFORM.

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

  DATA: return_code TYPE i.
* CL_GUI_CFW=>DISPATCH must be called if events are registered
* that trigger PAI
* this method calls the event handler method of an event
  CALL METHOD cl_gui_cfw=>dispatch
    IMPORTING return_code = return_code.
  IF return_code <> cl_gui_cfw=>rc_noevent.
    CLEAR g_ok_code.
    EXIT.
  ENDIF.

  CASE g_ok_code.

    WHEN 'BACK'.
*-- Verlassen des Dynpros: Vorher aufräumen!
      IF NOT g_custom_container IS INITIAL.
        g_custom_container->free( ).
        CLEAR g_custom_container.
        CLEAR g_tree.
      ENDIF.
      SET SCREEN 0. LEAVE SCREEN.
  ENDCASE.

ENDMODULE.                 " USER_COMMAND_0100  INPUT

 

Enno Wulff
Letzte Artikel von Enno Wulff (Alle anzeigen)

Leave a Comment