summaryrefslogtreecommitdiffstats
path: root/src/com/vaadin/tests/book/BookTestApplication.java
blob: e0a6309f795a6f13c4acc3d27d52d68cacbcb009 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
/* 
@ITMillApache2LicenseForJavaFiles@
 */

package com.vaadin.tests.book;

import java.net.URL;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Iterator;
import java.util.Locale;
import java.util.Map;
import java.util.Set;

import com.vaadin.data.Item;
import com.vaadin.data.Validator;
import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.data.Property.ValueChangeListener;
import com.vaadin.data.util.QueryContainer;
import com.vaadin.data.validator.StringLengthValidator;
import com.vaadin.terminal.ClassResource;
import com.vaadin.terminal.DownloadStream;
import com.vaadin.terminal.ExternalResource;
import com.vaadin.terminal.ParameterHandler;
import com.vaadin.terminal.Sizeable;
import com.vaadin.terminal.StreamResource;
import com.vaadin.terminal.URIHandler;
import com.vaadin.terminal.UserError;
import com.vaadin.terminal.gwt.server.WebApplicationContext;
import com.vaadin.ui.AbstractSelect;
import com.vaadin.ui.Button;
import com.vaadin.ui.CheckBox;
import com.vaadin.ui.Component;
import com.vaadin.ui.CustomLayout;
import com.vaadin.ui.DateField;
import com.vaadin.ui.Embedded;
import com.vaadin.ui.Form;
import com.vaadin.ui.FormLayout;
import com.vaadin.ui.GridLayout;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.InlineDateField;
import com.vaadin.ui.Label;
import com.vaadin.ui.Link;
import com.vaadin.ui.MenuBar;
import com.vaadin.ui.NativeSelect;
import com.vaadin.ui.Panel;
import com.vaadin.ui.PopupDateField;
import com.vaadin.ui.ProgressIndicator;
import com.vaadin.ui.RichTextArea;
import com.vaadin.ui.Select;
import com.vaadin.ui.TabSheet;
import com.vaadin.ui.Table;
import com.vaadin.ui.TextField;
import com.vaadin.ui.Tree;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Layout.AlignmentHandler;
import com.vaadin.ui.MenuBar.MenuItem;

public class BookTestApplication extends com.vaadin.Application {
	Window main = new Window("Application window");

	TheButton butts1;
	TheButtons butts2;
	TheButtons2 butts3;

	Label mylabel1;
	Label mylabel2;
	Label mylabel3;

	StreamResource strres;
	VerticalLayout ol;
	int getwincount = 0;

	@Override
	public void init() {
		setTheme("tests-book");

		setMainWindow(main);

		// Demo the use of parameter and URI handlers
		main.addParameterHandler(new MyParameterHandler());
		main.addURIHandler(new MyURIHandler());

		MyDynamicResource myresource = new MyDynamicResource();
		main.addParameterHandler(myresource);
		main.addURIHandler(myresource);

		main.addURIHandler(new BookTestURIHandler());
	}

	class MyParameterHandler implements ParameterHandler {
		public void handleParameters(Map parameters) {
			// Print out the parameters to standard output
			for (Iterator it = parameters.keySet().iterator(); it.hasNext();) {
				String key = (String) it.next();
				String value = ((String[]) parameters.get(key))[0];
				System.out.println("Key: " + key + ", value: " + value);
			}
		}
	}

	class MyURIHandler implements URIHandler {
		public DownloadStream handleURI(URL context, String relativeUri) {
			System.out.println("Context: " + context.toString()
					+ ", relative: " + relativeUri);
			return null; // Let the Application provide the response
		}
	}

	class BookTestURIHandler implements URIHandler {
		public DownloadStream handleURI(URL context, String relativeUri) {
			String example;
			String param = null;

			final int slashPos = relativeUri.indexOf("/");
			if (slashPos > 0) {
				example = relativeUri.substring(0, slashPos);
				param = relativeUri.substring(slashPos + 1);
			} else {
				example = relativeUri;
			}

			/* Remove existing components and windows. */
			main.removeAllComponents();
			final Set childwindows = main.getChildWindows();
			for (final Iterator cwi = childwindows.iterator(); cwi.hasNext();) {
				final Window child = (Window) cwi.next();
				main.removeWindow(child);
			}

			// The index is listed inside a grid layout
			main.setLayout(new VerticalLayout());
			GridLayout grid = new GridLayout(4, 4);
			grid.addStyleName("index");
			main.addComponent(grid);

			if (example.equals("index")) {
				final String examples[] = { "defaultbutton", "label",
						"labelcontent", "tree", "embedded", "textfield",
						"textfieldvalidation", "datefield", "button",
						"select/select", "select/native", "select/optiongroup",
						"select/twincol", "filterselect", "validator", "table",
						"table/select", "table/component", "table/paging",
						"table/editable", "upload", "link", "gridlayout",
						"orderedlayout", "formlayout", "form", "form/simple",
						"form/layout", "panel", "expandlayout",
						"expandlayout/root", "tabsheet", "alignment",
						"alignment/grid", "window", "window/opener",
						"window/multiple", "classresource", "usererror",
						"progress/window", "progress/thread", "progress",
						"customlayout", "spacing", "margin", "clientinfo",
						"fillinform/templates", "notification", "print",
						"richtextfield", "querycontainer", "menubar" };
				for (int i = 0; i < examples.length; i++) {
					grid.addComponent(new Label("<a href='"
							+ context.toString() + examples[i] + "'>"
							+ examples[i] + "</a>", Label.CONTENT_XHTML));
				}
				return null;
			}

			if (example.equals("defaultbutton")) {
				example_defaultButton(main, param);
			} else if (example.equals("label")) {
				example_Label(main, param);
			} else if (example.equals("labelcontent")) {
				example_LabelContent(main, param);
			} else if (example.equals("tree")) {
				example_Tree(main, param);
			} else if (example.equals("embedded")) {
				example_Embedded(main, param);
			} else if (example.equals("textfield")) {
				example_TextField(main, param);
			} else if (example.equals("textfieldvalidation")) {
				example_TextFieldValidation(main, param);
			} else if (example.equals("usererror")) {
				example_UserError(main, param);
			} else if (example.equals("datefield")) {
				example_DateField(main, param);
			} else if (example.equals("button")) {
				example_Button(main, param);
			} else if (example.equals("checkbox")) {
				example_CheckBox(main, param);
			} else if (example.equals("select")) {
				example_Select(main, param);
			} else if (example.equals("filterselect")) {
				example_FilterSelect(main, param);
			} else if (example.equals("validator")) {
				example_Validator(main, param);
			} else if (example.equals("table")) {
				example_Table(main, param);
			} else if (example.equals("upload")) {
				example_Upload(main, param);
			} else if (example.equals("link")) {
				example_Link(main, param);
			} else if (example.equals("gridlayout")) {
				example_GridLayout(main, param);
			} else if (example.equals("orderedlayout")) {
				example_OrderedLayout(main, param);
			} else if (example.equals("formlayout")) {
				example_FormLayout(main, param);
			} else if (example.equals("form")) {
				example_Form(main, param);
			} else if (example.equals("tabsheet")) {
				example_TabSheet(main, param);
			} else if (example.equals("panel")) {
				example_Panel(main, param);
			} else if (example.equals("expandlayout")) {
				example_ExpandLayout(main, param);
			} else if (example.equals("alignment")) {
				example_Alignment(main, param);
			} else if (example.equals("window")) {
				example_Window(main, param);
			} else if (example.equals("classresource")) {
				example_ClassResource(main, param);
			} else if (example.equals("progress")) {
				example_ProgressIndicator(main, param);
			} else if (example.equals("customlayout")) {
				example_CustomLayout(main, param);
			} else if (example.equals("spacing")) {
				example_Spacing(main, param);
			} else if (example.equals("margin")) {
				example_Margin(main, param);
			} else if (example.equals("clientinfo")) {
				example_ClientInfo(main, param);
			} else if (example.equals("fillinform")) {
				example_FillInForm(main, param);
			} else if (example.equals("notification")) {
				example_Notification(main, param);
			} else if (example.equals("print")) {
				example_Print(main, param);
			} else if (example.equals("richtextfield")) {
				example_RichTextArea(main, param);
			} else if (example.equals("querycontainer")) {
				example_QueryContainer(main, param);
			} else if (example.equals("menubar")) {
				example_MenuBar(main, param);
			} else {
				; // main.addComponent(new
				// Label("Unknown test '"+example+"'."));
			}

			return null;
		}
	}

