Source code: org/gjt/sp/jedit/gui/FontSelector.java
1 /*
2 * FontSelector.java - Font selector
3 * :tabSize=8:indentSize=8:noTabs=false:
4 * :folding=explicit:collapseFolds=1:
5 *
6 * Copyright (C) 2000, 2003 Slava Pestov
7 * Portions copyright (C) 1999 Jason Ginchereau
8 * Portions copyright (C) 2003 mike dillon
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version 2
13 * of the License, or any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 */
24
25 package org.gjt.sp.jedit.gui;
26
27 //{{{ Imports
28 import java.awt.event.*;
29 import java.awt.*;
30 import java.util.Vector;
31 import javax.swing.border.*;
32 import javax.swing.event.*;
33 import javax.swing.*;
34 import org.gjt.sp.jedit.*;
35 import org.gjt.sp.util.Log;
36 //}}}
37
38 //{{{ FontSelector class
39 /**
40 * A font chooser widget.
41 * @author Slava Pestov
42 * @version $Id: FontSelector.java,v 1.7 2003/11/26 01:04:33 spestov Exp $
43 */
44 public class FontSelector extends JButton
45 {
46 //{{{ FontSelector constructor
47 /**
48 * Creates a new font selector control.
49 * @param font The font
50 */
51 public FontSelector(Font font)
52 {
53 this(font,false);
54 } //}}}
55
56 //{{{ FontSelector constructor
57 /**
58 * Creates a new font selector control.
59 * @param font The font
60 * @param antiAlias Is anti-aliasing enabled?
61 * @since jEdit 4.2pre7
62 */
63 public FontSelector(Font font, boolean antiAlias)
64 {
65 setFont(font);
66 this.antiAlias = antiAlias;
67
68 updateText();
69
70 setRequestFocusEnabled(false);
71
72 addActionListener(new ActionHandler());
73 } //}}}
74
75 //{{{ paintComponent() method
76 public void paintComponent(Graphics g)
77 {
78 setAntiAliasEnabled(g);
79 super.paintComponent(g);
80 } //}}}
81
82 //{{{ isAntiAliasEnabled() method
83 public boolean isAntiAliasEnabled()
84 {
85 return antiAlias;
86 } //}}}
87
88 //{{{ setAntiAliasEnabled() method
89 public void setAntiAliasEnabled(boolean antiAlias)
90 {
91 this.antiAlias = antiAlias;
92 } //}}}
93
94 //{{{ updateText() method
95 private void updateText()
96 {
97 Font font = getFont();
98 String styleString;
99 switch(font.getStyle())
100 {
101 case Font.PLAIN:
102 styleString = jEdit.getProperty("font-selector.plain");
103 break;
104 case Font.BOLD:
105 styleString = jEdit.getProperty("font-selector.bold");
106 break;
107 case Font.ITALIC:
108 styleString = jEdit.getProperty("font-selector.italic");
109 break;
110 case Font.BOLD | Font.ITALIC:
111 styleString = jEdit.getProperty("font-selector.bolditalic");
112 break;
113 default:
114 styleString = "UNKNOWN!!!???";
115 break;
116 }
117
118 setText(font.getName() + " " + font.getSize() + " " + styleString);
119 } //}}}
120
121 //{{{ setAntiAliasEnabled() method
122 void setAntiAliasEnabled(Graphics g)
123 {
124 if (antiAlias)
125 {
126 Graphics2D g2 = (Graphics2D)g;
127 g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
128 RenderingHints.VALUE_ANTIALIAS_ON);
129 }
130 } //}}}
131
132 private boolean antiAlias;
133
134 //{{{ ActionHandler class
135 class ActionHandler implements ActionListener
136 {
137 public void actionPerformed(ActionEvent evt)
138 {
139 Font font;
140
141 JDialog dialog = GUIUtilities.getParentDialog(FontSelector.this);
142 if(dialog == null)
143 {
144 font = new FontSelectorDialog(
145 JOptionPane.getFrameForComponent(
146 FontSelector.this),getFont(),
147 FontSelector.this)
148 .getSelectedFont();
149 }
150 else
151 {
152 font = new FontSelectorDialog(dialog,getFont(),
153 FontSelector.this)
154 .getSelectedFont();
155 }
156
157 if(font != null)
158 {
159 setFont(font);
160 updateText();
161 }
162 }
163 } //}}}
164 } //}}}
165
166 //{{{ FontSelectorDialog class
167 class FontSelectorDialog extends EnhancedDialog
168 {
169 //{{{ FontSelectorDialog constructor
170 public FontSelectorDialog(Frame parent, Font font)
171 {
172 super(parent,jEdit.getProperty("font-selector.title"),true);
173 init(font);
174 } //}}}
175
176 //{{{ FontSelectorDialog constructor
177 public FontSelectorDialog(Dialog parent, Font font)
178 {
179 super(parent,jEdit.getProperty("font-selector.title"),true);
180 init(font);
181 } //}}}
182
183 //{{{ FontSelectorDialog constructor
184 public FontSelectorDialog(Frame parent, Font font,
185 FontSelector fontSelector)
186 {
187 super(parent,jEdit.getProperty("font-selector.title"),true);
188 this.fontSelector = fontSelector;
189 init(font);
190 } //}}}
191
192 //{{{ FontSelectorDialog constructor
193 public FontSelectorDialog(Dialog parent, Font font,
194 FontSelector fontSelector)
195 {
196 super(parent,jEdit.getProperty("font-selector.title"),true);
197 this.fontSelector = fontSelector;
198 init(font);
199 } //}}}
200
201 //{{{ ok() method
202 public void ok()
203 {
204 isOK = true;
205 dispose();
206 } //}}}
207
208 //{{{ cancel() method
209 public void cancel()
210 {
211 dispose();
212 } //}}}
213
214 //{{{ getSelectedFont() method
215 public Font getSelectedFont()
216 {
217 if(!isOK)
218 return null;
219
220 int size;
221 try
222 {
223 size = Integer.parseInt(sizeField.getText());
224 }
225 catch(Exception e)
226 {
227 size = 12;
228 }
229
230 return new Font(familyField.getText(),styleList
231 .getSelectedIndex(),size);
232 } //}}}
233
234 //{{{ Private members
235
236 //{{{ Instance variables
237 private FontSelector fontSelector;
238 private boolean isOK;
239 private JTextField familyField;
240 private JList familyList;
241 private JTextField sizeField;
242 private JList sizeList;
243 private JTextField styleField;
244 private JList styleList;
245 private JLabel preview;
246 private JButton ok;
247 private JButton cancel;
248 //}}}
249
250 /**
251 * For some reason the default Java fonts show up in the
252 * list with .bold, .bolditalic, and .italic extensions.
253 */
254 private static final String[] HIDEFONTS = {
255 ".bold",
256 ".italic"
257 };
258
259 //{{{ init() method
260 private void init(Font font)
261 {
262 JPanel content = new JPanel(new BorderLayout());
263 content.setBorder(new EmptyBorder(12,12,12,12));
264 setContentPane(content);
265
266 JPanel listPanel = new JPanel(new GridLayout(1,3,6,6));
267
268 String[] fonts;
269 try
270 {
271 fonts = getFontList();
272 }
273 catch(Exception e)
274 {
275 Log.log(Log.ERROR,this,"Broken Java implementation!");
276 /* Log.log(Log.ERROR,this,"Using deprecated Toolkit.getFontList()"); */
277 Log.log(Log.ERROR,this,e);
278
279 /* fonts = getToolkit().getFontList(); */
280 fonts = new String[] { "Broken Java implementation!" };
281 }
282
283 JPanel familyPanel = createTextFieldAndListPanel(
284 "font-selector.family",
285 familyField = new JTextField(),
286 familyList = new JList(fonts));
287 listPanel.add(familyPanel);
288
289 String[] sizes = { "9", "10", "12", "14", "16", "18", "24" };
290 JPanel sizePanel = createTextFieldAndListPanel(
291 "font-selector.size",
292 sizeField = new JTextField(),
293 sizeList = new JList(sizes));
294 listPanel.add(sizePanel);
295
296 String[] styles = {
297 jEdit.getProperty("font-selector.plain"),
298 jEdit.getProperty("font-selector.bold"),
299 jEdit.getProperty("font-selector.italic"),
300 jEdit.getProperty("font-selector.bolditalic")
301 };
302
303 JPanel stylePanel = createTextFieldAndListPanel(
304 "font-selector.style",
305 styleField = new JTextField(),
306 styleList = new JList(styles));
307 styleField.setEditable(false);
308 listPanel.add(stylePanel);
309
310 familyList.setSelectedValue(font.getFamily(),true);
311 familyField.setText(font.getFamily());
312 sizeList.setSelectedValue(String.valueOf(font.getSize()),true);
313 sizeField.setText(String.valueOf(font.getSize()));
314 styleList.setSelectedIndex(font.getStyle());
315 styleField.setText((String)styleList.getSelectedValue());
316
317 ListHandler listHandler = new ListHandler();
318 familyList.addListSelectionListener(listHandler);
319 sizeList.addListSelectionListener(listHandler);
320 styleList.addListSelectionListener(listHandler);
321
322 content.add(BorderLayout.NORTH,listPanel);
323
324 preview = new JLabel(jEdit.getProperty("font-selector.long-text")) {
325 public void paintComponent(Graphics g)
326 {
327 if(fontSelector != null)
328 fontSelector.setAntiAliasEnabled(g);
329 super.paintComponent(g);
330 }
331 };
332 preview.setBorder(new TitledBorder(jEdit.getProperty(
333 "font-selector.preview")));
334
335 updatePreview();
336
337 Dimension prefSize = preview.getPreferredSize();
338 prefSize.height = 50;
339 preview.setPreferredSize(prefSize);
340
341 content.add(BorderLayout.CENTER,preview);
342
343 JPanel buttons = new JPanel();
344 buttons.setLayout(new BoxLayout(buttons,BoxLayout.X_AXIS));
345 buttons.setBorder(new EmptyBorder(12,0,0,0));
346 buttons.add(Box.createGlue());
347
348 ok = new JButton(jEdit.getProperty("common.ok"));
349 ok.addActionListener(new ActionHandler());
350 getRootPane().setDefaultButton(ok);
351 buttons.add(ok);
352
353 buttons.add(Box.createHorizontalStrut(6));
354
355 cancel = new JButton(jEdit.getProperty("common.cancel"));
356 cancel.addActionListener(new ActionHandler());
357 buttons.add(cancel);
358
359 buttons.add(Box.createGlue());
360
361 content.add(BorderLayout.SOUTH,buttons);
362
363 pack();
364 setLocationRelativeTo(getParent());
365 show();
366 } //}}}
367
368 //{{{ getFontList() method
369 private String[] getFontList()
370 {
371 String[] nameArray = GraphicsEnvironment
372 .getLocalGraphicsEnvironment()
373 .getAvailableFontFamilyNames();
374 Vector nameVector = new Vector(nameArray.length);
375
376 for(int i = 0, j; i < nameArray.length; i++)
377 {
378 for(j = 0; j < HIDEFONTS.length; j++)
379 {
380 if(nameArray[i].indexOf(HIDEFONTS[j]) >= 0)
381 break;
382 }
383
384 if(j == HIDEFONTS.length)
385 nameVector.addElement(nameArray[i]);
386 }
387
388 String[] _array = new String[nameVector.size()];
389 nameVector.copyInto(_array);
390 return _array;
391 } //}}}
392
393 //{{{ createTextFieldAndListPanel() method
394 private JPanel createTextFieldAndListPanel(String label,
395 JTextField textField, JList list)
396 {
397 GridBagLayout layout = new GridBagLayout();
398 JPanel panel = new JPanel(layout);
399
400 GridBagConstraints cons = new GridBagConstraints();
401 cons.gridx = cons.gridy = 0;
402 cons.gridwidth = cons.gridheight = 1;
403 cons.fill = GridBagConstraints.BOTH;
404 cons.weightx = 1.0f;
405
406 JLabel _label = new JLabel(jEdit.getProperty(label));
407 layout.setConstraints(_label,cons);
408 panel.add(_label);
409
410 cons.gridy = 1;
411 Component vs = Box.createVerticalStrut(6);
412 layout.setConstraints(vs,cons);
413 panel.add(vs);
414
415 cons.gridy = 2;
416 layout.setConstraints(textField,cons);
417 panel.add(textField);
418
419 cons.gridy = 3;
420 vs = Box.createVerticalStrut(6);
421 layout.setConstraints(vs,cons);
422 panel.add(vs);
423
424 cons.gridy = 4;
425 cons.gridheight = GridBagConstraints.REMAINDER;
426 cons.weighty = 1.0f;
427 JScrollPane scroller = new JScrollPane(list);
428 layout.setConstraints(scroller,cons);
429 panel.add(scroller);
430
431 return panel;
432 } //}}}
433
434 //{{{ updatePreview() method
435 private void updatePreview()
436 {
437 String family = familyField.getText();
438 int size;
439 try
440 {
441 size = Integer.parseInt(sizeField.getText());
442 }
443 catch(Exception e)
444 {
445 size = 12;
446 }
447 int style = styleList.getSelectedIndex();
448
449 preview.setFont(new Font(family,style,size));
450 } //}}}
451
452 //}}}
453
454 //{{{ ActionHandler class
455 class ActionHandler implements ActionListener
456 {
457 public void actionPerformed(ActionEvent evt)
458 {
459 if(evt.getSource() == ok)
460 ok();
461 else if(evt.getSource() == cancel)
462 cancel();
463 }
464 } //}}}
465
466 //{{{ ListHandler class
467 class ListHandler implements ListSelectionListener
468 {
469 public void valueChanged(ListSelectionEvent evt)
470 {
471 Object source = evt.getSource();
472 if(source == familyList)
473 {
474 String family = (String)familyList.getSelectedValue();
475 if(family != null)
476 familyField.setText(family);
477 }
478 else if(source == sizeList)
479 {
480 String size = (String)sizeList.getSelectedValue();
481 if(size != null)
482 sizeField.setText(size);
483 }
484 else if(source == styleList)
485 {
486 String style = (String)styleList.getSelectedValue();
487 if(style != null)
488 styleField.setText(style);
489 }
490
491 updatePreview();
492 }
493 } //}}}
494 } //}}}