Skip to main content
summaryrefslogtreecommitdiffstats
blob: 862b26bc295dffc95a7463211d567f92a74befc2 (plain) (blame)
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
/*******************************************************************************
 * Copyright (c) 2006 Sybase, Inc. and others.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Sybase, Inc. - initial API and implementation
 *******************************************************************************/
package org.eclipse.jst.jsf.common.ui.internal.guiutils;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.events.ControlEvent;
import org.eclipse.swt.events.ControlListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.Text;

/**
 * This utility class provides convenience methods in creating controls on
 * preference pages.
 * 
 * @author mengbo
 */
public class SWTUtils {
	// Defaults of controls
	private static final int DEFAULT_BUTTON_WIDTH = 70;

	private static final int DEFAULT_COMBO_WIDTH = 100;

	private static final int DEFAULT_TEXTBOX_WIDTH = 100;

	private static final int DEFAULT_RADIO_FILL = GridData.HORIZONTAL_ALIGN_BEGINNING
			| GridData.VERTICAL_ALIGN_CENTER;

	/**
	 * Creates a new checkbox and sets the default layout data.
	 * 
	 * @param parent
	 *            the composite in which to create the checkbox
	 * @return the new checkbox
	 */
	public static Button createCheckBox(Composite parent) {
		return createCheckBox(parent, null, 1, 0);
	}

	/**
	 * Creates a new checkbox and sets the default layout data.
	 * 
	 * @param parent
	 *            the composite in which to create the checkbox
	 * @param label
	 *            the string to set into the checkbox
	 * @param numColumns
	 *            the number of columns the new checkbox is to occupy
	 * @return the new checkbox
	 */
	public static Button createCheckBox(Composite parent, String label,
			int numColumns) {
		return createCheckBox(parent, label, numColumns, 0);
	}

	/**
	 * Creates a new checkbox and sets the default layout data.
	 * 
	 * @param parent
	 *            the composite in which to create the checkbox
	 * @param label
	 *            the string to set into the checkbox
	 * @param numColumns
	 *            the number of columns the new checkbox is to occupy
	 * @param indent
	 *            the number of pixels to indent from the left
	 * @return the new checkbox
	 */
	public static Button createCheckBox(Composite parent, String label,
			int numColumns, int indent) {
		Button button = new Button(parent, SWT.CHECK | SWT.LEFT);
		if (label == null) {
			button.setAlignment(SWT.CENTER);
		}
		GridData data = new GridData(GridData.FILL);
		data.horizontalSpan = numColumns;
		data.horizontalIndent = indent;
		button.setLayoutData(data);
		if (label != null) {
			button.setText(label);
		}
		return button;
	}

	/**
	 * Creates a combo box and sets the default layout data.
	 * 
	 * @param parent
	 *            the composite in which to create the combo
	 * @param items
	 *            the items in the combo
	 * @param numColumns
	 *            the number of columns the new combo is to occupy
	 * @return the new combo box
	 */
	public static Combo createCombo(Composite parent, String[] items,
			int numColumns) {
		return createCombo(parent, items, numColumns, DEFAULT_COMBO_WIDTH);
	}

	/**
	 * Creates a combo box and sets the default layout data.
	 * 
	 * @param parent
	 *            the composite in which to create the combo
	 * @param tokenString
	 *            a tokenized string that will be split into the fields.
	 * @param numColumns
	 *            the number of columns the new combo is to occupy
	 * @return the new combo box
	 */
	public static Combo createCombo(Composite parent, String tokenString,
			int numColumns) {
		return createCombo(parent, getTokenNames(tokenString), numColumns,
				DEFAULT_COMBO_WIDTH);
	}

	/**
	 * Creates a combo box and sets the default layout data.
	 * 
	 * @param parent
	 *            the composite in which to create the combo
	 * @param tokenString
	 *            a tokenized string that will be split into the fields.
	 * @param numColumns
	 *            the number of columns the new combo is to occupy
	 * @param minWidth
	 *            minimum width of combo box in pixels
	 * @return the new combo box
	 */
	public static Combo createCombo(Composite parent, String tokenString,
			int numColumns, int minWidth) {
		return createCombo(parent, getTokenNames(tokenString), numColumns,
				minWidth);
	}