	/*
	 * public Window getWindow(String name) { Window superwin =
	 * super.getWindow(name); if (superwin != null) return superwin;
	 * 
	 * main.addComponent(new Label("Request 2 for window '"+name+"'.")); if
	 * (name.equals("panel")) { Window window = new Window("Other Window " +
	 * getwincount++); example_Panel(window, null); return window; } return
	 * null; }
	 */
	public void handleButton(Button.ClickEvent event) {
		ol.addStyleName("myLayout2");
	}

	void example_defaultButton(Window main, String param) {
		main.addComponent(new DefaultButtonExample());
	}

	void example_Label(Window main, String param) {
		/* Some container for the Label. */
		final Panel panel = new Panel("Panel Containing a Label");
		main.addComponent(panel);

		panel.addComponent(new Label(
				"This is a Label inside a Panel. There is enough "
						+ "text in the label to make the text wrap if it "
						+ "exceeds the width of the panel."));
	}

	void example_LabelContent(Window main, String param) {
		final GridLayout labelgrid = new GridLayout(2, 1);
		labelgrid.addStyleName("labelgrid");
		labelgrid.addComponent(new Label("CONTENT_DEFAULT"));
		labelgrid.addComponent(new Label(
				"This is a label in default mode: <plain text>",
				Label.CONTENT_DEFAULT));
		labelgrid.addComponent(new Label("CONTENT_PREFORMATTED"));
		labelgrid
				.addComponent(new Label(
						"This is a preformatted label.\nThe newline character \\n breaks the line.",
						Label.CONTENT_PREFORMATTED));
		labelgrid.addComponent(new Label("CONTENT_RAW"));
		labelgrid
				.addComponent(new Label(
						"This is a label in raw mode.<br>It can contain, for example, unbalanced markup.",
						Label.CONTENT_RAW));
		labelgrid.addComponent(new Label("CONTENT_TEXT"));
		labelgrid.addComponent(new Label(
				"This is a label in (plain) text mode", Label.CONTENT_TEXT));
		labelgrid.addComponent(new Label("CONTENT_XHTML"));
		labelgrid.addComponent(new Label(
				"<i>This</i> is an <b>XHTML<b> formatted label",
				Label.CONTENT_XHTML));
		labelgrid.addComponent(new Label("CONTENT_XML"));
		labelgrid.addComponent(new Label(
				"This is an <myelement>XML</myelement> formatted label",
				Label.CONTENT_XML));
		main.addComponent(labelgrid);

		final ClassResource labelimage = new ClassResource("smiley.jpg", this);
		main.addComponent(new Label("Here we have an image <img src=\""
				+ getRelativeLocation(labelimage) + "\"/> within some text.",
				Label.CONTENT_XHTML));
	}

	void example_Tree(Window main, String param) {
		final Object[][] planets = new Object[][] {
				new Object[] { "Mercury" },
				new Object[] { "Venus" },
				new Object[] { "Earth", "The Moon" },
				new Object[] { "Mars", "Phobos", "Deimos" },
				new Object[] { "Jupiter", "Io", "Europa", "Ganymedes",
						"Callisto" },
				new Object[] { "Saturn", "Titan", "Tethys", "Dione", "Rhea",
						"Iapetus" },
				new Object[] { "Uranus", "Miranda", "Ariel", "Umbriel",
						"Titania", "Oberon" },
				new Object[] { "Neptune", "Triton", "Proteus", "Nereid",
						"Larissa" } };

		final Tree tree = new Tree();

		// Add planets as root items in the tree.
		for (int i = 0; i < planets.length; i++) {
			final String planet = (String) (planets[i][0]);
			tree.addItem(planet);

			if (planets[i].length == 1) {
				// The planet has no moons so make it a leaf.
				tree.setChildrenAllowed(planet, false);
			} else {
				// Add children (moons) under the planets.
				for (int j = 1; j < planets[i].length; j++) {
					final String moon = (String) planets[i][j];

					// Add the item as a regular item.
					tree.addItem(moon);

					// Set it to be a child.
					tree.setParent(moon, planet);

					// Make the moons look like leaves.
					tree.setChildrenAllowed(moon, false);
				}

				// Expand the subtree.
				tree.expandItemsRecursively(planet);
			}
		}

		// Horizontal layout with the tree on the left and a details panel on
		// the right.
		final HorizontalLayout horlayout = new HorizontalLayout();
		horlayout.addStyleName("treeexample");
		horlayout.setSizeFull();

		final Panel treepanel = new Panel("The Planets and Major Moons");
		treepanel.addComponent(tree);
		horlayout.addComponent(treepanel);

		final Panel detailspanel = new Panel("Details");
		horlayout.addComponent(detailspanel);
		horlayout.setExpandRatio(detailspanel, 1);

		final VerticalLayout detailslayout = new VerticalLayout();
		detailspanel.setLayout(detailslayout);

		// Allow null selection - this is the default actually.
		tree.setNullSelectionAllowed(true);

		// When a tree item (planet or moon) is clicked, open the item in
		// Details view.
		tree.setImmediate(true);
		tree.addListener(new ValueChangeListener() {
			String lastselected = null;

			public void valueChange(ValueChangeEvent event) {
				String planet = (String) tree.getValue();

				// Reselect a selected item if it is unselected by clicking it.
				if (planet == null) {
					planet = lastselected;
					tree.setValue(planet);
				}
				lastselected = planet;

				detailspanel.setCaption("Details on " + planet);
				detailslayout.removeAllComponents();

				// Put some stuff in the Details view.
				detailslayout.addComponent(new Label("Where is the cat?"));
				detailslayout.addComponent(new Label("The cat is in " + planet
						+ "."));

			}
		});

		main.setLayout(horlayout);
	}

	void example_Select(Window main, String param) {
		final HorizontalLayout layout = new HorizontalLayout();
		layout.addStyleName("aligntop");

		if (param.equals("twincol")) {
			final SelectExample select1 = new SelectExample(this, param,
					"Select some items", true);
			layout.addComponent(select1);
		} else if (param.equals("filter")) {
			final SelectExample select1 = new SelectExample(this, param,
					"Enter containing substring", false);
			layout.addComponent(select1);
		} else {
			final SelectExample select1 = new SelectExample(this, param,
					"Single Selection Mode", false);
			final SelectExample select2 = new SelectExample(this, param,
					"Multiple Selection Mode", true);
			layout.addComponent(select1);
			layout.addComponent(select2);
		}
		main.addComponent(layout);
	}

