SAP ABAP OBJECTS Tutorials

Example 7: Using multiple controls

In this example a second TextEdit control will be added to the screen. The new TextEdit control will be designed to act as a clipboard for short texts.

Steps:

Add a new container to the screen and name it MYCONTAINER2.

Code:

Insert global datadeclaration:

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

* Implementing a second Scratch TextEdit control

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

DATA:

scratch            TYPE REF TO cl_gui_textedit,

custom_container2  TYPE REF TO cl_gui_custom_container.

Insert the following code in the PBO module:

*——————————————————

* The SCRATCH TextEdit control

*——————————————————

IF scratch IS INITIAL.

*   Create obejct for custom container2

CREATE OBJECT custom_container2

EXPORTING

container_name              = ‘MYCONTAINER2’

EXCEPTIONS

cntl_error                  = 1

cntl_system_error           = 2

create_error                = 3

lifetime_error              = 4

lifetime_dynpro_dynpro_link = 5

others                      = 6

.

IF sy-subrc <> 0.

MESSAGE ID sy-msgid TYPE ‘I’ NUMBER sy-msgno

WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.

ENDIF.

*   Create obejct for the SCRATCH TextEditor control

CREATE OBJECT scratch

EXPORTING

parent         = custom_container2

wordwrap_mode  =

cl_gui_textedit=>wordwrap_at_windowborder

wordwrap_to_linebreak_mode = cl_gui_textedit=>true.

*   Remove the staus bar

CALL METHOD scratch->set_statusbar_mode

EXPORTING statusbar_mode = cl_gui_textedit=>false.

ENDIF.

Result:

1  REPORT sapmz_hf_controls1 .

2

3  CONSTANTS:

4    line_length TYPE i VALUE 254.

5

6  DATA: ok_code LIKE sy-ucomm.

7

8  DATA:

9  * Create reference to the custom container

10    custom_container TYPE REF TO cl_gui_custom_container,

11  * Create reference to the TextEdit control

12    editor TYPE REF TO cl_gui_textedit,

13    repid LIKE sy-repid.

14

15  **********************************************************************

16  * Utillity table to load texts

17  **********************************************************************

18  TYPES:

19     BEGIN OF t_texttable,

20       line(line_length) TYPE c,

21     END OF t_texttable.

22

23  DATA:

24    i_texttable TYPE TABLE OF t_texttable,

25    wa_texttable TYPE t_texttable,

26    g_loaded(1) TYPE c.

27

28  **********************************************************************

29  * Data for the protection example

30  **********************************************************************

31  DATA:

32    from_idx TYPE i,

33    to_idx   TYPE i,

34    index    TYPE i.

35

36  **********************************************************************

37  * Implementing a second Scratch TextEdit control

38  **********************************************************************

39  DATA:

40    scratch            TYPE REF TO cl_gui_textedit,

41    custom_container2  TYPE REF TO cl_gui_custom_container.

42

43

44  **********************************************************************

45  * Implementing events

46  **********************************************************************

47  DATA:

48    event_type(20) TYPE c,

49  * Internal table for events that should be registred

50    i_events TYPE cntl_simple_events,

51  * Structure for oneline of the table

52    wa_events TYPE cntl_simple_event.

53

54  *———————————————————————*

55  *       CLASS lcl_event_handler DEFINITION

56  *———————————————————————*

57  *       ……..                                                      *

58  *———————————————————————*

59  CLASS lcl_event_handler DEFINITION.

60    PUBLIC SECTION.

61      CLASS-METHODS:

62        catch_dblclick FOR EVENT dblclick

63           OF cl_gui_textedit IMPORTING sender.

64

65  ENDCLASS.

66

67  *———————————————————————*

68  *       CLASS lcl_event_handler IMPLEMENTATION

69  *———————————————————————*

70  *       ……..                                                      *

71  *———————————————————————*

72  CLASS lcl_event_handler IMPLEMENTATION.

73    METHOD catch_dblclick.

74      DATA:

75        from_line TYPE i,

76        from_pos  TYPE i,

77        to_line TYPE i,

78        to_pos TYPE i.

79

80

81  * Used for the sytem event

82      CALL METHOD cl_gui_cfw=>set_new_ok_code

83        EXPORTING new_code = ‘SHOW’.

84

85

86  * Read the position of the double click