	/**
	 * Creates a combo box and sets the default layout data.
	 * 
	 * @param parent
	 *            the composite in which to create the combo
	 * @param items
	 *            the items in the combo
	 * @param numColumns
	 *            the number of columns the new combo is to occupy
	 * @param minWidth
	 *            minimum width of combo box in pixels
	 * @return the new combo box
	 */
	public static Combo createCombo(Composite parent, String[] items,
			int numColumns, int minWidth) {
		return createCombo(parent, items, numColumns, minWidth, false);
	}

	/**
	 * Creates a combo box and sets the default layout data.
	 * 
	 * @param parent
	 *            the composite in which to create the combo
	 * @param tokenString 
	 * @param numColumns
	 *            the number of columns the new combo is to occupy
	 * @param minWidth
	 *            minimum width of combo box in pixels
	 * @param editable
	 *            whether the items in the combo is editable
	 * @return the new combo box
	 */
	public static Combo createCombo(Composite parent, String tokenString,
			int numColumns, int minWidth, boolean editable) {
		return createCombo(parent, getTokenNames(tokenString), numColumns,
				minWidth, editable);
	}

	/**
	 * Creates a combo box and sets the default layout data.
	 * 
	 * @param parent
	 *            the composite in which to create the combo
	 * @param items
	 *            the items in the combo
	 * @param numColumns
	 *            the number of columns the new combo is to occupy
	 * @param minWidth
	 *            minimum width of combo box in pixels
	 * @param editable
	 *            whether the items in the combo is editable
	 * @return the new combo box
	 */
	public static Combo createCombo(Composite parent, String[] items,
			int numColumns, int minWidth, boolean editable) {
		Combo combo;
		GridData data;
		if (editable) {
			combo = new Combo(parent, SWT.DROP_DOWN);
			data = new GridData(GridData.FILL_HORIZONTAL);
		} else {
			combo = new Combo(parent, SWT.DROP_DOWN | SWT.READ_ONLY);
			data = new GridData(GridData.FILL);
		}
		data.horizontalSpan = numColumns;
		data.widthHint = minWidth;
		combo.setLayoutData(data);
		combo.setItems(items);
		return combo;
	}

	/**
	 * Creates composite control and sets the default layout data.
	 * 
	 * @param parent
	 *            the parent of the new composite
	 * @param numColumns
	 *            the number of columns for the new composite
	 * @return the newly-created coposite
	 */
	public static Composite createComposite(Composite parent, int numColumns) {
		return createComposite(parent, numColumns, -1, -1,
				GridData.FILL_HORIZONTAL, -1, -1, -1);
	}

	/**
	 * Creates composite control and sets the default layout data.
	 * 
	 * @param parent
	 *            the parent of the new composite
	 * @param numColumns
	 *            the number of columns for the new composite
	 * @param verticalSpacing
	 *            the spacing between rows.
	 * @param horizontalSpan
	 *            the span for this new composite over the original composite.
	 * @return the newly-created coposite
	 */
	public static Composite createComposite(Composite parent, int numColumns,
			int verticalSpacing, int horizontalSpan) {
		return createComposite(parent, numColumns, verticalSpacing,
				horizontalSpan, GridData.FILL_HORIZONTAL, -1, -1, -1);
	}

	/**
	 * Creates composite control and sets the default layout data.
	 * 
	 * @param parent
	 *            the parent of the new composite
	 * @param numColumns
	 *            the number of columns for the new composite
	 * @param verticalSpacing
	 *            the spacing between rows.
	 * @param horizontalSpan
	 *            the span for this new composite over the original composite.
	 * @param gridDataFill
	 *            the fill to use for this composite.
	 * @return the newly-created coposite
	 */
	public static Composite createComposite(Composite parent, int numColumns,
			int verticalSpacing, int horizontalSpan, int gridDataFill) {
		return createComposite(parent, numColumns, verticalSpacing,
				horizontalSpan, gridDataFill, -1, -1, -1);
	}

