Clover coverage report - DrJava Test Coverage (drjava-20120422-r5456)
Coverage timestamp: Sun Apr 22 2012 03:13:25 CDT
file stats: LOC: 206   Methods: 19
NCLOC: 104   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
FileSelectorStringComponent.java 0% 29.5% 21.1% 22.4%
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.io.*;
 45   
 46    import edu.rice.cs.util.FileOps;
 47   
 48    /** Just like FileSelectorComponent, but it converts the file to a different string that gets displayed. */
 49    public class FileSelectorStringComponent extends JPanel {
 50   
 51    /** The default number of columns for the text box. */
 52    public static final int DEFAULT_NUM_COLS = 30;
 53   
 54    /** The default font size for the text box. */
 55    public static final float DEFAULT_FONT_SIZE = 10f;
 56   
 57    /** The parent component of this component. */
 58    protected final Component _parent;
 59   
 60    /** Text field with the name of the selected file. */
 61    protected final JTextField _textField;
 62   
 63    /** "..." button to open the file chooser. */
 64    protected final JButton _chooserButton;
 65   
 66    /** File chooser to open when clicking the "..." button. */
 67    protected final FileChooser _chooser;
 68   
 69    /** The current file */
 70    protected volatile File _file;
 71   
 72    /** Creates a new DirectorySelectorStringComponent with default dimensions.
 73    * @param parent Parent of this component.
 74    * @param chooser File chooser to display from the "..." button. Assumed non-null!
 75    */
 76  0 public FileSelectorStringComponent(Component parent, FileChooser chooser) {
 77  0 this(parent, chooser, DEFAULT_NUM_COLS, DEFAULT_FONT_SIZE);
 78    }
 79   
 80    /** Creates a new DirectorySelectorStringComponent.
 81    * @param parent Parent of this component.
 82    * @param chooser File chooser to display from the "..." button. Assumed non-null!
 83    * @param numCols Number of columns to display in the text field
 84    * @param fontSize Font size for the text field
 85    */
 86  38 public FileSelectorStringComponent(Component parent, FileChooser chooser, int numCols, float fontSize) {
 87  38 _parent = parent;
 88  38 _chooser = chooser;
 89  38 _file = FileOps.NULL_FILE;
 90   
 91  38 _textField = new JTextField(numCols) {
 92  76 public Dimension getMaximumSize() { return new Dimension(Short.MAX_VALUE, super.getPreferredSize().height); }
 93    };
 94  38 _textField.setFont(_textField.getFont().deriveFont(fontSize));
 95  38 _textField.setPreferredSize(new Dimension(22,22));
 96   
 97  38 _chooserButton = new JButton("...");
 98  38 _chooserButton.addActionListener(new ActionListener() {
 99  0 public void actionPerformed(ActionEvent e) { _chooseFile(); }
 100    });
 101  38 _chooserButton.setMaximumSize(new Dimension(22, 22));
 102  38 _chooserButton.setMargin(new Insets(0,5,0,5));
 103    // Add components
 104  38 this.setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
 105  38 this.add(_textField);
 106  38 this.add(_chooserButton);
 107    }
 108   
 109  76 public void setEnabled(boolean isEnabled) {
 110  76 _textField.setEnabled(isEnabled);
 111  76 _chooserButton.setEnabled(isEnabled);
 112  76 super.setEnabled(isEnabled);
 113    }
 114   
 115    /** Returns the file text field. */
 116  38 public JTextField getTextField() { return _textField; }
 117   
 118    /** Returns the file chooser. */
 119  0 public FileChooser getFileChooser() { return _chooser; }
 120   
 121    /** Converts a string representation from the text field into a File. */
 122  0 public File convertStringToFile(String s) {
 123  0 s = s.trim();
 124  0 if (s.equals("")) return null;
 125  0 return new File(s);
 126    }
 127   
 128    /** Converts a file to the string representation of the text field. */
 129  0 public String convertFileToString(File f) {
 130  0 if (f == null) return "";
 131  0 return f.toString();
 132    }
 133   
 134    /** Returns the last file that was selected. */
 135  0 public File getFileFromField() {
 136    // Get the file from the chooser
 137  0 String newValue = _textField.getText();
 138  0 File newFile = FileOps.NULL_FILE;
 139  0 if (! newValue.equals("")) {
 140  0 newFile = convertStringToFile(newValue);
 141  0 if (! newFile.isDirectory() && ! _chooser.isFileSelectionEnabled()) newFile = newFile.getParentFile();
 142    }
 143   
 144  0 if (newFile != null && ! newFile.exists()) newFile = _file;
 145   
 146  0 return newFile;
 147    }
 148   
 149    /** Returns the string in the text field. */
 150  0 public String getText() { return _textField.getText(); }
 151   
 152    /** Sets the string in the text field. */
 153  0 public void setText(String s) { _textField.setText(s); }
 154   
 155    /** Sets the text of the file field to be the given file.
 156    * @param file File to display in the file field.
 157    */
 158  0 public void setFileField(File file) {
 159  0 _file = file;
 160  0 if (file != null && ! file.getPath().equals("")) {
 161  0 try { _file = file.getCanonicalFile(); }
 162    catch(IOException e) { /* do nothing */ }
 163    }
 164  0 resetFileField();
 165    }
 166   
 167  0 public void resetFileField() {
 168  0 _textField.setText(convertFileToString(_file));
 169  0 _textField.setCaretPosition(_textField.getText().length());
 170    }
 171   
 172  0 public void setToolTipText(String text) {
 173  0 super.setToolTipText(text);
 174  0 _textField.setToolTipText(text);
 175  0 _chooserButton.setToolTipText(text);
 176    }
 177   
 178    /** Adds a filter to decide if a directory can be chosen. */
 179  0 public void addChoosableFileFilter(FileFilter filter) {
 180  0 _chooser.addChoosableFileFilter(filter);
 181    }
 182   
 183    /** Removes the given filefilter from the chooser */
 184  0 public void removeChoosableFileFilter(FileFilter filter) {
 185  0 _chooser.removeChoosableFileFilter(filter);
 186    }
 187   
 188  0 public void clearChoosableFileFilters() {
 189  0 _chooser.resetChoosableFileFilters();
 190    }
 191   
 192    /** Opens the file chooser to select a file, putting the result in the file field. */
 193  0 protected void _chooseFile() {
 194  0 File f = getFileFromField();
 195  0 if (f != null && f.exists()) {
 196  0 _chooser.setCurrentDirectory(f);
 197  0 _chooser.setSelectedFile(f);
 198    }
 199  0 int returnValue = _chooser.showDialog(_parent, null);
 200  0 if (returnValue == FileChooser.APPROVE_OPTION) {
 201  0 File chosen = _chooser.getSelectedFile();
 202  0 if (chosen != null) { setFileField(chosen); }
 203    }
 204    }
 205   
 206    }