1 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 66 public class FontChooserPanel extends JPanel { 67 68 69 public static final String [] SIZES = {"9", "10", "11", "12", "14", "16", "18", 70 "20", "22", "24", "28", "36", "48", "72"}; 71 72 73 private JList fontlist; 74 75 76 private JList sizelist; 77 78 79 private JCheckBox bold; 80 81 82 private JCheckBox italic; 83 84 85 protected static ResourceBundle localizationResources = 86 ResourceBundle.getBundle("org.jfree.ui.LocalizationBundle"); 87 88 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 141 public Font getSelectedFont() { 142 return new Font (getSelectedName(), getSelectedStyle(), getSelectedSize()); 143 } 144 145 150 public String getSelectedName() { 151 return (String ) this.fontlist.getSelectedValue(); 152 } 153 154 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 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 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 } 223 | Popular Tags |