	/**
	 * Creates composite control and sets the default layout data.
	 * 
	 * @param parent
	 *            the parent of the new composite
	 * @param numColumns
	 *            the number of columns for the new composite
	 * @param verticalSpacing
	 *            the spacing between rows.
	 * @param horizontalSpan
	 *            the span for this new composite over the original composite.
	 * @param gridDataFill
	 *            the fill to use for this composite.
	 * @param horizontalSpacing
	 *            the spacing between objects.
	 * @param marginWidth
	 *            the spacing at start and end of composite.
	 * @param marginHeight
	 *            the spacing above and below composite.
	 * @return the newly-created coposite
	 */
	public static Composite createComposite(Composite parent, int numColumns,
			int verticalSpacing, int horizontalSpan, int gridDataFill,
			int horizontalSpacing, int marginWidth, int marginHeight) {
		Composite composite = new Composite(parent, SWT.NULL);
		GridLayout layout = new GridLayout();
		layout.numColumns = numColumns;
		if (verticalSpacing >= 0) {
			layout.verticalSpacing = verticalSpacing;
		}
		if (horizontalSpacing >= 0) {
			layout.horizontalSpacing = horizontalSpacing;
		}
		if (marginWidth >= 0) {
			layout.marginWidth = marginWidth;
		}
		if (marginHeight >= 0) {
			layout.marginHeight = marginHeight;
		}
		composite.setLayout(layout);
		GridData gd = new GridData(gridDataFill);
		if (horizontalSpan > 0) {
			gd.horizontalSpan = horizontalSpan;
		}
		composite.setLayoutData(gd);

		return composite;
	}

	/**
	 * Utility method that creates a group and sets the default layout data.
	 * 
	 * @param parent
	 *            the parent for the new group
	 * @param title
	 *            the label for the new group
	 * @param numColumns
	 *            the number of columns for the new group
	 * @return the newly created group
	 */
	public static Group createGroup(Composite parent, String title,
			int numColumns) {
		return createGroup(parent, title, numColumns, -1,
				GridData.FILL_HORIZONTAL);
	}

	/**
	 * Utility method that creates a group and sets the default layout data.
	 * 
	 * @param parent
	 *            the parent for the new group
	 * @param title
	 *            the label for the new group
	 * @param numColumns
	 *            the number of columns for the new group
	 * @param horizontalSpan
	 *            the number of columns this group should span on the parent
	 *            composite.
	 * @param gridDataFill
	 *            the fill style of the new group -- set to for filling just
	 *            around the object: GridData.BEGINNING | GridData.CENTER
	 * @return the newly created group
	 */
	public static Group createGroup(Composite parent, String title,
			int numColumns, int horizontalSpan, int gridDataFill) {
		Group group = new Group(parent, SWT.SHADOW_ETCHED_IN);
		GridLayout layout = new GridLayout();
		layout.numColumns = numColumns;
		group.setLayout(layout);
		GridData data = new GridData(gridDataFill);

		if (horizontalSpan > 0) {
			data.horizontalSpan = horizontalSpan;
		}
		group.setLayoutData(data);
		group.setText(title);
		return group;
	}

	/**
	 * Utility method that creates a label instance and sets the default layout
	 * data.
	 * 
	 * @param parent
	 *            the parent for the new label
	 * @param text
	 *            the text for the new label
	 * @param numColumns
	 *            the number of columns for the new composite
	 * @return the new label
	 */
	public static Label createLabel(Composite parent, String text,
			int numColumns) {
		return createLabel(parent, text, numColumns, 0);
	}

	/**
	 * Utility method that creates a label instance and sets the default layout
	 * data.
	 * 
	 * @param parent
	 *            the parent for the new label
	 * @param text
	 *            the text for the new label
	 * @param numColumns
	 *            the number of columns for the new composite
	 * @param indent
	 *            number of pixels to indent from the left
	 * @return the new label
	 */
	public static Label createLabel(Composite parent, String text,
			int numColumns, int indent) {
		Label label = new Label(parent, SWT.LEFT);
		GridData data = new GridData();
		data.horizontalSpan = numColumns;
		data.horizontalAlignment = GridData.FILL;
		data.horizontalIndent = indent;
		label.setLayoutData(data);
		label.setText(text);
		return label;
	}

