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.BorderLayout;
045 import java.awt.Font;
046 import java.awt.GraphicsEnvironment;
047 import java.awt.GridLayout;
048 import java.awt.event.ActionEvent;
049 import java.awt.event.ActionListener;
050 import java.util.Locale;
051 import java.util.ResourceBundle;
052
053 import javax.swing.BorderFactory;
054 import javax.swing.JCheckBox;
055 import javax.swing.JLabel;
056 import javax.swing.JList;
057 import javax.swing.JPanel;
058 import javax.swing.JScrollPane;
059 import javax.swing.ListModel;
060 import javax.swing.event.ListSelectionEvent;
061 import javax.swing.event.ListSelectionListener;
062
063 /**
064 * A panel for choosing a font from the available system fonts - still a bit of
065 * a hack at the moment, but good enough for demonstration applications.
066 *
067 */
068 public class FontChooserPanel extends JPanel {
069
070 /** The font sizes that can be selected. */
071 public static final String[] SIZES = { "9", "10", "11", "12", "14", "16",
072 "18", "20", "22", "24", "28", "36", "48", "72" };
073
074 /** The list of fonts. */
075 private JList fontlist;
076
077 /** The list of sizes. */
078 private JList sizelist;
079
080 /** The checkbox that indicates whether the font is bold. */
081 private JCheckBox bold;
082
083 /** The checkbox that indicates whether or not the font is italic. */
084 private JCheckBox italic;
085
086 /** The resourceBundle for the localization. */
087 protected static ResourceBundle localizationResources = ResourceBundle
088 .getBundle("JBotherBundle", Locale.getDefault());
089
090 private SelectionListener listener = new SelectionListener();
091
092 private JLabel testLabel = new JLabel(localizationResources
093 .getString("quickFox"));
094
095 /**
096 * Standard constructor - builds a FontChooserPanel initialised with the
097 * specified font.
098 *
099 * @param font
100 * the initial font to display.
101 */
102 public FontChooserPanel(final Font font) {
103
104 final GraphicsEnvironment g = GraphicsEnvironment
105 .getLocalGraphicsEnvironment();
106 final String[] fonts = g.getAvailableFontFamilyNames();
107
108 setLayout(new BorderLayout());
109 final JPanel right = new JPanel(new BorderLayout());
110
111 final JPanel fontPanel = new JPanel(new BorderLayout());
112 fontPanel
113 .setBorder(BorderFactory.createTitledBorder(BorderFactory
114 .createEtchedBorder(), localizationResources
115 .getString("font")));
116 this.fontlist = new JList(fonts);
117 final JScrollPane fontpane = new JScrollPane(this.fontlist);
118 fontpane.setBorder(BorderFactory.createEtchedBorder());
119 fontPanel.add(fontpane);
120 add(fontPanel, BorderLayout.CENTER);
121
122 final JPanel testPanel = new JPanel(new BorderLayout());
123 testPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory
124 .createEtchedBorder(), localizationResources
125 .getString("sample")));
126 testPanel.add(testLabel);
127 add(testPanel, BorderLayout.SOUTH);
128
129 final JPanel sizePanel = new JPanel(new BorderLayout());
130 sizePanel
131 .setBorder(BorderFactory.createTitledBorder(BorderFactory
132 .createEtchedBorder(), localizationResources
133 .getString("size")));
134 this.sizelist = new JList(SIZES);
135 final JScrollPane sizepane = new JScrollPane(this.sizelist);
136 sizepane.setBorder(BorderFactory.createEtchedBorder());
137 sizePanel.add(sizepane);
138
139 final JPanel attributes = new JPanel(new GridLayout(1, 2));
140 this.bold = new JCheckBox(localizationResources.getString("bold"));
141 this.italic = new JCheckBox(localizationResources.getString("italic"));
142 attributes.add(this.bold);
143 attributes.add(this.italic);
144 attributes.setBorder(BorderFactory.createTitledBorder(BorderFactory
145 .createEtchedBorder(), localizationResources
146 .getString("attributes")));
147
148 bold.addActionListener(listener);
149 italic.addActionListener(listener);
150 fontlist.addListSelectionListener(listener);
151 sizelist.addListSelectionListener(listener);
152
153 right.add(sizePanel, BorderLayout.CENTER);
154 right.add(attributes, BorderLayout.SOUTH);
155
156 add(right, BorderLayout.EAST);
157
158 setSelectedFont(font);
159 }
160
161 private class SelectionListener implements ActionListener,
162 ListSelectionListener {
163 public void actionPerformed(ActionEvent e) {
164 testLabel.setFont(getSelectedFont());
165 }
166
167 public void valueChanged(ListSelectionEvent e) {
168 testLabel.setFont(getSelectedFont());
169 }
170 }
171
172 /**
173 * Returns a Font object representing the selection in the panel.
174 *
175 * @return the font.
176 */
177 public Font getSelectedFont() {
178 return new Font(getSelectedName(), getSelectedStyle(),
179 getSelectedSize());
180 }
181
182 /**
183 * Returns the selected name.
184 *
185 * @return the name.
186 */
187 public String getSelectedName() {
188 return (String) this.fontlist.getSelectedValue();
189 }
190
191 /**
192 * Returns the selected style.
193 *
194 * @return the style.
195 */
196 public int getSelectedStyle() {
197 if (this.bold.isSelected() && this.italic.isSelected()) {
198 return Font.BOLD + Font.ITALIC;
199 }
200 if (this.bold.isSelected()) {
201 return Font.BOLD;
202 }
203 if (this.italic.isSelected()) {
204 return Font.ITALIC;
205 } else {
206 return Font.PLAIN;
207 }
208 }
209
210 /**
211 * Returns the selected size.
212 *
213 * @return the size.
214 */
215 public int getSelectedSize() {
216 final String selected = (String) this.sizelist.getSelectedValue();
217 if (selected != null) {
218 return Integer.parseInt(selected);
219 } else {
220 return 10;
221 }
222 }
223
224 /**
225 * Initializes the contents of the dialog from the given font object.
226 *
227 * @param font
228 * the font from which to read the properties.
229 */
230 public void setSelectedFont(final Font font) {
231 if (font == null) {
232 throw new NullPointerException();
233 }
234 this.bold.setSelected(font.isBold());
235 this.italic.setSelected(font.isItalic());
236
237 final String fontName = font.getName();
238 ListModel model = this.fontlist.getModel();
239 this.fontlist.clearSelection();
240 for (int i = 0; i < model.getSize(); i++) {
241 if (fontName.equals(model.getElementAt(i))) {
242 this.fontlist.setSelectedIndex(i);
243 break;
244 }
245 }
246
247 final String fontSize = String.valueOf(font.getSize());
248 model = this.sizelist.getModel();
249 this.sizelist.clearSelection();
250 for (int i = 0; i < model.getSize(); i++) {
251 if (fontSize.equals(model.getElementAt(i))) {
252 this.sizelist.setSelectedIndex(i);
253 break;
254 }
255 }
256
257 testLabel.setFont(getSelectedFont());
258 }
259 }