3

我有一个实例CL_SIMPLE_TREE_MODEL,我设法触发并处理了CONTEXT_MENU_REQUEST事件,并构建了我的上下文菜单。

我按照我想要的方式添加了我的功能。

问题是,当我从上下文菜单中选择一个选项时,什么也没有发生。换句话说,程序流不会进入CONTEXT_MENU_SELECT.

我当然假设当我单击上下文菜单中的功能时会触发此事件。

我找到了官方文档,但仅适用于您使用 Shift+F10 访问的“默认上下文菜单”,它需要程序中的某些子例程才能触发。

我做了什么:

  1. 我确实定义并实现了一个方法,该方法是该事件的处理程序。
  2. 我确实在为事件设置处理程序的同一位置设置了CONTEXT_MENU_REQUEST事件处理程序
  3. 我没有注册该事件,SET_REGISTERED_EVENTS因为在该事件的类属性中没有定义任何 ID - 例如该CONTEXT_MENU_REQUEST事件。

编码:

REPORT.

CLASS lcl_tree_handler DEFINITION.

  PUBLIC SECTION.
    METHODS:
      pbo,
      on_ctx_menu_request FOR EVENT node_context_menu_request OF cl_simple_tree_model
        IMPORTING node_key menu sender, "TYPE TM_NODEKEY CL_CTMENU
      on_ctx_menu_select FOR EVENT node_context_menu_select OF cl_simple_tree_model
        IMPORTING node_key fcode. "TYPE TM_NODEKEY SY-UCOMM
    DATA:
      po_tree_model TYPE REF TO cl_simple_tree_model,
      gt_tree       TYPE TABLE OF treemsnodt,
      control       TYPE REF TO cl_gui_control.
ENDCLASS.

CLASS lcl_tree_handler IMPLEMENTATION.

  METHOD pbo.
    DATA: lt_events TYPE cntl_simple_events,
          ls_event  TYPE cntl_simple_event.
    FIELD-SYMBOLS <gs_tree> TYPE treemsnodt.

    CHECK po_tree_model IS NOT BOUND.

    CREATE OBJECT po_tree_model
      EXPORTING
        node_selection_mode = po_tree_model->node_sel_mode_single.

    APPEND INITIAL LINE TO gt_tree ASSIGNING <gs_tree>.
    <gs_tree>-node_key = 'Node key 1'.
    <gs_tree>-text = 'First node'.
    <gs_tree>-isfolder = 'X'.
    APPEND INITIAL LINE TO gt_tree ASSIGNING <gs_tree>.
    <gs_tree>-node_key = 'Node key 2'.
    <gs_tree>-relatkey = 'Node key 1'.
    <gs_tree>-relatship = cl_tree_model=>relat_last_child.
    <gs_tree>-text = 'First child'.
    po_tree_model->add_nodes(
          node_table = gt_tree ).

    ls_event-eventid = cl_simple_tree_model=>eventid_node_context_menu_req.
    ls_event-appl_event = 'X'.              "tried with space too
    APPEND ls_event TO lt_events.

    CALL METHOD po_tree_model->set_registered_events
      EXPORTING
        events = lt_events.

    SET HANDLER on_ctx_menu_request FOR po_tree_model.
    SET HANDLER on_ctx_menu_select FOR po_tree_model.

    po_tree_model->create_tree_control(
      EXPORTING
        parent = cl_gui_container=>screen0
      IMPORTING
        control = control ).

  ENDMETHOD.

  METHOD on_ctx_menu_request.                 "I initialize the context menu object here.
    DATA: lt_chidren_keys TYPE treemnotab,
          ls_child_key    TYPE tm_nodekey,
          lv_text         TYPE gui_text.

    CALL METHOD sender->node_get_children
      EXPORTING
        node_key       = node_key
      IMPORTING
        node_key_table = lt_chidren_keys
      EXCEPTIONS
        OTHERS         = 2.

    LOOP AT lt_chidren_keys INTO ls_child_key.
      lv_text = ls_child_key.
      CALL METHOD menu->add_function
        EXPORTING
          fcode = 'ONE'
          text  = lv_text
          ftype = 'B'.
    ENDLOOP.

    menu->add_separator( ).
    CALL METHOD menu->add_function
      EXPORTING
        fcode = 'ALL'
        text  = 'All the work groups'
        ftype = 'W'.

  ENDMETHOD.

  METHOD on_ctx_menu_select.
    BREAK-POINT.                 "tried actual code here too.
  ENDMETHOD.

ENDCLASS.


DATA: go_tree_handler TYPE REF TO lcl_tree_handler.

PARAMETERS dummy.

INITIALIZATION.
  CREATE OBJECT go_tree_handler.

AT SELECTION-SCREEN OUTPUT.
  go_tree_handler->pbo( ).

AT SELECTION-SCREEN ON EXIT-COMMAND.
  go_tree_handler->control->free( ).

显示树,右键单击上下文菜单。

但是当我选择一个菜单项时没有任何反应。我错过了什么吗?

4

1 回答 1

2

不支持您指定的函数类型 (ftype = 'W'和)(检查底层域的固定值)。在这种情况下,什么都不会发生。'B'CUA_FUNTYP

经典的解决方案是使用ftype = ' '(正常功能):

    LOOP AT lt_chidren_keys INTO ls_child_key.
      lv_text = ls_child_key.
      CALL METHOD menu->add_function
        EXPORTING
          fcode = 'ONE'
          text  = lv_text
          ftype = ' '.
    ENDLOOP.

    menu->add_separator( ).
    CALL METHOD menu->add_function
      EXPORTING
        fcode = 'ALL'
        text  = 'All the work groups'
        ftype = ' '.

可能的ftype值是(来源:域CUA_FUNTYP):

  • ' ' : 正常功能
  • 'H' : 帮助功能 (PROCESS ON HELP REQUEST)
  • 'S' : 系统函数(由 DYNP 直接处理)
  • 'T' : 交易调用 (LEAVE TO TRANSACTION)
  • 'E' : 'AT EXIT COMMAND' 的访问模块 -> /E 作为前缀
  • 'I' :包括菜单(在运行时替换 - 不支持)
  • 'N' : 'AT EXIT COMMAND' 功能,> DYNP > /N 作为前缀

备注:问题的第一个版本中的代码缺少第二个事件处理程序的注册SET HANDLER go_tree_handler->on_ctx_menu_select FOR po_tree_model.(现在可以了)

于 2012-11-07T10:51:44.933 回答