add font combo box
[jSite2.git] / src / net / pterodactylus / jsite / gui / FontComboBox.java
1 /*
2  * jSite2 - FontList.java -
3  * Copyright © 2008 David Roden
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  */
19
20 package net.pterodactylus.jsite.gui;
21
22 import java.awt.Component;
23 import java.awt.Font;
24 import java.awt.GraphicsEnvironment;
25
26 import javax.swing.DefaultComboBoxModel;
27 import javax.swing.JComboBox;
28 import javax.swing.JLabel;
29 import javax.swing.JList;
30 import javax.swing.ListCellRenderer;
31
32 /**
33  * Combobox that displays a list of all available fonts, showing the name of the
34  * font in the font itself.
35  *
36  * @see GraphicsEnvironment#getAllFonts()
37  * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
38  * @version $Id$
39  */
40 public class FontComboBox extends JComboBox {
41
42         /**
43          * Creates a new font combo box.
44          */
45         public FontComboBox() {
46                 super();
47                 Font[] allFonts = GraphicsEnvironment.getLocalGraphicsEnvironment().getAllFonts();
48                 String[] fontNames = new String[allFonts.length];
49                 int fontIndex = 0;
50                 for (Font font: allFonts) {
51                         fontNames[fontIndex++] = font.getName();
52                 }
53                 setModel(new DefaultComboBoxModel(fontNames));
54                 setRenderer(new FontCellRenderer());
55         }
56
57         /**
58          * The cell renderer for the font name cells.
59          *
60          * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
61          * @version $Id$
62          */
63         private static class FontCellRenderer extends JLabel implements ListCellRenderer {
64
65                 /**
66                  * Creates a new list cell renderer.
67                  */
68                 public FontCellRenderer() {
69                         setOpaque(true);
70                 }
71
72                 /**
73                  * {@inheritDoc}
74                  */
75                 public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
76                         Font font = Font.decode((String) value);
77                         setFont(font);
78                         setText((String) value);
79                         setBackground(isSelected ? list.getSelectionBackground() : list.getBackground());
80                         setForeground(isSelected ? list.getSelectionForeground() : list.getForeground());
81                         return this;
82                 }
83
84         }
85
86 }