	void example_FilterSelect(Window main, String param) {
		final Select select = new Select("Enter containing substring");
		main.addComponent(select);

		select
				.setFilteringMode(AbstractSelect.Filtering.FILTERINGMODE_CONTAINS);

		/* Fill the component with some items. */
		final String[] planets = new String[] { "Mercury", "Venus", "Earth",
				"Mars", "Jupiter", "Saturn", "Uranus", "Neptune" };

		for (int i = 0; i < planets.length; i++) {
			for (int j = 0; j < planets.length; j++) {
				select.addItem(planets[j] + " to " + planets[i]);
			}
		}
	}

	void example_TextField(Window main, String param) {
		/* Add a single-line text field. */
		final TextField subject = new TextField("Subject");
		subject.setColumns(40);
		main.addComponent(subject);

		/* Add a multi-line text field. */
		final TextField message = new TextField("Message");
		message.setRows(7);
		message.setColumns(40);
		main.addComponent(message);
	}

	void example_TextFieldValidation(Window main, String param) {
		// Create a text field with a label
		final TextField username = new TextField("Username");
		main.addComponent(username);

		// Set visible length to 16 characters
		username.setColumns(16);

		// Set content length to minimum of 6 and maximum of 16 characters.
		// The string also may not be null.
		username.addValidator(new StringLengthValidator(
				"Must be 6 to 16 characters long", 6, 16, false));

		// Setting component immediate causes a ValueChangeEvent to occur
		// when the TextField loses focus.
		username.setImmediate(true);

		// Listen for ValueChangeEvents and handle them
		username.addListener(new ValueChangeListener() {
			public void valueChange(ValueChangeEvent event) {
				// Get the source of the event
				final TextField username = (TextField) (event.getProperty());

				try {
					// Validate the field value.
					username.validate();
				} catch (final Validator.InvalidValueException e) {
					// The value was not ok. The error was set.
				}
			}
		});
	}

	void example_UserError(final Window main, String param) {
		if (param != null) {
			if (param.equals("form")) {

				final FormLayout layout = new FormLayout();
				main.addComponent(layout);

				final TextField textfield = new TextField("Enter code");
				layout.addComponent(textfield);
				textfield.setComponentError(null);

				final Button button = new Button("Ok!");
				layout.addComponent(button);

				button.addListener(new Button.ClickListener() {
					public void buttonClick(ClickEvent event) {
						if (((String) textfield.getValue()).length() == 0) {
							textfield.setComponentError(new UserError(
									"Must be letters and numbers."));
						} else {
							textfield.setComponentError(null);
						}
					}
				});
			}
		} else {
			main.setLayout(new HorizontalLayout());

			// Create a field.
			final TextField textfield = new TextField("Enter code");
			main.addComponent(textfield);

			// Let the component error be initially clear. (It actually is by
			// default.)
			textfield.setComponentError(null);

			// Have a button right of the field (and align it properly).
			final Button button = new Button("Ok!");
			main.addComponent(button);
			((HorizontalLayout) main.getLayout()).setComponentAlignment(button,
					HorizontalLayout.ALIGNMENT_LEFT,
					HorizontalLayout.ALIGNMENT_BOTTOM);

			// Handle button clicks
			button.addListener(new Button.ClickListener() {
				public void buttonClick(ClickEvent event) {
					// If the field value is bad, set its error.
					// (Here the content must be only alphanumeric characters.)
					if (!((String) textfield.getValue()).matches("^\\w*$")) {
						// Put the component in error state and set the error
						// message.
						textfield.setComponentError(new UserError(
								"Must be letters and numbers"));
					} else {
						// Otherwise clear it.
						textfield.setComponentError(null);
					}
				}
			});
		}
	}

	void example_DateField(Window main, String param) {
		HorizontalLayout layout = new HorizontalLayout();

		/* Create a DateField with the calendar style. */
		final DateField popupdate = new PopupDateField("Popup calendar field");

		/* Set resolution of the date/time display. */
		popupdate.setResolution(DateField.RESOLUTION_MIN);

		/* Set the date and time to present. */
		popupdate.setValue(new java.util.Date());

		/* Create a DateField with the calendar style. */
		final DateField inlinedate = new InlineDateField(
				"Inline calendar field");

		/* Set locale of the DateField to American English. */
		inlinedate.setLocale(new Locale("en", "US"));

		/* Set the date and time to present. */
		inlinedate.setValue(new java.util.Date());

		/* Set resolution of the date/time display. */
		inlinedate.setResolution(DateField.RESOLUTION_MIN);

		layout.addComponent(popupdate);
		layout.addComponent(inlinedate);
		layout.setSpacing(true);
		main.addComponent(layout);
	}

	void example_Validator(Window main, String param) {
		if (param != null && param.equals("required")) {
			Form form = new Form();
			form.setCaption("My Form");
			form.setRequired(true);
			main.addComponent(form);

			TextField text = new TextField("This is a required text field");
			text.setRequired(true);
			text.setImmediate(true);
			form.getLayout().addComponent(text);
			return;
		}
		main.addComponent(new SSNField());
	}

	class PagingTable extends Table {
		@Override
		public String getTag() {
			return "pagingtable";
		}
	}

	void example_Table(Window main, String param) {
		if (param != null) {
			if (param.equals("select")) {
				main.addComponent(new TableExample2());
			} else if (param.equals("component")) {
				main.addComponent(new TableExample3());
			} else if (param.equals("editable")) {
				main.addComponent(new TableEditable());
			} else if (param.equals("bean")) {
				main.addComponent(new TableEditableBean());
			} else if (param.equals("long")) {
				main.addComponent(new TableExample());
			} else if (param.equals("cellstyle")) {
				main.addComponent(new TableCellStyle());
			} else if (param.equals("huge")) {
				main.addComponent(new TableHuge());
			} else if (param.equals("paging")) {
				PagingTable table = new PagingTable();
				table.addContainerProperty("Column 1", String.class, null);
				for (int i = 0; i < 100; i++) {
					table.addItem(new Object[] { "Item " + i }, new Integer(i));
				}
				main.addComponent(table);
			}
		} else {
			main.addComponent(new TableExample1());
		}
	}

	void example_Upload(Window main, String param) {
		main.addComponent(new MyUploader());
	}

	void example_Link(Window main, String param) {

		/* Create a link that opens the popup window. */
		final Link alink = new Link();

		/* Set the resource to be opened in the window. */
		alink.setResource(new ExternalResource("http://www.vaadin.com/"));

		main.addComponent(alink);

		final ClassResource mydocument = new ClassResource("mydocument.pdf",
				this);
		main.addComponent(new Link("The document (pdf)", mydocument));
		main.addComponent(new Link("link to a resource", new ExternalResource(
				"http://www.vaadin.com/")));
	}

	void example_Button(Window main, String param) {
		if (param != null) {
			if (param.equals("buttons")) {
				main.addComponent(new TheButton());
			}
			return;
		}

		// butts1 = new TheButton ();
		// main.addComponent(butts1);

		// butts2 = new TheButtons (main);
		// butts3 = new TheButtons2 (main);

		// Button checkbox = new Button ("This is a checkbox");

		// main.addComponent(checkbox);
		final Button button = new Button("My Button");
		main.addComponent(button);
	}

