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
|
package gjt;
import java.awt.*;
/**
* A dialog used for selecting a font. FontDialog is
* constructed with a Frame, DialogClient, initial font to
* display, and boolean that indicates modality.<p>
*
* FontDialog contains a preview panel which previews the
* currently selected font. Updating of the preview panel is
* triggered by a preview button at the bottom of the dialog.<p>
*
* FontDialog contains 3 methods which define the labels for
* the buttons it contains:
* <dl>
* <dd> String getPreviewButtonLabel()
* <dd> String getOkButtonLabel()
* <dd> String getCancelButtonLabel()
* </dl><p>
*
* By default the 3 methods return "Preview", "Ok" and "Cancel"
* respectively. FontDialog may be extended and the 3 methods
* overridden to customize the labels displayed in the
* buttons.<p>
*
* FontDialog uses Toolkit to get a list of fonts by invoking
* Toolkit.getFontList(). This is done in the getFontNames()
* method, which may be overridden by extensions of FontDialog
* in case the standard set of font names are inadequate.<p>
*
* Finally, font sizes are obtained by the getFontSizes()
* method. FontDialog defines 8 sizes by default: 8, 12, 14,
* 16, 18, 24, 48 and 64. Extensions of FontDialog may override
* getFontSizes() to provide a different list of sizes.<p>
*
* See gjt.test.FontDialogTest for an example of an extension
* of FontDialog which overrides the methods discussed above.<p>
*
* @version 1.0, Apr 1 1996
* @author David Geary
* @see java.awt.Dialog
* @see java.awt.Toolkit
* @see DialogClient
* @see gjt.test.FontDialogTest
*/
public class FontDialog extends Dialog {
private static String _defaultSizes[] =
{ "8", "12", "14", "16", "18", "24", "48", "64" };
private FontPanel fontPanel;
private Font fontSelected;
private DialogClient client;
public FontDialog(Frame frame,
DialogClient client,
Font font, // initial font
boolean modal) {
super(frame, "Select A Font", modal);
this.client = client;
setLayout(new BorderLayout());
add("Center", fontPanel = new FontPanel(this, font));
}
public boolean handleEvent(Event event) {
if(event.id == Event.WINDOW_DESTROY)
done(null);
return super.handleEvent(event);
}
public String[] getFontNames() {
return getToolkit().getFontList();
}
public String[] getFontSizes() {
return _defaultSizes;
}
public String getPreviewButtonLabel() { return "Preview"; }
public String getOkButtonLabel () { return "Ok"; }
public String getCancelButtonLabel () { return "Cancel"; }
public void show() {
Point frameLoc = getParent().location();
reshape(frameLoc.x + 50, frameLoc.x + 50, 550, 450);
super.show();
}
public void done(Font font) {
fontSelected = font;
client.dialogDismissed(this);
hide ();
dispose();
}
public Font getFontSelected() {
return fontSelected;
}
public void listSelectedInPicker() {
fontPanel.getPreviewButton().requestFocus();
}
}
class FontPanel extends Panel {
private static Font defaultFont =
new Font("TimesRoman", Font.PLAIN, 12);
private FontPreviewPanel preview;
private FontSelectionPanel fsp;
public FontPanel(FontDialog dialog, Font f) {
Font font = f == null ? defaultFont : f;
setLayout(new BorderLayout());
add("North", preview = new FontPreviewPanel ());
add("Center", fsp =
new FontSelectionPanel(dialog, preview, font));
}
public Button getPreviewButton() {
return fsp.getPreviewButton();
}
}
class FontPreviewPanel extends Panel {
TextField textField = new TextField();
Box box = new Box(textField, "Preview");
public FontPreviewPanel() {
textField.setEditable(false);
setLayout(new BorderLayout());
add("Center", box);
}
public void setPreviewFont(Font font) {
String name = font.getName();
String size = String.valueOf(font.getSize());
String style = new String();
if(font.isPlain () == true) style = "Plain";
else {
if(font.isBold () == true) style += "Bold";
if(font.isItalic() == true) style += "Italic";
}
textField.setFont(font);
textField.setText(name + " " + style + " " + size);
retrofitPreviewPanel();
}
private void retrofitPreviewPanel() {
Dimension tfps, tfs;
FontPanel fontPanel = (FontPanel)getParent();
tfps = textField.preferredSize();
tfs = textField.size();
if(tfps.width != tfs.width ||
tfps.height != tfs.height) {
fontPanel.invalidate();
fontPanel.getParent().validate();
box.repaint(); // Only necessary on Win95
}
}
}
class FontSelectionPanel extends Panel {
private FontPickerPanel picker;
private FontButtonsPanel buttons;
private FontPreviewPanel preview;
private Font initialFont;
public FontSelectionPanel(FontDialog dialog,
FontPreviewPanel preview,
Font initialFont) {
this.preview = preview;
this.initialFont = initialFont;
picker = new FontPickerPanel (dialog, initialFont);
buttons = new FontButtonsPanel(dialog, picker, preview);
setLayout(new BorderLayout());
add("Center", picker);
add("South", buttons);
}
public void addNotify() {
super.addNotify();
preview.setPreviewFont(initialFont);
}
public Button getPreviewButton() {
return buttons.getPreviewButton();
}
}
class FontPickerPanel extends Panel {
private FontDialog dialog;
private Button previewButton;
private List fonts = new List();
private List styles = new List();
private List sizes = new List();
private Font initialFont;
public FontPickerPanel(FontDialog dialog,
Font initialFont) {
GridBagLayout gbl = new GridBagLayout();
GridBagConstraints gbc = new GridBagConstraints();
Label family = new Label("Family");
Label style = new Label("Style");
Label size = new Label("Size");
this.initialFont = initialFont;
this.dialog = dialog;
populateFonts ();
populateStyles();
populateSizes ();
setLayout(gbl);
gbc.anchor = GridBagConstraints.NORTH;
gbc.gridwidth = 1;
gbl.setConstraints(family, gbc); add(family);
gbl.setConstraints(style, gbc); add(style);
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbl.setConstraints(size, gbc); add(size);
gbc.gridwidth = 1;
gbc.weighty = 1.0;
gbc.weightx = 1.0;
gbc.fill = GridBagConstraints.BOTH;
gbl.setConstraints(fonts, gbc); add(fonts);
gbl.setConstraints(styles, gbc); add(styles);
gbl.setConstraints(sizes, gbc); add(sizes);
}
public boolean handleEvent(Event event) {
if(event.id == Event.LIST_SELECT) {
dialog.listSelectedInPicker();
return true;
}
return false;
}
public void addNotify() {
super.addNotify();
String initialFamily = initialFont.getName();
int initialSize = initialFont.getSize();
int initialStyle = initialFont.getStyle();
styles.select(initialStyle);
for(int i=0; i < fonts.countItems(); ++i) {
String nextFamily = fonts.getItem(i);
if(nextFamily.equals(initialFamily))
fonts.select(i);
}
for(int i=0; i < sizes.countItems(); ++i) {
String nextSize = sizes.getItem(i);
if(nextSize.equals(String.valueOf(initialSize)))
sizes.select(i);
}
}
public String fontSelected() {
return fonts.getSelectedItem ();
}
public String styleSelected() {
return styles.getSelectedItem();
}
public int sizeSelected() {
String szstring = sizes.getSelectedItem();
if(szstring != null) {
Integer integer = new Integer(szstring);
return integer.intValue();
}
else
return 0;
}
private void populateFonts() {
String names[] = dialog.getFontNames();
for(int i=0; i < names.length; ++i) {
fonts.addItem(names[i]);
}
}
private void populateSizes() {
String sizeArray[] = dialog.getFontSizes();
for(int i=0; i < sizeArray.length; ++i) {
sizes.addItem(sizeArray[i]);
}
}
private void populateStyles() {
styles.addItem("Plain");
styles.addItem("Bold");
styles.addItem("Italic");
styles.addItem("BoldItalic");
}
}
class FontButtonsPanel extends Panel {
private FontDialog dialog;
private FontPickerPanel picker;
private FontPreviewPanel preview;
private Button previewButton,
okButton,
cancelButton;
public FontButtonsPanel(FontDialog dialog,
FontPickerPanel picker,
FontPreviewPanel preview) {
this.picker = picker;
this.preview = preview;
this.dialog = dialog;
add(previewButton =
new Button(dialog.getPreviewButtonLabel()));
add(cancelButton =
new Button(dialog.getCancelButtonLabel()));
add(okButton =
new Button(dialog.getOkButtonLabel()));
}
public void addNotify() {
super.addNotify();
cancelButton.requestFocus();
}
public boolean action(Event event, Object object) {
Button button = (Button)event.target;
boolean handledEvent = true;
if(event.target == previewButton) {
Font selectedFont = fontSelected();
if(selectedFont != null) {
preview.setPreviewFont(selectedFont);
okButton.requestFocus();
}
}
else if(event.target == okButton)
dialog.done(fontSelected());
else if(event.target == cancelButton)
dialog.done(null);
else
handledEvent = false;
return handledEvent;
}
public Button getPreviewButton() {
return previewButton;
}
private Font fontSelected() {
String font = picker.fontSelected ();
String style = picker.styleSelected();
int size = picker.sizeSelected ();
int istyle = Font.PLAIN;
if(font != null && style != null && size > 0) {
if(style.equals("Bold")) istyle = Font.BOLD;
if(style.equals("Plain")) istyle = Font.PLAIN;
if(style.equals("Italic")) istyle = Font.ITALIC;
if(style.equals("BoldItalic"))
istyle = Font.BOLD + Font.ITALIC;
return new Font(font, istyle, size);
}
else
return null;
}
}
|