天天看點

WDA 中的Helper Class

本文截自 Web_Dynpro_for_ABAP_Tips_and_Tricks(已經上傳,有興趣的可以Google或者在我的資源中下載下傳 http://download.csdn.net/detail/wren2004/5246372)

先看下代碼對比,體會一下OO的妙處吧

向導生成的代碼:

WDA 中的Helper Class

使用Helper Class後的代碼:

WDA 中的Helper Class

添加helper Class 引用:

WDA 中的Helper Class

Helper Class 初始化:

WDA 中的Helper Class

完整代碼:

class ZBITS_WD_CONTEXT_HELPER definition

  public

  create public .

*"* public components of class ZBITS_WD_CONTEXT_HELPER

*"* do not include other source files here!!!

public section.

  data O_CONTEXT type ref to IF_WD_CONTEXT_NODE .

  methods GET_ATTRIBUTE

    importing

      !PATH type STRING

    exporting

      value(VALUE) type DATA .

  methods GET_ATTRIBUTES

    importing

      !PATH type STRING

    exporting

      !VALUES type DATA .

  methods SET_ATTRIBUTE

    importing

      !PATH type STRING

      !VALUE type DATA .

  methods SET_ATTRIBUTES

    importing

      !PATH type STRING

      !VALUES type DATA .

  methods GET_STATIC_ATTRIBUTES_TABLE

    importing

      !PATH type STRING

      !FROM type I default 1

      !TO type I default 2147483647

    exporting

      !TABLE type ANY TABLE .

  type-pools ABAP .

  methods BIND_TABLE

    importing

      !PATH type STRING

      !NEW_ITEMS type ANY TABLE

      !SET_INITIAL_ELEMENTS type ABAP_BOOL default ABAP_TRUE

      !INDEX type I optional .

  methods IS_CHANGED_BY_CLIENT

    importing

      !PATH type STRING

    returning

      value(FLAG) type ABAP_BOOL .

  methods GET_NODE

    importing

      !PATH type STRING

    returning

      value(RESULT) type ref to IF_WD_CONTEXT_NODE .

  methods SET_CONTEXT

    importing

      !CONTEXT type ref to IF_WD_CONTEXT_NODE .

  methods CONSTRUCTOR

    importing

      !CONTEXT type ref to IF_WD_CONTEXT_NODE optional .

  methods SET_LEAD_SELECTION

    importing

      !PATH type STRING

      !ELEMENT type ref to IF_WD_CONTEXT_ELEMENT .

  methods RESET_CHANGED_BY_CLIENT

    importing

      !PATH type STRING .

  methods SET_CHANGED_BY_CLIENT

    importing

      !PATH type STRING

      !FLAG type ABAP_BOOL .

*"* private components of class ZBITS_WD_CONTEXT_HELPER

*"* do not include other source files here!!!

private section.

  methods GET_ELEMENT_PATH

    importing

      !PATH type STRING

    exporting

      !ELPATH type STRING

      !ATTRIBUTE type DATA .

  methods GET_ELEMENT

    importing

      !PATH type STRING

    returning

      value(ELEMENT) type ref to IF_WD_CONTEXT_ELEMENT .

method bind_table.

*********************************************************************

  data: lo_node        type ref to   if_wd_context_node.

*********************************************************************

  " Validate necessary object initialisation

  check o_context is bound.

  " Get the element

  lo_node = get_node( path ).

  lo_node->bind_table( exporting new_items = new_items set_initial_elements = set_initial_elements index = index ).

endmethod.

method CONSTRUCTOR.

  o_context = context.

endmethod.

method GET_ATTRIBUTE.

*********************************************************************

  DATA: lv_elpath      TYPE          string,

        lv_attribute   TYPE          string,

        lo_element     TYPE REF TO   if_wd_context_element.

*********************************************************************

  " Validate necessary object initialisation

  CHECK o_context IS BOUND.

  " Determine element path and attribute name

  get_element_path( EXPORTING path = path IMPORTING elpath = lv_elpath attribute = lv_attribute ).

  " Get the element

  lo_element = get_element( lv_elpath ).

  lo_element->get_attribute( EXPORTING name = lv_attribute IMPORTING value = value ).

endmethod.

method get_attributes.

*********************************************************************

  data: lo_node        type ref to   if_wd_context_node.

*********************************************************************

  " Validate necessary object initialisation

  check o_context is bound.

  " Get the element

  lo_node = get_node( path ).

  lo_node->get_static_attributes( importing static_attributes = values ).

endmethod.

method GET_NODE.

*********************************************************************

  DATA: lv_path        TYPE          string,

        lt_path        TYPE TABLE OF string,

        lv_index       TYPE          i,

        lv_length      TYPE          i,

        lo_node        TYPE REF TO   if_wd_context_node.

*********************************************************************

  " Validate necessary object initialisation

  CHECK me->o_context IS BOUND.

  " Read the path into a table

  SPLIT path AT '/' INTO TABLE lt_path.

  " Get the path depth

  DESCRIBE TABLE lt_path LINES lv_length.

  " Root attributes

  IF lv_length = 0.

    lo_node = me->o_context.

  ENDIF.

  " Follow the path

  lv_index = 0.

  LOOP AT lt_path INTO lv_path.

    lv_index = lv_index + 1.

    IF lv_index = 1.

      " Get first node in the path

      lo_node = me->o_context->get_child_node( lv_path ).

    ELSE.

      " Get next node down the path

      lo_node = lo_node->get_child_node( lv_path ).

    ENDIF.

  ENDLOOP.

  result = lo_node.

endmethod.

method get_static_attributes_table.

*********************************************************************

  data: lo_node        type ref to   if_wd_context_node.

*********************************************************************

  " Validate necessary object initialisation

  check o_context is bound.

  " Get the element

  lo_node = get_node( path ).

  lo_node->get_static_attributes_table( exporting from = from to = to importing table = table ).

endmethod.

method IS_CHANGED_BY_CLIENT.

*********************************************************************

  data: lo_node        type ref to   if_wd_context_node.

*********************************************************************

  " Validate necessary object initialisation

  check o_context is bound.

  " Get the element

  lo_node = get_node( path ).

  flag = lo_node->IS_CHANGED_BY_CLIENT( ).

endmethod.

method RESET_CHANGED_BY_CLIENT.

*********************************************************************

  data: lo_node        type ref to   if_wd_context_node.

*********************************************************************

  " Validate necessary object initialisation

  check o_context is bound.

  " Get the element

  lo_node = get_node( path ).

  lo_node->RESET_CHANGED_BY_CLIENT( ).

endmethod.

method SET_ATTRIBUTE.

*********************************************************************

  DATA: lv_elpath      TYPE          string,

        lv_attribute   TYPE          string,

        lo_element     TYPE REF TO   if_wd_context_element.

*********************************************************************

  " Validate necessary object initialisation

  CHECK o_context IS BOUND.

  " Determine element path and attribute name

  get_element_path( EXPORTING path = path IMPORTING elpath = lv_elpath attribute = lv_attribute ).

  " Get the element

  lo_element = get_element( lv_elpath ).

  lo_element->set_attribute( EXPORTING name = lv_attribute value = value ).

endmethod.

method set_attributes.

*********************************************************************

  data: lo_node        type ref to   if_wd_context_node.

*********************************************************************

  " Validate necessary object initialisation

  check o_context is bound.

  " Get the element

  lo_node = get_node( path ).

  lo_node->set_static_attributes( exporting static_attributes = values ).

endmethod.

method SET_CHANGED_BY_CLIENT.

*********************************************************************

  data: lo_node        type ref to   if_wd_context_node.

*********************************************************************

  " Validate necessary object initialisation

  check o_context is bound.

  " Get the element

  lo_node = get_node( path ).

  lo_node->SET_CHANGED_BY_CLIENT( flag ).

endmethod.

method SET_CONTEXT.

 o_context = context.

endmethod.

method set_lead_selection.

*********************************************************************

  data: lo_node        type ref to   if_wd_context_node.

*********************************************************************

  " Validate necessary object initialisation

  check o_context is bound.

  " Get the element

  lo_node = get_node( path ).

  lo_node->set_lead_selection( exporting element = element ).

endmethod.

method GET_ELEMENT.

*********************************************************************

  DATA: lo_node        TYPE REF TO   if_wd_context_node.

*********************************************************************

  " Validate necessary object initialisation

  CHECK me->o_context IS BOUND.

  lo_node = get_node( path ).

  element = lo_node->get_element(  ).

endmethod.

method GET_ELEMENT_PATH.

*********************************************************************

  DATA: lv_path        TYPE          string,

        lt_path        TYPE TABLE OF string,

        lv_index       TYPE          i,

        lv_length      TYPE          i,

        lv_lengm1      TYPE          i.

*********************************************************************

  " Read the path into a table

  SPLIT path AT '/' INTO TABLE lt_path.

  " Get the path depth

  DESCRIBE TABLE lt_path LINES lv_length.

  lv_lengm1 = lv_length - 1.

  " Follow the path

  lv_index = 0.

  LOOP AT lt_path INTO lv_path.

    lv_index = lv_index + 1.

    IF lv_index < lv_length.

      CONCATENATE elpath lv_path INTO elpath.

      IF lv_index < lv_lengm1.

        CONCATENATE elpath '/' INTO elpath.

      ENDIF.

    ELSE.

      attribute = lv_path.

    ENDIF.

  ENDLOOP.

endmethod.