	void example_CheckBox(Window main, String param) {
		/* A check box with default state (not checked, i.e., false). */
		final CheckBox checkbox1 = new CheckBox("My CheckBox");
		checkbox1.addStyleName("mybox");
		main.addComponent(checkbox1);

		/* Another check box with explicitly set checked state. */
		final CheckBox checkbox2 = new CheckBox("Checked CheckBox");
		/*
		 * @TODO: Build fails here, why? checkbox2.setValue(true);
		 */
		main.addComponent(checkbox2);

		/*
		 * Make some application logic. We use anynymous listener classes here.
		 * The above references were defined as "final" to allow accessing them
		 * from inside anonymous classes.
		 */
		checkbox1.addListener(new ValueChangeListener() {
			public void valueChange(ValueChangeEvent event) {
				/* Copy the value to the other checkbox. */
				checkbox2.setValue(checkbox1.getValue());
			}
		});
		checkbox2.addListener(new ValueChangeListener() {
			public void valueChange(ValueChangeEvent event) {
				/* Copy the value to the other checkbox. */
				checkbox1.setValue(checkbox2.getValue());
			}
		});
	}

	void example_Panel(Window main, String param) {
		// Create a panel with a caption.
		final Panel panel = new Panel("Contact Information");

		// Create a layout inside the panel
		final FormLayout form = new FormLayout();

		// Set the layout as the root layout of the panel
		panel.setLayout(form);

		// Add some components
		form.addComponent(new TextField("Name"));
		form.addComponent(new TextField("Email"));

		// Add the panel to the main window
		final ClassResource icon = new ClassResource("smiley.jpg", main
				.getApplication());
		form.addComponent(new Embedded("Image", icon));
		panel.setIcon(icon);
		panel.addComponent(form);
		main.addComponent(panel);
	}

	void example_GridLayout(Window main, String param) {
		if (param.equals("embedded")) {
			final GridLayout grid = new GridLayout(3, 3);
			for (int i = 0; i < 3 * 3; i++) {
				ClassResource img = new ClassResource("smiley.jpg", main
						.getApplication());
				Embedded embedded = new Embedded("", img);
				grid.addComponent(embedded);
			}
			main.addComponent(grid);
			return;
		}
		/* Create a 4 by 4 grid layout. */
		final GridLayout grid = new GridLayout(4, 4);
		grid.addStyleName("example-gridlayout");

		/* Fill out the first row using the cursor. */
		grid.addComponent(new Button("R/C 1"));
		for (int i = 0; i < 3; i++) {
			grid.addComponent(new Button("Col " + (grid.getCursorX() + 1)));
		}

		/* Fill out the first column using coordinates. */
		for (int i = 1; i < 4; i++) {
			grid.addComponent(new Button("Row " + i), 0, i);
		}

		/* Add some components of various shapes. */
		grid.addComponent(new Button("3x1 button"), 1, 1, 3, 1);
		grid.addComponent(new Label("1x2 cell"), 1, 2, 1, 3);
		final InlineDateField date = new InlineDateField("A 2x2 date field");
		date.setResolution(DateField.RESOLUTION_DAY);
		grid.addComponent(date, 2, 2, 3, 3);

		main.addComponent(grid);
	}

	void example_Alignment(Window main, String param) {
		if (param.equals("grid")) {
			/* Create a 3 by 3 grid layout. */
			final GridLayout layout = new GridLayout(3, 3);
			// OrderedLayout layout = new
			// OrderedLayout(OrderedLayout.ORIENTATION_VERTICAL);
			main.setLayout(layout);
			layout.addStyleName("example-alignment");

			layout.setWidth(400, Sizeable.UNITS_PIXELS);
			layout.setHeight(400, Sizeable.UNITS_PIXELS);

			/* Define cells and their layouts to create. */

			Object cells[][] = {
					{ new Button("Top Left"),
							new Integer(AlignmentHandler.ALIGNMENT_LEFT),
							new Integer(AlignmentHandler.ALIGNMENT_TOP) },
					{
							new Label("Top Center"),
							new Integer(
									AlignmentHandler.ALIGNMENT_HORIZONTAL_CENTER),
							new Integer(AlignmentHandler.ALIGNMENT_TOP) },
					{ new Label("Top Right"),
							new Integer(AlignmentHandler.ALIGNMENT_RIGHT),
							new Integer(AlignmentHandler.ALIGNMENT_TOP) },
					{
							new Button("Center Left"),
							new Integer(AlignmentHandler.ALIGNMENT_LEFT),
							new Integer(
									AlignmentHandler.ALIGNMENT_VERTICAL_CENTER) },
					{
							new Button("Center Center"),
							new Integer(
									AlignmentHandler.ALIGNMENT_HORIZONTAL_CENTER),
							new Integer(
									AlignmentHandler.ALIGNMENT_VERTICAL_CENTER) },
					{
							new Button("Center Right"),
							new Integer(AlignmentHandler.ALIGNMENT_RIGHT),
							new Integer(
									AlignmentHandler.ALIGNMENT_VERTICAL_CENTER) },
					{ new Button("Bottom Left"),
							new Integer(AlignmentHandler.ALIGNMENT_LEFT),
							new Integer(AlignmentHandler.ALIGNMENT_BOTTOM) },
					{
							new Button("Bottom Center"),
							new Integer(
									AlignmentHandler.ALIGNMENT_HORIZONTAL_CENTER),
							new Integer(AlignmentHandler.ALIGNMENT_BOTTOM) },
					{ new Button("Bottom Right"),
							new Integer(AlignmentHandler.ALIGNMENT_RIGHT),
							new Integer(AlignmentHandler.ALIGNMENT_BOTTOM) } };

			for (int i = 0; i < 9; i++) {
				HorizontalLayout celllayout = new HorizontalLayout();
				celllayout.addComponent((Component) cells[i][0]);
				if (i == 0) {
					celllayout.setExpandRatio((Component) cells[i][0], 1);
				}

				celllayout.setComponentAlignment((Component) cells[i][0],
						((Integer) cells[i][1]).intValue(),
						((Integer) cells[i][2]).intValue());
				layout.addComponent(celllayout);
				// layout.setComponentAlignment((Component)cells[i][0],
				// ((Integer)cells[i][1]).intValue(),
				// ((Integer)cells[i][2]).intValue());
			}
		} else {
			final Panel panel = new Panel("A Panel with a Layout");
			main.addComponent(panel);

			// panel.addComponent(new )
		}
	}

	void example_OrderedLayout(Window main, String param) {
		final VerticalLayout layout = new VerticalLayout();
		layout.addComponent(new TextField("Name"));
		layout.addComponent(new TextField("Street address"));
		layout.addComponent(new TextField("Postal code"));
		main.addComponent(layout);
	}

	void example_FormLayout(Window main, String param) {
		final FormLayout layout = new FormLayout();
		layout.addComponent(new TextField("Text Field"));
		layout.addComponent(new CheckBox("Check Box"));
		layout.addComponent(new Select("Select"));
		main.addComponent(layout);
	}

