001    /* JAPI - (Yet another (hopefully) useful) Java API
002     *
003     * Copyright (C) 2004-2006 Christian Hujer
004     *
005     * This program is free software; you can redistribute it and/or
006     * modify it under the terms of the GNU General Public License as
007     * published by the Free Software Foundation; either version 2 of the
008     * License, or (at your option) any later version.
009     *
010     * This program is distributed in the hope that it will be useful, but
011     * WITHOUT ANY WARRANTY; without even the implied warranty of
012     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013     * General Public License for more details.
014     *
015     * You should have received a copy of the GNU General Public License
016     * along with this program; if not, write to the Free Software
017     * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
018     * 02111-1307, USA.
019     */
020    
021    package net.sf.japi.swing.font;
022    
023    import java.awt.Font;
024    import static java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment;
025    import java.util.HashMap;
026    import java.util.Map;
027    import javax.swing.JComboBox;
028    
029    /** ComboBox to choose the font family.
030     * @author <a href="mailto:chris@riedquat.de">Christian Hujer</a>
031     * @version $Id: FontFamilyComboBox.java,v 1.1 2006/03/26 01:26:27 christianhujer Exp $
032     */
033    public class FontFamilyComboBox extends JComboBox {
034    
035        /** Serial Version. */
036        private static final long serialVersionUID = 1L;
037    
038        /** The fonts to render.
039         * @serial include
040         */
041        private Map<String,Font> fonts;
042    
043        /** Create a FontFamilyComboBox. */
044        public FontFamilyComboBox() {
045            super(getLocalGraphicsEnvironment().getAvailableFontFamilyNames());
046            // initFonts(); the super class construction invokes setFont and thus initFonts();
047        }
048    
049        /** {@inheritDoc} */
050        @Override public void setFont(final Font font) {
051            super.setFont(font);
052            initFonts();
053        }
054    
055        /** Initialize fonts. */
056        private void initFonts() {
057            if (fonts == null) {
058                final FontFamilyListCellRenderer cellRenderer = new FontFamilyListCellRenderer();
059                setRenderer(cellRenderer);
060                fonts = new HashMap<String,Font>();
061                cellRenderer.setFonts(fonts);
062            }
063            Font base = getFont();
064            if (base == null) { base = Font.decode(null); }
065            final int style = base.getStyle();
066            final int size = base.getSize();
067            for (final String family : getLocalGraphicsEnvironment().getAvailableFontFamilyNames()) {
068                fonts.put(family, new Font(family, style, size));
069            }
070        }
071    
072    } // class FontFamilyComboBox