KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ejtools > util > export > CSVTools


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.export;
8
9 import java.io.File JavaDoc;
10 import java.io.FileWriter JavaDoc;
11 import java.io.IOException JavaDoc;
12
13 import javax.swing.JFileChooser JavaDoc;
14 import javax.swing.filechooser.FileFilter JavaDoc;
15
16 import org.apache.log4j.Logger;
17
18
19 /**
20  * Description of the Class
21  *
22  * @author Laurent Etiemble
23  * @version $Revision: 1.1 $
24  */

25 public class CSVTools
26 {
27    /** Description of the Field */
28    private static FileFilter JavaDoc CSV_FILE_FILTER =
29       new FileFilter JavaDoc()
30       {
31          public boolean accept(File JavaDoc file)
32          {
33             return file.getName().endsWith(".csv");
34          }
35
36
37          public String JavaDoc getDescription()
38          {
39             return "Comma Separated Values file (*.csv)";
40          }
41       };
42    /** Description of the Field */
43    private static Logger logger = Logger.getLogger(CSVTools.class);
44
45
46    /**Constructor for the CSVTools object */
47    private CSVTools()
48    {
49       super();
50    }
51
52
53    /**
54     * Description of the Method
55     *
56     * @param output Description of the Parameter
57     * @param content Description of the Parameter
58     */

59    public static void exportAsCVS(StringBuffer JavaDoc content, File JavaDoc output)
60    {
61       try
62       {
63          FileWriter JavaDoc writer = new FileWriter JavaDoc(output);
64          writer.write(content.toString());
65          writer.flush();
66          writer.close();
67       }
68       catch (IOException JavaDoc ioe)
69       {
70          logger.error("Can't export content as CSV", ioe);
71       }
72    }
73
74
75    /**
76     * Description of the Method
77     *
78     * @return Description of the Return Value
79     */

80    public static File JavaDoc selectCSVFile()
81    {
82       // Fix for JFileChooser/SecurityManager bug (#4264750)
83
SecurityManager JavaDoc s = System.getSecurityManager();
84       System.setSecurityManager(null);
85
86       // Choose file
87
JFileChooser JavaDoc chooser = new JFileChooser JavaDoc(System.getProperty("user.dir"));
88       chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
89       chooser.setFileFilter(CSV_FILE_FILTER);
90       int returnVal = chooser.showSaveDialog(null);
91       System.setSecurityManager(s);
92       if (returnVal != JFileChooser.APPROVE_OPTION)
93       {
94          return null;
95       }
96       return chooser.getSelectedFile();
97    }
98 }
99
Popular Tags