Clover coverage report - DrJava Test Coverage (drjava-20120422-r5456)
Coverage timestamp: Sun Apr 22 2012 03:13:25 CDT
file stats: LOC: 252   Methods: 18
NCLOC: 121   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
FileSelectorComponent.java 18.2% 53.6% 55.6% 46.8%
coverage 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 javax.swing.*;
 40    import javax.swing.filechooser.FileFilter;
 41    import java.awt.*;
 42    import java.awt.event.ActionEvent;
 43    import java.awt.event.ActionListener;
 44    import java.awt.event.FocusListener;
 45    import java.awt.event.FocusEvent;
 46    import java.io.*;
 47   
 48    import edu.rice.cs.util.FileOps;
 49    import edu.rice.cs.util.UnexpectedException;
 50   
 51    /** A JPanel with a text box and a "..." button used to select a file or directory. The file name is editable in the
 52    * text box, and a JFileChooser is displayed if the user clicks the "..." button.
 53    * TODO: make this inherit from FileSelectorStringComponent or factor the common code into an abstract class! Duplicated code!
 54    * @version $Id: FileSelectorComponent.java 5175 2010-01-20 08:46:32Z mgricken $
 55    */
 56    public class FileSelectorComponent extends JPanel {
 57   
 58    /** The default number of columns for the text box. */
 59    public static final int DEFAULT_NUM_COLS = 30;
 60   
 61    /** The default font size for the text box. */
 62    public static final float DEFAULT_FONT_SIZE = 10f;
 63   
 64    /** The parent frame of this component. */
 65    protected final SwingFrame _parent;
 66   
 67    /** Text field with the name of the selected file. */
 68    protected final JTextField _fileField;
 69   
 70    /** "..." button to open the file chooser. */
 71    protected final JButton _chooserButton;
 72   
 73    /** File chooser to open when clicking the "..." button. */
 74    protected final JFileChooser _chooser;
 75   
 76    /** File filter to use in the chooser. */
 77    protected volatile FileFilter _fileFilter;
 78   
 79    /** The current file */
 80    protected volatile File _file;
 81   
 82    /** True if file must exist. */
 83    protected volatile boolean _mustExist;
 84   
 85    /** Creates a new FileSelectorComponent with default dimensions.
 86    * @param parent Parent of this component.
 87    * @param chooser File chooser to display from the "..." button.
 88    */
 89  0 public FileSelectorComponent(SwingFrame parent, JFileChooser chooser) {
 90  0 this(parent, chooser, DEFAULT_NUM_COLS, DEFAULT_FONT_SIZE, true);
 91    }
 92   
 93    /** Creates a new FileSelectorComponent.
 94    * @param parent Parent of this component.
 95    * @param chooser File chooser to display from the "..." button.
 96    * @param numCols Number of columns to display in the text field
 97    * @param fontSize Font size for the text field
 98    */
 99  155 public FileSelectorComponent(SwingFrame parent, JFileChooser chooser, int numCols, float fontSize) {
 100  155 this(parent, chooser, numCols, fontSize, true);
 101    }
 102   
 103    /** Creates a new FileSelectorComponent.
 104    * @param parent Parent of this component; may be null if the FileSelector is supposed to stand-alone.
 105    * @param chooser File chooser to display from the "..." button.
 106    * @param numCols Number of columns to display in the text field
 107    * @param fontSize Font size for the text field
 108    * @param mustExist force selection of existing file
 109    */
 110  193 public FileSelectorComponent(SwingFrame parent, JFileChooser chooser, int numCols, float fontSize, boolean mustExist) {
 111   
 112  0 if (chooser == null) throw new UnexpectedException("Error: called new FileSelectorComponent(...) with a null chooser!");
 113   
 114  193 _parent = parent;
 115  193 _chooser = chooser;
 116  193 _fileFilter = null;
 117  193 _mustExist = mustExist;
 118   
 119  193 _fileField = new JTextField(numCols) {
 120  76 public Dimension getMaximumSize() {
 121  76 return new Dimension(Short.MAX_VALUE, super.getPreferredSize().height);
 122    }
 123    };
 124   
 125  193 _fileField.setFont(_fileField.getFont().deriveFont(fontSize));
 126  193 _fileField.setPreferredSize(new Dimension(22, 22));
 127  193 _fileField.addActionListener(new ActionListener() {
 128  0 public void actionPerformed(ActionEvent e) { validateTextField(); }
 129    });
 130   
 131  193 _fileField.addFocusListener(new FocusListener() {
 132  0 public void focusGained(FocusEvent e) { /* validateTextField(); */ }
 133  0 public void focusLost(FocusEvent e) { validateTextField(); }
 134    });
 135   
 136  193 _chooserButton = new JButton("...");
 137  193 _chooserButton.addActionListener(new ActionListener() {
 138  0 public void actionPerformed(ActionEvent e) { _chooseFile(); }
 139    });
 140   
 141  193 _chooserButton.setMaximumSize(new Dimension(22, 22));
 142  193 _chooserButton.setMargin(new Insets(0, 5 ,0, 5));
 143   
 144    // Add components
 145  193 this.setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
 146  193 this.add(_fileField);
 147  193 this.add(_chooserButton);
 148    }
 149   
 150  231 public void setEnabled(boolean isEnabled) {
 151  231 _fileField.setEnabled(isEnabled);
 152  231 _chooserButton.setEnabled(isEnabled);
 153  231 super.setEnabled(isEnabled);
 154    }
 155   
 156    /** Returns the file text field. */
 157  155 public JTextField getFileField() { return _fileField; }
 158   
 159    /** Returns the file chooser. */
 160  0 public JFileChooser getFileChooser() { return _chooser; }
 161   
 162    /** Returns the file currently typed into the file field. */
 163  80 public File getFileFromField() {
 164  80 String txt = _fileField.getText().trim();
 165  80 if (txt.equals("")) _file = FileOps.NULL_FILE;
 166  0 else _file = new File(txt);
 167   
 168  80 return _file;
 169    }
 170   
 171    /** Sets the text of the file field to be the given file.
 172    * @param file File to display in the file field.
 173    */
 174  5 public void setFileField(File file) {
 175  5 _file = file;
 176  5 if (file != null && !file.getPath().equals("")) {
 177  0 try {_file = file.getCanonicalFile(); }
 178    catch(IOException e) { /* do nothing */ }
 179    }
 180  5 resetFileField();
 181    }
 182   
 183  5 public void resetFileField() {
 184  0 if (_file == null) _fileField.setText("");
 185    else {
 186  5 _fileField.setText(_file.getPath());
 187  5 _fileField.setCaretPosition(_fileField.getText().length());
 188    }
 189    }
 190   
 191    /** Sets the file filter to use. */
 192  152 public void setFileFilter(FileFilter filter) { _fileFilter = filter; }
 193   
 194  152 public void setToolTipText(String text) {
 195  152 super.setToolTipText(text);
 196  152 _fileField.setToolTipText(text);
 197  152 _chooser.setToolTipText(text);
 198    }
 199   
 200    /** Opens the file chooser to select a file, putting the result in the file field. */
 201  0 protected void _chooseFile() {
 202    // Set the chooser to be in the right directory
 203  0 if (_file != null && _file.exists()) {
 204  0 _chooser.setCurrentDirectory(_file);
 205  0 _chooser.setSelectedFile(_file);
 206    }
 207   
 208    // Apply the filter
 209  0 if (_fileFilter != null) _chooser.setFileFilter(_fileFilter);
 210   
 211    // Get the file from the chooser
 212  0 int returnValue = _chooser.showDialog(_parent, null);
 213  0 if (returnValue == JFileChooser.APPROVE_OPTION) {
 214  0 File chosen = _chooser.getSelectedFile();
 215  0 if (chosen != null) setFileField(chosen);
 216    }
 217    }
 218   
 219    // used so that the focus listener and the action listener do not
 220    // both validate the incorrect text. This ensures that only the first
 221    // one gets it.
 222    // private boolean _validationInProgress = false;
 223   
 224    /** The chooser method for the validation of filenames that are manually entered into the text field.
 225    * @return False, if file does not exist. True, otherwise.
 226    */
 227  0 public boolean validateTextField() {
 228    // if (_validationInProgress) return true;
 229    // _validationInProgress = true;
 230   
 231  0 String newValue = _fileField.getText().trim();
 232   
 233  0 File newFile = FileOps.NULL_FILE;
 234  0 if (! newValue.equals(""))
 235  0 newFile = new File(newValue);
 236   
 237  0 if (newFile != FileOps.NULL_FILE && _mustExist && !newFile.exists()) {
 238  0 JOptionPane.showMessageDialog(_parent, "The file '" + newValue + "'\nis invalid because it does not exist.",
 239    "Invalid File Name", JOptionPane.ERROR_MESSAGE);
 240  0 if (_file != null && ! _file.exists()) _file = FileOps.NULL_FILE;
 241  0 resetFileField(); // revert if not valid
 242   
 243    // _validationInProgress = false;
 244  0 return false;
 245    }
 246    else {
 247  0 setFileField(newFile);
 248    // _validationInProgress = false;
 249  0 return true;
 250    }
 251    }
 252    }