|
1 /* |
|
2 * Copyright (c) 2009-2010 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 */ |
|
17 |
|
18 /** |
|
19 * import midp classes. |
|
20 */ |
|
21 import javax.microedition.midlet.*; |
|
22 import javax.microedition.lcdui.*; |
|
23 |
|
24 /** |
|
25 * This class is to test all the StringItem tests |
|
26 */ |
|
27 |
|
28 public class FormStringItemTests extends Form implements CommandListener, ItemCommandListener |
|
29 { |
|
30 |
|
31 //referemce to the parent MIDlet |
|
32 private Midp_StringItem_01 m = null; |
|
33 |
|
34 //the form to add the StringItems |
|
35 private Form stringItemForm = null; |
|
36 |
|
37 //the label to create the StringItem |
|
38 private TextField label = null; |
|
39 |
|
40 //the text to create the StringItem |
|
41 private TextField text = null; |
|
42 |
|
43 //the text to create the StringItem |
|
44 private TextField width = null; |
|
45 //the text to create the StringItem |
|
46 private TextField height = null; |
|
47 |
|
48 //the StringItem to test |
|
49 private StringItem si = null; |
|
50 |
|
51 private ChoiceGroup cg = null; |
|
52 private String[] cgTypes = {"LAYOUT_DEFAULT","LAYOUT_LEFT","LAYOUT_RIGHT","LAYOUT_CENTER","LAYOUT_TOP","LAYOUT_BOTTOM", |
|
53 "LAYOUT_VCENTER","LAYOUT_NEWLINE_BEFORE","LAYOUT_NEWLINE_AFTER","LAYOUT_SHRINK","LAYOUT_VSHRINK", |
|
54 "LAYOUT_EXPAND","LAYOUT_VEXPAND" |
|
55 }; |
|
56 |
|
57 //the command to go back to the main screen |
|
58 private Command cmdBack = new Command("Back", Command.BACK, 1); |
|
59 |
|
60 //the command to create the StringItem with the entered label and text |
|
61 private Command cmdCreate = new Command("Create", Command.SCREEN, 1); |
|
62 |
|
63 //the command to create the StringItem with the entered label and text and with focus |
|
64 private Command cmdCreateHL = new Command("Create as Hyperlink", Command.SCREEN, 1); |
|
65 |
|
66 //the command to create the StringItem with the entered label and text and with focus |
|
67 private Command cmdCreateButton = new Command("Create as Button", Command.SCREEN, 1); |
|
68 |
|
69 //the command to unlock the Item |
|
70 private Command cmdUnlock = new Command("Unlock", Command.SCREEN, 1); |
|
71 //the command for the item |
|
72 private Command cmdItem = new Command("Item cmd", Command.ITEM, 1); |
|
73 |
|
74 //the command to remove/restore the item command or the first item |
|
75 private Command cmdRemoveCommand = new Command("Remove command", Command.SCREEN, 1); |
|
76 private Command cmdRestoreCommand = new Command("Restore command", Command.SCREEN, 1); |
|
77 private Command cmdRemoveItem = new Command("Remove first item", Command.SCREEN, 1); |
|
78 |
|
79 //Command for adding the itemcommandlisteners for the stringitems in layout tests |
|
80 private Command cmdAddListeners = new Command("Add listeners", Command.SCREEN, 1); |
|
81 |
|
82 //the command for layout test |
|
83 private Command cmdLayout = new Command("Layout Test", Command.SCREEN, 1); |
|
84 //the command for layout test |
|
85 private Command cmdLayoutButton = new Command("Layout Button Test", Command.SCREEN, 1); |
|
86 //the command for layout test |
|
87 private Command cmdLayoutHL = new Command("Layout Hyperlink Test", Command.SCREEN, 1); |
|
88 //the command for layout test |
|
89 private Command cmdVLayout = new Command("Vertical Layout Test", Command.SCREEN, 1); |
|
90 //the command for layout test |
|
91 private Command cmdVLayoutHL = new Command("Vertical Layout Hyperlink Test", Command.SCREEN, 1); |
|
92 //the command for layout test |
|
93 private Command cmdVLayoutButton = new Command("Vertical Layout Button Test", Command.SCREEN, 1); |
|
94 //the command for adding a StringItem with long text |
|
95 private Command cmdLongText = new Command("Long text layout test", Command.SCREEN, 1); |
|
96 |
|
97 private Command cmdNext = new Command("Next", Command.SCREEN, 1); |
|
98 private Command cmdExit = new Command("Exit", Command.EXIT, 1); |
|
99 |
|
100 static int change = -1; |
|
101 |
|
102 public FormStringItemTests(Midp_StringItem_01 m) |
|
103 { |
|
104 //set the name of the form to be the name of MIDlet |
|
105 super(m.getClass().getName()); |
|
106 this.m = m; |
|
107 |
|
108 //create TextFields |
|
109 label = new TextField("Label:", null, 200, TextField.ANY); |
|
110 text = new TextField("Text:", null, 200, TextField.ANY); |
|
111 width = new TextField("Set preferred width:", null, 5, TextField.NUMERIC); |
|
112 height = new TextField("Set preferred height:", null, 5, TextField.NUMERIC); |
|
113 cg = new ChoiceGroup("Layout:",ChoiceGroup.EXCLUSIVE,cgTypes,null); |
|
114 append(label); |
|
115 append(text); |
|
116 append(width); |
|
117 append(height); |
|
118 append(cg); |
|
119 addCommand(cmdCreate); |
|
120 addCommand(cmdCreateHL); |
|
121 addCommand(cmdCreateButton); |
|
122 addCommand(cmdLayout); |
|
123 addCommand(cmdLayoutHL); |
|
124 addCommand(cmdLayoutButton); |
|
125 addCommand(cmdVLayout); |
|
126 addCommand(cmdVLayoutHL); |
|
127 addCommand(cmdVLayoutButton); |
|
128 addCommand(cmdLongText); |
|
129 addCommand(cmdExit); |
|
130 setCommandListener(this); |
|
131 |
|
132 Display.getDisplay(m).setCurrent(this); |
|
133 |
|
134 //create StringItemForm |
|
135 stringItemForm = new Form("StringItem"); |
|
136 stringItemForm.addCommand(cmdBack); |
|
137 stringItemForm.setCommandListener(this); |
|
138 } |
|
139 |
|
140 /** |
|
141 * This method handles command invocations. |
|
142 * |
|
143 *@param c This is the command responsible for the event. |
|
144 *@param s Should be equal to this. |
|
145 */ |
|
146 public void commandAction(Command c, Displayable s) |
|
147 { |
|
148 if (c == cmdBack) |
|
149 { |
|
150 label.setString(null); |
|
151 text.setString(null); |
|
152 Display.getDisplay(m).setCurrent(this); |
|
153 } |
|
154 else if (c == cmdLongText) |
|
155 { |
|
156 String longString = "This is a really long string. Check that you can successfully" + |
|
157 " read the last line (congratulations).\n" + |
|
158 "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas" + |
|
159 " tempor nisl in quam aliquet eu posuere eros tempor. Vivamus felis" + |
|
160 " urna, hendrerit id fringilla eu, feugiat blandit tellus. Curabitur" + |
|
161 " scelerisque tempus ante, eu convallis ligula ultrices nec. Nullam" + |
|
162 " vulputate aliquet ullamcorper. Lorem ipsum dolor sit amet," + |
|
163 " consectetur adipiscing elit. Proin at odio vel urna luctus dignissim" + |
|
164 " a quis urna. Lorem ipsum dolor sit amet, consectetur adipiscing elit." + |
|
165 " Phasellus aliquet luctus eros vitae aliquam. Suspendisse ut magna a" + |
|
166 " ipsum fermentum imperdiet. Maecenas eu nunc sed metus placerat" + |
|
167 " adipiscing. Duis lacus lacus, vestibulum id vestibulum a, tempus at" + |
|
168 " nulla. Proin sagittis nibh vitae dui interdum varius. Cras purus" + |
|
169 " nulla, lobortis eu laoreet a, ultricies vitae sem. Integer aliquam" + |
|
170 " ultricies metus. Nulla nec dignissim mi. Nullam risus augue, egestas" + |
|
171 " vitae facilisis eu, adipiscing eu turpis. Nulla convallis semper" + |
|
172 " lobortis. Nullam eget lacus nunc. Nam ipsum lectus, mattis in mollis" + |
|
173 " eu, tristique nec nibh. Nam pellentesque euismod est, a laoreet turpis" + |
|
174 " pulvinar bibendum. Sed malesuada lacus eu ante luctus ut faucibus odio" + |
|
175 " tristique. Donec quis mi ante. Praesent a porta velit. Duis tincidunt" + |
|
176 " velit eget sapien viverra pulvinar.\n" + |
|
177 "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas" + |
|
178 " tempor nisl in quam aliquet eu posuere eros tempor. Vivamus felis" + |
|
179 " urna, hendrerit id fringilla eu, feugiat blandit tellus. Curabitur" + |
|
180 " scelerisque tempus ante, eu convallis ligula ultrices nec. Nullam" + |
|
181 " vulputate aliquet ullamcorper. Lorem ipsum dolor sit amet," + |
|
182 " consectetur adipiscing elit. Proin at odio vel urna luctus dignissim" + |
|
183 " a quis urna. Lorem ipsum dolor sit amet, consectetur adipiscing elit." + |
|
184 " Phasellus aliquet luctus eros vitae aliquam. Suspendisse ut magna a" + |
|
185 " ipsum fermentum imperdiet. Maecenas eu nunc sed metus placerat" + |
|
186 " adipiscing. Duis lacus lacus, vestibulum id vestibulum a, tempus at" + |
|
187 " nulla. Proin sagittis nibh vitae dui interdum varius. Cras purus" + |
|
188 " nulla, lobortis eu laoreet a, ultricies vitae sem. Integer aliquam" + |
|
189 " ultricies metus. Nulla nec dignissim mi. Nullam risus augue, egestas" + |
|
190 " vitae facilisis eu, adipiscing eu turpis. Nulla convallis semper" + |
|
191 " lobortis. Nullam eget lacus nunc. Nam ipsum lectus, mattis in mollis" + |
|
192 " eu, tristique nec nibh. Nam pellentesque euismod est, a laoreet turpis" + |
|
193 " pulvinar bibendum. Sed malesuada lacus eu ante luctus ut faucibus odio" + |
|
194 " tristique. Donec quis mi ante. Praesent a porta velit. Duis tincidunt" + |
|
195 " velit eget sapien viverra pulvinar.\n" + |
|
196 "Now, congratulations if you can read this final line!"; |
|
197 si = new StringItem("The StringItem", longString); |
|
198 stringItemForm.append(si); |
|
199 Display.getDisplay(m).setCurrent(stringItemForm); |
|
200 } |
|
201 else if (c == cmdNext) |
|
202 { |
|
203 change++; |
|
204 changeForm(); |
|
205 } |
|
206 else if (c == cmdRemoveCommand) |
|
207 { |
|
208 Item item = null; |
|
209 item = stringItemForm.get(0); |
|
210 item.removeCommand(cmdUnlock); |
|
211 item.removeCommand(cmdItem); |
|
212 } |
|
213 else if (c == cmdRestoreCommand) |
|
214 { |
|
215 Item item = null; |
|
216 item = stringItemForm.get(0); |
|
217 item.addCommand(cmdUnlock); |
|
218 item.setDefaultCommand(cmdItem); |
|
219 } |
|
220 else if (c == cmdRemoveItem) |
|
221 { |
|
222 stringItemForm.delete(0); |
|
223 } |
|
224 else if (c == cmdLayout) |
|
225 { |
|
226 |
|
227 layoutTest(Item.PLAIN); |
|
228 |
|
229 } |
|
230 else if (c == cmdLayoutHL) |
|
231 { |
|
232 |
|
233 layoutTest(Item.HYPERLINK); |
|
234 |
|
235 } |
|
236 else if (c == cmdLayoutButton) |
|
237 { |
|
238 |
|
239 layoutTest(Item.BUTTON); |
|
240 |
|
241 } |
|
242 else if (c == cmdVLayout) |
|
243 { |
|
244 |
|
245 verticalLayoutTest(Item.PLAIN); |
|
246 |
|
247 } |
|
248 else if (c == cmdVLayoutHL) |
|
249 { |
|
250 |
|
251 verticalLayoutTest(Item.HYPERLINK); |
|
252 |
|
253 } |
|
254 else if (c == cmdVLayoutButton) |
|
255 { |
|
256 |
|
257 verticalLayoutTest(Item.BUTTON); |
|
258 |
|
259 } |
|
260 else if (c == cmdAddListeners) |
|
261 { |
|
262 int size = stringItemForm.size(); |
|
263 for (int i = 0; i < size; i++) |
|
264 { |
|
265 Item item = null; |
|
266 item = stringItemForm.get(i); |
|
267 item.setItemCommandListener(this); |
|
268 } |
|
269 } |
|
270 else if (c == cmdExit) |
|
271 { |
|
272 m.destroyApp(false); |
|
273 m.notifyDestroyed(); |
|
274 } |
|
275 else |
|
276 { |
|
277 String l = label.getString(); |
|
278 String t = text.getString(); |
|
279 int index = cg.getSelectedIndex(); |
|
280 String lo = cg.getString(index); |
|
281 int layout = 0; |
|
282 |
|
283 if (c == cmdCreate) |
|
284 si = new StringItem(l, t); |
|
285 else if (c == cmdCreateHL) |
|
286 { |
|
287 si = new StringItem(l, t, Item.HYPERLINK); |
|
288 si.setDefaultCommand(cmdItem); |
|
289 } |
|
290 else if (c == cmdCreateButton) |
|
291 { |
|
292 si = new StringItem(l, t, Item.BUTTON); |
|
293 si.setDefaultCommand(cmdItem); |
|
294 } |
|
295 |
|
296 if (lo.equals(cgTypes[0])) |
|
297 { |
|
298 layout = Item.LAYOUT_DEFAULT; |
|
299 } |
|
300 else if (lo.equals(cgTypes[1])) |
|
301 { |
|
302 layout = Item.LAYOUT_LEFT; |
|
303 } |
|
304 else if (lo.equals(cgTypes[2])) |
|
305 { |
|
306 layout = Item.LAYOUT_RIGHT; |
|
307 } |
|
308 else if (lo.equals(cgTypes[3])) |
|
309 { |
|
310 layout = Item.LAYOUT_CENTER; |
|
311 } |
|
312 else if (lo.equals(cgTypes[4])) |
|
313 { |
|
314 layout = Item.LAYOUT_TOP; |
|
315 } |
|
316 else if (lo.equals(cgTypes[5])) |
|
317 { |
|
318 layout = Item.LAYOUT_BOTTOM; |
|
319 } |
|
320 else if (lo.equals(cgTypes[6])) |
|
321 { |
|
322 layout = Item.LAYOUT_VCENTER; |
|
323 } |
|
324 else if (lo.equals(cgTypes[7])) |
|
325 { |
|
326 layout = Item.LAYOUT_NEWLINE_BEFORE; |
|
327 } |
|
328 else if (lo.equals(cgTypes[8])) |
|
329 { |
|
330 layout = Item.LAYOUT_NEWLINE_AFTER; |
|
331 } |
|
332 else if (lo.equals(cgTypes[9])) |
|
333 { |
|
334 layout = Item.LAYOUT_SHRINK; |
|
335 } |
|
336 else if (lo.equals(cgTypes[10])) |
|
337 { |
|
338 layout = Item.LAYOUT_VSHRINK; |
|
339 } |
|
340 else if (lo.equals(cgTypes[11])) |
|
341 { |
|
342 layout = Item.LAYOUT_EXPAND; |
|
343 } |
|
344 else if (lo.equals(cgTypes[12])) |
|
345 { |
|
346 layout = Item.LAYOUT_VEXPAND; |
|
347 } |
|
348 si.setItemCommandListener(this); |
|
349 si.setLayout(layout); |
|
350 setSize(width.getString(),height.getString()); |
|
351 stringItemForm.append(si); |
|
352 stringItemForm.addCommand(cmdRemoveCommand); |
|
353 stringItemForm.addCommand(cmdRestoreCommand); |
|
354 stringItemForm.addCommand(cmdRemoveItem); |
|
355 Display.getDisplay(m).setCurrent(stringItemForm); |
|
356 } |
|
357 } |
|
358 public void commandAction(Command c, Item i) |
|
359 { |
|
360 if (c == cmdBack) |
|
361 { |
|
362 label.setString(null); |
|
363 text.setString(null); |
|
364 Display.getDisplay(m).setCurrent(this); |
|
365 } |
|
366 else if (c == cmdUnlock) |
|
367 { |
|
368 i.setPreferredSize(-1,-1); |
|
369 } |
|
370 else if (c == cmdItem) |
|
371 { |
|
372 i.setLabel(i.getLabel() + "."); |
|
373 } |
|
374 } |
|
375 private void setSize(String width, String height) |
|
376 { |
|
377 if (width.equals("") && height.equals("")) |
|
378 { |
|
379 si.setPreferredSize(-1,-1); |
|
380 } |
|
381 else if (height.equals("")) |
|
382 { |
|
383 si.setPreferredSize(Integer.parseInt(width),-1); |
|
384 } |
|
385 else if (width.equals("")) |
|
386 { |
|
387 si.setPreferredSize(-1,Integer.parseInt(height)); |
|
388 } |
|
389 else |
|
390 { |
|
391 si.setPreferredSize(Integer.parseInt(width),Integer.parseInt(height)); |
|
392 } |
|
393 } |
|
394 private void layoutTest(int mode) |
|
395 { |
|
396 stringItemForm.deleteAll(); |
|
397 stringItemForm.addCommand(cmdAddListeners); |
|
398 si = new StringItem("Default Layout","StringItem",mode); |
|
399 |
|
400 StringItem si2 = new StringItem("Layout Right","StringItem",mode); |
|
401 si2.setLayout(StringItem.LAYOUT_RIGHT); |
|
402 |
|
403 StringItem si3 = new StringItem("Layout Left","StringItem",mode); |
|
404 si3.setLayout(StringItem.LAYOUT_LEFT); |
|
405 |
|
406 StringItem si4 = new StringItem("Layout Center","StringItem",mode); |
|
407 si4.setLayout(StringItem.LAYOUT_CENTER); |
|
408 |
|
409 StringItem si5 = new StringItem("Layout Expand","StringItem",mode); |
|
410 si5.setLayout(StringItem.LAYOUT_EXPAND | StringItem.LAYOUT_NEWLINE_BEFORE); |
|
411 si5.setDefaultCommand(cmdBack); |
|
412 |
|
413 if (mode != Item.PLAIN) |
|
414 { |
|
415 si.setDefaultCommand(cmdBack); |
|
416 si2.setDefaultCommand(cmdBack); |
|
417 si3.setDefaultCommand(cmdBack); |
|
418 si4.setDefaultCommand(cmdBack); |
|
419 } |
|
420 stringItemForm.append(si); |
|
421 stringItemForm.append(si2); |
|
422 stringItemForm.append(si3); |
|
423 stringItemForm.append(si4); |
|
424 stringItemForm.append(si5); |
|
425 |
|
426 Display.getDisplay(m).setCurrent(stringItemForm); |
|
427 } |
|
428 |
|
429 private void verticalLayoutTest(int mode) |
|
430 { |
|
431 stringItemForm.deleteAll(); |
|
432 si = new StringItem("Default","String",mode); |
|
433 Spacer sp = new Spacer(10, this.getHeight()); |
|
434 if (mode != Item.PLAIN) |
|
435 { |
|
436 si.setDefaultCommand(cmdBack); |
|
437 } |
|
438 stringItemForm.addCommand(cmdNext); |
|
439 |
|
440 stringItemForm.append(si); |
|
441 stringItemForm.append(sp); |
|
442 Display.getDisplay(m).setCurrent(stringItemForm); |
|
443 } |
|
444 private void changeForm() |
|
445 { |
|
446 |
|
447 switch (change) |
|
448 { |
|
449 case -1: |
|
450 stringItemForm.setTitle("Default"); |
|
451 si.setLabel("Default"); |
|
452 si.setLayout(StringItem.LAYOUT_DEFAULT); |
|
453 break; |
|
454 case 0: |
|
455 stringItemForm.setTitle("Default(75,75)"); |
|
456 si.setPreferredSize(75,75); |
|
457 break; |
|
458 case 1: |
|
459 stringItemForm.setTitle("Top"); |
|
460 si.setPreferredSize(-1,-1); |
|
461 si.setLabel("Top"); |
|
462 si.setLayout(StringItem.LAYOUT_TOP); |
|
463 break; |
|
464 case 2: |
|
465 stringItemForm.setTitle("Top(75,75)"); |
|
466 si.setPreferredSize(75,75); |
|
467 break; |
|
468 case 3: |
|
469 stringItemForm.setTitle("Vcenter"); |
|
470 si.setPreferredSize(-1,-1); |
|
471 si.setLabel("VCenter"); |
|
472 si.setLayout(StringItem.LAYOUT_VCENTER); |
|
473 break; |
|
474 case 4: |
|
475 stringItemForm.setTitle("Vcenter(75,75)"); |
|
476 si.setPreferredSize(75,75); |
|
477 break; |
|
478 case 5: |
|
479 stringItemForm.setTitle("Bottom"); |
|
480 si.setPreferredSize(-1,-1); |
|
481 si.setLabel("Bottom"); |
|
482 si.setLayout(StringItem.LAYOUT_BOTTOM); |
|
483 break; |
|
484 case 6: |
|
485 stringItemForm.setTitle("Bottom(75,75)"); |
|
486 si.setPreferredSize(75,75); |
|
487 break; |
|
488 case 7: |
|
489 stringItemForm.setTitle("Vexpand"); |
|
490 si.setPreferredSize(-1,-1); |
|
491 si.setLabel("Vexpand"); |
|
492 si.setLayout(StringItem.LAYOUT_VEXPAND); |
|
493 break; |
|
494 case 8: |
|
495 change = -1; |
|
496 changeForm(); |
|
497 return; |
|
498 } |
|
499 } |
|
500 } |