Clover coverage report - Common - 1.0.0
Coverage timestamp: sam. déc. 27 2003 15:13:46 CET
file stats: LOC: 149   Methods: 6
NCLOC: 67   Classes: 2
 
 Source file Conditionals Statements Methods TOTAL
FileTools.java 0% 0% 0% 0%
coverage
 1   
 /*
 2   
  * EJTools, the Enterprise Java Tools
 3   
  *
 4   
  * Distributable under LGPL license.
 5   
  * See terms of license at www.gnu.org.
 6   
  */
 7   
 package org.ejtools.util;
 8   
 
 9   
 import java.io.File;
 10   
 import java.text.MessageFormat;
 11   
 import java.util.ResourceBundle;
 12   
 
 13   
 import javax.swing.JFileChooser;
 14   
 import javax.swing.JOptionPane;
 15   
 import javax.swing.filechooser.FileFilter;
 16   
 
 17   
 /**
 18   
  * Helper class to make easy the file selection. Handles the overwrite in case of
 19   
  * save actions.
 20   
  *
 21   
  * @author    Laurent Etiemble
 22   
  * @version   $Revision: 1.2 $
 23   
  */
 24   
 public class FileTools
 25   
 {
 26   
    /** Resource bundle */
 27   
    private static ResourceBundle resources = ResourceBundle.getBundle("org.ejtools.util.Resources");
 28   
 
 29   
 
 30   
    /** Default constructor */
 31  0
    protected FileTools() { }
 32   
 
 33   
 
 34   
    /**
 35   
     * Select a file through a JFileChooser.
 36   
     *
 37   
     * @param title        The title for the JFileChooser
 38   
     * @param approveText  The text that appears on the approve button
 39   
     * @param type         The type of operation
 40   
     * @param filter       The FileFilter to use
 41   
     * @return             The selected file otherwise null
 42   
     * @see                javax.swing.JFileChooser#OPEN_DIALOG
 43   
     * @see                javax.swing.JFileChooser#SAVE_DIALOG
 44   
     */
 45  0
    public static File selectFile(String title, String approveText, int type, SimpleFileFilter filter)
 46   
    {
 47  0
       File selectedFile = null;
 48   
 
 49   
       // Fix for JFileChooser/SecurityManager bug (#4264750)
 50  0
       SecurityManager s = System.getSecurityManager();
 51  0
       System.setSecurityManager(null);
 52   
 
 53   
       // Choose file
 54  0
       JFileChooser chooser = new JFileChooser(System.getProperty("user.dir"));
 55  0
       chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
 56  0
       chooser.setDialogTitle(title);
 57  0
       chooser.setDialogType(type);
 58  0
       chooser.setFileFilter(filter);
 59   
 
 60  0
       int returnVal = chooser.showDialog(null, approveText);
 61  0
       System.setSecurityManager(s);
 62  0
       if (returnVal == JFileChooser.APPROVE_OPTION)
 63   
       {
 64  0
          selectedFile = chooser.getSelectedFile();
 65   
       }
 66   
 
 67  0
       if (selectedFile != null)
 68   
       {
 69   
          // Check if the extension is correct
 70  0
          if (!filter.accept(selectedFile))
 71   
          {
 72  0
             selectedFile = new File(selectedFile.getName() + filter.getExtension());
 73   
          }
 74   
 
 75   
          // If it is a save action, check if file exists
 76  0
          if ((type == JFileChooser.SAVE_DIALOG) && (selectedFile.exists()))
 77   
          {
 78  0
             int ret = JOptionPane.showConfirmDialog(null, MessageFormat.format(resources.getString("file.dialog.overwrite.text"), new Object[]{selectedFile.getName()}));
 79  0
             if (ret != JOptionPane.OK_OPTION)
 80   
             {
 81  0
                selectedFile = null;
 82   
             }
 83   
          }
 84   
       }
 85   
 
 86  0
       return selectedFile;
 87   
    }
 88   
 
 89   
 
 90   
    /**
 91   
     * An helper class that simplifies the creation of a FileFilter.
 92   
     *
 93   
     * @author    Laurent Etiemble
 94   
     * @version   $Revision: 1.2 $
 95   
     */
 96   
    protected static class SimpleFileFilter extends FileFilter
 97   
    {
 98   
       private String description;
 99   
       private String extension;
 100   
 
 101   
 
 102   
       /**
 103   
        * Constructor for SimpleFileFilter class
 104   
        *
 105   
        * @param extension    The file extension. Must start with a dot.
 106   
        * @param description  The description that will appears in the JFileChooser
 107   
        */
 108  0
       public SimpleFileFilter(String extension, String description)
 109   
       {
 110  0
          this.extension = extension;
 111  0
          this.description = description;
 112   
       }
 113   
 
 114   
 
 115   
       /**
 116   
        * Check wheter or not the file ends with the right extension.
 117   
        *
 118   
        * @param file  The file to test
 119   
        * @return      True if the file is accepted
 120   
        */
 121  0
       public boolean accept(File file)
 122   
       {
 123  0
          return file.getName().endsWith(this.extension);
 124   
       }
 125   
 
 126   
 
 127   
       /**
 128   
        * Returns the description string for displaying inside the JFileChooser
 129   
        *
 130   
        * @return   The description
 131   
        */
 132  0
       public String getDescription()
 133   
       {
 134  0
          return this.description;
 135   
       }
 136   
 
 137   
 
 138   
       /**
 139   
        * Gets the extension attribute of the SimpleFileFilter object
 140   
        *
 141   
        * @return   The extension value
 142   
        */
 143  0
       public String getExtension()
 144   
       {
 145  0
          return this.extension;
 146   
       }
 147   
    }
 148   
 }
 149