001    /* ========================================================================
002     * JCommon : a free general purpose class library for the Java(tm) platform
003     * ========================================================================
004     *
005     * (C) Copyright 2000-2004, by Object Refinery Limited and Contributors.
006     *
007     * Project Info:  http://www.jfree.org/jcommon/index.html
008     *
009     * This library is free software; you can redistribute it and/or modify it under the terms
010     * of the GNU Lesser General Public License as published by the Free Software Foundation;
011     * either version 2.1 of the License, or (at your option) any later version.
012     *
013     * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
014     * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
015     * See the GNU Lesser General Public License for more details.
016     *
017     * You should have received a copy of the GNU Lesser General Public License along with this
018     * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
019     * Boston, MA 02111-1307, USA.
020     *
021     * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
022     * in the United States and other countries.]
023     *
024     * ---------------------
025     * FontChooserPanel.java
026     * ---------------------
027     * (C) Copyright 2000-2004, by Object Refinery Limited.
028     *
029     * Original Author:  David Gilbert (for Object Refinery Limited);
030     * Contributor(s):   Arnaud Lelievre;
031     *
032     * $Id: FontChooserPanel.java,v 1.12 2004/04/26 19:15:44 taqua Exp $
033     *
034     * Changes (from 26-Oct-2001)
035     * --------------------------
036     * 26-Oct-2001 : Changed package to com.jrefinery.ui.*;
037     * 14-Oct-2002 : Fixed errors reported by Checkstyle (DG);
038     * 08-Sep-2003 : Added internationalization via use of properties resourceBundle (RFE 690236) (AL);
039     * 21-Feb-2004 : The FontParameter of the constructor was never used (TM);
040     */
041    
042    package org.jfree.ui;
043    
044    import java.awt.*;
045    import java.awt.event.*;
046    import java.util.*;
047    
048    import javax.swing.*;
049    import javax.swing.event.*;
050    
051    import com.valhalla.jbother.*;
052    
053    /**
054     * A panel for choosing a font from the available system fonts - still a bit of a hack at the
055     * moment, but good enough for demonstration applications.
056     *
057     */
058    public class FontChooserPanel extends JPanel {
059    
060        /** The font sizes that can be selected. */
061        public static final String[] SIZES = {"9", "10", "11", "12", "14", "16", "18",
062                                              "20", "22", "24", "28", "36", "48", "72"};
063    
064        /** The list of fonts. */
065        private JList fontlist;
066    
067        /** The list of sizes. */
068        private JList sizelist;
069    
070        /** The checkbox that indicates whether the font is bold. */
071        private JCheckBox bold;
072    
073        /** The checkbox that indicates whether or not the font is italic. */
074        private JCheckBox italic;
075    
076        /** The resourceBundle for the localization. */
077        protected static ResourceBundle localizationResources =
078            ResourceBundle.getBundle( "JBotherBundle", Locale.getDefault() );
079    
080            private SelectionListener listener = new SelectionListener();
081    
082            private JLabel testLabel = new JLabel( localizationResources.getString( "quickFox" ) );
083    
084        /**
085         * Standard constructor - builds a FontChooserPanel initialised with the specified font.
086         *
087         * @param font  the initial font to display.
088         */
089        public FontChooserPanel(final Font font) {
090    
091            final GraphicsEnvironment g = GraphicsEnvironment.getLocalGraphicsEnvironment();
092            final String[] fonts = g.getAvailableFontFamilyNames();
093    
094            setLayout(new BorderLayout());
095            final JPanel right = new JPanel(new BorderLayout());
096    
097            final JPanel fontPanel = new JPanel(new BorderLayout());
098            fontPanel.setBorder(BorderFactory.createTitledBorder(
099                                BorderFactory.createEtchedBorder(),
100                                localizationResources.getString("font")));
101            this.fontlist = new JList(fonts);
102            final JScrollPane fontpane = new JScrollPane(this.fontlist);
103            fontpane.setBorder(BorderFactory.createEtchedBorder());
104            fontPanel.add(fontpane);
105            add(fontPanel, BorderLayout.CENTER );
106    
107                    final JPanel testPanel = new JPanel( new BorderLayout() );
108                    testPanel.setBorder(BorderFactory.createTitledBorder(
109                                BorderFactory.createEtchedBorder(),
110                                localizationResources.getString("sample")));
111                    testPanel.add( testLabel );
112                    add(testPanel, BorderLayout.SOUTH );
113    
114            final JPanel sizePanel = new JPanel(new BorderLayout());
115            sizePanel.setBorder(BorderFactory.createTitledBorder(
116                                BorderFactory.createEtchedBorder(),
117                                localizationResources.getString("size")));
118            this.sizelist = new JList(SIZES);
119            final JScrollPane sizepane = new JScrollPane(this.sizelist);
120            sizepane.setBorder(BorderFactory.createEtchedBorder());
121            sizePanel.add(sizepane);
122    
123            final JPanel attributes = new JPanel(new GridLayout(1, 2));
124            this.bold = new JCheckBox(localizationResources.getString("bold"));
125            this.italic = new JCheckBox(localizationResources.getString("italic"));
126            attributes.add(this.bold);
127            attributes.add(this.italic);
128            attributes.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(),
129                                 localizationResources.getString("attributes")));
130    
131                    bold.addActionListener( listener );
132                    italic.addActionListener( listener );
133                    fontlist.addListSelectionListener( listener );
134                    sizelist.addListSelectionListener( listener );
135    
136            right.add(sizePanel, BorderLayout.CENTER);
137            right.add(attributes, BorderLayout.SOUTH);
138    
139            add(right, BorderLayout.EAST);
140    
141            setSelectedFont(font);
142        }
143    
144            private class SelectionListener implements ActionListener, ListSelectionListener
145            {
146                    public void actionPerformed( ActionEvent e )
147                    {
148                            testLabel.setFont( getSelectedFont() );
149                    }
150    
151                    public void valueChanged( ListSelectionEvent e )
152                    {
153                            testLabel.setFont( getSelectedFont() );
154                    }
155            }
156    
157        /**
158         * Returns a Font object representing the selection in the panel.
159         *
160         * @return the font.
161         */
162        public Font getSelectedFont() {
163            return new Font(getSelectedName(), getSelectedStyle(), getSelectedSize());
164        }
165    
166        /**
167         * Returns the selected name.
168         *
169         * @return the name.
170         */
171        public String getSelectedName() {
172            return (String) this.fontlist.getSelectedValue();
173        }
174    
175        /**
176         * Returns the selected style.
177         *
178         * @return the style.
179         */
180        public int getSelectedStyle() {
181            if (this.bold.isSelected() && this.italic.isSelected()) {
182                return Font.BOLD + Font.ITALIC;
183            }
184            if (this.bold.isSelected()) {
185                return Font.BOLD;
186            }
187            if (this.italic.isSelected()) {
188                return Font.ITALIC;
189            }
190            else {
191                return Font.PLAIN;
192            }
193        }
194    
195        /**
196         * Returns the selected size.
197         *
198         * @return the size.
199         */
200        public int getSelectedSize() {
201            final String selected = (String) this.sizelist.getSelectedValue();
202            if (selected != null) {
203                return Integer.parseInt(selected);
204            }
205            else {
206                return 10;
207            }
208        }
209    
210        /**
211         * Initializes the contents of the dialog from the given font
212         * object.
213         *
214         * @param font the font from which to read the properties.
215         */
216        public void setSelectedFont (final Font font) {
217            if (font == null) {
218                throw new NullPointerException();
219            }
220            this.bold.setSelected(font.isBold());
221            this.italic.setSelected(font.isItalic());
222    
223            final String fontName = font.getName();
224            ListModel model = this.fontlist.getModel();
225            this.fontlist.clearSelection();
226            for (int i = 0; i < model.getSize(); i++) {
227                if (fontName.equals(model.getElementAt(i))) {
228                    this.fontlist.setSelectedIndex(i);
229                    break;
230                }
231            }
232    
233            final String fontSize = String.valueOf(font.getSize());
234            model = this.sizelist.getModel();
235            this.sizelist.clearSelection();
236            for (int i = 0; i < model.getSize(); i++) {
237                if (fontSize.equals(model.getElementAt(i))) {
238                    this.sizelist.setSelectedIndex(i);
239                    break;
240                }
241            }
242    
243                    testLabel.setFont( getSelectedFont() );
244        }
245    }