	/**
	 * Create a image label for sticking in a composite. The backgroud color is
	 * optional. Because images can have "transparent" natures, you might want
	 * to say the background is something other than the defaults composites
	 * background.
	 * 
	 * NOTE: the caller is responsible for cleanup of the image and color
	 * objects.
	 * 
	 * @param parent
	 *            the parent for the new label
	 * @param theImage
	 *            the image for the new label
	 * @param numColumns
	 *            the number of columns for the new composite
	 * @param background
	 *            pass null to use the composites background.
	 * @return the new label
	 */
	public static Label createLabelImage(Composite parent, Image theImage,
			int numColumns, Color background) {
		Label label = new Label(parent, SWT.LEFT);
		GridData data = new GridData();
		data.horizontalSpan = numColumns;
		data.horizontalAlignment = GridData.FILL;
		label.setLayoutData(data);
		if (background != null) {
			label.setBackground(background);
		}
		label.setImage(theImage);
		return label;
	}

	/**
	 * Utility method that creates a push button instance and sets the default
	 * layout data.
	 * 
	 * @param parent
	 *            the parent for the new button
	 * @param label
	 *            the label for the new button
	 * @return the newly-created button
	 */
	public static Button createPushButton(Composite parent, String label) {
		return createPushButton(parent, label, DEFAULT_BUTTON_WIDTH);
	}

	/**
	 * Utility method that creates a push button instance and sets the default
	 * layout data.
	 * 
	 * @param parent
	 *            the parent for the new button
	 * @param label
	 *            the label for the new button
	 * @param widthHint
	 *            use this width for the button.
	 * @return the newly-created button
	 */
	public static Button createPushButton(Composite parent, String label,
			int widthHint) {
		Button button = new Button(parent, SWT.PUSH);
		GridData data = new GridData();
		data.horizontalAlignment = GridData.FILL_HORIZONTAL;
		data.widthHint = widthHint;
		button.setLayoutData(data);
		button.setText(label);
		return button;
	}

	/**
	 * Utility method that creates a push button instance and sets the default
	 * layout data.
	 * 
	 * @param parent
	 *            the parent for the new button
	 * @param theImage
	 *            the label for the new button
	 * @param widthHint
	 *            use this width for the button.
	 * @return the newly-created button
	 */
	public static Button createPushButton(Composite parent, Image theImage,
			int widthHint) {
		Button button = new Button(parent, SWT.PUSH);
		GridData data = new GridData();
		data.horizontalAlignment = GridData.FILL_HORIZONTAL;
		data.widthHint = widthHint;
		button.setLayoutData(data);
		button.setImage(theImage);
		button.setAlignment(SWT.CENTER);
		return button;
	}

	/**
	 * Utility method that creates a radio button instance and sets the default
	 * layout data.
	 * 
	 * @param parent
	 *            the parent for the new button
	 * @param label
	 *            the label for the new button
	 * @return the newly-created button
	 */
	public static Button createRadioButton(Composite parent, String label) {
		return createRadioButton(parent, label, DEFAULT_RADIO_FILL);
	}

	/**
	 * Utility method that creates a radio button instance and sets the default
	 * layout data.
	 * 
	 * @param parent
	 *            the parent for the new button
	 * @param label
	 *            the label for the new button
	 * @param gridDataFill 
	 * @return the newly-created button
	 */
	public static Button createRadioButton(Composite parent, String label,
			int gridDataFill) {
		return createRadioButton(parent, label, gridDataFill, 1);
	}

	/**
	 * Utility method that creates a radio button instance and sets the default
	 * layout data.
	 * 
	 * @param parent
	 *            the parent for the new button
	 * @param label
	 *            the label for the new button
	 * @param gridDataFill 
	 * @param horizontalSpan
	 *            number of columns occupied by button
	 * @return the newly-created button
	 */
	public static Button createRadioButton(Composite parent, String label,
			int gridDataFill, int horizontalSpan) {
		Button button = new Button(parent, SWT.RADIO | SWT.LEFT);
		GridData data = new GridData(gridDataFill);
		data.horizontalSpan = horizontalSpan;
		button.setLayoutData(data);
		button.setText(label);
		return button;
	}

	/**
	 * Utility method that creates an empty line
	 * 
	 * @param parent
	 *            the parent for the new label
	 * @param numColumns
	 *            the number of columns for the new composite
	 */
	public static void createSpacer(Composite parent, int numColumns) {
		createSpacer(parent, numColumns, 0);
	}

