1
/* ========================================================================
2
* JCommon : a free general purpose class library for the Java(tm) platform
3
* ========================================================================
5
* (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
7
* Project Info: http://www.jfree.org/jcommon/index.html
9
* This library is free software; you can redistribute it and/or modify it
10
* under the terms of the GNU Lesser General Public License as published by
11
* the Free Software Foundation; either version 2.1 of the License, or
12
* (at your option) any later version.
14
* This library is distributed in the hope that it will be useful, but
15
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
17
* License for more details.
19
* You should have received a copy of the GNU Lesser General Public
20
* License along with this library; if not, write to the Free Software
21
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
24
* [Java is a trademark or registered trademark of Sun Microsystems, Inc.
25
* in the United States and other countries.]
27
* ---------------------
28
* FontChooserPanel.java
29
* ---------------------
30
* (C) Copyright 2000-2004, by Object Refinery Limited.
32
* Original Author: David Gilbert (for Object Refinery Limited);
33
* Contributor(s): Arnaud Lelievre;
35
* $Id: FontChooserPanel.java,v 1.3 2005/10/18 13:18:34 mungady Exp $
37
* Changes (from 26-Oct-2001)
38
* --------------------------
39
* 26-Oct-2001 : Changed package to com.jrefinery.ui.*;
40
* 14-Oct-2002 : Fixed errors reported by Checkstyle (DG);
41
* 08-Sep-2003 : Added internationalization via use of properties resourceBundle (RFE 690236) (AL);
42
* 21-Feb-2004 : The FontParameter of the constructor was never used (TM);
47
import java.awt.BorderLayout;
49
import java.awt.GraphicsEnvironment;
50
import java.awt.GridLayout;
51
import java.util.ResourceBundle;
53
import javax.swing.BorderFactory;
54
import javax.swing.JCheckBox;
55
import javax.swing.JList;
56
import javax.swing.JPanel;
57
import javax.swing.JScrollPane;
58
import javax.swing.ListModel;
61
* A panel for choosing a font from the available system fonts - still a bit of a hack at the
62
* moment, but good enough for demonstration applications.
64
* @author David Gilbert
66
public class FontChooserPanel extends JPanel {
68
/** The font sizes that can be selected. */
69
public static final String[] SIZES = {"9", "10", "11", "12", "14", "16", "18",
70
"20", "22", "24", "28", "36", "48", "72"};
72
/** The list of fonts. */
73
private JList fontlist;
75
/** The list of sizes. */
76
private JList sizelist;
78
/** The checkbox that indicates whether the font is bold. */
79
private JCheckBox bold;
81
/** The checkbox that indicates whether or not the font is italic. */
82
private JCheckBox italic;
84
/** The resourceBundle for the localization. */
85
protected static ResourceBundle localizationResources =
86
ResourceBundle.getBundle("org.jfree.ui.LocalizationBundle");
89
* Standard constructor - builds a FontChooserPanel initialised with the specified font.
91
* @param font the initial font to display.
93
public FontChooserPanel(final Font font) {
95
final GraphicsEnvironment g = GraphicsEnvironment.getLocalGraphicsEnvironment();
96
final String[] fonts = g.getAvailableFontFamilyNames();
98
setLayout(new BorderLayout());
99
final JPanel right = new JPanel(new BorderLayout());
101
final JPanel fontPanel = new JPanel(new BorderLayout());
102
fontPanel.setBorder(BorderFactory.createTitledBorder(
103
BorderFactory.createEtchedBorder(),
104
localizationResources.getString("Font")));
105
this.fontlist = new JList(fonts);
106
final JScrollPane fontpane = new JScrollPane(this.fontlist);
107
fontpane.setBorder(BorderFactory.createEtchedBorder());
108
fontPanel.add(fontpane);
111
final JPanel sizePanel = new JPanel(new BorderLayout());
112
sizePanel.setBorder(BorderFactory.createTitledBorder(
113
BorderFactory.createEtchedBorder(),
114
localizationResources.getString("Size")));
115
this.sizelist = new JList(SIZES);
116
final JScrollPane sizepane = new JScrollPane(this.sizelist);
117
sizepane.setBorder(BorderFactory.createEtchedBorder());
118
sizePanel.add(sizepane);
120
final JPanel attributes = new JPanel(new GridLayout(1, 2));
121
this.bold = new JCheckBox(localizationResources.getString("Bold"));
122
this.italic = new JCheckBox(localizationResources.getString("Italic"));
123
attributes.add(this.bold);
124
attributes.add(this.italic);
125
attributes.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(),
126
localizationResources.getString("Attributes")));
128
right.add(sizePanel, BorderLayout.CENTER);
129
right.add(attributes, BorderLayout.SOUTH);
131
add(right, BorderLayout.EAST);
133
setSelectedFont(font);
137
* Returns a Font object representing the selection in the panel.
141
public Font getSelectedFont() {
142
return new Font(getSelectedName(), getSelectedStyle(), getSelectedSize());
146
* Returns the selected name.
150
public String getSelectedName() {
151
return (String) this.fontlist.getSelectedValue();
155
* Returns the selected style.
159
public int getSelectedStyle() {
160
if (this.bold.isSelected() && this.italic.isSelected()) {
161
return Font.BOLD + Font.ITALIC;
163
if (this.bold.isSelected()) {
166
if (this.italic.isSelected()) {
175
* Returns the selected size.
179
public int getSelectedSize() {
180
final String selected = (String) this.sizelist.getSelectedValue();
181
if (selected != null) {
182
return Integer.parseInt(selected);
190
* Initializes the contents of the dialog from the given font
193
* @param font the font from which to read the properties.
195
public void setSelectedFont (final Font font) {
197
throw new NullPointerException();
199
this.bold.setSelected(font.isBold());
200
this.italic.setSelected(font.isItalic());
202
final String fontName = font.getName();
203
ListModel model = this.fontlist.getModel();
204
this.fontlist.clearSelection();
205
for (int i = 0; i < model.getSize(); i++) {
206
if (fontName.equals(model.getElementAt(i))) {
207
this.fontlist.setSelectedIndex(i);
212
final String fontSize = String.valueOf(font.getSize());
213
model = this.sizelist.getModel();
214
this.sizelist.clearSelection();
215
for (int i = 0; i < model.getSize(); i++) {
216
if (fontSize.equals(model.getElementAt(i))) {
217
this.sizelist.setSelectedIndex(i);