~ubuntu-branches/ubuntu/lucid/libjcommon-java/lucid

« back to all changes in this revision

Viewing changes to source/org/jfree/ui/FontChooserPanel.java

  • Committer: Bazaar Package Importer
  • Author(s): Wolfgang Baer
  • Date: 2006-02-09 15:58:13 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20060209155813-fzi9zwh2rzedbllq
Tags: 1.0.0-1
* New stable upstream release (closes: #328574)
* Move to main - build with kaffe
* Use cdbs build system - added cdbs build-dependency
* Move package to pkg-java-maintainers for comaintenance, 
  added Christian Bayle and myself as uploaders
* Removed unneeded README.Debian
* Added README.Debian-source how the upstream tarball was cleaned
* Move big documentation in an own -doc package
* Register javadoc api with doc-base
* Standards-Version 3.6.2 (no changes)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* ========================================================================
 
2
 * JCommon : a free general purpose class library for the Java(tm) platform
 
3
 * ========================================================================
 
4
 *
 
5
 * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
 
6
 * 
 
7
 * Project Info:  http://www.jfree.org/jcommon/index.html
 
8
 *
 
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.
 
13
 *
 
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.
 
18
 *
 
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, 
 
22
 * USA.  
 
23
 *
 
24
 * [Java is a trademark or registered trademark of Sun Microsystems, Inc. 
 
25
 * in the United States and other countries.]
 
26
 * 
 
27
 * ---------------------
 
28
 * FontChooserPanel.java
 
29
 * ---------------------
 
30
 * (C) Copyright 2000-2004, by Object Refinery Limited.
 
31
 *
 
32
 * Original Author:  David Gilbert (for Object Refinery Limited);
 
33
 * Contributor(s):   Arnaud Lelievre;
 
34
 *
 
35
 * $Id: FontChooserPanel.java,v 1.3 2005/10/18 13:18:34 mungady Exp $
 
36
 *
 
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);
 
43
 */
 
44
 
 
45
package org.jfree.ui;
 
46
 
 
47
import java.awt.BorderLayout;
 
48
import java.awt.Font;
 
49
import java.awt.GraphicsEnvironment;
 
50
import java.awt.GridLayout;
 
51
import java.util.ResourceBundle;
 
52
 
 
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;
 
59
 
 
60
/**
 
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.
 
63
 *
 
64
 * @author David Gilbert
 
65
 */
 
66
public class FontChooserPanel extends JPanel {
 
67
 
 
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"};
 
71
 
 
72
    /** The list of fonts. */
 
73
    private JList fontlist;
 
74
 
 
75
    /** The list of sizes. */
 
76
    private JList sizelist;
 
77
 
 
78
    /** The checkbox that indicates whether the font is bold. */
 
79
    private JCheckBox bold;
 
80
 
 
81
    /** The checkbox that indicates whether or not the font is italic. */
 
82
    private JCheckBox italic;
 
83
 
 
84
    /** The resourceBundle for the localization. */
 
85
    protected static ResourceBundle localizationResources = 
 
86
        ResourceBundle.getBundle("org.jfree.ui.LocalizationBundle");
 
87
 
 
88
    /**
 
89
     * Standard constructor - builds a FontChooserPanel initialised with the specified font.
 
90
     *
 
91
     * @param font  the initial font to display.
 
92
     */
 
93
    public FontChooserPanel(final Font font) {
 
94
 
 
95
        final GraphicsEnvironment g = GraphicsEnvironment.getLocalGraphicsEnvironment();
 
96
        final String[] fonts = g.getAvailableFontFamilyNames();
 
97
 
 
98
        setLayout(new BorderLayout());
 
99
        final JPanel right = new JPanel(new BorderLayout());
 
100
 
 
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);
 
109
        add(fontPanel);
 
110
 
 
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);
 
119
 
 
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")));
 
127
 
 
128
        right.add(sizePanel, BorderLayout.CENTER);
 
129
        right.add(attributes, BorderLayout.SOUTH);
 
130
 
 
131
        add(right, BorderLayout.EAST);
 
132
 
 
133
        setSelectedFont(font);
 
134
    }
 
135
 
 
136
    /**
 
137
     * Returns a Font object representing the selection in the panel.
 
138
     *
 
139
     * @return the font.
 
140
     */
 
141
    public Font getSelectedFont() {
 
142
        return new Font(getSelectedName(), getSelectedStyle(), getSelectedSize());
 
143
    }
 
144
 
 
145
    /**
 
146
     * Returns the selected name.
 
147
     *
 
148
     * @return the name.
 
149
     */
 
150
    public String getSelectedName() {
 
151
        return (String) this.fontlist.getSelectedValue();
 
152
    }
 
153
 
 
154
    /**
 
155
     * Returns the selected style.
 
156
     *
 
157
     * @return the style.
 
158
     */
 
159
    public int getSelectedStyle() {
 
160
        if (this.bold.isSelected() && this.italic.isSelected()) {
 
161
            return Font.BOLD + Font.ITALIC;
 
162
        }
 
163
        if (this.bold.isSelected()) {
 
164
            return Font.BOLD;
 
165
        }
 
166
        if (this.italic.isSelected()) {
 
167
            return Font.ITALIC;
 
168
        }
 
169
        else {
 
170
            return Font.PLAIN;
 
171
        }
 
172
    }
 
173
 
 
174
    /**
 
175
     * Returns the selected size.
 
176
     *
 
177
     * @return the size.
 
178
     */
 
179
    public int getSelectedSize() {
 
180
        final String selected = (String) this.sizelist.getSelectedValue();
 
181
        if (selected != null) {
 
182
            return Integer.parseInt(selected);
 
183
        }
 
184
        else {
 
185
            return 10;
 
186
        }
 
187
    }
 
188
 
 
189
    /**
 
190
     * Initializes the contents of the dialog from the given font
 
191
     * object.
 
192
     *
 
193
     * @param font the font from which to read the properties.
 
194
     */
 
195
    public void setSelectedFont (final Font font) {
 
196
        if (font == null) {
 
197
            throw new NullPointerException();
 
198
        }
 
199
        this.bold.setSelected(font.isBold());
 
200
        this.italic.setSelected(font.isItalic());
 
201
 
 
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);
 
208
                break;
 
209
            }
 
210
        }
 
211
 
 
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);
 
218
                break;
 
219
            }
 
220
        }
 
221
    }
 
222
}