	/**
	 * Utility method that creates an empty line
	 * 
	 * @param parent
	 *            the parent for the new label
	 * @param numColumns
	 *            the number of columns for the new composite
	 * @param minWidth
	 *            minimum width of spacer
	 */
	public static void createSpacer(Composite parent, int numColumns,
			int minWidth) {
		Label label = new Label(parent, SWT.NONE);
		GridData data = new GridData();
		data.horizontalSpan = numColumns;
		data.widthHint = minWidth;
		label.setLayoutData(data);
	}

	/**
	 * Create a separator that goes across the entire page
	 * 
	 * @param parent
	 *            the parent for the new label
	 * @param numColumns
	 *            the number of columns for the new composite
	 */
	public static void createSeparator(Composite parent, int numColumns) {
		Label separator = new Label(parent, SWT.SEPARATOR | SWT.HORIZONTAL);
		GridData data = new GridData();
		data.horizontalSpan = numColumns;
		data.horizontalAlignment = GridData.FILL;
		data.grabExcessHorizontalSpace = true;
		separator.setLayoutData(data);
	}

	/**
	 * Create a table from a Composite object
	 * 
	 * @param composite
	 *            the Composite this table is to be created from
	 * @param tokenString
	 *            A string containing names of the columns in the order that
	 *            they should be displayed in the table with each column
	 *            separated by a comma(',') or null if no columns need to be
	 *            created.
	 * @param tablewidth
	 *            the minimum width for the table
	 * @param tableHeight 
	 * @return the new table
	 */
	public static Table createTable(Composite composite, String tokenString,
			int tablewidth, int tableHeight) {
		// SINGLE, MULTI, CHECK, FULL_SELECTION, HIDE_SELECTION
		int style = SWT.V_SCROLL | SWT.H_SCROLL | SWT.SINGLE | SWT.BORDER
				| SWT.FULL_SELECTION;
		Table table = new Table(composite, style);
		GridData gridData = new GridData(GridData.FILL_BOTH);
		if (tablewidth > 0) {
			gridData.widthHint = tablewidth;
		}
		if (tableHeight > 0) {
			gridData.heightHint = tableHeight;
		}
		table.setLayoutData(gridData);
		table.setHeaderVisible(true);
		table.setLinesVisible(true);

		if (tokenString != null) {
			String[] columns = getTokenNames(tokenString);

			int columnSize = 50;
			if (tablewidth > 0) {
				columnSize = tablewidth / columns.length;
			}
			for (int ii = 0; ii < columns.length; ii++) {
				/*(void)*/ createTableColumn(table, columns[ii], ii,
						columnSize);
			}
		}

		return table;
	}

	/**
	 * Create a table from a Composite object
	 * 
	 * @param composite
	 *            the Composite this table is to be created from
	 * @param columns
	 *            A string array containing names of the columns in the order
	 *            that they should be displayed in the table, or null if no
	 *            columns need to be created.
	 * @param tablewidth
	 *            the minimum width for the table
	 * @return the new table
	 */
	public static Table createTable(Composite composite, String[] columns,
			int tablewidth) {
		int style = SWT.BORDER | SWT.FULL_SELECTION;
		Table table = new Table(composite, style);
		GridData gridData = new GridData(GridData.FILL_BOTH);
		gridData.widthHint = tablewidth;
		table.setLayoutData(gridData);
		table.setHeaderVisible(true);
		table.setLinesVisible(true);

		if (columns != null) {
			for (int i = 0; i < columns.length; i++) {
				/*(void)*/ createTableColumn(table, columns[i], i);
			}
		}

		return table;
	}

	/**
	 * Create a table column
	 * 
	 * @param parent
	 *            the table that contains this column
	 * @param name
	 *            name of this column
	 * @param index
	 *            the column within the parent composite
	 * @return the new table column
	 */
	public static TableColumn createTableColumn(Table parent, String name,
			int index) {
		TableColumn column = new TableColumn(parent, SWT.LEFT, index);
		column.setText(name);
		return column;
	}

	/**
	 * Create a table column with the image and the width of the column is set
	 * to the image width.
	 * 
	 * @param parent
	 *            the table that contains this column
	 * @param image
	 *            iamge for this column
	 * @param index 
	 * @return the new table column
	 */
	public static TableColumn createTableColumn(Table parent, Image image,
			int index) {
		TableColumn column = new TableColumn(parent, SWT.LEFT, index);
		column.setImage(image);
		column.setWidth(image.getBounds().width);
		column.setResizable(false);
		return column;
	}

