二维码

[Tree] 使用oo实现简单的Tree control实例

Twilight发表于 2014-01-22 16:29Twilight 最后回复于 2014-01-22 16:29 [复制链接] 4312 0

Tree control 是最常用的windows控件之一,在其他语言中,称之为“Tree View”等,abap中tree control能完成相似的功能。

效果如下,本例将实现以下功能:
(1)、建立一个以国家、城市、城区三次的tree control对象(固定数据);
(2)、当双击某个节点时,在文本框显示节点的key值和节点文本值。
tree_control.jpg


实现步骤如下(前两部为基本功能,不在详述):
(1)、建立一个abap程序。
(2)、使用屏幕制作器,建立screen。
         添加“定制控制”对象;
         添加两个文本框,存放节点key值和节点文本值。
(3)、建立程序。
         处理窗口调用,退出等基本功能;
         创建窗口定制对象,tree control 对象,测试运行时能看到tree区域;
         实现tree节点的创建,测试运行,能看到tree的所有节点;
         定义对象,建立双击方法,并将双击方法分配tree对象,实现双击事件。

建立好的屏幕和对象界面如下图所示。
ELEMENT_LIST_TREE_CONTROL.jpg


主程序:
  1. REPORT  ZDEMON_009.
  2. *功能码返回值
  3. DATA: OK_CODE TYPE SY-UCOMM,
  4.       SAVE_OK LIKE OK_CODE.
  5. DATA:WA_CONTAINER TYPE SCRFNAME VALUE 'TREE1',
  6.      WA_CUSTOM_CONTAINER TYPE REF TO CL_GUI_CUSTOM_CONTAINER,
  7.      WA_TREE TYPE REF TO CL_GUI_SIMPLE_TREE.
  8. *-----------------------------------------------------------
  9. *NODE节点表
  10. DATA:NODE_TABLE LIKE TABLE OF MTREESNODE,
  11. *节点结构
  12.      NODE1 TYPE MTREESNODE.

  13. DATA WA_SPFLI TYPE TABLE OF SPFLI."WITH HEADER LINE.
  14. *当双击时取得节点的key值与节点文本值变量
  15. DATA:NODEKEY(200) VALUE 'NONE',NODETEXT(200) VALUE 'NODE'.
  16. *-----------------------------------------------------------
  17. CLASS LCL_APPLICATION DEFINITION DEFERRED.
  18. CLASS LCL_APPLICATION DEFINITION.
  19.     PUBLIC SECTION.
  20. *自定义双击方法,参数为节点关键字
  21.     METHODS HANDLE_NODE_DOUBLE_CLICK
  22.             FOR EVENT NODE_DOUBLE_CLICK
  23.        OF CL_GUI_SIMPLE_TREE
  24.        IMPORTING NODE_KEY.
  25. ENDCLASS.
  26. *-----------------------------------------------------------
  27. CLASS LCL_APPLICATION IMPLEMENTATION.
  28.     METHOD HANDLE_NODE_DOUBLE_CLICK.
  29. *当双击时弹出信息窗口,调试用
  30. *    MESSAGE I208(00) WITH 'DOUBLE'.
  31. *取得key值,在窗口显示
  32.     NODEKEY = NODE_KEY.
  33. *从节点内表中按关键字读取单个节点
  34.     READ TABLE NODE_TABLE  WITH KEY NODE_KEY = NODE_KEY
  35.       INTO NODE1.
  36. *将节点文本显示在窗口显示
  37.       NODETEXT = NODE1-TEXT.
  38.    ENDMETHOD.
  39. ENDCLASS.
  40. *-----------------------------------------------------------
  41. DATA:G_APPLICATION TYPE REF TO LCL_APPLICATION.
  42. START-OF-SELECTION.
  43. *创建自定义class对象
  44.   CREATE OBJECT G_APPLICATION.
  45. *直接调用窗口 ,在pbo里分配双击对象
  46.   SET SCREEN 100.
  47. *------------------------------------------------------------
  48. MODULE STATUS_0100 OUTPUT.
  49. *若果窗口还没创建tree对象则创建它
  50. IF WA_CUSTOM_CONTAINER IS INITIAL.
  51.    PERFORM CREATE_TREE.
  52. ENDIF.
  53. ENDMODULE.
  54. *------------------------------------------------------------
  55. FORM CREATE_TREE.
  56. *事件内表及单个事件对象
  57. DATA:EVENTS TYPE CNTL_SIMPLE_EVENTS,
  58.      EVENT1 TYPE CNTL_SIMPLE_EVENT.
  59. *建立定制控制对象
  60. CREATE OBJECT WA_CUSTOM_CONTAINER
  61.      EXPORTING CONTAINER_NAME = WA_CONTAINER.
  62. *------------------------------------------------------------
  63. *建立树对象
  64. CREATE OBJECT WA_TREE
  65.    EXPORTING
  66.    PARENT = WA_CUSTOM_CONTAINER
  67.    NODE_SELECTION_MODE =
  68.    CL_GUI_SIMPLE_TREE=>NODE_SEL_MODE_SINGLE
  69.    .
  70. *------------------------------------------------------------
  71. IF SY-SUBRC <> 0.
  72. ENDIF.
  73. *建立以国家->城市->城区 三层的树
  74. CLEAR NODE1.
  75. *节点关键字
  76. NODE1-NODE_KEY = '国家'.
  77. *第一层
  78. CLEAR NODE1-RELATKEY.
  79. CLEAR NODE1-RELATSHIP.
  80. *是文件夹
  81. NODE1-HIDDEN = ''.
  82. NODE1-DISABLED = ''.
  83. NODE1-ISFOLDER = 'X'.
  84. *不带icon
  85. CLEAR NODE1-N_IMAGE.
  86. CLEAR NODE1-EXP_IMAGE.
  87. CLEAR NODE1-EXPANDER.
  88. *节点文本
  89. NODE1-TEXT = '中国'.
  90. *添加到节点表
  91. APPEND NODE1 TO NODE_TABLE.
  92. *-------------------------------------------------------------
  93. *二层节点
  94. CLEAR NODE1.
  95. NODE1-NODE_KEY = '城市北京'.
  96. *上层关系
  97. NODE1-RELATKEY = '国家'.
  98. NODE1-RELATSHIP = CL_GUI_SIMPLE_TREE=>RELAT_LAST_CHILD.
  99. NODE1-HIDDEN = ''.
  100. NODE1-DISABLED = ''.
  101. NODE1-ISFOLDER = 'X'.
  102. CLEAR NODE1-N_IMAGE.
  103. CLEAR NODE1-EXP_IMAGE.
  104. CLEAR NODE1-EXPANDER.
  105. NODE1-TEXT = '北京'.
  106. APPEND NODE1 TO NODE_TABLE.
  107. *--------------------------------------------------------------
  108. CLEAR NODE1.
  109. NODE1-NODE_KEY = '城市上海'.
  110. NODE1-RELATKEY = '国家'.
  111. NODE1-RELATSHIP = CL_GUI_SIMPLE_TREE=>RELAT_LAST_CHILD.
  112. NODE1-HIDDEN = ''.
  113. NODE1-DISABLED = ''.
  114. NODE1-ISFOLDER = 'X'.
  115. CLEAR NODE1-N_IMAGE.
  116. CLEAR NODE1-EXP_IMAGE.
  117. CLEAR NODE1-EXPANDER.
  118. NODE1-TEXT = '上海'.
  119. APPEND NODE1 TO NODE_TABLE.
  120. *--------------------------------------------------------------
  121. CLEAR NODE1.
  122. NODE1-NODE_KEY = 'KEYCHONGWEN'.
  123. NODE1-RELATKEY = '城市北京'.
  124. NODE1-RELATSHIP = CL_GUI_SIMPLE_TREE=>RELAT_LAST_CHILD.
  125. NODE1-HIDDEN = ''.
  126. NODE1-DISABLED = ''.
  127. NODE1-ISFOLDER = ''.
  128. *图标,可运行showicon查阅
  129. NODE1-N_IMAGE = '@9Y@'.
  130. NODE1-EXP_IMAGE = '@9Y@'.
  131. CLEAR NODE1-EXPANDER.
  132. NODE1-TEXT = '崇文区'.
  133. APPEND NODE1 TO NODE_TABLE.
  134. *---------------------------------------------------------------
  135. CLEAR NODE1.
  136. NODE1-NODE_KEY = 'KEYXICHENG'.
  137. NODE1-RELATKEY = '城市北京'.
  138. NODE1-RELATSHIP = CL_GUI_SIMPLE_TREE=>RELAT_LAST_CHILD.
  139. NODE1-HIDDEN = ''.
  140. NODE1-DISABLED = ''.
  141. NODE1-ISFOLDER = ''.
  142. NODE1-N_IMAGE = '@DF@'.
  143. NODE1-EXP_IMAGE = '@DF@'.
  144. CLEAR NODE1-EXPANDER.
  145. NODE1-TEXT = '西城区'.
  146. APPEND NODE1 TO NODE_TABLE.
  147. *------------------------------------------------------------------
  148. CLEAR NODE1.
  149. NODE1-NODE_KEY = 'KEYMINHANG'.
  150. NODE1-RELATKEY = '城市上海'.
  151. NODE1-RELATSHIP = CL_GUI_SIMPLE_TREE=>RELAT_LAST_CHILD.
  152. NODE1-HIDDEN = ''.
  153. NODE1-DISABLED = ''.
  154. NODE1-ISFOLDER = ''.
  155. NODE1-N_IMAGE = '@NL@'.
  156. NODE1-EXP_IMAGE = '@NL@'.
  157. CLEAR NODE1-EXPANDER.
  158. NODE1-TEXT = '闵行区'.
  159. APPEND NODE1 TO NODE_TABLE.
  160. *------------------------------------------------------------------
  161. CALL METHOD WA_TREE->ADD_NODES
  162.      EXPORTING
  163.       TABLE_STRUCTURE_NAME = 'MTREESNODE'
  164.       NODE_TABLE = NODE_TABLE.
  165. IF SY-SUBRC <> 0.
  166. ENDIF.
  167. *------------------------------------------------------------------
  168. *定义双击事件
  169. EVENT1-EVENTID = CL_GUI_SIMPLE_TREE=>EVENTID_NODE_DOUBLE_CLICK.
  170. EVENT1-APPL_EVENT = 'X'.
  171. APPEND EVENT1 TO EVENTS.
  172. *------------------------------------------------------------------
  173. *添加事件内表
  174. CALL METHOD WA_TREE->SET_REGISTERED_EVENTS
  175.      EXPORTING
  176.      EVENTS = EVENTS.
  177. IF SY-SUBRC <> 0.
  178. ENDIF.
  179. *将已定义的双击事件分配到树对象
  180. SET HANDLER G_APPLICATION->HANDLE_NODE_DOUBLE_CLICK FOR WA_TREE.
  181. ENDFORM.
  182. *&---------------------------------------------------------------------*
  183. *&      Module  USER_COMMAND_0100  INPUT
  184. *&---------------------------------------------------------------------*
  185. *       text
  186. *----------------------------------------------------------------------*
  187. MODULE USER_COMMAND_0100 INPUT.
  188. SAVE_OK = OK_CODE.
  189. CLEAR OK_CODE.
  190. CASE SAVE_OK.
  191.   WHEN 'EXIT'.
  192.     LEAVE PROGRAM.
  193. ENDCASE.
  194. ENDMODULE.                 " USER_COMMAND_0100  INPUT
复制代码




回复

使用道具 举报

快速回帖

本版积分规则
您需要登录后才可以回帖 登录 | 注册有礼

快速回复 返回顶部 返回列表