87      CALL METHOD sender->get_selection_pos

88        IMPORTING

89           from_line = from_line

90           from_pos  = from_pos

91           to_line   = to_line

92           to_pos    = to_pos.

93

94  *   Texts in the TextEdit control can have been changed, so

95  *   first reload text from the control into the internal

96  *   table that contains text

97      IF NOT g_loaded IS INITIAL.

98        CALL METHOD sender->get_text_as_r3table

99             IMPORTING table = i_texttable.

100  *   Read the line of the internal table that was clicked

101        READ TABLE i_texttable INDEX from_line INTO wa_texttable.

102        IF sy-subrc <> 0.

103          EXIT.

104        ENDIF.

105

106        IF wa_texttable+0(1) CS ‘*’.

107          SHIFT wa_texttable.

108        ELSEIF wa_texttable+0(1) NS ‘*’.

109          SHIFT wa_texttable RIGHT.

110          wa_texttable+0(1) = ‘*’.

111        ENDIF.

112        MODIFY i_texttable FROM wa_texttable INDEX from_line.

113  *     Reload texts from h einternal table

114        PERFORM load_texts.

115

116

117      ENDIF.

118

119

120    ENDMETHOD.

121  ENDCLASS.

122

123

124

125  START-OF-SELECTION.

126    CLEAR wa_events.

127    REFRESH: i_events.

128

129    SET SCREEN ‘100’.

130

131

132

133  *———————————————————————*

134  *       MODULE USER_COMMAND_0100 INPUT                                *

135  *———————————————————————*

136  MODULE user_command_0100 INPUT.

137

138    CASE ok_code.

139      WHEN ‘EXIT’.

140        LEAVE TO SCREEN 0.

141      WHEN ‘SHOW’.

142        event_type = ‘System dblclick’.

143      WHEN ‘IMP’.

144        PERFORM load_texts.

145      WHEN ‘PROTECT’.

146        PERFORM protect.

147

148      WHEN OTHERS.

149  *    CALL METHOD cl_gui_cfw=>dispatch. “Not used for system events

150    ENDCASE.

151

152

153  ENDMODULE.                 ” USER_COMMAND_0100  INPUT

154  *&———————————————————————*

155  *&      Module  STATUS_0100  OUTPUT

156  *&———————————————————————*

157  MODULE status_0100 OUTPUT.

158  * The TextEdit control shoul only be initialized the first time the

159  * PBO module executes

160    IF editor IS INITIAL.

161      repid = sy-repid.

162  *   Create obejct for custom container

163      CREATE OBJECT custom_container

164        EXPORTING

165          container_name              = ‘MYCONTAINER1’

166        EXCEPTIONS

167          cntl_error                  = 1

168          cntl_system_error           = 2

169          create_error                = 3

170          lifetime_error              = 4

171          lifetime_dynpro_dynpro_link = 5

172          others                      = 6

173          .

174      IF sy-subrc <> 0.

175        MESSAGE ID sy-msgid TYPE ‘I’ NUMBER sy-msgno

176                   WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.

177      ENDIF.

178

179  *   Create obejct for the TextEditor control

180      CREATE OBJECT editor

181        EXPORTING

182

183           wordwrap_mode          =

184                  cl_gui_textedit=>wordwrap_at_fixed_position

185           wordwrap_position      = line_length

186           wordwrap_to_linebreak_mode = cl_gui_textedit=>true

187          parent                  = custom_container

188        EXCEPTIONS

189          error_cntl_create      = 1

190          error_cntl_init        = 2

191          error_cntl_link        = 3

192          error_dp_create        = 4

193          gui_type_not_supported = 5

194          others                 = 6

195          .

196      IF sy-subrc <> 0.

197        MESSAGE ID sy-msgid TYPE ‘I’ NUMBER sy-msgno

198                   WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.

199      ENDIF.

200

201  *   Link the event handler method to the event and the

202  *   TextEdit control

203      SET HANDLER lcl_event_handler=>catch_dblclick FOR editor.

204

205  *   Register the event in the internal table i_events

206      wa_events-eventid = cl_gui_textedit=>event_double_click.

207

208  *    wa_events-appl_event = ‘X’. “This is an application event

209      wa_events-appl_event = space. “This is a system event

210

211

212      APPEND wa_events TO i_events.

213  *   Pass the table to the TextEdit control uding method