	/**
	 * Create a table column
	 * 
	 * @param parent
	 *            the table that contains this column
	 * @param name
	 *            name of this column
	 * @param index
	 *            the column within the parent composite
	 * @param tablewidth
	 *            the width for the column
	 * @return the new table column
	 */
	public static TableColumn createTableColumn(Table parent, String name,
			int index, int tablewidth) {
		TableColumn column = new TableColumn(parent, SWT.LEFT, index);
		column.setText(name);
		column.setWidth(tablewidth);
		return column;
	}

	/**
	 * Create a text field
	 * 
	 * @param parent
	 *            the parent of the new text field
	 * @return the new text field
	 */
	public static Text createTextBox(Composite parent) {
		return createTextBox(parent, 1, DEFAULT_TEXTBOX_WIDTH);
	}

	/**
	 * Create a text field
	 * 
	 * @param parent
	 *            the parent of the new text field
	 * @param text 
	 * @return the new text field
	 */
	public static Text createTextBox(Composite parent, String text) {
		Text textbox = createTextBox(parent, 1);
		textbox.setText(text);
		return textbox;
	}

	/**
	 * Create a text field
	 * 
	 * @param parent
	 *            the parent of the new text field
	 * @param numColumns
	 *            number of columns the text box is to occupy
	 * @return the new text field
	 */
	public static Text createTextBox(Composite parent, int numColumns) {
		return createTextBox(parent, numColumns, DEFAULT_TEXTBOX_WIDTH);
	}

	/**
	 * Create a text field
	 * 
	 * @param parent
	 *            the parent of the new text field
	 * @param numColumns
	 *            number of columns the text box is to occupy
	 * @param minWidth
	 *            minimum width of text field
	 * @return the new text field
	 */
	public static Text createTextBox(Composite parent, int numColumns,
			int minWidth) {
		return createTextBox(parent, numColumns, minWidth, SWT.DEFAULT);
	}

	/**
	 * Create a text field
	 * 
	 * @param parent
	 *            the parent of the new text field
	 * @param numColumns
	 *            number of columns the text box is to occupy
	 * @param minWidth
	 *            minimum width of text field
	 * @param minHeight 
	 * @return the new text field
	 */
	public static Text createTextBox(Composite parent, int numColumns,
			int minWidth, int minHeight) {
		Text text = new Text(parent, SWT.SINGLE | SWT.BORDER);
		GridData data = new GridData(GridData.FILL);
		data.horizontalSpan = numColumns;
		data.widthHint = minWidth;
		data.heightHint = minHeight;
		text.setLayoutData(data);
		return text;
	}

	/**
	 * Create a text field that is scrollable.
	 * 
	 * @param parent
	 *            the parent of the new text field
	 * @param numColumns
	 *            number of columns the text box is to occupy
	 * @param minWidth
	 *            minimum width of text field
	 * @param minHeight
	 *            minimum height of text field
	 * @return the new text field
	 */
	public static Text createTextBoxScrollable(Composite parent,
			int numColumns, int minWidth, int minHeight) {
		Text text = new Text(parent, SWT.BORDER | SWT.V_SCROLL | SWT.WRAP);
		GridData data = new GridData((minWidth > 0) ? GridData.FILL
				: GridData.FILL_HORIZONTAL);
		data.horizontalSpan = numColumns;
		if (minWidth > 0) {
			data.widthHint = minWidth;
		}
		data.heightHint = minHeight;

		text.setLayoutData(data);
		return text;
	}

	/**
	 * Create a list with the items listed in it.
	 * 
	 * @param parent
	 *            the parent of the new text field
	 * @param numColumns
	 *            number of columns the text box is to occupy
	 * @param minWidth
	 *            minimum width of text field
	 * @param minHeight
	 *            minimum height of text field
	 * @param items
	 *            the items in the list
	 * @return the new list
	 */
	public static List createList(Composite parent, int numColumns,
			int minWidth, int minHeight, String[] items) {
		return createList(parent, numColumns, minWidth, minHeight, items, true);
	}

