KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ejtools > util > FileTools


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 JavaDoc;
10 import java.text.MessageFormat JavaDoc;
11 import java.util.ResourceBundle JavaDoc;
12
13 import javax.swing.JFileChooser JavaDoc;
14 import javax.swing.JOptionPane JavaDoc;
15 import javax.swing.filechooser.FileFilter JavaDoc;
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 JavaDoc resources = ResourceBundle.getBundle("org.ejtools.util.Resources");
28
29
30    /** Default constructor */
31    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    public static File JavaDoc selectFile(String JavaDoc title, String JavaDoc approveText, int type, SimpleFileFilter filter)
46    {
47       File JavaDoc selectedFile = null;
48
49       // Fix for JFileChooser/SecurityManager bug (#4264750)
50
SecurityManager JavaDoc s = System.getSecurityManager();
51       System.setSecurityManager(null);
52
53       // Choose file
54
JFileChooser JavaDoc chooser = new JFileChooser JavaDoc(System.getProperty("user.dir"));
55       chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
56       chooser.setDialogTitle(title);
57       chooser.setDialogType(type);
58       chooser.setFileFilter(filter);
59
60       int returnVal = chooser.showDialog(null, approveText);
61       System.setSecurityManager(s);
62       if (returnVal == JFileChooser.APPROVE_OPTION)
63       {
64          selectedFile = chooser.getSelectedFile();
65       }
66
67       if (selectedFile != null)
68       {
69          // Check if the extension is correct
70
if (!filter.accept(selectedFile))
71          {
72             selectedFile = new File JavaDoc(selectedFile.getName() + filter.getExtension());
73          }
74
75          // If it is a save action, check if file exists
76
if ((type == JFileChooser.SAVE_DIALOG) && (selectedFile.exists()))
77          {
78             int ret = JOptionPane.showConfirmDialog(null, MessageFormat.format(resources.getString("file.dialog.overwrite.text"), new Object JavaDoc[]{selectedFile.getName()}));
79             if (ret != JOptionPane.OK_OPTION)
80             {
81                selectedFile = null;
82             }
83          }
84       }
85
86       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 JavaDoc
97    {
98       private String JavaDoc description;
99       private String JavaDoc 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       public SimpleFileFilter(String JavaDoc extension, String JavaDoc description)
109       {
110          this.extension = extension;
111          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       public boolean accept(File JavaDoc file)
122       {
123          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       public String JavaDoc getDescription()
133       {
134          return this.description;
135       }
136
137
138       /**
139        * Gets the extension attribute of the SimpleFileFilter object
140        *
141        * @return The extension value
142        */

143       public String JavaDoc getExtension()
144       {
145          return this.extension;
146       }
147    }
148 }
149
Popular Tags