	void example_Form(Window main, String param) {
		if (param != null && param.equals("simple")) {
			main.addComponent(new FormExample2());
		} else if (param != null && param.equals("layout")) {
			Form form = new Form();
			form.setCaption("Form Caption");
			form
					.setDescription("This is a description of the Form that is "
							+ "displayed in the upper part of the form. You normally enter some "
							+ "descriptive text about the form and its use here.");

			// Add a field directly to the layout. This field will not be bound
			// to
			// the data source Item of the form.
			form.getLayout().addComponent(new TextField("A Field"));

			// Add a field and bind it to an named item property.
			form.addField("another", new TextField("Another Field"));

			form.setComponentError(new UserError(
					"This is the error indicator of the Form."));

			// Set the footer layout and add some text.
			form.setFooter(new VerticalLayout());
			form
					.getFooter()
					.addComponent(
							new Label(
									"This is the footer area of the Form. "
											+ "You can use any layout here. This is nice for buttons."));

			// Add an Ok (commit), Reset (discard), and Cancel buttons for the
			// form.
			HorizontalLayout okbar = new HorizontalLayout();
			okbar.setHeight("25px");
			Button okbutton = new Button("OK", form, "commit");
			okbar.addComponent(okbutton);
			okbar.setExpandRatio(okbutton, 1);
			okbar.setComponentAlignment(okbutton,
					AlignmentHandler.ALIGNMENT_RIGHT,
					AlignmentHandler.ALIGNMENT_TOP);
			okbar.addComponent(new Button("Reset", form, "discard"));
			okbar.addComponent(new Button("Cancel"));
			form.getFooter().addComponent(okbar);

			main.addComponent(form);
		} else {
			main.addComponent(new FormExample());
		}
	}

	void example_ExpandLayout(Window main, String param) {
		if (param != null && param.equals("centered")) {
			Label widget = new Label("Here is text");

			HorizontalLayout layout = new HorizontalLayout();
			layout.addComponent(widget);
			layout.setExpandRatio(widget, 1);
			layout.setComponentAlignment(widget,
					AlignmentHandler.ALIGNMENT_HORIZONTAL_CENTER,
					AlignmentHandler.ALIGNMENT_VERTICAL_CENTER);
			layout.setWidth(100, Sizeable.UNITS_PERCENTAGE);
			layout.setHeight(100, Sizeable.UNITS_PERCENTAGE);

			main.setLayout(layout);

			return;
		} else if (param != null && param.equals("window")) {
			Window window = new Window("Progress");
			window.setHeight(100, Sizeable.UNITS_PIXELS);
			window.setWidth(200, Sizeable.UNITS_PIXELS);
			main.addWindow(window);

			ProgressIndicator progress = new ProgressIndicator(new Float(0.4));
			progress.addStyleName("fullwidth");
			progress.setPollingInterval(1000000);
			progress.setIndeterminate(false);

			HorizontalLayout layout = new HorizontalLayout();
			layout.setHeight(100, Sizeable.UNITS_PERCENTAGE);
			layout.setComponentAlignment(progress,
					HorizontalLayout.ALIGNMENT_HORIZONTAL_CENTER,
					HorizontalLayout.ALIGNMENT_VERTICAL_CENTER);
			window.setLayout(layout);
			window.addComponent(progress);

			return;
		} else if (param != null && param.equals("root")) {
			final Window mainwin = main;

			// Layout to switch to
			final VerticalLayout expand2 = new VerticalLayout();
			expand2.addComponent(new Label("I am layout too."));

			// Original layout
			final VerticalLayout expand1 = new VerticalLayout();
			Button switchButton = new Button("Switch to other layout");
			switchButton.addListener(new Button.ClickListener() {
				public void buttonClick(ClickEvent event) {
					mainwin.setLayout(null);
					mainwin.setLayout(expand2);
				}
			});
			expand1.addComponent(switchButton);
			main.setLayout(expand1);

			return;
		} else if (param != null && param.equals("size")) {
			VerticalLayout layout = new VerticalLayout();
			layout.setSizeFull();
			main.setLayout(layout);

			Button button = new Button("This is a button in middle of nowhere");
			layout.addComponent(button);
			layout.setComponentAlignment(button,
					VerticalLayout.ALIGNMENT_HORIZONTAL_CENTER,
					VerticalLayout.ALIGNMENT_VERTICAL_CENTER);
			layout.setExpandRatio(button, 1.0f);
			return;
		}

		for (int w = 0; w < 2; w++) {
			final VerticalLayout layout = new VerticalLayout();

			/* Set the expanding layout as the root layout of a child window. */
			final Window window = new Window("A Child Window", layout);
			main.addWindow(window);

			/* Add some component above the expanding one. */
			layout.addComponent(new Label("Here be some component."));

			/* Create the expanding component. */
			final Table table = new Table("My Ever-Expanding Table");
			/*
			 * FIXME Java 5 -> 1.4 for (int i=0; i<5; i++)
			 * table.addContainerProperty("col "+(i+1), Integer.class, 0); for
			 * (int j=0; j<20; j++) table.addItem(new Object[]{1j,2j,3j,4j,5j},
			 * j);
			 */
			layout.addComponent(table);

			/* Designate the table to be the expanding component. */
			layout.setExpandRatio(table, 1.0f);

			/* Set it to use all available area. */
			table.setSizeFull();

			/* Add some component below the expanding one. */
			final Button button2 = new Button("Ok");
			layout.addComponent(button2);
			layout.setComponentAlignment(button2,
					AlignmentHandler.ALIGNMENT_RIGHT, 0);
		}
	}

	void example_TabSheet(Window main, String param) {
		if (param.equals("icon")) {
			final TabSheet tabsheet = new TabSheet();

			tabsheet.addTab(new Label("Contents of the first tab"),
					"First Tab", new ClassResource("images/Mercury_small.png",
							main.getApplication()));
			tabsheet.addTab(new Label("Contents of the second tab"),
					"Second Tab", new ClassResource("images/Venus_small.png",
							this));
			tabsheet.addTab(new Label("Contents of the third tab"),
					"Third tab", new ClassResource("images/Earth_small.png",
							this));

			main.addComponent(tabsheet);
			// main.addComponent(new Embedded("Emb", new ClassResource
			// ("images/Mercury_small.png", this)));
		} else if (param.equals("expanding")) {
			// Create the layout
			VerticalLayout expanding = new VerticalLayout();

			// It is important to set the expanding layout as the root layout
			// of the containing window, in this case the main window, and not
			// use addComponent(), which would place the layout inside the
			// default root layout.
			main.setLayout(expanding);

			// Create a tab sheet that fills the expanding layout
			final TabSheet tabsheet = new TabSheet();
			tabsheet.addTab(new Label("Contents of the first tab"),
					"First Tab", null);
			tabsheet.addTab(new Label("Contents of the second tab"),
					"Second Tab", null);
			tabsheet.addTab(new Label("Contents of the third tab"),
					"Third tab", null);

			// Set the tabsheet to scale to full size inside its container
			tabsheet.setWidth(100, Sizeable.UNITS_PERCENTAGE);
			tabsheet.setHeight(100, Sizeable.UNITS_PERCENTAGE);

			// Add the tab sheet to the layout as usual
			expanding.addComponent(tabsheet);

			// Set the tab sheet to be the expanding component
			expanding.setExpandRatio(tabsheet, 1);
		} else if (param.equals("ordered")) {
			// Create the layout
			VerticalLayout layout = new VerticalLayout();

			// It is important to set the expanding layout as the root layout
			// of the containing window, in this case the main window, and not
			// use addComponent(), which would place the layout inside the
			// default root layout.
			main.setLayout(layout);

			// Create a tab sheet that fills the expanding layout
			final TabSheet tabsheet = new TabSheet();
			tabsheet.addTab(new Label("Contents of the first tab"),
					"First Tab", null);
			tabsheet.addTab(new Label("Contents of the second tab"),
					"Second Tab", null);
			tabsheet.addTab(new Label("Contents of the third tab"),
					"Third tab", null);

			// Set the tabsheet to scale to full size inside its container
			tabsheet.setWidth(100, Sizeable.UNITS_PERCENTAGE);
			// tabsheet().setHeight(100, Sizeable.UNITS_PERCENTAGE);

			// Add the tab sheet to the layout as usual
			layout.addComponent(tabsheet);
		} else {
			main.addComponent(new TabSheetExample());
		}
	}