214  *   set_registred_events

215      CALL METHOD editor->set_registered_events

216         EXPORTING events = i_events.

217

218  * Create internal table with texts taht can be uploaded to

219  * the TextEdit control

220    APPEND ‘This a method that fills the TextEdit control’ TO i_texttable.

221      APPEND ‘with a text.’ TO i_texttable.

222      DO 10 TIMES.

223        APPEND ‘hallo world !’ TO i_texttable.

224      ENDDO.

225    ENDIF.

226

227  *——————————————————

228  * The SCRATCH TextEdit control

229  *——————————————————

230    IF scratch IS INITIAL.

231  *   Create obejct for custom container2

232      CREATE OBJECT custom_container2

233        EXPORTING

234          container_name              = ‘MYCONTAINER2’

235        EXCEPTIONS

236          cntl_error                  = 1

237          cntl_system_error           = 2

238          create_error                = 3

239          lifetime_error              = 4

240          lifetime_dynpro_dynpro_link = 5

241          others                      = 6

242          .

243      IF sy-subrc <> 0.

244        MESSAGE ID sy-msgid TYPE ‘I’ NUMBER sy-msgno

245                   WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.

246      ENDIF.

247

248  *   Create obejct for the SCRATCH TextEditor control

249      CREATE OBJECT scratch

250        EXPORTING

251           parent         = custom_container2

252           wordwrap_mode  =

253                  cl_gui_textedit=>wordwrap_at_windowborder

254          wordwrap_to_linebreak_mode = cl_gui_textedit=>true.

255

256  *   Remove the staus bar

257      CALL METHOD scratch->set_statusbar_mode

258        EXPORTING statusbar_mode = cl_gui_textedit=>false.

259

260    ENDIF.

261

262

263  ENDMODULE.                 ” STATUS_0100  OUTPUT

264

265  *&———————————————————————*

266  *&      Form Load_texts

267  *&———————————————————————*

268  * This form loads the lines of the internal table i_texttable into

269  * the TextEdit control

270  *———————————————————————-*

271  FORM load_texts.

272

273  * Load TextEdit control with texts

274    CALL METHOD editor->set_text_as_r3table

275      EXPORTING table = i_texttable.

276    IF sy-subrc > 0.

277  *   Display an error message

278      EXIT.

279    ENDIF.

280

281  * All methods that operates on controls are transferred to the frontend

282  * by a RFC calls. the method FLUSH is used to determine when this is

283  * done.

284    CALL METHOD cl_gui_cfw=>flush.

285    IF sy-subrc > 0.

286  *   Display an error message

287    ENDIF.

288    g_loaded = ‘X’.

289  ENDFORM.                    ” create_texts

290  *&———————————————————————*

291  *&      Form  protect

292  *&———————————————————————*

293  * Protects marked lines in a TextEdit control

294  *———————————————————————-*

295  FORM protect.

296  * Determine the area selected by the user

297    CALL METHOD editor->get_selection_pos

298       IMPORTING

299         from_line = from_idx

300         to_line   = to_idx

301       EXCEPTIONS

302         error_cntl_call_method = 1.

303

304  * Synchronize execution in the control with the ABAP program.

305  * Without this synchronization the variables from_idx and

306  * to_idx will have obsolutete values (The initial value for

307  * both, are 0)

308    CALL METHOD cl_gui_cfw=>flush.

309    IF sy-subrc > 0.

310  * Errormessage: Error in flush

311    ENDIF.

312

313  * Protect the lines selected

314    IF to_idx > from_idx.

315      to_idx = to_idx – 1.

316    ENDIF.

317    CALL METHOD editor->protect_lines

318      EXPORTING

319        from_line = from_idx

320        to_line   = to_idx.

321

322

323  * The PROTECT_SELECTION method could be used instead, eliminating the

324  * need of line numbers and the last FLUSH

325

326  * call method editor->protect_selection.

327

328

329  * Flush again to protect immidately

330    CALL METHOD cl_gui_cfw=>flush.

331    IF sy-subrc > 0.

332  * Errormessage: Error in flush

333    ENDIF.

334

335

336

337  ENDFORM.                    ” protect

Print Friendly, PDF & Email

[ FREE ] SUBSCRIBE
RECEIVE UPDATES & SAP ARTICLES RIGHT IN YOUR INBOX.