add font combo box
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Thu, 24 Apr 2008 23:58:22 +0000 (23:58 +0000)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Thu, 24 Apr 2008 23:58:22 +0000 (23:58 +0000)
git-svn-id: http://trooper/svn/projects/jSite/trunk@761 c3eda9e8-030b-0410-8277-bc7414b0a119

src/net/pterodactylus/jsite/gui/FontComboBox.java [new file with mode: 0644]

diff --git a/src/net/pterodactylus/jsite/gui/FontComboBox.java b/src/net/pterodactylus/jsite/gui/FontComboBox.java
new file mode 100644 (file)
index 0000000..8c37cb4
--- /dev/null
@@ -0,0 +1,86 @@
+/*
+ * jSite2 - FontList.java -
+ * Copyright © 2008 David Roden
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+
+package net.pterodactylus.jsite.gui;
+
+import java.awt.Component;
+import java.awt.Font;
+import java.awt.GraphicsEnvironment;
+
+import javax.swing.DefaultComboBoxModel;
+import javax.swing.JComboBox;
+import javax.swing.JLabel;
+import javax.swing.JList;
+import javax.swing.ListCellRenderer;
+
+/**
+ * Combobox that displays a list of all available fonts, showing the name of the
+ * font in the font itself.
+ *
+ * @see GraphicsEnvironment#getAllFonts()
+ * @author David ‘Bombe’ Roden &lt;bombe@freenetproject.org&gt;
+ * @version $Id$
+ */
+public class FontComboBox extends JComboBox {
+
+       /**
+        * Creates a new font combo box.
+        */
+       public FontComboBox() {
+               super();
+               Font[] allFonts = GraphicsEnvironment.getLocalGraphicsEnvironment().getAllFonts();
+               String[] fontNames = new String[allFonts.length];
+               int fontIndex = 0;
+               for (Font font: allFonts) {
+                       fontNames[fontIndex++] = font.getName();
+               }
+               setModel(new DefaultComboBoxModel(fontNames));
+               setRenderer(new FontCellRenderer());
+       }
+
+       /**
+        * The cell renderer for the font name cells.
+        *
+        * @author David ‘Bombe’ Roden &lt;bombe@freenetproject.org&gt;
+        * @version $Id$
+        */
+       private static class FontCellRenderer extends JLabel implements ListCellRenderer {
+
+               /**
+                * Creates a new list cell renderer.
+                */
+               public FontCellRenderer() {
+                       setOpaque(true);
+               }
+
+               /**
+                * {@inheritDoc}
+                */
+               public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
+                       Font font = Font.decode((String) value);
+                       setFont(font);
+                       setText((String) value);
+                       setBackground(isSelected ? list.getSelectionBackground() : list.getBackground());
+                       setForeground(isSelected ? list.getSelectionForeground() : list.getForeground());
+                       return this;
+               }
+
+       }
+
+}