BCALV_DND_01

dragging rows of the grid control to nodes of the tree control.

[@more@]

*&---------------------------------------------------------------------*
*& Program BCALV_DRAG_N_DROP_01 *
*& *
*&---------------------------------------------------------------------*
* Purpose
* ~~~~~~~
* This example shows how to define a drag and drop behaviour using
* a drag and drop control.
* Direction: drag from tree and drop in ALV control.
* The drag behaviour is equal for all lines of the table displayed
* with ALV.
*--------------------------------------------------------------------
* To check program behaviour:
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~
* The ALV control shows booking data for carrier LH flight 0400
* for all customers who have got an id less than 100 and whose
* booking id is less than 100 as well.
* Try to move or copy (using the CTRL key) lines of the table
* into a folder. When moving a line the alv control is updated
* accordingly.
*---------------------------------------------------------------------
* Essential steps: (Search for '
')
* ~~~~~~~~~~~~~~~~
* 1. Define reference variables to CL_DRAGDROP:
* One for your drag source and one for your drop target.
* 2. Define a behaviour for drag and drop on alv objects
* and tree nodes. Get a handle for each defined behaviour.
* 3. Transfer behaviour handles to each involved object of
* the drag and drop operation.
* 4. Define a class for a data object to exchange data
* between two controls using the drag and drop operation.
* 5. Define methods for three events raised by the drag and drop
* control. (Note that the names for these events depend on
* the classes for which the behaviour was defined!).
* 6. In the 'onDrag' event handler create a data object and fill it with
* appropriate data for your intended operation. This event is used
* to 'fetch' information from the drag source.
* 7. Assign your data object to the refering event parameter.
* This parameter ensures that your data object can be referenced
* in each of the following events.
* 8. Implement the event handler for event 'OnDrop'. This event is used
* to use your dragged information in combination with your drop
* source. What is more, you should make all checks
* if the operation is successful _at this point_.
* 9. Implement the event handler for event 'OnDropComplete'. This event
* is used to change the state after a successful drag and drop
* operation, e.g., update your internal table if a row has been
* moved.
*----------------------------------------------------------------------
REPORT BCALVC_DND_01 MESSAGE-ID TREE_CONTROL_MSG.


CLASS LCL_APPLICATION DEFINITION DEFERRED.
CLASS CL_GUI_CFW DEFINITION LOAD.

TYPES: NODE_TABLE_TYPE LIKE STANDARD TABLE OF MTREESNODE
WITH DEFAULT KEY.
* CAUTION: MTREESNODE is the name of the node structure which must
* be defined by the programmer. DO NOT USE MTREESNODE!

DATA: G_APPLICATION TYPE REF TO LCL_APPLICATION,
G_DOCKING_CONTAINER_1 TYPE REF TO CL_GUI_docking_CONTAINER,
G_DOCKING_CONTAINER_2 TYPE REF TO CL_GUI_docking_CONTAINER,
G_TREE TYPE REF TO CL_GUI_SIMPLE_TREE,
G_ALV TYPE REF TO CL_GUI_ALV_GRID,
g_node_table type node_table_type,

* 1. Define reference variables to CL_DRAGDROP:
* One for your drag source and one for your drop target.
g_behaviour_tree TYPE REF TO CL_DRAGDROP,
g_behaviour_alv TYPE REF TO CL_DRAGDROP,
*G_REPID like sy-repid,
G_OK_CODE TYPE SY-UCOMM,
gt_outtab type table of sbook,
g_max type i value 50.
* You need the layout structure of alv to transfer the handle
* of your defined behaviour (see step 2).Data: gs_layout type lvc_s_layo.


*
4. Define a class for a data object to exchange data
* between two controls using the drag and drop operation.
CLASS lcl_dragdropobj DEFINITION.
PUBLIC SECTION.
DATA: ls_sbook TYPE sbook,
index TYPE i.
ENDCLASS.

*---------------------------------------------------------------------*
* CLASS LCL_APPLICATION DEFINITION
*---------------------------------------------------------------------*
* ........ *
*---------------------------------------------------------------------*
*
5. Define methods for three events raised by the drag and drop
* control. (Note that the names for these events depend on
* the classes for which the behaviour was defined!).
CLASS lcl_application DEFINITION.

PUBLIC SECTION.
DATA: next_node_key type i.

METHODS:
handle_alv_drag
FOR EVENT ondrag
OF cl_gui_alv_grid
IMPORTING e_row e_column e_dragdropobj,
handle_tree_drop
FOR EVENT on_drop
OF cl_gui_simple_tree
IMPORTING node_key drag_drop_object,
handle_alv_drop_complete
FOR EVENT ondropcomplete
OF cl_gui_alv_grid
IMPORTING e_row e_column e_dragdropobj.
ENDCLASS.

*---------------------------------------------------------------------*
* CLASS LCL_APPLICATION IMPLEMENTATION
*---------------------------------------------------------------------*
* ........ *
*---------------------------------------------------------------------*
CLASS lcl_application IMPLEMENTATION.
*-------------------------------------------------------------------
*
6.In the 'onDrag' event handler create a data object and fill it with
* appropriate data for your intended operation. This event is used
* to 'fetch' information from the drag source.
METHOD handle_alv_drag.
DATA: dataobj TYPE REF TO lcl_dragdropobj,
line TYPE sbook.

* Read dragged rowREAD TABLE gt_outtab INDEX e_row-index INTO line.

* create and fill dataobject for events ONDROP and ONDROPCOMPLETECREATE OBJECT dataobj.
MOVE line TO dataobj->ls_sbook.

* remember also the row index in case of effect 'move'MOVE e_row-index TO dataobj->index.

* 7. Assign your data object to the refering event parameter.
* This parameter ensures that your data object can be referenced
* in each of the following events.

e_dragdropobj->object = dataobj.
ENDMETHOD.
*--------------------------------------------------------------------
*
8.Implement the event handler for event 'OnDrop'. This event is used
* to use your dragged information in combination with your drop
* source. What is more, you should make all checks
* if the operation is successful _at this point_.
METHOD handle_tree_drop.
DATA: local_node_table TYPE node_table_type,
new_node TYPE mtreesnode,
dataobj TYPE REF TO lcl_dragdropobj,
l_keytxt(30) TYPE C.

*!!!
* Very importent: 'e_dragDropObj->object' can have any instance type
* The needed cast type may lead to a system-exception if the
* cast can not be performed.
* For this reason: use ALWAYS the Catch-Statement to make sure
* that the drag&drop-Operation is aborted properly.
*!!!
CATCH SYSTEM-EXCEPTIONS move_cast_error = 1.

dataobj ?= drag_drop_object->object.

new_node-node_key = next_node_key.
ADD 1 TO next_node_key.
new_node-relatkey = node_key.
new_node-relatship = cl_gui_simple_tree=>relat_last_child.
perform set_keytxt using dataobj->ls_sbook
changing l_keytxt.

new_node-text = l_keytxt.

APPEND new_node TO local_node_table.
APPEND new_node TO g_node_table.
CALL METHOD g_tree->add_nodes
EXPORTING
table_structure_name = 'MTREESNODE'
node_table = local_node_table
EXCEPTIONS
failed = 1
error_in_node_table = 2
dp_error = 3
table_structure_name_not_found = 4
OTHERS = 5.
IF sy-subrc <> 0.
MESSAGE a000.
ENDIF.
* expand nodeCALL METHOD g_tree->expand_node
EXPORTING node_key = node_key.

ENDCATCH.
IF sy-subrc <> 0.
* If anything went wrong this is the clean way of aborting the
* drag and drop operation:CALL METHOD drag_drop_object->abort.
ENDIF.
ENDMETHOD.
*-------------------------------------------------------------------
*
9.Implement the event handler for event 'OnDropComplete'. This event
* is used to change the state after a successful drag and drop
* operation, e.g., update your internal table if a row has been
* moved.
METHOD handle_alv_drop_complete.
DATA: dataobj TYPE REF TO lcl_dragdropobj.

* Again: do not forget to catch this system exception!CATCH SYSTEM-EXCEPTIONS move_cast_error = 1.

dataobj ?= e_dragdropobj->object.
IF e_dragdropobj->effect = cl_dragdrop=>move.
* delete one line from internal table and refresh display
DELETE gt_outtab INDEX dataobj->index.
IF sy-subrc eq 0.
* refresh alv controlCALL METHOD g_alv->refresh_table_display.
ENDIF.
endif.
ENDCATCH.
IF sy-subrc <> 0.
* If anything went wrong this is the clean way of aborting the
* drag and drop operation:CALL METHOD e_dragDropObj->abort.
ENDIF.
ENDMETHOD.

ENDCLASS.
*&---------------------------------------------------------------------*
*& Module PBO_0400 OUTPUT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*MODULE PBO_100 OUTPUT.

SET PF-STATUS 'MAIN'.
IF G_TREE IS INITIAL.
PERFORM CREATE_AND_INIT_CONTROLS.
ENDIF.

ENDMODULE. " PBO_0100 OUTPUT
*&---------------------------------------------------------------------*
*& Module PAI_0100 INPUT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*MODULE PAI_100 INPUT.

CASE G_OK_CODE.
WHEN 'BACK'. " Finish program
IF NOT g_docking_container_1 IS INITIAL.
" destroy tree containers (detroys contained tree control, too)
CALL METHOD g_docking_container_1->FREE
EXCEPTIONS
CNTL_SYSTEM_ERROR = 1
CNTL_ERROR = 2.
IF SY-SUBRC <> 0.
MESSAGE A000.
ENDIF.
CALL METHOD g_docking_container_2->FREE
EXCEPTIONS
CNTL_SYSTEM_ERROR = 1
CNTL_ERROR = 2.
IF SY-SUBRC <> 0.
MESSAGE A000.
ENDIF.
CALL METHOD CL_GUI_CFW=>FLUSH
EXCEPTIONS
CNTL_SYSTEM_ERROR = 1
CNTL_ERROR = 2.
IF SY-SUBRC <> 0.
MESSAGE A000.
ENDIF.
CLEAR g_docking_container_1.
CLEAR G_tree.
CLEAR g_docking_container_2.
CLEAR g_alv.
ENDIF.
LEAVE PROGRAM.
ENDCASE.

* CAUTION: clear ok code!CLEAR G_OK_CODE.
ENDMODULE. " PAI_0100 INPUT
*&---------------------------------------------------------------------*
*& Form CREATE_AND_INIT_CONTROLS
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --&gt p1 text
* *----------------------------------------------------------------------*FORM create_and_init_controls.
g_repid = sy-repid.

* create docking container for tree controlCREATE OBJECT g_docking_container_1
EXPORTING
repid = g_repid
dynnr = '100'
extension = 205
side = cl_gui_docking_container=>dock_at_left.
IF sy-subrc <> 0.
MESSAGE a000.
ENDIF.

* create docking container for alv controlCREATE OBJECT g_docking_container_2
EXPORTING
repid = g_repid
dynnr = '100'
extension = 160
side = cl_gui_docking_container=>dock_at_top.
IF sy-subrc <> 0.
MESSAGE a000.
ENDIF.

* create left tree controlCREATE OBJECT g_tree
EXPORTING
parent = g_docking_container_1
node_selection_mode = cl_gui_simple_tree=>node_sel_mode_single
EXCEPTIONS
lifetime_error = 1
cntl_system_error = 2
create_error = 3
failed = 4
illegal_node_selection_mode = 5.
IF sy-subrc <> 0.
MESSAGE a000.
ENDIF.

* create alv controlCREATE OBJECT g_alv
EXPORTING i_parent = g_docking_container_2.

* create the application object
* this object is needed to handle the ABAP Objects Events of
* Controls
CREATE OBJECT G_APPLICATION.

* Events alv controlSET HANDLER g_application->handle_alv_drag FOR g_alv.
SET HANDLER g_application->handle_alv_drop_complete FOR g_alv.


* Events tree controlSET HANDLER g_application->handle_tree_drop FOR g_tree.

* build tree nodes and describe behaviour of drag&dropPERFORM build_nodes_and_handles.

CALL METHOD g_tree->add_nodes
EXPORTING
table_structure_name = 'MTREESNODE'
node_table = g_node_table
EXCEPTIONS
failed = 1
error_in_node_table = 2
dp_error = 3
table_structure_name_not_found = 4
OTHERS = 5.
IF sy-subrc <> 0.
MESSAGE a000.
ENDIF.

CALL METHOD g_tree->expand_node
EXPORTING node_key = 'SBOOK'.

* select and display data from SBOOKSELECT * FROM sbook INTO TABLE gt_outtab up to g_max rows
WHERE carrid = 'LH'
AND connid = '0400'
AND bookid < 100
AND customid < 100.

gs_layout-grid_title = text-101.

CALL METHOD g_alv->set_table_for_first_display
EXPORTING i_structure_name = 'SBOOK'
is_layout = gs_layout
CHANGING it_outtab = gt_outtab.

ENDFORM. " CREATE_AND_INIT_CONTROLS
*&---------------------------------------------------------------------*
*& Form build_nodes_and_handles
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --&gt p1 text
* *----------------------------------------------------------------------*

FORM build_nodes_and_handles.

DATA: node LIKE mtreesnode,
effect TYPE i,
handle_tree TYPE i,
handle_alv TYPE i.

* 2.Define a behaviour for drag and drop on alv objects
* and tree nodes. Get a handle for each defined behaviour.
* define a drag & Drop behaviour for the two leaves in the left tree

CREATE OBJECT g_behaviour_tree.
effect = cl_dragdrop=>move + cl_dragdrop=>copy.
CALL METHOD g_behaviour_tree->add
EXPORTING
flavor = 'Line' "#EC NOTEXT
dragsrc = ' '
droptarget = 'X'
effect = effect.
CALL METHOD g_behaviour_tree->get_handle
IMPORTING handle = handle_tree.


* define a drag & Drop behaviour for the whole gridCREATE OBJECT g_behaviour_alv.
effect = cl_dragdrop=>move + cl_dragdrop=>copy.
CALL METHOD g_behaviour_alv->add
EXPORTING
flavor = 'Line'
dragsrc = 'X'
droptarget = ' '
effect = effect.
CALL METHOD g_behaviour_alv->get_handle
IMPORTING handle = handle_alv.

*..........
*
3. Transfer behaviour handles to each involved object of
* the drag and drop operation.
*
* Remark: The place at which you transfer your handle is control
* dependend!

* node table of the tree
* Only the three subfolders shall provide a drag and drop behaviour,
* so the first one does not get a handle.
CLEAR node.
node-node_key = 'SBOOK'.
node-isfolder = 'X'.
node-text = 'Bookings'(140).
APPEND node TO g_node_table.

CLEAR node.
node-node_key = 'PAYED'.
node-isfolder = 'X'.
node-relatkey = 'SBOOK'.
node-relatship = cl_gui_simple_tree=>relat_last_child.
node-text = 'Bill payed'(130).
* In case of trees you transfer a handle by using field 'dragdropid':node-dragdropid = handle_tree.
APPEND node TO g_node_table.

CLEAR node.
node-node_key = 'REMINDED'.
node-isfolder = 'X'.
node-relatkey = 'SBOOK'.
node-relatship = cl_gui_simple_tree=>relat_last_child.
node-text = 'Client reminded'(120).
node-dragdropid = handle_tree.
APPEND node TO g_node_table.

CLEAR node.
node-node_key = 'CANCELED'.
node-isfolder = 'X'.
node-relatkey = 'SBOOK'.
node-relatship = cl_gui_simple_tree=>relat_last_child.
node-text = 'Canceled'(110).
node-dragdropid = handle_tree.
APPEND node TO g_node_table.

* provide handle to alv control using the layout-structure
* In this example all rows obtain the same drag and drop behaviour:
gs_layout-s_dragdrop-row_ddid = handle_alv.

ENDFORM. " build_nodes_and_handles

*&---------------------------------------------------------------------*
*& Form SET_KEYTXT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* * --&gtP_DATAOBJ_>LS_SBOOK text
*----------------------------------------------------------------------*
FORM set_keytxt USING ls_sbook TYPE sbook
CHANGING keytxt TYPE c.
DATA: s1(4) VALUE 'B:',
s2(2),
s3(4) VALUE ' C:',
s4(2),
s5(4) VALUE ' D:',
custid(20),
bookid(20),
date(8),
len TYPE i,
offset TYPE i.
* SBOOK-CUSTID and SBOOK-BOOKID are always <100, so two characters
* are sufficient.MOVE ls_sbook-bookid TO bookid.
COMPUTE len = strlen( bookid ).
offset = len - 2.
s2 = bookid+offset(2).

MOVE ls_sbook-customid TO custid.
COMPUTE len = strlen( custid ).
offset = len - 2.
s4 = custid+offset(2).

date = ls_sbook-fldate.
CONCATENATE s1 s2 s3 s4 s5 date INTO keytxt.

ENDFORM. " SET_KEYTXT

START-OF-SELECTION.
SET SCREEN 100.

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/8214011/viewspace-922923/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/8214011/viewspace-922923/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值