	void example_Embedded(Window main, String param) {
		final Embedded image = new Embedded("", new ClassResource("smiley.jpg",
				this));
		image.addStyleName("omaimage");
		main.addComponent(image);

		final EmbeddedButton button = new EmbeddedButton(new ClassResource(
				"smiley.jpg", this));
		main.addComponent(button);
	}

	void example_Window(Window main, String param) {
		if (param != null) {
			if (param.equals("opener")) {
				main.addComponent(new WindowOpener("Window Opener", main));
			} else if (param.equals("multiple")) {
				/* Create a new window. */
				final Window mywindow = new Window("Second Window");
				mywindow.setName("mywindow");
				mywindow.addComponent(new Label("This is a second window."));

				/* Add the window to the application. */
				main.getApplication().addWindow(mywindow);

				/* Add link to the second window in the main window. */
				main.addComponent(new Label("Second window: <a href='"
						+ mywindow.getURL() + "'>middle-click to open</a>",
						Label.CONTENT_XHTML));
				main.addComponent(new Label(
						"The second window can be accessed through URL: "
								+ mywindow.getURL()));
			}
			return;
		}

		/* Create a new window. */
		final Window mywindow = new Window("My Window");
		mywindow.setName("mywindow");

		/* Add some components in the window. */
		mywindow.addComponent(new Label("A text label in the window."));
		final Button okbutton = new Button("OK");
		mywindow.addComponent(okbutton);

		/* Set window size. */
		mywindow.setHeight("200px");
		mywindow.setWidth("400px");

		/* Set window position. */
		mywindow.setPositionX(200);
		mywindow.setPositionY(50);

		/* Add the window to the Application object. */
		main.addWindow(mywindow);

	}

	void example_ClassResource(Window main, String param) {
		final DateField df = new DateField();
		main.addComponent(df);
		df.setIcon(new ClassResource("smiley.jpg", main.getApplication()));
		main.addComponent(new Embedded("This is Embedded", new ClassResource(
				"smiley.jpg", main.getApplication())));
	}

	void example_ProgressIndicator(final Window main, String param) {
		if (param != null) {
			if (param.equals("thread")) {

				// Create the indicator
				final ProgressIndicator indicator = new ProgressIndicator(
						new Float(0.0));
				main.addComponent(indicator);

				// Set polling frequency to 0.5 seconds.
				indicator.setPollingInterval(1000);

				// indicator.addStyleName("invisible");
				final Label text = new Label("-- Not running --");
				main.addComponent(text);

				// Add a button to start the progress
				final Button button = new Button("Click to start");
				main.addComponent(button);

				// Another thread to do some work
				class WorkThread extends Thread {
					@Override
					public void run() {
						double current = 0.0;
						while (true) {
							// Do some "heavy work"
							try {
								sleep(50); // Sleep for 50 milliseconds
							} catch (InterruptedException e) {
							}

							// Grow the progress value until it reaches 1.0.
							current += 0.01;
							if (current > 1.0) {
								indicator.setValue(new Float(1.0));
							} else {
								indicator.setValue(new Float(current));
							}

							// After the progress is full for a while, stop.
							if (current > 1.2) {
								// Restore the state to initial.
								indicator.setValue(new Float(0.0));
								button.setVisible(true);
								break;
							}
						}
					}
				}

				// Clicking the button creates and runs a work thread
				button.addListener(new Button.ClickListener() {
					public void buttonClick(ClickEvent event) {
						final WorkThread thread = new WorkThread();
						thread.start();

						// The button hides until the work is done.
						button.setVisible(false);
					}
				});
			} else if (param.equals("window")) {
				// Create a table in the main window to hold items added in the
				// second window
				final Table table = new Table();
				table.setPageLength(5);
				table.setWidth(100, Sizeable.UNITS_PERCENTAGE);
				table.addContainerProperty("Name", String.class, "");
				main.addComponent(table);

				// Create the second window
				final Window adderWindow = new Window("Add Items");
				adderWindow.setName("win-adder");
				main.getApplication().addWindow(adderWindow);

				// Create selection component to add items to the table
				final NativeSelect select = new NativeSelect(
						"Select item to add");
				select.setImmediate(true);
				adderWindow.addComponent(select);

				// Add some items to the selection
				String items[] = new String[] { "-- Select --", "Mercury",
						"Venus", "Earth", "Mars", "Jupiter", "Saturn",
						"Uranus", "Neptune" };
				for (int i = 0; i < items.length; i++) {
					select.addItem(items[i]);
				}
				select.setNullSelectionItemId(items[0]);

				// When an item is selected in the second window, add
				// table in the main window
				select.addListener(new ValueChangeListener() {
					public void valueChange(ValueChangeEvent event) {
						// If the selected value is something else but null
						// selection item.
						if (select.getValue() != null) {
							// Add the selected item to the table in the main
							// window
							table.addItem(new Object[] { select.getValue() },
									new Integer(table.size()));
						}
					}
				});

				// Link to open the selection window
				Link link = new Link("Click to open second window",
						new ExternalResource(adderWindow.getURL()), "_new", 50,
						200, Link.TARGET_BORDER_DEFAULT);
				main.addComponent(link);

				// Enable polling to update the main window
				ProgressIndicator poller = new ProgressIndicator();
				poller.addStyleName("invisible");
				main.addComponent(poller);
			} else if (param.equals("centered")) {
				/*
				 * GridLayout grid = new GridLayout(3,3); main.setLayout(grid);
				 * grid().setWidth(100, Sizeable.UNITS_PERCENTAGE);
				 * 
				 * ExpandLayout layout2 = new
				 * ExpandLayout(OrderedLayout.ORIENTATION_HORIZONTAL);
				 * layout2().setWidth(50, Sizeable.UNITS_PERCENTAGE);
				 * 
				 * ProgressIndicator poller = new ProgressIndicator(new
				 * Float(0.4)); poller.setPollingInterval(1000000);
				 * poller.setIndeterminate(false); layout2.addComponent(poller);
				 * 
				 * grid.addComponent(layout2, 1, 1);
				 */

				// ExpandLayout layout2 = new
				// ExpandLayout(OrderedLayout.ORIENTATION_HORIZONTAL);
				/*
				 * ProgressIndicator poller = new ProgressIndicator(new
				 * Float(0.4)); poller.setPollingInterval(1000000);
				 * poller.setIndeterminate(false);
				 */
				/*
				 * layout2.addComponent(poller); layout2().setWidth(50,
				 * Sizeable.UNITS_PERCENTAGE);
				 */

				// layout.setComponentAlignment(poller,
				// AlignmentHandler.ALIGNMENT_HORIZONTAL_CENTER,
				// AlignmentHandler.ALIGNMENT_VERTICAL_CENTER);
				/*
				 * GridLayout grid = new GridLayout(1,1);
				 * grid.addComponent(layout2, 0, 0); grid().setWidth(100,
				 * Sizeable.UNITS_PERCENTAGE);
				 */

				/*
				 * GridLayout layout = new GridLayout(1,1);
				 * //OrderedLayout.ORIENTATION_HORIZONTAL);
				 * layout.addComponent(poller); //layout.expand(poller);
				 * layout.setComponentAlignment(poller,
				 * AlignmentHandler.ALIGNMENT_HORIZONTAL_CENTER,
				 * AlignmentHandler.ALIGNMENT_VERTICAL_CENTER);
				 * layout().setWidth(100, Sizeable.UNITS_PERCENTAGE);
				 * layout().setHeight(100, Sizeable.UNITS_PERCENTAGE);
				 */

			}
		} else {
			ProgressIndicator poller = new ProgressIndicator(new Float(0.0));
			poller.setPollingInterval(1000000);
			poller.setIndeterminate(true);
			main.addComponent(poller);
		}
	}

