Friday, September 16, 2011

Add Image in Selection Screen - Source Code

Pre-requisite: MIME object should be present in SAP i.e. image should be uploaded in SAP through transaction SMW0. For details, Click here


*&---------------------------------------------------------------------*
*& Report ZSELSCR
*&---------------------------------------------------------------------*

REPORT zselscr.

*===Data Declaration
DATA: o_docking TYPE REF TO cl_gui_docking_container,
            o_picctrl TYPE REF TO cl_gui_picture,
            v_url(256) TYPE c,
            v_rcode TYPE w3param-ret_code,
            v_type TYPE w3param-cont_type,
            v_length TYPE w3param-cont_len.

*===Constants Declaration
CONSTANTS: c_name TYPE w3query-name VALUE '_OBJECT_ID',
                     c_mimeobj TYPE w3query-value VALUE 'ZSELSCRIMG'. "SMW0 Object Name

*===Internal Table and Work Area Declaration
DATA: t_query TYPE TABLE OF w3query,
            t_html TYPE TABLE OF w3html,
            t_picdata TYPE TABLE OF w3mime,
            w_query TYPE w3query.

*===Selection Screen
SELECTION-SCREEN: BEGIN OF BLOCK b1 WITH FRAME TITLE text-001 NO INTERVALS.
SELECTION-SCREEN BEGIN OF LINE.
SELECTION-SCREEN COMMENT 4(25) text-c01. "URL
SELECTION-SCREEN POSITION 30.
PARAMETERS: p_url(50) OBLIGATORY VISIBLE LENGTH 25.
SELECTION-SCREEN END OF LINE.
SELECTION-SCREEN: END OF BLOCK b1.

*===At Selection Screen Event
AT SELECTION-SCREEN OUTPUT.

PERFORM sub_show_image.

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

*===Subroutine
*&-------------------------------------------------------------------
*& Form sub_show_image
*&-------------------------------------------------------------------
FORM sub_show_image.

*===Create Container for Image
CREATE OBJECT o_picctrl
 EXPORTING
  parent = o_docking.

CHECK sy-subrc = 0.

*===Set display mode
CALL METHOD o_picctrl->set_display_mode
 EXPORTING
  display_mode = cl_gui_picture=>display_mode_stretch.

*===Set position of Image in Selection Screen
CALL METHOD o_picctrl->set_position
 EXPORTING
  height = 70
  left = 475
  top = 00
  width = 150.

IF v_url IS INITIAL.
 CLEAR: t_query, w_query.
  w_query-name = c_name.
  w_query-value = c_mimeobj.
  APPEND w_query TO t_query.

*===Get MIME object from tcode SMW0
CALL FUNCTION 'WWW_GET_MIME_OBJECT'
TABLES
  query_string = t_query
  html = t_html
  mime = t_picdata
CHANGING
  return_code = v_rcode
  content_type = v_type
  content_length = v_length
EXCEPTIONS
  object_not_found = 1
  parameter_not_found = 2
  OTHERS = 3.

*===Create URL for MIME object
CALL FUNCTION 'DP_CREATE_URL'
EXPORTING
  type = 'Image'
  subtype = cndp_sap_tab_unknown "X-UNKNOWN
TABLES
  data = t_picdata
CHANGING
  url = v_url
EXCEPTIONS
  OTHERS = 1.
ENDIF.

*===Load Picture
CALL METHOD o_picctrl->load_picture_from_url
EXPORTING
  url = v_url.

ENDFORM. "sub_show_image

No comments:

Post a Comment