|
1 /* |
|
2 * Copyright (c) 2003-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 the methods of the Form like |
|
26 * appending many items, appending one Item, deleting an Item and |
|
27 * changing/replacing an Item etc. |
|
28 * |
|
29 * changed arrange of image items to get layout shown correctly |
|
30 */ |
|
31 |
|
32 public class FormMethodsTests extends List implements CommandListener, ItemStateListener |
|
33 { |
|
34 |
|
35 private static Midp_Form_01 parent = null; |
|
36 |
|
37 private static FormMethodsTests instance = null; |
|
38 |
|
39 private final String exCgStr = "Exclusive ChoiceGroup"; |
|
40 |
|
41 private final String mulCgStr = "Multiple ChoiceGroup"; |
|
42 |
|
43 private final String popCgStr = "Popup ChoiceGroup"; |
|
44 |
|
45 private final String dfStr = "DateField"; |
|
46 |
|
47 private final String iGStr = "Interactive Gauge"; |
|
48 |
|
49 private final String nGStr = "Non interactive Gauge"; |
|
50 |
|
51 private final String siStr = "StringItem"; |
|
52 |
|
53 private final String tfStr = "TextField"; |
|
54 |
|
55 private final String bigImgStr = "Big Image"; |
|
56 |
|
57 private final String imgStr = "ImageItem"; |
|
58 |
|
59 private final String spacerStr = "Spacer"; |
|
60 |
|
61 private ChoiceGroup cg = null; |
|
62 |
|
63 private DateField df = null; |
|
64 |
|
65 private Gauge g = null; |
|
66 |
|
67 private StringItem si = null; |
|
68 |
|
69 private TextField tf = null; |
|
70 |
|
71 private Spacer sp = null; |
|
72 |
|
73 private List tests; |
|
74 |
|
75 private Form itemForm = null; |
|
76 |
|
77 private Command cmdBack = new Command("Back", Command.BACK, 1); |
|
78 |
|
79 private Command cmdAppend = new Command("Append Item", Command.SCREEN, 1); |
|
80 |
|
81 private Command cmdReplace = new Command("Replace Item", Command.SCREEN, 1); |
|
82 |
|
83 private Command cmdDelete = new Command("Delete Item", Command.SCREEN, 1); |
|
84 |
|
85 private Command cmdManyItems = new Command("Many Items", Command.SCREEN, 1); |
|
86 |
|
87 private Command cmdItemState = new Command("Item State Test", Command.SCREEN, 1); |
|
88 |
|
89 private Command cmdChangeStringItem = new Command("Change StringItem", Command.SCREEN, 1); |
|
90 |
|
91 private Command cmdChangeImageItem = new Command("Change ImageItem", Command.SCREEN, 1); |
|
92 |
|
93 private Command cmdExit = new Command("Exit", Command.EXIT, 1); |
|
94 |
|
95 public FormMethodsTests(Midp_Form_01 parent) |
|
96 { |
|
97 super(parent.getClass().getName(), List.EXCLUSIVE); |
|
98 this.parent = parent; |
|
99 |
|
100 itemForm = new Form("Items"); |
|
101 addCommand(cmdManyItems); |
|
102 addCommand(cmdAppend); |
|
103 addCommand(cmdItemState); |
|
104 addCommand(cmdExit); |
|
105 |
|
106 itemForm.addCommand(cmdBack); |
|
107 itemForm.setCommandListener(this); |
|
108 itemForm.setItemStateListener(this); |
|
109 |
|
110 setCommandListener(this); |
|
111 if (instance == null) |
|
112 { |
|
113 instance = this; |
|
114 } |
|
115 instance.append(exCgStr, null); |
|
116 instance.append(mulCgStr, null); |
|
117 instance.append(popCgStr, null); |
|
118 instance.append(dfStr, null); |
|
119 instance.append(iGStr, null); |
|
120 instance.append(nGStr, null); |
|
121 instance.append(siStr, null); |
|
122 instance.append(tfStr, null); |
|
123 instance.append(imgStr, null); |
|
124 instance.append(bigImgStr, null); |
|
125 instance.append(spacerStr, null); |
|
126 show(); |
|
127 } |
|
128 |
|
129 /** |
|
130 * Sets this to be the current screen |
|
131 * |
|
132 *@return The Parent value |
|
133 */ |
|
134 public static MIDlet getParent() |
|
135 { |
|
136 return parent; |
|
137 } |
|
138 |
|
139 /** |
|
140 * Show this screen |
|
141 */ |
|
142 public static void show() |
|
143 { |
|
144 Display.getDisplay(parent).setCurrent(instance); |
|
145 } |
|
146 |
|
147 private void addChoiceGroup(String title, int type) |
|
148 { |
|
149 cg = new ChoiceGroup(title, type); |
|
150 cg.append("1stElement", null); |
|
151 cg.append("2ndElement", null); |
|
152 cg.append("3ndElement", null); |
|
153 cg.append("4thElement", null); |
|
154 cg.append("5thElement", null); |
|
155 cg.append("6thElement", null); |
|
156 cg.append("7thElement", null); |
|
157 cg.append("8thElement", null); |
|
158 cg.append("9thElement", null); |
|
159 itemForm.append(cg); |
|
160 } |
|
161 |
|
162 private void addGauge(String title, boolean mode) |
|
163 { |
|
164 g = new Gauge(title, mode, 10, 5); |
|
165 itemForm.append(g); |
|
166 } |
|
167 |
|
168 private void addDateField(String title, int mode) |
|
169 { |
|
170 df = new DateField(title, mode); |
|
171 itemForm.append(df); |
|
172 } |
|
173 |
|
174 private void addSpacer(int minWidth, int minHeight) |
|
175 { |
|
176 sp = new Spacer(minWidth, minHeight); |
|
177 itemForm.append(sp); |
|
178 } |
|
179 |
|
180 private void addStringItem(String title, String text) |
|
181 { |
|
182 si = new StringItem(title, text); |
|
183 itemForm.append(si); |
|
184 } |
|
185 |
|
186 private void addBigImage() |
|
187 { |
|
188 try |
|
189 { |
|
190 Image bigImage = Image.createImage("/bigImage.png"); |
|
191 itemForm.append(bigImage); |
|
192 } |
|
193 catch (java.io.IOException err) |
|
194 { |
|
195 System.out.println("Error in creating the image!"); |
|
196 } |
|
197 } |
|
198 |
|
199 private void addImageItem() |
|
200 { |
|
201 try |
|
202 { |
|
203 Image img = Image.createImage("/plasma.png"); |
|
204 |
|
205 itemForm.append(new ImageItem("Default Layout", |
|
206 img, |
|
207 ImageItem.LAYOUT_DEFAULT, |
|
208 "Image Cannot be shown")); |
|
209 |
|
210 itemForm.append(new ImageItem("Layout Center", |
|
211 img, |
|
212 ImageItem.LAYOUT_CENTER, |
|
213 "Image Cannot be shown")); |
|
214 |
|
215 itemForm.append(new ImageItem("Layout Left", |
|
216 img, |
|
217 ImageItem.LAYOUT_LEFT, |
|
218 "Image Cannot be shown")); |
|
219 |
|
220 itemForm.append(new ImageItem("Layout Center After", |
|
221 img, |
|
222 ImageItem.LAYOUT_CENTER | |
|
223 ImageItem.LAYOUT_NEWLINE_AFTER, |
|
224 "Image Cannot be shown")); |
|
225 |
|
226 itemForm.append(new ImageItem("Layout Right", |
|
227 img, |
|
228 ImageItem.LAYOUT_RIGHT, |
|
229 "Image Cannot be shown")); |
|
230 |
|
231 itemForm.append(new ImageItem("Layout Center Before", |
|
232 img, |
|
233 ImageItem.LAYOUT_CENTER | |
|
234 ImageItem.LAYOUT_NEWLINE_BEFORE, |
|
235 "Image Cannot be shown")); |
|
236 |
|
237 } |
|
238 catch (java.io.IOException err) |
|
239 { |
|
240 System.out.println("Error in creating the image!"); |
|
241 } |
|
242 } |
|
243 |
|
244 private void addTextField(String label, int constraints) |
|
245 { |
|
246 if (label.equals("UNEDITABLE")) |
|
247 { |
|
248 tf = new TextField(label, "uneditable", 30, constraints); |
|
249 } |
|
250 else |
|
251 tf = new TextField(label, "", 30, constraints); |
|
252 itemForm.append(tf); |
|
253 } |
|
254 |
|
255 private void deleteFormItems() |
|
256 { |
|
257 int size = itemForm.size(); |
|
258 if (size != 0) |
|
259 { |
|
260 for (int i=0; i<size; i++) |
|
261 itemForm.delete(0); |
|
262 } |
|
263 } |
|
264 public void itemStateChanged(Item item) |
|
265 { |
|
266 itemForm.setTitle(item.getLabel() + " modified!"); |
|
267 } |
|
268 |
|
269 /** |
|
270 * This method handles command invocations. |
|
271 * |
|
272 *@param c This is the command responsible for the event. |
|
273 *@param s Should be equal to this. |
|
274 */ |
|
275 public void commandAction(Command c, Displayable s) |
|
276 { |
|
277 int index = instance.getSelectedIndex(); |
|
278 String item = instance.getString(index); |
|
279 if (c == cmdItemState) |
|
280 { |
|
281 addChoiceGroup(exCgStr, ChoiceGroup.EXCLUSIVE); |
|
282 addChoiceGroup(mulCgStr, ChoiceGroup.MULTIPLE); |
|
283 addChoiceGroup(popCgStr, ChoiceGroup.POPUP); |
|
284 addGauge(iGStr, true); |
|
285 addDateField("DATE_TIME", DateField.DATE_TIME); |
|
286 addTextField("ANY", TextField.ANY); |
|
287 addStringItem("StringItemLabel", "StringItemText"); |
|
288 addImageItem(); |
|
289 itemForm.addCommand(cmdChangeStringItem); |
|
290 itemForm.addCommand(cmdChangeImageItem); |
|
291 } |
|
292 if (c == cmdAppend || c == cmdManyItems) |
|
293 { |
|
294 if (c == cmdAppend) |
|
295 { |
|
296 deleteFormItems(); |
|
297 itemForm.removeCommand(cmdReplace); |
|
298 itemForm.removeCommand(cmdDelete); |
|
299 } |
|
300 if (c == cmdManyItems) |
|
301 { |
|
302 itemForm.addCommand(cmdReplace); |
|
303 itemForm.addCommand(cmdDelete); |
|
304 } |
|
305 |
|
306 if (item.equals(exCgStr)) |
|
307 addChoiceGroup(exCgStr, ChoiceGroup.EXCLUSIVE); |
|
308 else if (item.equals(mulCgStr)) |
|
309 addChoiceGroup(mulCgStr, ChoiceGroup.MULTIPLE); |
|
310 else if (item.equals(popCgStr)) |
|
311 addChoiceGroup(popCgStr, ChoiceGroup.POPUP); |
|
312 else if (item.equals(iGStr)) |
|
313 addGauge(iGStr, true); |
|
314 else if (item.equals(nGStr)) |
|
315 addGauge(nGStr, false); |
|
316 else if (item.equals(dfStr)) |
|
317 { |
|
318 addDateField("DATE", DateField.DATE); |
|
319 addDateField("TIME", DateField.TIME); |
|
320 addDateField("DATE_TIME", DateField.DATE_TIME); |
|
321 } |
|
322 else if (item.equals(siStr)) |
|
323 addStringItem("StringItemLabel", "StringItemText"); |
|
324 else if (item.equals(imgStr)) |
|
325 addImageItem(); |
|
326 else if (item.equals(spacerStr)) |
|
327 addSpacer(10,10); |
|
328 else if (item.equals(tfStr)) |
|
329 { |
|
330 addTextField("ANY", TextField.ANY); |
|
331 addTextField("EMAILADDR", TextField.EMAILADDR); |
|
332 addTextField("NUMERIC", TextField.NUMERIC); |
|
333 addTextField("PHONENUMBER", TextField.PHONENUMBER); |
|
334 addTextField("URL", TextField.URL); |
|
335 addTextField("PASSWORD", TextField.PASSWORD); |
|
336 addTextField("PASSWORD|NUMERIC", TextField.PASSWORD|TextField.NUMERIC); |
|
337 addTextField("PASSWORD|PHONENUMBER", TextField.PASSWORD|TextField.PHONENUMBER); |
|
338 addTextField("PASSWORD|URL", TextField.PASSWORD|TextField.URL); |
|
339 addTextField("PASSWORD|EMAILADDR", TextField.PASSWORD|TextField.EMAILADDR); |
|
340 addTextField("DECIMAL", TextField.DECIMAL); |
|
341 addTextField("INITIAL_CAPS_SENTENCE", TextField.INITIAL_CAPS_SENTENCE); |
|
342 addTextField("PINITIAL_CAPS_WORD", TextField.INITIAL_CAPS_WORD); |
|
343 addTextField("UNEDITABLE", TextField.UNEDITABLE); |
|
344 addTextField("NON_PREDICTIVE", TextField.NON_PREDICTIVE); |
|
345 addTextField("SENSITIVE", TextField.SENSITIVE); |
|
346 } |
|
347 else if (item.equals(bigImgStr)) |
|
348 addBigImage(); |
|
349 } |
|
350 if (c == cmdExit) |
|
351 { |
|
352 parent.destroyApp(false); |
|
353 parent.notifyDestroyed(); |
|
354 } |
|
355 Display.getDisplay(parent).setCurrent(itemForm); |
|
356 if (c == cmdBack) |
|
357 Display.getDisplay(parent).setCurrent(instance); |
|
358 if (c == cmdReplace) |
|
359 { |
|
360 //Item to replace 1st Item |
|
361 Item si = new StringItem("Replaced StringItem", "StringItem Text"); |
|
362 if (itemForm.size() != 0) |
|
363 itemForm.set(0, si); |
|
364 Display.getDisplay(parent).setCurrent(itemForm); |
|
365 } |
|
366 if (c == cmdDelete) |
|
367 { |
|
368 if (itemForm.size() == 0) |
|
369 { |
|
370 //to tell the user Form is empty |
|
371 Alert a = new Alert("Info", "Form is empty,add Items before testing delete", null, AlertType.INFO); |
|
372 a.setTimeout(5000); |
|
373 Display.getDisplay(parent).setCurrent(a); |
|
374 } |
|
375 else |
|
376 { |
|
377 itemForm.delete(0); |
|
378 } |
|
379 Display.getDisplay(parent).setCurrent(itemForm); |
|
380 } |
|
381 if (c == cmdChangeStringItem) |
|
382 { |
|
383 (itemForm.get(6)).setLabel("StringItem Changed!"); |
|
384 (itemForm.get(6)).notifyStateChanged(); |
|
385 } |
|
386 if (c == cmdChangeImageItem) |
|
387 { |
|
388 (itemForm.get(7)).setLabel("ImageItem Changed!"); |
|
389 (itemForm.get(7)).notifyStateChanged(); |
|
390 } |
|
391 } |
|
392 } |