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