	void example_CustomLayout(final Window main, String param) {
		Window sub = new Window("Login");
		sub.setModal(true);
		main.addWindow(sub);

		// Create the custom layout and set it as the root layout of
		// the containing window.
		final CustomLayout custom = new CustomLayout("layoutname");
		sub.setLayout(custom);

		// Create components and bind them to the location tags
		// in the custom layout.
		TextField username = new TextField();
		custom.addComponent(username, "username");

		TextField password = new TextField();
		custom.addComponent(password, "password");

		final Button ok = new Button("Login");
		custom.addComponent(ok, "okbutton");

		final Button deny = new Button("No can do!");

		Button.ClickListener listener = new Button.ClickListener() {
			public void buttonClick(ClickEvent event) {
				// Switch between ok and deny
				if (custom.getComponent("okbutton") == ok) {
					System.out.println("Changing to deny button.");
					custom.addComponent(deny, "okbutton");
				} else {
					System.out.println("Changing to ok button.");
					custom.addComponent(ok, "okbutton");
				}
			}
		};

		ok.addListener(listener);
		deny.addListener(listener);
	}

	void example_Spacing(final Window main, String param) {
		VerticalLayout containinglayout = new VerticalLayout();
		main.setLayout(containinglayout);

		GridLayout grid = new GridLayout(4, 3);
		grid.addStyleName("spacingexample");
		containinglayout.addComponent(grid);
		grid.addComponent(new Label(""), 0, 0);
		grid.addComponent(new Label(""), 1, 0);

		grid.addComponent(new Label("No spacing:"), 0, 1);
		HorizontalLayout layout1 = new HorizontalLayout();
		grid.addComponent(layout1, 1, 1);
		layout1.addStyleName("spacingexample");
		layout1.addComponent(new Button("Component 1"));
		layout1.addComponent(new Button("Component 2"));
		layout1.addComponent(new Button("Component 3"));

		grid.addComponent(new Label("Horizontal spacing:"), 0, 2);
		HorizontalLayout layout2 = new HorizontalLayout();
		grid.addComponent(layout2, 1, 2);
		layout2.addStyleName("spacingexample");
		layout2.setSpacing(true);
		layout2.addComponent(new Button("Component 1"));
		layout2.addComponent(new Button("Component 2"));
		layout2.addComponent(new Button("Component 3"));

		grid.addComponent(new Label("No spacing:"), 2, 0);
		VerticalLayout layout3 = new VerticalLayout();
		grid.addComponent(layout3, 2, 1, 2, 2);
		layout3.addStyleName("spacingexample");
		layout3.addComponent(new Button("Component 1"));
		layout3.addComponent(new Button("Component 2"));
		layout3.addComponent(new Button("Component 3"));

		grid.addComponent(new Label("Vertical spacing:"), 3, 0);
		VerticalLayout layout4 = new VerticalLayout();
		grid.addComponent(layout4, 3, 1, 3, 2);
		layout4.addStyleName("spacingexample");
		layout4.setSpacing(true);
		layout4.addComponent(new Button("Component 1"));
		layout4.addComponent(new Button("Component 2"));
		layout4.addComponent(new Button("Component 3"));
	}

	void example_Margin(final Window main, String param) {
		HorizontalLayout hor = new HorizontalLayout();
		main.setLayout(hor);

		VerticalLayout containinglayout = new VerticalLayout();
		hor.addComponent(containinglayout);

		VerticalLayout layout1 = new VerticalLayout();
		containinglayout.addComponent(new Label("Regular layout margins:"));
		containinglayout.addComponent(layout1);
		layout1.addStyleName("marginexample1");
		layout1.addComponent(new Button("Component 1"));
		layout1.addComponent(new Button("Component 2"));
		layout1.addComponent(new Button("Component 3"));

		// Create a layout
		VerticalLayout layout2 = new VerticalLayout();
		containinglayout.addComponent(new Label(
				"Layout with a special margin element:"));
		containinglayout.addComponent(layout2);

		// Set style name for the layout to allow styling it
		layout2.addStyleName("marginexample2");

		// Have margin on all sides around the layout
		layout2.setMargin(true);

		// Put something inside the layout
		layout2.addComponent(new Button("Component 1"));
		layout2.addComponent(new Button("Component 2"));
		layout2.addComponent(new Button("Component 3"));
	}

	void example_ClientInfo(final Window main, String param) {
		// Get the client identification string
		WebApplicationContext context2 = (WebApplicationContext) getContext();
		String browserApplication = context2.getBrowser()
				.getBrowserApplication();

		// Add a browser-dependent style name for the main window
		if (browserApplication.indexOf("Firefox/2") != -1) {
			main.addStyleName("firefox2");
		}

		// Display the client identification string
		main.addComponent(new Label(browserApplication));
	}

	void example_FillInForm(final Window main, String param) {
		if (param.equals("templates")) {
			// Create a custom layout from the fill-in-form.html template.
			CustomLayout fillinlayout = new CustomLayout("fill-in-form");

			// The style will set the display to be "inline".
			fillinlayout.addStyleName("fillinlayout");

			// Create the fields that occur in the text.
			TextField field1 = new TextField();
			TextField field2 = new TextField();
			fillinlayout.addComponent(field1, "q1");
			fillinlayout.addComponent(field2, "q2");

			main.addComponent(fillinlayout);
		} else {
			String fillintext = "The <q1> is mightier than <q2>.";
			int pos = 0;
			while (pos < fillintext.length()) {
				int nexttag = fillintext.indexOf("<", pos);
				if (nexttag == -1) {

				}
			}
		}
	}

