Skip to Content
SAP Visual Business

ABAP Development: HowTo add a Context Menu

Tags:

As described in the ABAP Development: HowTo handle frontend events document, there are multiple options to receive and handle event from the GeoMap component. In this example we focus only on the implementation in an FPM environment. Despite the differences in how the event handler get invoked and the event data is passed, the actual event handling is equal in all cases.

Important notice

In case the event is handled directly in method HANDLE_EVENT or in the Service Provider HANDLE_MAP_EVENT you need to push the context menu to the frontend by calling method CONTROL_ADAPTER->PUSH_CONTEXT_MENU( LV_CTMENU ). In the special event handler GET_CONTEXT_MENU this is not the case!

The requirement for this extra call in the internal handling gives you the freedom for alternate reactions of the application to the given frontend event. If you find this a better UI approach you may open a popup instead of a context menu.

Handling the context menu request

Pressing the right mouse button leads to backend event IF_VBI_CONST=>GC_CONTROL_EVENT-CT_MENU_REQ. This event is per default passed on to method GET_CONTEXT_MENU. The associated data is passed as import parameters to this specific handler method. You receive the following information:

  • IV_OBJECT_TYPE: type of the object the event is raised for. Object types are defined as constants in interface IF_VBI_CONST attribute structure GC_SCENE_OBJECT_TYPE.
  • IV_OBJECT_GUID: GUID of the object the event is raised for
  • IS_POINTER_POS: position of the mouse pointer in 3D-/geo-coordinates when the event was raised.

The GET_CONTEXT_MENU method is expected to return an instance of CL_CTMENU. Class CL_CTMENU is probably already well known from the classical SAPGUI programming. It allows to interactively define a context menu with multiple levels. You simply define the actual context menu within this instance and the Visual Business component will display it according to your definitions.


Information

The context menu is always defined fully dynamic by the application. However, the class supports also the use of templates via method ADD_MENU. With this method you can copy the definitions from an existing instance to the one passed in the event, which allows you to hold context menu templates in your application code and to simply copy them to the actual menu.

Further it is also possible to define context menus statically in a GUI status. Class CL_CTMENU supports also loading the context menu from a given GUI status with method LOAD_GUI_STATUS.

Context menu for an object

You receive type and GUID of the object the event was raised on. Base on this information you can dynamically create a context menu specifically for this object instance.

For each entry in the context menu you need to provide an application defined FCODE, which can later be handled in method HANDLE_FCODE.
The following code sample from the sample program VBI_GUI_TEST shows the event handling and the dynamic definition of a context menu:


   data: LV_SUBMENU type ref to CL_CTMENU,

   LV_SUBSUBMENU type ref to CL_CTMENU.

...

* remember CT Menu position for call back functions

   ME->CT_MENU_POS = IS_POINTER_POS.

   create object RV_RESULT.

...

   if IV_OBJECT_TYPE = IF_VBI_CONST=>GC_SCENE_OBJECT_TYPE-MAP.

   RV_RESULT->ADD_FUNCTION( FCODE = 'CREATE_SPOT'

   TEXT = 'Create Spot at position'(060) ).

   create object LV_SUBMENU.

   LV_SUBMENU->ADD_FUNCTION( FCODE = 'CREATE_GEOCIRC_1'

   TEXT = '1 km'(080) ).

   LV_SUBMENU->ADD_FUNCTION( FCODE = 'CREATE_GEOCIRC_2'

   TEXT = '10 km'(081) ).

   LV_SUBMENU->ADD_FUNCTION( FCODE = 'CREATE_GEOCIRC_3'

   TEXT = '100 km'(082) ).

   RV_RESULT->ADD_SUBMENU( MENU = LV_SUBMENU

   TEXT = 'Create perimeter'(083) ).

   RV_RESULT->ADD_FUNCTION( FCODE = 'COPY_POSITION'

   TEXT = 'Copy Geo-position'(090) ).

   RV_RESULT->ADD_SEPARATOR( ).

   endif.

...

* Examples for centrally handled scene events

   data: LV_FCODE type UI_FUNC value IF_VBI_CONST=>GC_EVENT-OBJECT_FLYTO.

   RV_RESULT->ADD_FUNCTION( FCODE = LV_FCODE

   TEXT = 'Fly to object'(072) ).

   LV_FCODE = IF_VBI_CONST=>GC_EVENT-OBJECT_DELETE.

   RV_RESULT->ADD_FUNCTION( FCODE = LV_FCODE

   TEXT = 'Delete object'(074) ).

...

Context menu for the map

In general the context menu will be requested for a certain object, given via its GUID. However, the user can also request a context menu on the map itself. In this case we cannot pass a GUID in the event data, but we provide a geo-position as IS_POINTER_POS. The given position is determine from the back-projection of the mouse pointer position onto the map. With this information it is possible for application to provide the interactive creation of objects on the the map. The user will not need to enter geo-coordinates or address data. He just click on the position at the map.

Important notice

Since the actual creation function needs to be selected by the user from the context menu, the creation can only be executed on the second round trip in the FCode handling. For this event we cannot provide the geo-position, sinse the mouse pointer has already been move. Thus it is absolutely necessary to remember the position on application side from the context menu creation event until the FCode handling. 

Handling selected functions

After the context menu is presented to the user he can select one of the provided functions. This selection leads to backend event which ID is the FCODE of the selected function.
Per default this event is passed on to handler method HANDLE_FCODE and the actual FCODE is given as import parameter IV_FCODE. The following code sample from program VBI_GUI_TEST shows the a possible handling of standard functions:


   case IV_FCODE.

...

   when 'CREATE_SPOT'.

   data: LS_DATA type VBI_S_GUI_TEST_UI.

   LS_DATA-OBJECT_ID = VBI_S_GUI_TEST_UI-OBJECT_ID.

   LS_DATA-TOOLTIP = VBI_S_GUI_TEST_UI-TOOLTIP.

   LS_DATA-SPOT_POS_LON = ME->CT_MENU_POS-XPOS.

   LS_DATA-SPOT_POS_LAT = ME->CT_MENU_POS-YPOS.

   LS_DATA-ROLE = VBI_S_GUI_TEST_UI-ROLE.

   ME->ADD_SPOT( LS_DATA ).

...

   when 'SELECT'.

   message S001(VBI_GUI_TEST) with IV_FCODE.

   ME->SERVICE_PROVIDER->HANDLE_SCENE_EVENT( IV_EVENT_ID = IF_VBI_CONST=>GC_EVENT-DISPLAY_SCENE ).

   when others.

...

   endcase.

   endcase.

   clear ME->CT_MENU_POS.

No comments