02ec429f753f286e03a9f4d84bacd77d081805b7
[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         /** The font names. */
43         private static final String[] fontNames;
44
45         static {
46                 Font[] allFonts = GraphicsEnvironment.getLocalGraphicsEnvironment().getAllFonts();
47                 System.out.println("got " + allFonts.length + " fonts");
48                 fontNames = new String[allFonts.length];
49                 int fontIndex = 0;
50                 for (Font font: allFonts) {
51                         fontNames[fontIndex++] = font.getName();
52                 }
53         }
54
55         /**
56          * Creates a new font combo box.
57          */
58         public FontComboBox() {
59                 super();
60                 setModel(new DefaultComboBoxModel(fontNames));
61                 setRenderer(new FontCellRenderer());
62         }
63
64         /**
65          * The cell renderer for the font name cells.
66          * 
67          * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
68          * @version $Id$
69          */
70         private static class FontCellRenderer extends JLabel implements ListCellRenderer {
71
72                 /**
73                  * Creates a new list cell renderer.
74                  */
75                 public FontCellRenderer() {
76                         setOpaque(true);
77                 }
78
79                 /**
80                  * {@inheritDoc}
81                  */
82                 public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
83                         Font font = Font.decode((String) value);
84                         setFont(font);
85                         setText((String) value);
86                         setBackground(isSelected ? list.getSelectionBackground() : list.getBackground());
87                         setForeground(isSelected ? list.getSelectionForeground() : list.getForeground());
88                         return this;
89                 }
90
91         }
92
93 }