Clover coverage report - DrJava Test Coverage (drjava-20120422-r5456)
Coverage timestamp: Sun Apr 22 2012 03:13:25 CDT
file stats: LOC: 242   Methods: 20
NCLOC: 119   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
DirectorySelectorComponent.java 16.7% 46.4% 40% 40.2%
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   
 50    /** A JPanel with a text box and a "..." button to select a file or directory. The file name is editable in the text
 51    * box, and a JFileChooser is displayed if the user clicks the "..." button.
 52    * @version $Id: DirectorySelectorComponent.java 5175 2010-01-20 08:46:32Z mgricken $
 53    */
 54    public class DirectorySelectorComponent extends JPanel {
 55   
 56    /** The default number of columns for the text box. */
 57    public static final int DEFAULT_NUM_COLS = 30;
 58   
 59    /** The default font size for the text box. */
 60    public static final float DEFAULT_FONT_SIZE = 10f;
 61   
 62    /** The parent component of this component. */
 63    protected final Component _parent;
 64   
 65    /** Text field with the name of the selected file. */
 66    protected final JTextField _fileField;
 67   
 68    /** "..." button to open the file chooser. */
 69    protected final JButton _chooserButton;
 70   
 71    /** File chooser to open when clicking the "..." button. */
 72    protected final DirectoryChooser _chooser;
 73   
 74    /** The current file */
 75    protected File _file;
 76   
 77    /** true if the file specified must exist and a file that doesn't exist will be rejected. */
 78    protected boolean _mustExist;
 79   
 80    /** Creates a new DirectorySelectorComponent with default dimensions whose file must exist.
 81    * @param parent Parent of this component.
 82    * @param chooser File chooser to display from the "..." button.
 83    */
 84  0 public DirectorySelectorComponent(Component parent, DirectoryChooser chooser) {
 85  0 this(parent, chooser, DEFAULT_NUM_COLS, DEFAULT_FONT_SIZE);
 86    }
 87   
 88    /** Creates a new DirectorySelectorComponent whose file must exist.
 89    * @param parent Parent of this component.
 90    * @param chooser File chooser to display from the "..." button.
 91    * @param numCols Number of columns to display in the text field
 92    * @param fontSize Font size for the text field
 93    */
 94  76 public DirectorySelectorComponent(Component parent, DirectoryChooser chooser, int numCols, float fontSize) {
 95  76 this(parent, chooser, numCols, fontSize, true);
 96    }
 97   
 98    /** Creates a new DirectorySelectorComponent.
 99    * @param parent Parent of this component.
 100    * @param chooser File chooser to display from the "..." button.
 101    * @param numCols Number of columns to display in the text field
 102    * @param fontSize Font size for the text field
 103    * @param mustExist true if the file specified in the field must exist
 104    */
 105  114 public DirectorySelectorComponent(Component parent, DirectoryChooser chooser, int numCols, float fontSize,
 106    boolean mustExist) {
 107   
 108  114 _parent = parent;
 109  114 _chooser = chooser;
 110  114 _file = FileOps.NULL_FILE;
 111  114 _mustExist = mustExist;
 112   
 113  114 _fileField = new JTextField(numCols) {
 114  76 public Dimension getMaximumSize() { return new Dimension(Short.MAX_VALUE, super.getPreferredSize().height); }
 115    };
 116   
 117  114 _fileField.setFont(_fileField.getFont().deriveFont(fontSize));
 118  114 _fileField.setPreferredSize(new Dimension(22,22));
 119  114 _fileField.addActionListener(new ActionListener() {
 120  0 public void actionPerformed(ActionEvent e) { validateTextField(); }
 121    });
 122   
 123  114 _fileField.addFocusListener(new FocusListener() {
 124  0 public void focusGained(FocusEvent e) { /* validateTextField(); */ }
 125  0 public void focusLost(FocusEvent e) { validateTextField(); }
 126    });
 127   
 128  114 _chooserButton = new JButton("...");
 129  114 _chooserButton.addActionListener(new ActionListener() {
 130  0 public void actionPerformed(ActionEvent e) { _chooseFile(); }
 131    });
 132   
 133  114 _chooserButton.setMaximumSize(new Dimension(22, 22));
 134  114 _chooserButton.setMargin(new Insets(0,5,0,5));
 135   
 136    // Add components
 137  114 this.setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
 138  114 this.add(_fileField);
 139  114 this.add(_chooserButton);
 140    }
 141   
 142  76 public void setEnabled(boolean isEnabled) {
 143  76 _fileField.setEnabled(isEnabled);
 144  76 _chooserButton.setEnabled(isEnabled);
 145  76 super.setEnabled(isEnabled);
 146    }
 147   
 148    /** Returns the file text field. */
 149  76 public JTextField getFileField() { return _fileField; }
 150   
 151    /** Returns the file chooser. */
 152  0 public DirectoryChooser getFileChooser() { return _chooser; }
 153   
 154    /** Returns the file currently typed into the file field. THE SIDE EFFECTS OF THIS METHOD ARE OBSCENE! Corky 2/5/06 */
 155  38 public File getFileFromField() {
 156  38 String txt = _fileField.getText().trim();
 157  38 if (txt.equals("")) _file = FileOps.NULL_FILE;
 158  0 else _file = new File(txt);
 159   
 160  38 return _file;
 161    }
 162   
 163    /** Sets the text of the file field to be the given file.
 164    * @param file File to display in the file field.
 165    */
 166  76 public void setFileField(File file) {
 167  76 _file = file;
 168  76 if (file != null && ! file.getPath().equals("")) {
 169  0 try { _file = file.getCanonicalFile(); }
 170    catch(IOException e) { /* do nothing */ }
 171    }
 172  76 resetFileField();
 173    }
 174   
 175  76 public void resetFileField() {
 176  0 if (_file == null) _fileField.setText("");
 177    else {
 178  76 _fileField.setText(_file.toString());
 179  76 _fileField.setCaretPosition(_fileField.getText().length());
 180    }
 181    }
 182   
 183  0 public void setToolTipText(String text) {
 184  0 super.setToolTipText(text);
 185  0 _fileField.setToolTipText(text);
 186  0 _chooserButton.setToolTipText(text);
 187    }
 188   
 189    /** Adds a filter to decide if a directory can be chosen. */
 190  0 public void addChoosableFileFilter(FileFilter filter) { _chooser.addChoosableFileFilter(filter); }
 191   
 192    /** Removes the given filefilter from the chooser. */
 193  0 public void removeChoosableFileFilter(FileFilter filter) { _chooser.removeChoosableFileFilter(filter); }
 194   
 195  0 public void clearChoosableFileFilters() { _chooser.resetChoosableFileFilters(); }
 196   
 197    /** Flag indicating that validation by the focus listener or action listener is pending. The flag is used to avoid
 198    * duplicating the validation process.
 199    */
 200    private boolean _validationInProgress = false;
 201   
 202    /** The chooser method for the validation of filenames that are manually entered into the text field.
 203    * @return False, if file does not exist. True, otherwise.
 204    */
 205  0 public boolean validateTextField() {
 206  0 if (_validationInProgress) return true;
 207  0 _validationInProgress = true;
 208   
 209  0 String newValue = _fileField.getText().trim();
 210   
 211  0 File newFile = FileOps.NULL_FILE;
 212  0 if (! newValue.equals("")) {
 213  0 newFile = new File(newValue);
 214  0 if (! newFile.isDirectory() && _chooser.isFileSelectionEnabled()) newFile = newFile.getParentFile();
 215    }
 216   
 217  0 if (newFile != FileOps.NULL_FILE && _mustExist && ! newFile.exists()) {
 218  0 JOptionPane.showMessageDialog(_parent, "The file '" + newValue + "'\nis invalid because it does not exist.",
 219    "Invalid File Name", JOptionPane.ERROR_MESSAGE);
 220  0 resetFileField(); // revert if not valid
 221  0 _validationInProgress = false;
 222  0 return false;
 223    }
 224    else {
 225  0 setFileField(newFile);
 226  0 _validationInProgress = false;
 227  0 return true;
 228    }
 229    }
 230   
 231    /** Opens the file chooser to select a file, putting the result in the file field. */
 232  0 protected void _chooseFile() {
 233   
 234    // Get the file from the chooser
 235  0 int returnValue = _chooser.showDialog(_file);
 236  0 if (returnValue == DirectoryChooser.APPROVE_OPTION) {
 237  0 File chosen = _chooser.getSelectedDirectory();
 238  0 if (chosen != null) setFileField(chosen);
 239    }
 240    }
 241   
 242    }