Coverage Report - us.daveread.basicquery.gui.FontChooser
 
Classes in this File Line Coverage Branch Coverage Complexity
FontChooser
95%
88/93
88%
7/8
0
FontChooser$1
100%
3/3
N/A
0
FontChooser$2
100%
3/3
N/A
0
FontChooser$3
100%
3/3
N/A
0
 
 1  
 package us.daveread.basicquery.gui;
 2  
 
 3  
 import java.awt.BorderLayout;
 4  
 import java.awt.Container;
 5  
 import java.awt.Dimension;
 6  
 import java.awt.Font;
 7  
 import java.awt.Frame;
 8  
 import java.awt.GraphicsEnvironment;
 9  
 import java.awt.event.ActionEvent;
 10  
 import java.awt.event.ActionListener;
 11  
 import java.awt.event.KeyEvent;
 12  
 import java.awt.event.KeyListener;
 13  
 import java.awt.event.WindowAdapter;
 14  
 import java.awt.event.WindowEvent;
 15  
 import java.util.ArrayList;
 16  
 import java.util.Collections;
 17  
 import java.util.HashMap;
 18  
 import java.util.Iterator;
 19  
 import java.util.List;
 20  
 import java.util.Map;
 21  
 
 22  
 import javax.swing.JButton;
 23  
 import javax.swing.JCheckBox;
 24  
 import javax.swing.JComboBox;
 25  
 import javax.swing.JDialog;
 26  
 import javax.swing.JLabel;
 27  
 import javax.swing.JPanel;
 28  
 import javax.swing.JTextField;
 29  
 import javax.swing.SwingConstants;
 30  
 import javax.swing.text.SimpleAttributeSet;
 31  
 import javax.swing.text.StyleConstants;
 32  
 
 33  
 import org.apache.log4j.Logger;
 34  
 
 35  
 import us.daveread.basicquery.util.Resources;
 36  
 
 37  
 /**
 38  
  * <p>
 39  
  * Title: FontChooser
 40  
  * </p>
 41  
  * 
 42  
  * <p>
 43  
  * Description: A font chooser that allows users to pick a font by name, size,
 44  
  * style, and color. The color selection is provided by a JColorChooser pane.
 45  
  * This dialog builds an AttributeSet suitable for use with JTextPane.
 46  
  * </p>
 47  
  * 
 48  
  * <p>
 49  
  * Copyright: Copyright (c) 2006
 50  
  * </p>
 51  
  * 
 52  
  * <p>
 53  
  * Company:
 54  
  * </p>
 55  
  * 
 56  
  * @author David Read
 57  
  */
 58  3
 public class FontChooser extends JDialog implements ActionListener, KeyListener {
 59  
   /**
 60  
    * Serial UID
 61  
    */
 62  
   private static final long serialVersionUID = -6831041847770546773L;
 63  
 
 64  
   /**
 65  
    * Logger
 66  
    */
 67  1
   private static final Logger LOGGER = Logger.getLogger(FontChooser.class);
 68  
 
 69  
   // private JColorChooser colorChooser;
 70  
   /**
 71  
    * The font name choice
 72  
    */
 73  
   private JComboBox fontName;
 74  
 
 75  
   /**
 76  
    * The font bold selection
 77  
    */
 78  
   private JCheckBox fontBold;
 79  
 
 80  
   /**
 81  
    * The font italic selection
 82  
    */
 83  
   private JCheckBox fontItalic;
 84  
 
 85  
   /**
 86  
    * The font size setting
 87  
    */
 88  
   private JTextField fontSize;
 89  
 
 90  
   /**
 91  
    * The preview area
 92  
    */
 93  
   private JLabel previewLabel;
 94  
 
 95  
   /**
 96  
    * The font attributes
 97  
    */
 98  
   private SimpleAttributeSet attributes;
 99  
 
 100  
   /**
 101  
    * The chosen font
 102  
    */
 103  
   private Font newFont;
 104  
 
 105  
   // private Color newColor;
 106  
 
 107  
   /**
 108  
    * Setup a font chooser dialog with the default font information
 109  
    * 
 110  
    * @param parent
 111  
    *          The parent frame for this dialog
 112  
    */
 113  
   public FontChooser(Frame parent) {
 114  7
     super(parent, Resources.getString("dlgFontChooserTitle"), true);
 115  
     Font currentFont;
 116  
 
 117  7
     currentFont = parent.getFont();
 118  
 
 119  
     // setSize(450, 450);
 120  7
     attributes = new SimpleAttributeSet();
 121  7
     StyleConstants.setBold(attributes, currentFont.isBold());
 122  7
     StyleConstants.setItalic(attributes, currentFont.isItalic());
 123  7
     StyleConstants.setFontFamily(attributes, currentFont.getFamily());
 124  7
     StyleConstants.setFontSize(attributes, currentFont.getSize());
 125  
 
 126  
     // Make sure that any way the user cancels the window does the right thing
 127  7
     addWindowListener(new WindowAdapter() {
 128  
       public void windowClosing(WindowEvent e) {
 129  1
         closeAndCancel();
 130  1
       }
 131  
     });
 132  
 
 133  
     // Start the long process of setting up our interface
 134  7
     final Container container = getContentPane();
 135  
 
 136  
     String[] fontFamilies;
 137  7
     fontFamilies = getFontFamilies();
 138  7
     final JPanel fontPanel = new JPanel();
 139  
 
 140  
     // fontName = new JComboBox(new String[] {"TimesRoman",
 141  
     // "Helvetica", "Courier"});
 142  7
     fontName = new JComboBox(fontFamilies);
 143  7
     fontName.setSelectedItem(currentFont.getFamily());
 144  7
     fontName.addActionListener(this);
 145  
 
 146  7
     fontSize = new JTextField(currentFont.getSize() + "", 4);
 147  7
     fontSize.setHorizontalAlignment(SwingConstants.RIGHT);
 148  
     // fontSize.addActionListener(this);
 149  7
     fontSize.addKeyListener(this);
 150  
 
 151  7
     fontBold = new JCheckBox(Resources.getString("dlgFontChooserBold"));
 152  7
     fontBold.setSelected(StyleConstants.isBold(attributes));
 153  7
     fontBold.addActionListener(this);
 154  
 
 155  7
     fontItalic = new JCheckBox(Resources.getString("dlgFontChooserItalic"));
 156  7
     fontItalic.setSelected(StyleConstants.isItalic(attributes));
 157  7
     fontItalic.addActionListener(this);
 158  
 
 159  7
     fontPanel.add(fontName);
 160  7
     fontPanel.add(new JLabel(Resources.getString("dlgFontChooserFontSize")));
 161  7
     fontPanel.add(fontSize);
 162  7
     fontPanel.add(fontBold);
 163  7
     fontPanel.add(fontItalic);
 164  
 
 165  7
     container.add(fontPanel, BorderLayout.NORTH);
 166  
 
 167  
     // Set up the color chooser panel and attach a change listener so that color
 168  
     // updates get reflected in our preview label.
 169  
     // colorChooser = new JColorChooser(Color.black);
 170  
     // colorChooser.getSelectionModel()
 171  
     // .addChangeListener(new ChangeListener() {
 172  
     // public void stateChanged(ChangeEvent e) {
 173  
     // updatePreviewColor();
 174  
     // }
 175  
     // });
 176  
     // c.add(colorChooser, BorderLayout.CENTER);
 177  
 
 178  7
     final JPanel previewPanel = new JPanel(new BorderLayout());
 179  7
     previewLabel = new JLabel(Resources.getString("dlgFontChooserSampleText"));
 180  
     // previewLabel.setForeground(colorChooser.getColor());
 181  7
     previewPanel.add(previewLabel, BorderLayout.CENTER);
 182  
 
 183  
     // Add in the Ok and Cancel buttons for our dialog box
 184  7
     final JButton okButton = new JButton(
 185  
         Resources.getString("dlgFontChooserOkay"));
 186  7
     okButton.addActionListener(new ActionListener() {
 187  
       public void actionPerformed(ActionEvent ae) {
 188  1
         closeAndSave();
 189  1
       }
 190  
     });
 191  7
     final JButton cancelButton = new JButton(Resources.getString(
 192  
         "dlgFontChooserCancel"));
 193  7
     cancelButton.addActionListener(new ActionListener() {
 194  
       public void actionPerformed(ActionEvent ae) {
 195  1
         closeAndCancel();
 196  1
       }
 197  
     });
 198  
 
 199  7
     final JPanel controlPanel = new JPanel();
 200  7
     controlPanel.add(okButton);
 201  7
     controlPanel.add(cancelButton);
 202  7
     previewPanel.add(controlPanel, BorderLayout.SOUTH);
 203  
 
 204  
     // Give the preview label room to grow.
 205  7
     previewPanel.setMinimumSize(new Dimension(100, 100));
 206  7
     previewPanel.setPreferredSize(new Dimension(100, 100));
 207  
 
 208  7
     container.add(previewPanel, BorderLayout.SOUTH);
 209  
 
 210  7
     pack();
 211  7
     updatePreviewFont();
 212  7
     GUIUtility.center(this, parent);
 213  7
   }
 214  
 
 215  
   /**
 216  
    * Get the names of available font families
 217  
    * 
 218  
    * @return Font family names installed locally
 219  
    */
 220  
   private String[] getFontFamilies() {
 221  
     String[] fontFamilies;
 222  
     Font[] fonts;
 223  
     Map<String, String> mapFamilies;
 224  
     List<String> lstFamilies;
 225  
     Iterator<String> itrFamilies;
 226  
 
 227  7
     fonts = GraphicsEnvironment.getLocalGraphicsEnvironment().getAllFonts();
 228  
 
 229  7
     mapFamilies = new HashMap<String, String>();
 230  
 
 231  
     // Get the unique list of font families
 232  9296
     for (int index = 0; index < fonts.length; ++index) {
 233  9289
       mapFamilies.put(fonts[index].getFamily(), null);
 234  
     }
 235  
 
 236  7
     lstFamilies = new ArrayList<String>();
 237  7
     itrFamilies = mapFamilies.keySet().iterator();
 238  
 
 239  4277
     while (itrFamilies.hasNext()) {
 240  4270
       lstFamilies.add(itrFamilies.next());
 241  
     }
 242  
 
 243  7
     Collections.sort(lstFamilies);
 244  7
     fontFamilies = lstFamilies.toArray(new String[lstFamilies.size()]);
 245  
 
 246  7
     return fontFamilies;
 247  
   }
 248  
 
 249  
   @Override
 250  
   public void actionPerformed(ActionEvent ae) {
 251  
     // Check the name of the font
 252  6
     if (!StyleConstants.getFontFamily(attributes)
 253  
         .equals(fontName.getSelectedItem())) {
 254  2
       StyleConstants.setFontFamily(attributes,
 255  
           (String) fontName.getSelectedItem());
 256  
     }
 257  
     // Check the font size (no error checking yet)
 258  
     // if (StyleConstants.getFontSize(attributes) !=
 259  
     // Integer.parseInt(fontSize.getText())) {
 260  
     // StyleConstants.setFontSize(attributes,
 261  
     // Integer.parseInt(fontSize.getText()));
 262  
     // }
 263  
     // Check to see if the font should be bold
 264  6
     if (StyleConstants.isBold(attributes) != fontBold.isSelected()) {
 265  1
       StyleConstants.setBold(attributes, fontBold.isSelected());
 266  
     }
 267  
     // Check to see if the font should be italic
 268  6
     if (StyleConstants.isItalic(attributes) != fontItalic.isSelected()) {
 269  1
       StyleConstants.setItalic(attributes, fontItalic.isSelected());
 270  
     }
 271  
     // and update our preview label
 272  6
     updatePreviewFont();
 273  6
   }
 274  
 
 275  
   /**
 276  
    * Get the appropriate font from our attributes object and update the preview
 277  
    * label
 278  
    */
 279  
   protected void updatePreviewFont() {
 280  14
     final String name = StyleConstants.getFontFamily(attributes);
 281  14
     final boolean bold = StyleConstants.isBold(attributes);
 282  14
     final boolean ital = StyleConstants.isItalic(attributes);
 283  14
     final int size = StyleConstants.getFontSize(attributes);
 284  
 
 285  
     // Bold and italic don’t work properly in beta 4.
 286  14
     newFont = new Font(name, (bold ? Font.BOLD : 0)
 287  
         + (ital ? Font.ITALIC : 0), size);
 288  14
     previewLabel.setFont(newFont);
 289  14
   }
 290  
 
 291  
   /**
 292  
    * Get the chosen font
 293  
    * 
 294  
    * @return The chosen font
 295  
    */
 296  
   public Font getNewFont() {
 297  14
     return newFont;
 298  
   }
 299  
 
 300  
   /**
 301  
    * Close the dialog, saving the choices made by the user
 302  
    */
 303  
   private void closeAndSave() {
 304  1
     setVisible(false);
 305  1
     dispose();
 306  1
   }
 307  
 
 308  
   /**
 309  
    * Close the dalog, discarding the choices made by the user
 310  
    */
 311  
   private void closeAndCancel() {
 312  2
     newFont = null;
 313  
 
 314  2
     setVisible(false);
 315  2
     dispose();
 316  2
   }
 317  
 
 318  
   @Override
 319  
   public void keyPressed(KeyEvent e) {
 320  0
   }
 321  
 
 322  
   @Override
 323  
   public void keyReleased(KeyEvent e) {
 324  
     try {
 325  1
       if (StyleConstants.getFontSize(attributes) != Integer.parseInt(fontSize
 326  
           .getText())) {
 327  1
         StyleConstants.setFontSize(attributes,
 328  
             Integer.parseInt(fontSize.getText()));
 329  1
         updatePreviewFont();
 330  
       }
 331  0
     } catch (Throwable any) {
 332  
       // Not a legal font size value - maybe empty
 333  
       // Don't bother user with this since they may be in the middle of typing
 334  
       // a font size
 335  0
       if (LOGGER.isDebugEnabled()) {
 336  0
         LOGGER
 337  
             .debug(
 338  
                 "Error parsing the font size - may be in the process of being edited",
 339  
                 any);
 340  
       }
 341  1
     }
 342  1
   }
 343  
 
 344  
   @Override
 345  
   public void keyTyped(KeyEvent e) {
 346  0
   }
 347  
 }