	/**
	 * Create a list with the items listed in it.
	 * 
	 * @param parent
	 *            the parent of the new list box
	 * @param numColumns
	 *            number of columns the list box is to occupy
	 * @param minWidth
	 *            minimum width of list box
	 * @param minHeight
	 *            minimum height of list box
	 * @param items
	 *            the items in the list
	 * @param bmulti
	 *            whether multiple item selection is allowed
	 * @return the new list
	 */
	public static List createList(Composite parent, int numColumns,
			int minWidth, int minHeight, String[] items, boolean bmulti) {
		return createList(parent, numColumns, minWidth, minHeight, items,
				bmulti, 1);
	}

	/**
	 * Create a list with the items listed in it.
	 * 
	 * @param parent
	 *            the parent of the new list box
	 * @param numColumns
	 *            number of columns the list box is to occupy
	 * @param minWidth
	 *            minimum width of list box
	 * @param minHeight
	 *            minimum height of list box
	 * @param items
	 *            the items in the list
	 * @param bmulti
	 *            whether multiple item selection is allowed
	 * @param verticalSpan
	 *            the number of rows the list box is to occupy
	 * @return the new list
	 */
	public static List createList(Composite parent, int numColumns,
			int minWidth, int minHeight, String[] items, boolean bmulti,
			int verticalSpan) {
		List theList;
		if (bmulti)
			theList = new List(parent, SWT.V_SCROLL | SWT.H_SCROLL | SWT.MULTI
					| SWT.BORDER);
		else
			theList = new List(parent, SWT.V_SCROLL | SWT.H_SCROLL | SWT.SINGLE
					| SWT.BORDER);
		GridData data = new GridData(GridData.FILL_HORIZONTAL
				| GridData.VERTICAL_ALIGN_BEGINNING | GridData.FILL_VERTICAL);
		data.horizontalSpan = numColumns;
		data.widthHint = minWidth;
		data.heightHint = minHeight;
		data.verticalSpan = verticalSpan;
		theList.setLayoutData(data);
		if (items != null) {
			theList.setItems(items);
		}

		return theList;
	}

	/**
	 * Computes the size of the composite inside the scroll area so that scroll
	 * bars show up correctly.
	 * 
	 * @param parentComposite
	 * @param childComposite
	 */
	public static void computeScrollArea(ScrolledComposite parentComposite,
			Composite childComposite) {
		// Point pt = childComposite.computeSize(SWT.DEFAULT, SWT.DEFAULT);
		// childComposite.setSize(pt);

		Point pt = childComposite.computeSize(SWT.DEFAULT, SWT.DEFAULT);
		parentComposite.setExpandHorizontal(true);
		parentComposite.setExpandVertical(true);
		parentComposite.setMinWidth(pt.x);
		parentComposite.setMinHeight(pt.y);
	}

	/**
	 * Builds an array of strings from a token list string. The token separator
	 * is a comma (',').
	 * 
	 * @param tokenString
	 * @return String[]
	 */
	public static String[] getTokenNames(String tokenString) {
		if (tokenString == null) {
			return new String[0];
		}

		return tokenString.split(","); //$NON-NLS-1$
	}

	/**
	 * Enable/Disable the widget and all its children.
	 * 
	 * @param widget
	 *            The widget to be enabled/disabled.
	 * @param state
	 *            Enable widget if true. Disable otherwise.
	 */
	public static void setWidgetState(Control widget, boolean state) {
		if (widget instanceof Composite) {
			Control widgets[] = ((Composite) widget).getChildren();
			for (int i = 0; i < widgets.length; i++) {
				setWidgetState(widgets[i], state);
			}
		}
		widget.setEnabled(state);
	}

	// ---------------------------------------------------------------------------
	// following is for workaround eclipse problem
	// https://bugs.eclipse.org/bugs/show_bug.cgi?id=40281
	static ControlListener _listener = new ControlListener() {
		public void controlResized(ControlEvent e) {
			final Composite c = (Composite) e.widget;
			c.getDisplay().asyncExec(new Runnable() {
				public void run() {
					if (!c.isDisposed()) {
						// XXX: in 3.0, should use c.layout(true)
						// in 3.1, should use c.layout(true, true)
						c.layout(true);
						c.redraw();
					}
				}
			});
		}

		public void controlMoved(ControlEvent e) {
		    // nothing for move
		}
	};

	/**
	 * @param composite
	 */
	public static void workaroundResize(Composite composite) {
		composite.addControlListener(_listener);
	}
}

Back to the top