1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.xnap.gui.component;
21
22
23 import java.awt.Component;
24 import java.awt.Font;
25
26 import org.apache.log4j.Logger;
27
28 /***
29 * This class provides a font chooser dialog. The user can choose from a list
30 * of fonts and attributes that are available on the system.
31 */
32 public class FontChooserDialog extends DefaultDialog
33 {
34
35
36
37
38
39 protected static Logger logger = Logger.getLogger(FontChooserDialog.class);
40
41 protected FontSelectionPanel fsp;
42 protected Font font = null;
43
44
45
46 public FontChooserDialog(Font font)
47 {
48 fsp = new FontSelectionPanel(font);
49 setMainComponent(fsp);
50
51 pack();
52
53 try {
54 fsp.setSelectedFont(font);
55 }
56 catch (Exception e) {
57 logger.debug("font selection failed", e);
58 }
59 }
60
61
62
63 public boolean apply()
64 {
65 try {
66 font = fsp.getSelectedFont();
67 }
68 catch (FontSelectionPanel.InvalidFontException e) {
69 }
70 return true;
71 }
72
73 /***
74 * Returns the selected font.
75 *
76 * @return null, if dialog was canceled; the font, otherwise
77 */
78 public Font getSelectedFont()
79 {
80 return font;
81 }
82
83 /***
84 * Displays a font dialog. The dialog is placed relative to the position
85 * of <code>c</code>.
86 *
87 * @param c the component; if null, the dialog will be placed at the center
88 * of the screen
89 * @param f the font that is to be selected, if available
90 * @return null, if dialog was canceled; the font, otherwise
91 */
92 public static Font showDialog(Component c, Font f)
93 {
94 FontChooserDialog d = new FontChooserDialog(f);
95 d.setModal(true);
96 d.show(c);
97 return d.getSelectedFont();
98 }
99
100 }
101
102