summaryrefslogtreecommitdiffstats
path: root/bundles/org.eclipse.swt/Eclipse SWT/motif/org/eclipse/swt/widgets/FontDialog.java
blob: a29c8ef5af004bb4faf82a93dd797a4be5288480 (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
/*******************************************************************************
 * Copyright (c) 2000, 2004 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Common Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v10.html
 * 
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.swt.widgets;

 
import org.eclipse.swt.*;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.layout.*;

import java.util.*;

/**
 * Instances of this class allow the user to select a font
 * from all available fonts in the system.
 * <dl>
 * <dt><b>Styles:</b></dt>
 * <dd>(none)</dd>
 * <dt><b>Events:</b></dt>
 * <dd>(none)</dd>
 * </dl>
 * <p>
 * IMPORTANT: This class is intended to be subclassed <em>only</em>
 * within the SWT implementation.
 * </p>
 */
public class FontDialog extends Dialog {
	private FontData [] fontData;
	private FontData currentFontData;
	private Font sampleFont;	// the displayed sample font
	private Color sampleColor;	// the displayed sample color
	private RGB rgb;
	private boolean okSelected = false;
	private boolean ignoreEvents = false;
	/*
	 * Table containing all available fonts as FontData objects.
	 * The table is structured as a series of embedded Hashtables as follows:
	 * <br>characterRegistryName -> faceName -> extendedStyle -> size -> style
	 */
	private Hashtable characterSets = new Hashtable ();

	// widgets	
	private Shell shell;
	private List fontSetList;
	private List charSetList, faceNameList, extStyleList;
	private List fontStyleList, fontSizeList;
	private Label sampleLabel;
	private Button upButton, downButton, newButton, removeButton;
	private Button okButton, cancelButton, colorButton;	

	// constants
	private static final String TEXT_SAMPLE = "AaBbYyZz";
	private static String SCALABLE_SIZES [];
	private static final int DEFAULT_SIZE = 14;
	private static final String DEFAULT_STYLE = "medium";
	private static final Integer SCALABLE_KEY = new Integer (0);
	private static final int LIST_WIDTH = 200;
	private static final int EXTSTYLE_WIDTH = 150;
	private static final int LIST_HEIGHT = 150;
	private static final int SAMPLE_HEIGHT = 75;
	private static final String PREFIX_ISO8859 = "iso8859";
	private static final String PREFIX_ISO646 = "iso646";
	private static final String PREFIX_UNICODE = "ucs";
	private static final String PREFIX_JAPANESE = "jis";
	private static final String PREFIX_SIMPLIFIEDCHINESE = "gb";
	private static final String PREFIX_TRADITIONALCHINESE = "cns";
	private static final String PREFIX_KOREAN = "ks";
	private static final String [] ISO_CHARSETS = new String [] {
		"",	// 0 undefined
		SWT.getMessage ("SWT_Charset_Western"),
		SWT.getMessage ("SWT_Charset_EastEuropean"),
		SWT.getMessage ("SWT_Charset_SouthEuropean"),
		SWT.getMessage ("SWT_Charset_NorthEuropean"),
		SWT.getMessage ("SWT_Charset_Cyrillic"),
		SWT.getMessage ("SWT_Charset_Arabic"),
		SWT.getMessage ("SWT_Charset_Greek"),
		SWT.getMessage ("SWT_Charset_Hebrew"),
		SWT.getMessage ("SWT_Charset_Turkish"),
		SWT.getMessage ("SWT_Charset_Nordic"),
		SWT.getMessage ("SWT_Charset_Thai"),
		"",	// 12 undefined
		SWT.getMessage ("SWT_Charset_BalticRim"),
		SWT.getMessage ("SWT_Charset_Celtic"),
		SWT.getMessage ("SWT_Charset_Euro"),
		SWT.getMessage ("SWT_Charset_Romanian")
	};

	static {
		SCALABLE_SIZES = new String [69];
		for (int i = 0; i < 69; i++) {
			SCALABLE_SIZES [i] = String.valueOf (i + 4);
		}
	}

/**
 * Constructs a new instance of this class given only its parent.
 *
 * @param parent a shell which will be the parent of the new instance
 *
 * @exception IllegalArgumentException <ul>
 *    <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
 * </ul>
 * @exception SWTException <ul>
 *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li>
 *    <li>ERROR_INVALID_SUBCLASS - if this class is not an allowed subclass</li>
 * </ul>
 */
public FontDialog (Shell parent) {
	this (parent, SWT.NONE);
}

/**
 * Constructs a new instance of this class given its parent
 * and a style value describing its behavior and appearance.
 * <p>
 * The style value is either one of the style constants defined in
 * class <code>SWT</code> which is applicable to instances of this
 * class, or must be built by <em>bitwise OR</em>'ing together 
 * (that is, using the <code>int</code> "|" operator) two or more
 * of those <code>SWT</code> style constants. The class description
 * lists the style constants that are applicable to the class.
 * Style bits are also inherited from superclasses.
 * </p>
 *
 * @param parent a shell which will be the parent of the new instance
 * @param style the style of dialog to construct
 *
 * @exception IllegalArgumentException <ul>
 *    <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
 * </ul>
 * @exception SWTException <ul>
 *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li>
 *    <li>ERROR_INVALID_SUBCLASS - if this class is not an allowed subclass</li>
 * </ul>
 */
public FontDialog (Shell parent, int style) {
	super (parent, style | SWT.TITLE | SWT.BORDER | SWT.APPLICATION_MODAL);
	checkSubclass ();
}

/**
 * Add the fonts found in 'fonts' to the list of fonts.
 * Fonts are stored by character set and face name. For each character 
 * set/face name combination there is one FontExtStyles object that 
 * captures the different extended styles and the sizes and styles 
 * available for that extended style.
 */
void addFonts (FontData fonts []) {
	for (int i = 0; i < fonts.length; i++) {
		FontData font = fonts [i];
		String charSetName = getTranslatedCharSet (font, true);
		Hashtable charSet = (Hashtable) characterSets.get (charSetName);
		if (charSet == null) {
			charSet = new Hashtable (9);
			characterSets.put (charSetName, charSet);
		}

		String faceName = getTranslatedFaceName (font);
		Hashtable faceSet = (Hashtable) charSet.get (faceName);
		if (faceSet == null) {
			faceSet = new Hashtable (9);
			charSet.put (faceName, faceSet);
		}

		String extStyleName = font.addStyle;
		Hashtable extStyleSet = (Hashtable) faceSet.get (extStyleName);
		if (extStyleSet == null) {
			extStyleSet = new Hashtable (9);
			faceSet.put (extStyleName, extStyleSet);
		}
		
		Integer sizeValue = new Integer (font.getHeight ());
		Hashtable sizeSet = (Hashtable) extStyleSet.get (sizeValue);
		if (sizeSet == null) {
			sizeSet = new Hashtable (9);
			extStyleSet.put (sizeValue, sizeSet);
		}
		
		String style = font.weight;
		sizeSet.put (style,font);
	}
}

void centerListIndex (List list, int index) {
	int visibleItems = list.getSize ().y / list.getItemHeight ();
	int topIndex = Math.max (0, index - visibleItems / 2);
	list.setTopIndex (topIndex);
}

FontData copyFontData (FontData data) {
	FontData result = new FontData ();
	result.addStyle = data.addStyle;
	result.averageWidth = data.averageWidth;
	result.characterSetName = data.characterSetName;
	result.characterSetRegistry = data.characterSetRegistry;
	result.fontFamily = data.fontFamily;
	result.foundry = data.foundry;
	result.horizontalResolution = data.horizontalResolution;
	result.pixels = data.pixels;
	result.points = data.points;
	result.setWidth = data.setWidth;
	result.slant = data.slant;
	result.spacing = data.spacing;
	result.verticalResolution = data.verticalResolution;
	result.weight = data.weight;
	return result;
}

void createButtons (Composite parent) {
	int buttonAlignment = GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_BEGINNING;
	okButton = new Button (parent, SWT.PUSH);
	okButton.setText (SWT.getMessage ("SWT_OK"));
	okButton.setLayoutData (new GridData (buttonAlignment));
	shell.setDefaultButton (okButton);
	
	cancelButton = new Button (parent, SWT.PUSH);
	cancelButton.setText (SWT.getMessage ("SWT_Cancel"));
	cancelButton.setLayoutData (new GridData (buttonAlignment));

	colorButton = new Button (parent, SWT.PUSH);
	colorButton.setText (SWT.getMessage ("SWT_Color"));
	colorButton.setLayoutData (new GridData (buttonAlignment));
}

void createControls (Composite parent) {
	Composite composite = new Composite (parent, SWT.NONE);
	GridLayout layout = new GridLayout ();
	layout.numColumns = 2;
	composite.setLayout (layout);

	Composite controls = new Composite (composite, SWT.NONE);
	layout = new GridLayout ();
	layout.marginHeight = layout.marginWidth = 0;
	layout.numColumns = 3;
	controls.setLayout (layout);
	
	// labels row (1)
	new Label (controls, SWT.NONE).setText (SWT.getMessage ("SWT_Character_set") + ":");
	new Label (controls, SWT.NONE).setText (SWT.getMessage ("SWT_Font") + ":");
	new Label (controls, SWT.NONE).setText (SWT.getMessage ("SWT_Extended_style") + ":");	

	// lists row (2)
	charSetList = new List (controls, SWT.V_SCROLL | SWT.BORDER);
	GridData gridData = new GridData (GridData.FILL_HORIZONTAL);
	gridData.heightHint = LIST_HEIGHT;
	gridData.widthHint = LIST_WIDTH;
	charSetList.setLayoutData (gridData);

	faceNameList = new List (controls, SWT.V_SCROLL | SWT.BORDER);
	gridData = new GridData (GridData.FILL_HORIZONTAL);
	gridData.heightHint = LIST_HEIGHT;
	gridData.widthHint = LIST_WIDTH;
	faceNameList.setLayoutData (gridData);

	extStyleList = new List (controls, SWT.V_SCROLL | SWT.MULTI | SWT.BORDER);
	gridData = new GridData (GridData.FILL_HORIZONTAL);
	gridData.heightHint = LIST_HEIGHT;
	gridData.widthHint = EXTSTYLE_WIDTH;
	extStyleList.setLayoutData (gridData);

	// labels row (3)
	new Label (controls, SWT.NONE).setText (SWT.getMessage ("SWT_Size") + ":");	
	new Label (controls, SWT.NONE).setText (SWT.getMessage ("SWT_Style") + ":");
	new Label (controls, SWT.NONE);		// filler

	// lists row (4)
	fontSizeList = new List (controls, SWT.V_SCROLL | SWT.BORDER);
	gridData = new GridData (GridData.FILL_HORIZONTAL);
	gridData.heightHint = LIST_HEIGHT;
	gridData.widthHint = LIST_WIDTH;
	fontSizeList.setLayoutData (gridData);

	fontStyleList = new List (controls, SWT.V_SCROLL | SWT.BORDER);
	gridData = new GridData (GridData.FILL_HORIZONTAL);
	gridData.heightHint = LIST_HEIGHT;
	gridData.widthHint = LIST_WIDTH;
	fontStyleList.setLayoutData (gridData);

	new Label (controls, SWT.NONE);		// filler

	// font sets group
	Group fontSetGroup = new Group (controls, SWT.NONE);
	fontSetGroup.setText(SWT.getMessage ("SWT_FontSet"));
	layout = new GridLayout ();
	layout.numColumns = 2;
	fontSetGroup.setLayout (layout);
	GridData data = new GridData (GridData.FILL_BOTH);
	data.horizontalSpan = 3;
	fontSetGroup.setLayoutData (data);

	fontSetList = new List (fontSetGroup, SWT.V_SCROLL | SWT.BORDER);
	data = new GridData (GridData.FILL_BOTH);
	data.grabExcessHorizontalSpace = true;
	fontSetList.setLayoutData (data);

	Composite buttonsGroup = new Composite (fontSetGroup, SWT.NONE);
	layout = new GridLayout ();
	layout.numColumns = 3;
	layout.makeColumnsEqualWidth = false;
	layout.marginHeight = layout.marginWidth = 0;
	layout.horizontalSpacing = layout.verticalSpacing = 0; 
	buttonsGroup.setLayout (layout);
	
	Composite upDownButtonsGroup = new Composite (buttonsGroup, SWT.NONE);
	layout = new GridLayout ();
	layout.marginHeight = layout.marginWidth = 0;
	layout.horizontalSpacing = layout.verticalSpacing = 0; 
	upDownButtonsGroup.setLayout(layout);

	int buttonAlignment = GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_BEGINNING;
	upButton = new Button (upDownButtonsGroup, SWT.PUSH);
	upButton.setLayoutData (new GridData (buttonAlignment));
	upButton.setText (SWT.getMessage ("SWT_Up"));
	downButton = new Button (upDownButtonsGroup, SWT.PUSH);
	downButton.setLayoutData (new GridData (buttonAlignment));
	downButton.setText (SWT.getMessage ("SWT_Down"));

	new Label (buttonsGroup, SWT.SEPARATOR | SWT.VERTICAL);	

	Composite newRemoveButtonsGroup = new Composite (buttonsGroup, SWT.NONE);
	layout = new GridLayout ();
	layout.marginHeight = layout.marginWidth = 0;
	layout.horizontalSpacing = layout.verticalSpacing = 0; 
	newRemoveButtonsGroup.setLayout(layout);
		
	newButton = new Button (newRemoveButtonsGroup, SWT.PUSH);
	newButton.setLayoutData (new GridData (buttonAlignment));
	newButton.setText (SWT.getMessage ("SWT_NewFont"));
	removeButton = new Button (newRemoveButtonsGroup, SWT.PUSH);
	removeButton.setLayoutData (new GridData (buttonAlignment));
	removeButton.setText (SWT.getMessage ("SWT_Remove"));
	
	// font sample group
	Group sampleGroup = new Group (controls, SWT.NONE);
	sampleGroup.setText (SWT.getMessage ("SWT_Sample"));
	gridData = new GridData ();
	gridData.heightHint = SAMPLE_HEIGHT;	
	gridData.horizontalSpan = 3;
	gridData.horizontalAlignment = GridData.FILL;	
	sampleGroup.setLayoutData (gridData);
	layout = new GridLayout ();
	layout.marginWidth = 10;
	layout.marginHeight = 10;
	sampleGroup.setLayout (layout);
	
	sampleLabel = new Label (sampleGroup, SWT.CENTER);
	sampleLabel.setText (TEXT_SAMPLE);
	gridData = new GridData ();
	gridData.grabExcessHorizontalSpace = true;
	gridData.grabExcessVerticalSpace = true;	
	gridData.verticalAlignment = GridData.FILL;	
	gridData.horizontalAlignment = GridData.FILL;	
	sampleLabel.setLayoutData (gridData);
	
	Composite okCancelGroup = new Composite (composite, SWT.NONE);
	layout = new GridLayout ();
	layout.marginHeight = layout.marginWidth = layout.verticalSpacing = 0;
	okCancelGroup.setLayout (layout);
	okCancelGroup.setLayoutData (new GridData (GridData.VERTICAL_ALIGN_BEGINNING));
	createButtons (okCancelGroup);
}

Hashtable getExtStyles (String charsetName, String faceName) {
	Hashtable faces = getFaces (charsetName);
	if (faces == null) return null;
	return (Hashtable) faces.get (faceName);
}

Hashtable getFaces (String charsetName) {
	return (Hashtable) getFonts ().get (charsetName);
}

/**
 * Returns a FontData object describing the font that was
 * selected in the dialog, or null if none is available.
 * 
 * @return the FontData for the selected font, or null
 * @deprecated use #getFontList ()
 */
public FontData getFontData () {
	if (fontData != null && fontData.length > 0) {
		return fontData [0];
	}
	return null;

}

FontData getFontData (String charsetName, String faceName, String extStyle, int size, String style) {
	Hashtable styles = getStyles (charsetName, faceName, extStyle, size);
	if (styles == null) return null;
	return (FontData) styles.get (style);
}

/**
 * Returns a FontData set describing the font that was
 * selected in the dialog, or null if none is available.
 * 
 * @return the FontData for the selected font, or null
 * @since 2.1.1
 */
public FontData [] getFontList () {
	return fontData;
}

/**
 * Returns the collection of fonts that are displayed by the 
 * receiver.
 * See the class definition for an explanation of the structure
 * of the returned Hashtable.
 */
Hashtable getFonts () {
	return characterSets;
}

String getListSelection (List list) {
	String [] selection = list.getSelection ();
	if (selection.length > 0) return selection [0];
	return "";
}

/**
 * Returns the currently selected color in the receiver.
 *
 * @return the RGB value for the selected color, may be null
 *
 * @see PaletteData#getRGBs
 * 
 * @since 2.1
 */
public RGB getRGB () {
	return rgb;
}

/**
 * Returns a FontData object that can be used to load the selected 
 * font.
 */
FontData getSelectionFontData () {
	String charSetName = getListSelection (charSetList);
	String faceName = getListSelection (faceNameList);
	String extStyle = getListSelection (extStyleList);
	int size = DEFAULT_SIZE;
	try {
		size = Integer.valueOf (getListSelection (fontSizeList)).intValue ();
		if (size < 1) size = DEFAULT_SIZE;
	} catch (NumberFormatException e) {
		/*
		 * This block is purposely left empty since a default
		 * value is already specified above.
		 */
	}
	String style = getListSelection (fontStyleList);
	FontData result = getFontData (charSetName, faceName, extStyle, size, style);

	if (result != null) {
		result = copyFontData (result);
	} else {
		/*
		* One or more of the dialog's widgets contain custom typed values.
		* Create a FontData that mirrors these values so that the Font created
		* below will try to find the best match.
		*/
		result = new FontData ();
		result.characterSetRegistry = charSetName;
		result.setName (faceName);
		result.addStyle = extStyle;
		result.weight = style;
	}
	result.setHeight (size);
	return result;
}

Hashtable getSizes (String charsetName, String faceName, String extStyle) {
	Hashtable extStyles = getExtStyles (charsetName, faceName);
	if (extStyles == null) return null;
	return (Hashtable) extStyles.get (extStyle);
}

Hashtable getStyles (String charsetName, String faceName, String extStyle, int size) {
	Hashtable sizes = getSizes (charsetName, faceName, extStyle);
	if (sizes == null) return null;
	Hashtable result = (Hashtable) sizes.get (new Integer (size));
	if (result == null)
		result = (Hashtable) sizes.get (SCALABLE_KEY);
	return result;
}
	
/**
 * Returns the character set found in 'fontData' prefixed
 * with a string explaining the character set.
 */
String getTranslatedCharSet (FontData fontData, boolean includeDescription) {
	String characterSet = fontData.characterSetRegistry;
	String translatedCharSet = null;

	if (characterSet.startsWith (PREFIX_ISO8859)) {
		int charSetName = 1;
		try {
			charSetName = Integer.valueOf (fontData.characterSetName).intValue ();
		} catch (NumberFormatException e) {
			/*
			 * This block is purposely left empty since a default
			 * value is already specified above.
			 */
		}
		characterSet = PREFIX_ISO8859 + "-" + charSetName;
		if (charSetName < ISO_CHARSETS.length) {
			translatedCharSet = ISO_CHARSETS [charSetName];
		}
	}
	else	
	if (characterSet.startsWith (PREFIX_ISO646)) {
		translatedCharSet = SWT.getMessage("SWT_Charset_ASCII");
	}
	else	
	if (characterSet.startsWith (PREFIX_UNICODE)) {
		translatedCharSet = SWT.getMessage("SWT_Charset_Unicode");
	}
	else	
	if (characterSet.startsWith (PREFIX_JAPANESE)) {
		translatedCharSet = SWT.getMessage("SWT_Charset_Japanese");
	}
	else	
	if (characterSet.startsWith (PREFIX_SIMPLIFIEDCHINESE)) {
		translatedCharSet = SWT.getMessage("SWT_Charset_SimplifiedChinese");
	}
	else	
	if (characterSet.startsWith (PREFIX_TRADITIONALCHINESE)) {
		translatedCharSet = SWT.getMessage("SWT_Charset_TraditionalChinese");
	}
	else	
	if (characterSet.startsWith (PREFIX_KOREAN)) {
		translatedCharSet = SWT.getMessage("SWT_Charset_Korean");
	}
	if (includeDescription && translatedCharSet != null) {
		translatedCharSet = characterSet + " (" + translatedCharSet + ')';
	}
	else {
		translatedCharSet = characterSet;
	}
	return translatedCharSet;
}

/**
 * Returns the face name as specified in FontData.familyName followed by
 * the foundry set in parantheses if available.
 * We display the face name first so that the list sorts the fonts by 
 * face name, not by foundry. Users generally want to select fonts based 
 * on the face name and not by foundry. Once they've found the desired 
 * face name in the list they can compare the font variations from 
 * different foundries if available.
 */
String getTranslatedFaceName (FontData fontData) {
	StringBuffer faceNameBuffer;
	
	if (fontData.foundry != null && fontData.foundry.length () > 0) {
		faceNameBuffer = new StringBuffer (fontData.fontFamily);
		faceNameBuffer.append (" (");
		faceNameBuffer.append (fontData.foundry);
		faceNameBuffer.append (')');			
	}
	else {
		faceNameBuffer = new StringBuffer (fontData.getName ());
	}
	return faceNameBuffer.toString ();
}

/**
 * Handle the events the receiver is listening to.
 * List selections cause the downstream lists to be initialized 
 * with font data and the sample text to be updated.
 */
void handleEvent (Event event) {
	if (ignoreEvents) return;
	if (event.widget instanceof List) {
		List list = (List) event.widget;
		String text = getListSelection (list);
		int oldSelectIndex = ((Integer)list.getData ()).intValue ();
		int newSelectIndex = list.indexOf (text);
		if (oldSelectIndex != newSelectIndex || newSelectIndex == -1) {
			ignoreEvents = true;
			if (list == charSetList) initFaceNameList ();
			else if (list == faceNameList) initExtStyleList ();
			else if (list == extStyleList) initSizeList ();
			else if (list == fontSizeList) initStyleList ();
			else if (event.widget == fontSetList) {
				currentFontData = fontData [fontSetList.getSelectionIndex ()];
				setFontControls (currentFontData);
				updateButtonEnablements ();
			}
	
			updateSampleFont ();
			updateFontList ();
			list.setData (new Integer (newSelectIndex));
			if (newSelectIndex != -1) {
				list.select (newSelectIndex);
			}
			ignoreEvents = false;
		}
		return;
	}
	
	if (event.widget instanceof Button) {
		if (event.widget == okButton) {
			okSelected = true;
			shell.close ();
		}
		else if (event.widget == cancelButton) {
			okSelected = false;
			shell.close ();
		}
		else if (event.widget == colorButton) {
			ColorDialog colorDialog = new ColorDialog (shell, SWT.NONE);
			colorDialog.setRGB (rgb);
			RGB newRgb = colorDialog.open ();
			if (newRgb != null) {
				rgb = newRgb;
				updateSampleColor ();
			}
		}
		else if (event.widget == newButton) {
			FontData [] newFontData = new FontData [fontData.length + 1];
			System.arraycopy (fontData, 0, newFontData, 0, fontData.length);
			FontData source = fontData [fontSetList.getSelectionIndex ()];
			FontData newFd = copyFontData (source);
			newFontData [newFontData.length - 1] = newFd;
			this.fontData = newFontData;
			updateFontList ();
			fontSetList.select (newFontData.length - 1);
			fontSetList.setData (new Integer (newFontData.length - 1));
			fontSetList.showSelection();
			updateButtonEnablements ();
		}
		else if (event.widget == removeButton) {
			int selectionIndex = fontSetList.getSelectionIndex ();
			FontData [] newFontData = new FontData [fontData.length - 1];
			System.arraycopy (fontData, 0, newFontData, 0, selectionIndex);
			System.arraycopy (fontData, selectionIndex + 1, newFontData, selectionIndex, newFontData.length - selectionIndex);
			fontData = newFontData;
			updateFontList ();
			updateButtonEnablements ();
			setFontControls (fontData [fontSetList.getSelectionIndex ()]);
		}
		else if (event.widget == upButton) {
			int selectionIndex = fontSetList.getSelectionIndex ();
			FontData temp = fontData [selectionIndex];
			fontData [selectionIndex] = fontData [selectionIndex - 1];
			fontData [selectionIndex - 1] = temp;
			fontSetList.select (selectionIndex - 1);
			fontSetList.setData (new Integer (selectionIndex - 1));
			updateFontList ();
			updateButtonEnablements ();
		}
		else if (event.widget == downButton) {
			int selectionIndex = fontSetList.getSelectionIndex ();
			FontData temp = fontData [selectionIndex];
			fontData [selectionIndex] = fontData [selectionIndex + 1];
			fontData [selectionIndex + 1] = temp;
			fontSetList.select (selectionIndex + 1);
			fontSetList.setData (new Integer (selectionIndex + 1));
			updateFontList ();
			updateButtonEnablements ();
		}
	}
}

void hookListeners () {
	Listener listener = new Listener () {
		public void handleEvent (Event event) {
			FontDialog.this.handleEvent (event);
		}
	};
	okButton.addListener (SWT.Selection, listener);
	cancelButton.addListener (SWT.Selection, listener);
	colorButton.addListener (SWT.Selection, listener);	
	charSetList.addListener (SWT.Selection, listener);
	faceNameList.addListener (SWT.Selection, listener);
	fontStyleList.addListener (SWT.Selection, listener);
	extStyleList.addListener (SWT.Selection, listener);
	fontSizeList.addListener (SWT.Selection, listener);
	newButton.addListener (SWT.Selection, listener);
	removeButton.addListener (SWT.Selection, listener);
	upButton.addListener (SWT.Selection, listener);
	downButton.addListener (SWT.Selection, listener);
	fontSetList.addListener (SWT.Selection, listener);
}

/**
 * Initialize the extended styles list with the extended styles
 * available for the selected font.
 * Downstream lists are initialized as well (style and size).
 */
void initExtStyleList () {
	String oldSelect = getListSelection (extStyleList);
	extStyleList.removeAll ();
	
	String characterSet = getListSelection (charSetList);
	String faceName = getListSelection (faceNameList);
	Hashtable extStyles = getExtStyles (characterSet, faceName);
	setItemsSorted (extStyleList, extStyles);
	
	int selectIndex = extStyleList.indexOf (oldSelect);
	extStyleList.select (selectIndex);
	extStyleList.setData (new Integer (selectIndex));
	centerListIndex (extStyleList, selectIndex);
	initSizeList ();
}

/**
 * Initialize the face name list with all font names 
 * available in the selected character set.
 * Downstream lists are initialized as well (extended style).
 */
void initFaceNameList () {
	String oldSelect = getListSelection (faceNameList);
	faceNameList.removeAll ();
	String charSetText = getListSelection (charSetList);
	if (charSetText.length () == 0) return;
	
	Hashtable faceNames = getFaces (charSetText);
	setItemsSorted (faceNameList, faceNames);
	
	int selectIndex = faceNameList.indexOf (oldSelect);
	selectIndex = Math.max (0, selectIndex);
	faceNameList.select (selectIndex);
	faceNameList.setData (new Integer (selectIndex));
	centerListIndex (faceNameList, selectIndex);
	initExtStyleList ();
}

/**
 * Initialize the widgets of the receiver with the data of 
 * all installed fonts.  If the user specified a default font
 * preselect that font in the lists.
 */
void initFonts () {
	Display display = shell.display;
	// get all fonts available on the current display
	addFonts (display.getFontList (null, false));
	addFonts (display.getFontList (null, true));
	setItemsSorted (charSetList, getFonts ());
	if (fontData != null) {
		// verify that the initial font data is a valid font
		Font font = new Font (display, fontData);
		fontData = font.getFontData ();
		currentFontData = fontData [0];
		font.dispose ();
	} else {
		fontData = display.textFont.getFontData ();
		currentFontData = fontData [0];
	}
}

/**
 * Initialize the size list with the sizes the selected font 
 * is available in.  If the selected font is scalable a selection
 * of preset sizes is used.
 */
void initSizeList () {
	String oldSelect = getListSelection (fontSizeList);
	fontSizeList.removeAll ();
	
	String characterSet = getListSelection (charSetList);
	String faceName = getListSelection (faceNameList);
	String extStyle = getListSelection (extStyleList);
	Hashtable sizes = getSizes (characterSet, faceName, extStyle);
	if (sizes != null) {
		if (sizes.get (SCALABLE_KEY) == null) {
			/*
			 * Font is not scalable so just present the provided sizes.
			 */
			setSizeItemsSorted (sizes.keys ());
		} else {
			/*
			 * Font is scalable so present the provided sizes and scalable
			 * sizes for selection.
			 */
			Vector allSizes = new Vector ();
			/*
			 * Add the scalable sizes.
			 */
			for (int i = 0; i < SCALABLE_SIZES.length; i++) {
				allSizes.addElement (Integer.valueOf (SCALABLE_SIZES [i]));
			}
			/*
			 * Add the provided sizes.
			 */
			Enumeration providedSizes = sizes.keys ();
			while (providedSizes.hasMoreElements ()) {
				Integer size = (Integer) providedSizes.nextElement ();
				if (!size.equals (SCALABLE_KEY) && !allSizes.contains (size)) {
					allSizes.addElement (size);
				}
			}
			setSizeItemsSorted (allSizes.elements ());
		}
	}
	
	int selectIndex = fontSizeList.indexOf (oldSelect);
	if (selectIndex == -1) {
		selectIndex = fontSizeList.indexOf (String.valueOf (DEFAULT_SIZE));
	}
	selectIndex = Math.max (0, selectIndex);
	fontSizeList.select (selectIndex);
	fontSizeList.setData (new Integer (selectIndex));
	centerListIndex (fontSizeList, selectIndex);
	initStyleList ();
}

/**
 * Initialize the styles list with the styles the selected font 
 * is available in.
 */
void initStyleList () {
	String oldSelect = getListSelection (fontStyleList);
	fontStyleList.removeAll ();
	
	String characterSet = getListSelection (charSetList);
	String faceName = getListSelection (faceNameList);
	String extStyle = getListSelection (extStyleList);
	try {
		int size = Integer.valueOf (getListSelection (fontSizeList)).intValue ();
		if (size > 0) {
			Hashtable styles = getStyles (characterSet, faceName, extStyle, size);
			setItemsSorted (fontStyleList, styles);
		}
	} catch (NumberFormatException e) {
		// fall through
	}

	int selectIndex = fontStyleList.indexOf (oldSelect);
	if (selectIndex == -1) {
		selectIndex = fontStyleList.indexOf (String.valueOf (DEFAULT_STYLE));
	}
	selectIndex = Math.max (0, selectIndex);
	fontStyleList.select (selectIndex);
	fontStyleList.setData (new Integer (selectIndex));
	centerListIndex (fontStyleList, selectIndex);
}

/**
 * Makes the dialog visible and brings it to the front
 * of the display.
 *
 * @return a FontData object describing the font that was selected,
 *         or null if the dialog was cancelled or an error occurred
 *
 * @exception SWTException <ul>
 *    <li>ERROR_WIDGET_DISPOSED - if the dialog has been disposed</li>
 *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the dialog</li>
 * </ul>
 */
public FontData open () {
	shell = new Shell (getParent (), getStyle () | SWT.TITLE | SWT.BORDER | SWT.APPLICATION_MODAL);
	shell.setLayout (new GridLayout ());
	createControls (shell);
	
	FontData [] originalFontData = fontData;
	RGB originalRGB = rgb;
	initFonts ();
	openDialog ();
	setFontControls (currentFontData);
	updateSampleFont ();
	updateSampleColor ();
	updateFontList ();
	fontSetList.select (0);
	fontSetList.setData (new Integer (0));
	updateButtonEnablements ();
	hookListeners ();
	Display display = shell.display;
	while (!shell.isDisposed ()) {
		if (!display.readAndDispatch ()) display.sleep ();
	}
	
	FontData result = null;
	if (okSelected) {
		result = fontData [0];
	} else {
		fontData = originalFontData;
		rgb = originalRGB;
	}	
	if (sampleFont != null) sampleFont.dispose ();
	sampleFont = null;
	if (sampleColor != null) sampleColor.dispose ();
	sampleColor = null;
	return result;
}

/**
 * Open the receiver and set its size to the size calculated by 
 * the layout manager.
 */
void openDialog () {
	// Start everything off by setting the shell size to its computed size.
	Point pt = shell.computeSize (SWT.DEFAULT, SWT.DEFAULT, false);
	
	// Ensure that the width of the shell fits the display.
	Display display = shell.display;
	Rectangle displayRect = display.getBounds ();
	int widthLimit = displayRect.width * 7 / 8;
	int heightLimit = displayRect.height * 7 / 8;
	if (pt.x > widthLimit) {
		pt = shell.computeSize (widthLimit, SWT.DEFAULT, false);
	}
	
	// centre the dialog on its parent, and ensure that the
	// whole dialog appears within the screen bounds
	Rectangle parentBounds = getParent ().getBounds ();
	int originX = (parentBounds.width - pt.x) / 2 + parentBounds.x;
	originX = Math.max (originX, 0);
	originX = Math.min (originX, widthLimit - pt.x);
	int originY = (parentBounds.height - pt.y) / 2 + parentBounds.y;
	originY = Math.max (originY, 0);
	originY = Math.min (originY, heightLimit - pt.y);
	shell.setBounds (originX, originY, pt.x, pt.y);
	
	String title = getText ();
	if (title.length () == 0) title = SWT.getMessage ("SWT_FontDialog_Title");
	shell.setText (title);
	
	// Open the window.
	shell.open ();
}

/**
 * Initialize the lists with the data of the preselected
 * font specified by the user.
 */
void setFontControls (FontData fontData) {
	ignoreEvents = true;
	String characterSet = getTranslatedCharSet (fontData, true);
	String faceName = getTranslatedFaceName (fontData);
	charSetList.select (new String[] {characterSet});
	int index = charSetList.indexOf (characterSet);
	charSetList.setData (new Integer (index));
	if (index != -1) centerListIndex (charSetList, index);

	initFaceNameList ();
	faceNameList.select (new String[] {faceName});
	index = faceNameList.indexOf (faceName);
	faceNameList.setData (new Integer (index));
	if (index != -1) centerListIndex (faceNameList, index);

	initExtStyleList ();
	extStyleList.select (new String[] {fontData.addStyle});
	index = extStyleList.indexOf (fontData.addStyle);
	extStyleList.setData (new Integer (index));
	if (index != -1) centerListIndex (extStyleList, index);

	initSizeList ();
	String value = String.valueOf (fontData.getHeight ());
	fontSizeList.select (new String[] {value});
	index = fontSizeList.indexOf (value);
	fontSizeList.setData (new Integer (index));
	if (index != -1) centerListIndex (fontSizeList, index);

	initStyleList ();
	fontStyleList.select (new String[] {fontData.weight});
	index = fontStyleList.indexOf (fontData.weight);
	fontStyleList.setData (new Integer (index));
	if (index != -1) centerListIndex (fontStyleList, index);
	ignoreEvents = false;
}

/**
 * Sets a FontData object describing the font to be
 * selected by default in the dialog, or null to let
 * the platform choose one.
 * 
 * @param fontData the FontData to use initially, or null
 * @deprecated use #setFontList (FontData [])
 */
public void setFontData (FontData fontData) {
	if (fontData == null) {
		this.fontData = null;
	} else {
		this.fontData = new FontData [1];
		this.fontData [0] = fontData;
	}
}

/**
 * Sets a set of FontData objects describing the font to
 * be selected by default in the dialog, or null to let
 * the platform choose one.
 * 
 * @param fontData the set of FontData objects to use initially, or null
 * @since 2.1.1
 */
public void setFontList (FontData [] fontData) {
	this.fontData = fontData;
}

/**
 * Set the contents of 'list' to the keys of 'items'.
 * Keys are sorted in ascending order first and have to be Strings.
 */
void setItemsSorted (List list, Hashtable items) {
	if (items == null) return;
	Enumeration itemKeys = items.keys ();
	String [] sortedItems = new String [items.size ()];
	int index = 0;
	while (itemKeys.hasMoreElements ()) {
		String item = (String) itemKeys.nextElement ();
		if (item.length () != 0) sortedItems [index++] = item;
	}
	if (index != sortedItems.length) {
		String [] newItems = new String [index];
		System.arraycopy (sortedItems, 0, newItems, 0, index);
		sortedItems = newItems;
	}
	sort (sortedItems);
	list.setItems (sortedItems);
}

/**
 * Sets the receiver's selected color to be the argument.
 *
 * @param rgb the new RGB value for the selected color, may be
 *        null to let the platform to select a default when
 *        open() is called
 *
 * @see PaletteData#getRGBs
 * 
 * @since 2.1
 */
public void setRGB (RGB rgb) {
	this.rgb = rgb;
}

/**
 * Set the contents of the size list to the keys of 'items'.
 * Keys are sorted in ascending order first and have to be Integers.
 */
void setSizeItemsSorted (Enumeration itemsEnum) {
	Vector items = new Vector ();
	while (itemsEnum.hasMoreElements ()) {
		items.addElement (itemsEnum.nextElement ());
	}
	Integer [] sortedItems = new Integer [items.size ()];
	items.copyInto (sortedItems);
	sort (sortedItems);
	String [] sortedItemStrings = new String [items.size ()];
	for (int i = 0; i < sortedItemStrings.length; i++) {
		sortedItemStrings [i] = String.valueOf (sortedItems [i].intValue ());
	}
	fontSizeList.setItems (sortedItemStrings);
}

/**
 * Sort 'items' in ascending order.
 */
void sort (Integer [] items) {
	/* Shell Sort from K&R, pg 108 */
	int length = items.length;
	for (int gap = length / 2; gap > 0; gap /= 2) {
		for (int i = gap; i < length; i++) {
			for (int j = i - gap; j >= 0; j -= gap) {
		   		if (items [j].intValue () > items [j + gap].intValue ()) {
					Integer swap = items [j];
					items [j] = items [j + gap];
					items [j + gap] = swap;
		   		}
	    	}
	    }
	}
}

/**
 * Sort 'items' in ascending order.
 */
void sort (String items []) {
	/* Shell Sort from K&R, pg 108 */
	int length = items.length;
	for (int gap = length / 2; gap > 0; gap /= 2) {
		for (int i = gap; i < length; i++) {
			for (int j = i - gap; j >= 0; j -= gap) {
		   		if (items [j].compareTo (items [j + gap]) > 0) {
					String swap = items [j];
					items [j] = items [j + gap];
					items [j + gap] = swap;
		   		}
	    	}
	    }
	}
}

void updateButtonEnablements () {
	removeButton.setEnabled (fontSetList.getItemCount () > 1);
	upButton.setEnabled (fontSetList.getSelectionIndex () > 0);
	downButton.setEnabled (fontSetList.getSelectionIndex () < fontSetList.getItemCount () - 1);
}

void updateFontList () {
	int selectionIndex = fontSetList.getSelectionIndex ();
	int topIndex = Math.max (0, fontSetList.getTopIndex ());
	String [] items = new String [fontData.length];
	for (int i = 0; i < fontData.length; i++) {
		StringBuffer buffer = new StringBuffer ();
		buffer.append (i);
		buffer.append (": ");
		buffer.append (getTranslatedCharSet (fontData [i], false));
		buffer.append ("-");
		buffer.append (getTranslatedFaceName (fontData [i]));
		buffer.append ("-");
		if (!fontData [i].addStyle.equals ("")) {
			buffer.append (fontData [i].addStyle);
			buffer.append ("-");
		}
		buffer.append (fontData [i].getHeight ());
		buffer.append ("-");
		buffer.append (fontData [i].weight);
		items [i] = buffer.toString (); 
	}
	fontSetList.setItems (items);
	if (selectionIndex >= items.length) selectionIndex--;
	fontSetList.select (selectionIndex);
	fontSetList.setData (new Integer (selectionIndex));
	fontSetList.setTopIndex (topIndex);
	fontSetList.showSelection ();
}

void updateSampleColor () {
	if (rgb == null) {
		rgb = new RGB (0, 0, 0);
	}
	if (sampleColor != null) {
		if (sampleColor.getRGB ().equals (rgb)) return;
		sampleColor.dispose ();
	}
	sampleColor = new Color (parent.display, rgb);
	sampleLabel.setForeground (sampleColor);
}

/**
 * Set the font of the sample text to the selected font.
 * Display an error in place of the sample text if the selected 
 * font could not be loaded.
 */
void updateSampleFont () {
	FontData selectionFontData = getSelectionFontData ();
	/*
	 * sampleFont may not be the same as the one specified in selectionFontData.
	 * This happens when selectionFontData specifies a font alias.
	 */
	if (sampleFont != null) sampleFont.dispose ();
	int selectionIndex = Math.max (0, fontSetList.getSelectionIndex ());
	fontData [selectionIndex] = selectionFontData;
	sampleFont = new Font (shell.display, selectionFontData);
	sampleLabel.setFont (sampleFont);
}

}