1   package org.wcb.gui.dialog.file;
2   
3   import org.wcb.gui.dialog.filter.CSVFileExtensionFilter;
4   
5   import javax.swing.*;
6   import java.io.File;
7   
8   /**
9    * <small>
10   * Copyright (c)  2006  wbogaardt.
11   * This library is free software; you can redistribute it and/or
12   * modify it under the terms of the GNU Lesser General Public
13   * License as published by the Free Software Foundation; either
14   * version 2.1 of the License, or (at your option) any later version.
15   *
16   * This library is distributed in the hope that it will be useful,
17   * but WITHOUT ANY WARRANTY; without even the implied warranty of
18   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19   * Lesser General Public License for more details.
20   *
21   * You should have received a copy of the GNU Lesser General Public
22   * License along with this library; if not, write to the Free Software
23   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
24   * <p/>
25   * $File:  $ <br>
26   * $Change:  $ submitted by $Author: wbogaardt $ at $DateTime: Apr 3, 2006 11:25:22 AM $ <br>
27   * </small>
28   *
29   * @author wbogaardt
30   * @version 1
31   *          Date: Apr 3, 2006
32   *          Time: 11:25:22 AM
33   */
34  
35  public class ImportFileDialog {
36  
37      private static JFileChooser jfcChooser = null;
38      private String sDirectory;
39      private boolean bIsFile = false;
40  
41      /**
42       * Constructor to create and show a open file dialog
43       * with only .cvs files showing as default.
44       */
45      public ImportFileDialog() {
46          if (jfcChooser == null)
47          {
48              jfcChooser = new JFileChooser();
49              jfcChooser.addChoosableFileFilter(new CSVFileExtensionFilter());
50          }
51          int fileState = jfcChooser.showOpenDialog(null);
52          File file = jfcChooser.getSelectedFile();
53          if (file != null && fileState == JFileChooser.APPROVE_OPTION)
54          {
55              setFilePath(file.getAbsolutePath());
56          }
57          else if (fileState == JFileChooser.CANCEL_OPTION)
58          {
59              bIsFile = false;
60          }
61      }
62  
63      /**
64       * Gets the file path directory from the open file dialog.
65       * @return String of the filepath.
66       */
67      public String getFilePath() {
68          return sDirectory;
69      }
70  
71      /**
72       * If a file was selected this returns a true.
73       * @return true file was selected else a false
74       */
75      public boolean isFileSelected() {
76          return bIsFile;
77      }
78  
79      /**
80       * Allows setting of the file path.
81       * @param fpath String of the file path
82       */
83      private void setFilePath(String fpath) {
84          sDirectory = fpath;
85          bIsFile = true;
86      }
87  }