	void example_Notification(final Window main, String param) {
		// final Window sub1 = new Window("");
		// main.addWindow(sub1);
		if (param.equals("example")) {
			main.showNotification("This is the caption",
					"This is the description");
			return;
		} else if (param.equals("type")) {
			main.showNotification("This is a warning",
					"<br/>This is the <i>last</i> warning",
					Window.Notification.TYPE_WARNING_MESSAGE);
			return;
		} else if (param.equals("pos")) {
			// Create a notification with the default settings for a warning.
			Window.Notification notif = new Window.Notification("Be warned!",
					"This message lurks in the top-left corner!",
					Window.Notification.TYPE_WARNING_MESSAGE);

			// Set the position.
			notif.setPosition(Window.Notification.POSITION_TOP_LEFT);

			// Let it stay there until the user clicks it
			notif.setDelayMsec(-1);

			// Show it in the main window.
			main.showNotification(notif);
			return;
		}

		main.setLayout(new HorizontalLayout());

		final Integer type_humanized = Window.Notification.TYPE_HUMANIZED_MESSAGE;
		final Integer type_warning = Window.Notification.TYPE_WARNING_MESSAGE;
		final Integer type_error = Window.Notification.TYPE_ERROR_MESSAGE;
		final Integer type_tray = Window.Notification.TYPE_TRAY_NOTIFICATION;
		final NativeSelect types = new NativeSelect();
		main.addComponent(types);
		types.addItem(type_humanized);
		types.addItem(type_warning);
		types.addItem(type_error);
		types.addItem(type_tray);
		types.setItemCaption(type_humanized, "Humanized");
		types.setItemCaption(type_warning, "Warning");
		types.setItemCaption(type_error, "Error");
		types.setItemCaption(type_tray, "Tray");

		Button show = new Button("Show Notification");
		main.addComponent(show);

		show.addListener(new Button.ClickListener() {
			public void buttonClick(ClickEvent event) {
				String caption = "";
				String description = "";
				switch (((Integer) types.getValue()).intValue()) {
				case Window.Notification.TYPE_HUMANIZED_MESSAGE:
					caption = "Humanized message";
					description = "<br/>For minimal annoyance";
					break;
				case Window.Notification.TYPE_WARNING_MESSAGE:
					caption = "Warning message";
					description = "<br/>For notifications of medium importance";
					break;
				case Window.Notification.TYPE_ERROR_MESSAGE:
					caption = "Error message";
					description = "<br/>For important notifications";
					break;
				case Window.Notification.TYPE_TRAY_NOTIFICATION:
					caption = "Tray notification";
					description = "<br/>Stays up longer - but away";
				}
				// main.showNotification("The default notification");
				Window.Notification notif = new Window.Notification(caption,
						description, (Integer) types.getValue());
				// notif.setPosition(Window.Notification.POSITION_TOP_LEFT);
				notif.setDelayMsec(-1);
				main.showNotification(notif);
			}
		});

		// Notification notif = new Notification("Title");
	}

	void example_Print(final Window main, String param) {
		if (param != null && param.equals("simple")) {
			main
					.addComponent(new Label(
							"<input type='button' onClick='print()' value='Click to Print'/>",
							Label.CONTENT_XHTML));
			return;
		}

		// A button to open the printer-friendly page.
		Button printButton = new Button("Click to Print");
		main.addComponent(printButton);
		printButton.addListener(new Button.ClickListener() {
			public void buttonClick(ClickEvent event) {
				// Create a window that contains stuff you want to print.
				Window printWindow = new Window("Window to Print");

				// Have some content to print.
				printWindow.addComponent(new Label(
						"Here's some dynamic content."));

				// To execute the print() JavaScript, we need to run it
				// from a custom layout.
				CustomLayout scriptLayout = new CustomLayout("printpage");
				printWindow.addComponent(scriptLayout);

				// Add the printing window as an application-level window.
				main.getApplication().addWindow(printWindow);

				// Open the printing window as a new browser window
				main.open(new ExternalResource(printWindow.getURL()), "_new");
			}
		});

		// main.addComponent(new
		// Label("<p>Print this!</p>\n<script type='text/javascript'>print();</script>",
		// Label.CONTENT_XHTML));
	}

	void example_RichTextArea(final Window main, String param) {
		main.setLayout(new HorizontalLayout());

		// Create a rich text area
		final RichTextArea rtarea = new RichTextArea();
		rtarea.addStyleName("richtextexample");
		// rtarea.setCaption("My Rich Text Area");

		// Set initial content as HTML
		rtarea
				.setValue("<h1>Hello</h1>\n<p>This rich text area contains some text.</p>");

		// Show the text edited in the rich text area as HTML.
		final Button show = new Button("Show HTML");
		final Label html = new Label((String) rtarea.getValue());
		show.addListener(new Button.ClickListener() {
			public void buttonClick(ClickEvent event) {
				html.setValue(rtarea.getValue());
			}
		});

		Panel rtPanel = new Panel("Rich Text Area");
		rtPanel.addComponent(rtarea);
		rtPanel.addComponent(show);

		Panel valuePanel = new Panel("Value");
		valuePanel.addComponent(html);

		main.addComponent(rtPanel);
		main.addComponent(valuePanel);
	}

	void example_QueryContainer(final Window main, String param) {
		try {
			// Create a database connection
			Class.forName("org.hsqldb.jdbcDriver");
			final Connection connection = DriverManager.getConnection(
					"jdbc:hsqldb:mem:qcexample", "sa", "");

			// Create an example table and put some data in it.
			Statement st = connection.createStatement();
			st
					.executeQuery("CREATE TABLE Prisoners (id INTEGER, name VARCHAR)");
			st.close();
			for (int i = 0; i < 100; i++) {
				st = connection.createStatement();
				st.executeQuery("INSERT INTO Prisoners (id, name) VALUES (" + i
						+ ",'I am number " + (i + 1) + "')");
				st.close();
			}

			// Query the database
			final QueryContainer qc = new QueryContainer(
					"SELECT id,name FROM Prisoners", connection);

			// Create a component for selecting a query result item.
			Select select = new Select("Select an item");

			// The items shown in the selection component are obtained from the
			// query.
			select.setContainerDataSource(qc);

			// The item captions are obtained from a field in the query result.
			select.setItemCaptionMode(Select.ITEM_CAPTION_MODE_PROPERTY);

			// Set the name of the field from which the item captions are
			// obtained.
			select.setItemCaptionPropertyId("name");

			// When selection changes, display the selected item.
			select.setImmediate(true);
			final Label selection = new Label("Currently selected: -");
			select.addListener(new ValueChangeListener() {
				public void valueChange(ValueChangeEvent event) {
					// Get the item id of the currently selected item
					Integer itemId = (Integer) event.getProperty().getValue();

					// Use the item ID to get the actual row from the query
					// result.
					Item qrItem = qc.getItem(itemId);

					// Display the item ID
					selection.setValue("Currently selected: result row "
							+ itemId.intValue() + " (id="
							+ qrItem.getItemProperty("id") + ", " + "name="
							+ qrItem.getItemProperty("name") + ")");
				}
			});

			main.addComponent(select);
			main.addComponent(selection);
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}

	void example_MenuBar(final Window main, String param) {
		// Create a menu bar
		final MenuBar menubar = new MenuBar();
		main.addComponent(menubar);

		// A feedback component
		final Label selection = new Label("");
		main.addComponent(selection);

		// Define a common menu command for all the menu items.
		MenuBar.Command mycommand = new MenuBar.Command() {
			public void menuSelected(MenuItem selectedItem) {
				selection.setValue("Ordered a " + selectedItem.getText()
						+ " from menu.");
			}
		};

		// Put some items in the menu hierarchically
		MenuBar.MenuItem beverages = menubar.addItem("Beverages", null, null);
		MenuBar.MenuItem hot_beverages = beverages.addItem("Hot", null, null);
		hot_beverages.addItem("Tea", null, mycommand);
		hot_beverages.addItem("Coffee", null, mycommand);
		MenuBar.MenuItem cold_beverages = beverages.addItem("Cold", null, null);
		cold_beverages.addItem("Milk", null, mycommand);

		// Another top-level item
		MenuBar.MenuItem snacks = menubar.addItem("Snacks", null, null);
		snacks.addItem("Weisswurst", null, mycommand);
		snacks.addItem("Salami", null, mycommand);

		// Yet another top-level item
		MenuBar.MenuItem services = menubar.addItem("Services", null, null);
		services.addItem("Car Service", null, mycommand);
	}
}