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 |
| package edu.rice.cs.util.swing; |
38 |
| |
39 |
| import edu.rice.cs.util.swing.Utilities; |
40 |
| import javax.swing.*; |
41 |
| import javax.swing.border.EmptyBorder; |
42 |
| import javax.swing.event.ListSelectionEvent; |
43 |
| import javax.swing.event.ListSelectionListener; |
44 |
| import java.awt.*; |
45 |
| import java.awt.event.*; |
46 |
| |
47 |
| |
48 |
| |
49 |
| |
50 |
| |
51 |
| |
52 |
| |
53 |
| |
54 |
| public class FontChooser extends JDialog { |
55 |
| |
56 |
| |
57 |
| private static final String[] STYLES = |
58 |
| new String[] { "Plain", "Bold", "Italic", "Bold Italic" }; |
59 |
| |
60 |
| |
61 |
| |
62 |
| private static final String[] SIZES = |
63 |
| new String[] { "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", |
64 |
| "13", "14", "15", "16", "17", "18", "19", "20", "22", |
65 |
| "24", "27", "30", "34", "39", "45", "51", "60"}; |
66 |
| |
67 |
| |
68 |
| private NwList _styleList; |
69 |
| private NwList _fontList; |
70 |
| private NwList _sizeList; |
71 |
| |
72 |
| |
73 |
| private JButton _okButton; |
74 |
| private JButton _cancelButton; |
75 |
| private JLabel _sampleText = new JLabel(); |
76 |
| |
77 |
| private boolean _clickedOK = false; |
78 |
| |
79 |
| |
80 |
| |
81 |
| |
82 |
0
| private FontChooser(Frame parent, Font font) {
|
83 |
0
| super(parent, true);
|
84 |
0
| initAll();
|
85 |
0
| if (font == null) font = _sampleText.getFont();
|
86 |
0
| _fontList.setSelectedItem(font.getName());
|
87 |
0
| _sizeList.setSelectedItem(font.getSize() + "");
|
88 |
0
| _styleList.setSelectedItem(STYLES[font.getStyle()]);
|
89 |
| |
90 |
| |
91 |
| } |
92 |
| |
93 |
| |
94 |
| |
95 |
| |
96 |
| |
97 |
| |
98 |
| |
99 |
| |
100 |
0
| public static Font showDialog(Frame parent, String title, Font font) {
|
101 |
0
| FontChooser fd = new FontChooser(parent, font);
|
102 |
0
| fd.setTitle(title);
|
103 |
| |
104 |
0
| Utilities.setPopupLoc(fd, parent);
|
105 |
0
| fd.setVisible(true);
|
106 |
| |
107 |
0
| Font chosenFont = null;
|
108 |
0
| if (fd.clickedOK()) {
|
109 |
0
| chosenFont = fd.getFont();
|
110 |
| } |
111 |
0
| fd.dispose();
|
112 |
0
| return (chosenFont);
|
113 |
| } |
114 |
| |
115 |
| |
116 |
| |
117 |
0
| public static Font showDialog(Frame parent, Font font) {
|
118 |
0
| return showDialog(parent, "Font Chooser", font);
|
119 |
| } |
120 |
| |
121 |
0
| private void initAll() {
|
122 |
0
| Container cp = getContentPane();
|
123 |
0
| GridBagLayout cpLayout = new GridBagLayout();
|
124 |
0
| GridBagConstraints c = new GridBagConstraints();
|
125 |
0
| cp.setLayout(cpLayout);
|
126 |
| |
127 |
| |
128 |
0
| c.fill = GridBagConstraints.BOTH;
|
129 |
0
| c.anchor = GridBagConstraints.NORTH;
|
130 |
0
| c.gridwidth = 1;
|
131 |
0
| c.gridheight = 1;
|
132 |
0
| c.gridx = 0;
|
133 |
0
| c.gridy = 0;
|
134 |
0
| c.weightx = 1.0;
|
135 |
0
| c.weighty = 1.0;
|
136 |
0
| _fontList = new NwList(GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames());
|
137 |
0
| cpLayout.setConstraints(_fontList, c);
|
138 |
0
| cp.add(_fontList);
|
139 |
| |
140 |
| |
141 |
| |
142 |
| |
143 |
| |
144 |
0
| c.fill = GridBagConstraints.VERTICAL;
|
145 |
0
| c.anchor = GridBagConstraints.NORTH;
|
146 |
0
| c.gridwidth = GridBagConstraints.RELATIVE;
|
147 |
0
| c.gridheight = 1;
|
148 |
0
| c.gridx = 1;
|
149 |
0
| c.gridy = 0;
|
150 |
0
| c.weightx = 0.3;
|
151 |
0
| c.weighty = 1.0;
|
152 |
0
| _styleList = new NwList(STYLES);
|
153 |
0
| cpLayout.setConstraints(_styleList , c);
|
154 |
0
| cp.add(_styleList);
|
155 |
| |
156 |
| |
157 |
| |
158 |
| |
159 |
| |
160 |
0
| c.fill = GridBagConstraints.BOTH;
|
161 |
0
| c.anchor = GridBagConstraints.NORTH;
|
162 |
0
| c.gridwidth = GridBagConstraints.REMAINDER;
|
163 |
0
| c.gridheight = 1;
|
164 |
0
| c.gridx = 2;
|
165 |
0
| c.gridy = 0;
|
166 |
0
| c.weightx = 0.3;
|
167 |
0
| c.weighty = 1.0;
|
168 |
0
| _sizeList = new NwList(SIZES);
|
169 |
0
| cpLayout.setConstraints(_sizeList, c);
|
170 |
0
| cp.add(_sizeList);
|
171 |
| |
172 |
| |
173 |
0
| c.fill = GridBagConstraints.BOTH;
|
174 |
0
| c.anchor = GridBagConstraints.WEST;
|
175 |
0
| c.gridwidth = GridBagConstraints.REMAINDER;
|
176 |
0
| c.gridheight = 1;
|
177 |
0
| c.gridx = 0;
|
178 |
0
| c.gridy = 1;
|
179 |
0
| c.weightx = 0.0;
|
180 |
0
| c.weighty = 0.0;
|
181 |
0
| _sampleText = new JLabel();
|
182 |
0
| _sampleText.setForeground(Color.black);
|
183 |
0
| cpLayout.setConstraints(_sampleText, c);
|
184 |
0
| cp.add(_sampleText);
|
185 |
| |
186 |
| |
187 |
0
| JPanel bottom = new JPanel();
|
188 |
0
| bottom.setBorder(new EmptyBorder(5,5,5,5));
|
189 |
0
| bottom.setLayout(new BoxLayout(bottom, BoxLayout.X_AXIS));
|
190 |
0
| bottom.add(Box.createHorizontalGlue());
|
191 |
| |
192 |
0
| _okButton = new JButton("OK");
|
193 |
0
| _cancelButton = new JButton("Cancel");
|
194 |
0
| bottom.add(_okButton);
|
195 |
0
| bottom.add(_cancelButton);
|
196 |
0
| _okButton.addActionListener(new ActionListener() {
|
197 |
0
| public void actionPerformed(ActionEvent e) {
|
198 |
0
| setVisible(false);
|
199 |
0
| _clickedOK = true;
|
200 |
| } |
201 |
| }); |
202 |
0
| _cancelButton.addActionListener(new ActionListener() {
|
203 |
0
| public void actionPerformed(ActionEvent e) {
|
204 |
0
| setVisible(false);
|
205 |
0
| _clickedOK = false;
|
206 |
| } |
207 |
| }); |
208 |
0
| bottom.add(Box.createHorizontalGlue());
|
209 |
| |
210 |
0
| c.fill = GridBagConstraints.NONE;
|
211 |
0
| c.anchor = GridBagConstraints.PAGE_END;
|
212 |
0
| c.gridwidth = GridBagConstraints.REMAINDER;
|
213 |
0
| c.gridheight = 1;
|
214 |
0
| c.gridx = 0;
|
215 |
0
| c.gridy = 2;
|
216 |
0
| c.weightx = 0.0;
|
217 |
0
| c.weighty = 0.0;
|
218 |
0
| cpLayout.setConstraints(bottom, c);
|
219 |
0
| cp.add(bottom);
|
220 |
| |
221 |
0
| addWindowListener(new WindowAdapter() {
|
222 |
0
| public void windowClosing(java.awt.event.WindowEvent e) {
|
223 |
0
| setVisible(false);
|
224 |
| } |
225 |
| }); |
226 |
| |
227 |
0
| setSize(425, 400);
|
228 |
| } |
229 |
| |
230 |
0
| private void showSample() {
|
231 |
0
| int g = 0;
|
232 |
0
| try { g = Integer.parseInt(_sizeList.getSelectedValue()); }
|
233 |
| catch (NumberFormatException nfe) { } |
234 |
0
| String st = _styleList.getSelectedValue();
|
235 |
0
| int s = Font.PLAIN;
|
236 |
0
| if (st.equalsIgnoreCase("Bold")) s = Font.BOLD;
|
237 |
0
| if (st.equalsIgnoreCase("Italic")) s = Font.ITALIC;
|
238 |
0
| if (st.equalsIgnoreCase("Bold Italic")) s = Font.BOLD | Font.ITALIC;
|
239 |
0
| _sampleText.setFont(new Font(_fontList.getSelectedValue(), s, g));
|
240 |
0
| _sampleText.setText("The quick brown fox jumped over the lazy dog.");
|
241 |
0
| _sampleText.setVerticalAlignment(SwingConstants.TOP);
|
242 |
| } |
243 |
| |
244 |
| |
245 |
0
| public boolean clickedOK() { return _clickedOK; }
|
246 |
| |
247 |
| |
248 |
0
| public Font getFont() { return _sampleText.getFont(); }
|
249 |
| |
250 |
| |
251 |
| |
252 |
| |
253 |
| public class NwList extends JPanel { |
254 |
| JList jl; |
255 |
| JScrollPane sp; |
256 |
| JLabel jt; |
257 |
| String si = " "; |
258 |
| |
259 |
0
| public NwList(String[] values) {
|
260 |
0
| GridBagLayout cpLayout = new GridBagLayout();
|
261 |
0
| GridBagConstraints c = new GridBagConstraints();
|
262 |
0
| setLayout(cpLayout);
|
263 |
| |
264 |
0
| jl = new JList(values);
|
265 |
0
| sp = new JScrollPane(jl);
|
266 |
0
| jt = new JLabel();
|
267 |
0
| jt.setBackground(Color.white);
|
268 |
0
| jt.setForeground(Color.black);
|
269 |
0
| jt.setOpaque(true);
|
270 |
0
| jt.setBorder(new JTextField().getBorder());
|
271 |
0
| jt.setFont(getFont());
|
272 |
0
| jl.addListSelectionListener(new ListSelectionListener() {
|
273 |
0
| public void valueChanged(ListSelectionEvent e) {
|
274 |
0
| jt.setText((String) jl.getSelectedValue());
|
275 |
0
| si = (String) jl.getSelectedValue();
|
276 |
0
| showSample();
|
277 |
| } |
278 |
| }); |
279 |
| |
280 |
0
| c.fill = GridBagConstraints.HORIZONTAL;
|
281 |
0
| c.anchor = GridBagConstraints.NORTH;
|
282 |
0
| c.gridwidth = GridBagConstraints.REMAINDER;
|
283 |
0
| c.gridheight = 1;
|
284 |
0
| c.gridx = 0;
|
285 |
0
| c.gridy = 0;
|
286 |
0
| c.weightx = 1.0;
|
287 |
0
| c.weighty = 0.0;
|
288 |
0
| cpLayout.setConstraints(jt, c);
|
289 |
0
| add(jt);
|
290 |
| |
291 |
0
| c.fill = GridBagConstraints.BOTH;
|
292 |
0
| c.anchor = GridBagConstraints.NORTH;
|
293 |
0
| c.gridwidth = GridBagConstraints.REMAINDER;
|
294 |
0
| c.gridheight = 1;
|
295 |
0
| c.gridx = 0;
|
296 |
0
| c.gridy = 1;
|
297 |
0
| c.weightx = 1.0;
|
298 |
0
| c.weighty = 1.0;
|
299 |
0
| cpLayout.setConstraints(sp, c);
|
300 |
0
| add(sp);
|
301 |
| } |
302 |
| |
303 |
0
| public String getSelectedValue() { return (si); }
|
304 |
| |
305 |
0
| public void setSelectedItem(String s) { jl.setSelectedValue(s, true);}
|
306 |
| } |
307 |
| } |