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