Clover coverage report - DrJava Test Coverage (drjava-20120422-r5456)
Coverage timestamp: Sun Apr 22 2012 03:13:25 CDT
file stats: LOC: 307   Methods: 14
NCLOC: 196   Classes: 2
 
 Source file Conditionals Statements Methods TOTAL
FontChooser.java 0% 0% 0% 0%
coverage
 1    /*BEGIN_COPYRIGHT_BLOCK
 2    *
 3    * Copyright (c) 2001-2010, JavaPLT group at Rice University (drjava@rice.edu)
 4    * All rights reserved.
 5    *
 6    * Redistribution and use in source and binary forms, with or without
 7    * modification, are permitted provided that the following conditions are met:
 8    * * Redistributions of source code must retain the above copyright
 9    * notice, this list of conditions and the following disclaimer.
 10    * * Redistributions in binary form must reproduce the above copyright
 11    * notice, this list of conditions and the following disclaimer in the
 12    * documentation and/or other materials provided with the distribution.
 13    * * Neither the names of DrJava, the JavaPLT group, Rice University, nor the
 14    * names of its contributors may be used to endorse or promote products
 15    * derived from this software without specific prior written permission.
 16    *
 17    * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 18    * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 19    * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 20    * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 21    * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 22    * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 23    * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 24    * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 25    * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 26    * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 27    * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 28    *
 29    * This software is Open Source Initiative approved Open Source Software.
 30    * Open Source Initative Approved is a trademark of the Open Source Initiative.
 31    *
 32    * This file is part of DrJava. Download the current version of this project
 33    * from http://www.drjava.org/ or http://sourceforge.net/projects/drjava/
 34    *
 35    * END_COPYRIGHT_BLOCK*/
 36   
 37    package edu.rice.cs.util.swing;
 38   
 39    import edu.rice.cs.util.swing.Utilities;
 40    import javax.swing.*;
 41    import javax.swing.border.EmptyBorder;
 42    import javax.swing.event.ListSelectionEvent;
 43    import javax.swing.event.ListSelectionListener;
 44    import java.awt.*;
 45    import java.awt.event.*;
 46   
 47    /**
 48    * FontChooser, adapted from NwFontChooserS by Noah Wairauch.
 49    * (see http:///forum.java.sun.com/thread.jsp?forum=57&thread=195067)
 50    *
 51    * @version $Id: FontChooser.java 5433 2011-06-13 02:22:35Z mgricken $
 52    */
 53   
 54    public class FontChooser extends JDialog {
 55    /** Available font styles.
 56    */
 57    private static final String[] STYLES =
 58    new String[] { "Plain", "Bold", "Italic", "Bold Italic" };
 59   
 60    /** Available font sizes.
 61    */
 62    private static final String[] SIZES =
 63    new String[] { "3", "4", "5", "6", "7", "8", "9", "10", "11", "12",
 64    "13", "14", "15", "16", "17", "18", "19", "20", "22",
 65    "24", "27", "30", "34", "39", "45", "51", "60"};
 66   
 67    // Lists to display
 68    private NwList _styleList;
 69    private NwList _fontList;
 70    private NwList _sizeList;
 71   
 72    // Swing elements
 73    private JButton _okButton;
 74    private JButton _cancelButton;
 75    private JLabel _sampleText = new JLabel();
 76   
 77    private boolean _clickedOK = false;
 78   
 79    /** Constructs a new modal FontChooser for the given frame,
 80    * using the specified font.
 81    */
 82  0 private FontChooser(Frame parent, Font font) {
 83  0 super(parent, true);
 84  0 initAll();
 85  0 if (font == null) font = _sampleText.getFont();
 86  0 _fontList.setSelectedItem(font.getName());
 87  0 _sizeList.setSelectedItem(font.getSize() + "");
 88  0 _styleList.setSelectedItem(STYLES[font.getStyle()]);
 89    //this.setResizable(false);
 90    //resize();
 91    }
 92   
 93    /** Method used to show the font chooser, and select a new font.
 94    *
 95    * @param parent The parent frame.
 96    * @param title The title for this window.
 97    * @param font The previously chosen font.
 98    * @return the newly chosen font.
 99    */
 100  0 public static Font showDialog(Frame parent, String title, Font font) {
 101  0 FontChooser fd = new FontChooser(parent, font);
 102  0 fd.setTitle(title);
 103   
 104  0 Utilities.setPopupLoc(fd, parent);
 105  0 fd.setVisible(true);
 106   
 107  0 Font chosenFont = null;
 108  0 if (fd.clickedOK()) {
 109  0 chosenFont = fd.getFont();
 110    }
 111  0 fd.dispose();
 112  0 return (chosenFont);
 113    }
 114   
 115    /** Shows the font chooser with a standard title ("Font Chooser").
 116    */
 117  0 public static Font showDialog(Frame parent, Font font) {
 118  0 return showDialog(parent, "Font Chooser", font);
 119    }
 120   
 121  0 private void initAll() {
 122  0 Container cp = getContentPane();
 123  0 GridBagLayout cpLayout = new GridBagLayout();
 124  0 GridBagConstraints c = new GridBagConstraints();
 125  0 cp.setLayout(cpLayout);
 126   
 127    // lists
 128  0 c.fill = GridBagConstraints.BOTH;
 129  0 c.anchor = GridBagConstraints.NORTH;
 130  0 c.gridwidth = 1;
 131  0 c.gridheight = 1;
 132  0 c.gridx = 0;
 133  0 c.gridy = 0;
 134  0 c.weightx = 1.0;
 135  0 c.weighty = 1.0;
 136  0 _fontList = new NwList(GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames());
 137  0 cpLayout.setConstraints(_fontList, c);
 138  0 cp.add(_fontList);
 139    // JPanel fontListPanel = new JPanel();
 140    // fontListPanel.setBackground(Color.RED);
 141    // cpLayout.setConstraints(fontListPanel, c);
 142    // cp.add(fontListPanel);
 143   
 144  0 c.fill = GridBagConstraints.VERTICAL;
 145  0 c.anchor = GridBagConstraints.NORTH;
 146  0 c.gridwidth = GridBagConstraints.RELATIVE;
 147  0 c.gridheight = 1;
 148  0 c.gridx = 1;
 149  0 c.gridy = 0;
 150  0 c.weightx = 0.3;
 151  0 c.weighty = 1.0;
 152  0 _styleList = new NwList(STYLES);
 153  0 cpLayout.setConstraints(_styleList , c);
 154  0 cp.add(_styleList);
 155    // JPanel styleListPanel = new JPanel();
 156    // styleListPanel.setBackground(Color.GREEN);
 157    // cpLayout.setConstraints(styleListPanel, c);
 158    // cp.add(styleListPanel);
 159   
 160  0 c.fill = GridBagConstraints.BOTH;
 161  0 c.anchor = GridBagConstraints.NORTH;
 162  0 c.gridwidth = GridBagConstraints.REMAINDER;
 163  0 c.gridheight = 1;
 164  0 c.gridx = 2;
 165  0 c.gridy = 0;
 166  0 c.weightx = 0.3;
 167  0 c.weighty = 1.0;
 168  0 _sizeList = new NwList(SIZES);
 169  0 cpLayout.setConstraints(_sizeList, c);
 170  0 cp.add(_sizeList);
 171   
 172    // sample text
 173  0 c.fill = GridBagConstraints.BOTH;
 174  0 c.anchor = GridBagConstraints.WEST;
 175  0 c.gridwidth = GridBagConstraints.REMAINDER;
 176  0 c.gridheight = 1;
 177  0 c.gridx = 0;
 178  0 c.gridy = 1;
 179  0 c.weightx = 0.0;
 180  0 c.weighty = 0.0;
 181  0 _sampleText = new JLabel();
 182  0 _sampleText.setForeground(Color.black);
 183  0 cpLayout.setConstraints(_sampleText, c);
 184  0 cp.add(_sampleText);
 185   
 186    // buttons
 187  0 JPanel bottom = new JPanel();
 188  0 bottom.setBorder(new EmptyBorder(5,5,5,5));
 189  0 bottom.setLayout(new BoxLayout(bottom, BoxLayout.X_AXIS));
 190  0 bottom.add(Box.createHorizontalGlue());
 191   
 192  0 _okButton = new JButton("OK");
 193  0 _cancelButton = new JButton("Cancel");
 194  0 bottom.add(_okButton);
 195  0 bottom.add(_cancelButton);
 196  0 _okButton.addActionListener(new ActionListener() {
 197  0 public void actionPerformed(ActionEvent e) {
 198  0 setVisible(false);
 199  0 _clickedOK = true;
 200    }
 201    });
 202  0 _cancelButton.addActionListener(new ActionListener() {
 203  0 public void actionPerformed(ActionEvent e) {
 204  0 setVisible(false);
 205  0 _clickedOK = false;
 206    }
 207    });
 208  0 bottom.add(Box.createHorizontalGlue());
 209   
 210  0 c.fill = GridBagConstraints.NONE;
 211  0 c.anchor = GridBagConstraints.PAGE_END;
 212  0 c.gridwidth = GridBagConstraints.REMAINDER;
 213  0 c.gridheight = 1;
 214  0 c.gridx = 0;
 215  0 c.gridy = 2;
 216  0 c.weightx = 0.0;
 217  0 c.weighty = 0.0;
 218  0 cpLayout.setConstraints(bottom, c);
 219  0 cp.add(bottom);
 220   
 221  0 addWindowListener(new WindowAdapter() {
 222  0 public void windowClosing(java.awt.event.WindowEvent e) {
 223  0 setVisible(false);
 224    }
 225    });
 226   
 227  0 setSize(425, 400);
 228    }
 229   
 230  0 private void showSample() {
 231  0 int g = 0;
 232  0 try { g = Integer.parseInt(_sizeList.getSelectedValue()); }
 233    catch (NumberFormatException nfe) { /* do nothing */ }
 234  0 String st = _styleList.getSelectedValue();
 235  0 int s = Font.PLAIN;
 236  0 if (st.equalsIgnoreCase("Bold")) s = Font.BOLD;
 237  0 if (st.equalsIgnoreCase("Italic")) s = Font.ITALIC;
 238  0 if (st.equalsIgnoreCase("Bold Italic")) s = Font.BOLD | Font.ITALIC;
 239  0 _sampleText.setFont(new Font(_fontList.getSelectedValue(), s, g));
 240  0 _sampleText.setText("The quick brown fox jumped over the lazy dog.");
 241  0 _sampleText.setVerticalAlignment(SwingConstants.TOP);
 242    }
 243   
 244    /** Returns whether the user clicked OK when the dialog was closed. (If false, the user clicked cancel.) */
 245  0 public boolean clickedOK() { return _clickedOK; }
 246   
 247    /** Returns the currently selected Font. */
 248  0 public Font getFont() { return _sampleText.getFont(); }
 249   
 250    /** Private inner class for a list which displays a list of options in addition to a label indicating the currently
 251    * selected item.
 252    */
 253    public class NwList extends JPanel {
 254    JList jl;
 255    JScrollPane sp;
 256    JLabel jt;
 257    String si = " ";
 258   
 259  0 public NwList(String[] values) {
 260  0 GridBagLayout cpLayout = new GridBagLayout();
 261  0 GridBagConstraints c = new GridBagConstraints();
 262  0 setLayout(cpLayout);
 263   
 264  0 jl = new JList(values);
 265  0 sp = new JScrollPane(jl);
 266  0 jt = new JLabel();
 267  0 jt.setBackground(Color.white);
 268  0 jt.setForeground(Color.black);
 269  0 jt.setOpaque(true);
 270  0 jt.setBorder(new JTextField().getBorder());
 271  0 jt.setFont(getFont());
 272  0 jl.addListSelectionListener(new ListSelectionListener() {
 273  0 public void valueChanged(ListSelectionEvent e) {
 274  0 jt.setText((String) jl.getSelectedValue());
 275  0 si = (String) jl.getSelectedValue();
 276  0 showSample();
 277    }
 278    });
 279   
 280  0 c.fill = GridBagConstraints.HORIZONTAL;
 281  0 c.anchor = GridBagConstraints.NORTH;
 282  0 c.gridwidth = GridBagConstraints.REMAINDER;
 283  0 c.gridheight = 1;
 284  0 c.gridx = 0;
 285  0 c.gridy = 0;
 286  0 c.weightx = 1.0;
 287  0 c.weighty = 0.0;
 288  0 cpLayout.setConstraints(jt, c);
 289  0 add(jt);
 290   
 291  0 c.fill = GridBagConstraints.BOTH;
 292  0 c.anchor = GridBagConstraints.NORTH;
 293  0 c.gridwidth = GridBagConstraints.REMAINDER;
 294  0 c.gridheight = 1;
 295  0 c.gridx = 0;
 296  0 c.gridy = 1;
 297  0 c.weightx = 1.0;
 298  0 c.weighty = 1.0;
 299  0 cpLayout.setConstraints(sp, c);
 300  0 add(sp);
 301    }
 302   
 303  0 public String getSelectedValue() { return (si); }
 304   
 305  0 public void setSelectedItem(String s) { jl.setSelectedValue(s, true);}
 306    }
 307    }