View Javadoc
1   package com.github.celldynamics.quimp.filesystem;
2   
3   import java.awt.Dialog;
4   import java.awt.FileDialog;
5   import java.awt.Frame;
6   import java.io.File;
7   import java.io.FilenameFilter;
8   import java.nio.file.Path;
9   import java.nio.file.Paths;
10  import java.util.Arrays;
11  
12  import com.github.celldynamics.quimp.QuimP;
13  
14  /**
15   * Implements file dialog with file filtering.
16   * 
17   * <p>If not stated otherwise, all constructors use default file config extension given by state of
18   * QuimP ToolBar checkbox (of {@link QuimP#newFileFormat}.
19   * 
20   * @author p.baniukiewicz
21   *
22   */
23  public class FileDialogEx extends FileDialog {
24  
25    private QuimpConfigFilefilter qcf = new QuimpConfigFilefilter();
26  
27    /**
28     * serialVersionUID.
29     */
30    private static final long serialVersionUID = -8657174496110858503L;
31  
32    /**
33     * Construct object with default file filtering.
34     * 
35     * @param parent parent
36     * @param title title
37     * @param mode mode
38     */
39    public FileDialogEx(Dialog parent, String title, int mode) {
40      super(parent, title, mode);
41    }
42  
43    /**
44     * Construct object with default file filtering.
45     * 
46     * @param parent parent
47     * @param title title
48     */
49    public FileDialogEx(Dialog parent, String title) {
50      super(parent, title);
51    }
52  
53    /**
54     * Construct object with default file filtering.
55     * 
56     * @param parent parent
57     */
58    public FileDialogEx(Dialog parent) {
59      super(parent);
60    }
61  
62    /**
63     * Construct object with default file filtering.
64     * 
65     * @param parent parent
66     * @param title title
67     * @param mode mode
68     */
69    public FileDialogEx(Frame parent, String title, int mode) {
70      super(parent, title, mode);
71    }
72  
73    /**
74     * Construct object with default file filtering.
75     * 
76     * @param parent parent
77     * @param title title
78     */
79    public FileDialogEx(Frame parent, String title) {
80      super(parent, title);
81    }
82  
83    /**
84     * Construct object with default file filtering.
85     * 
86     * @param parent parent
87     */
88    public FileDialogEx(Frame parent) {
89      super(parent);
90    }
91  
92    /**
93     * Construct object with given file extension filtering.
94     * 
95     * @param parent parent
96     * @param ext list of extensions (.ext). Can be null to use default
97     *        {@link FileDialogEx.QuimpConfigFilefilter}
98     */
99    public FileDialogEx(Frame parent, String... ext) {
100     super(parent);
101     if (ext != null) {
102       setExtension(ext);
103     }
104   }
105 
106   /**
107    * Set extensions to filter.
108    * 
109    * <p>Should be called before {@link #showOpenDialog()}.
110    * 
111    * @param ext list of extensions (.ext). Can be null to use default
112    *        {@link FileDialogEx.QuimpConfigFilefilter}
113    */
114   public void setExtension(String... ext) {
115     qcf = new QuimpConfigFilefilter(ext);
116   }
117 
118   /**
119    * Get path to selected file.
120    * 
121    * <p>Should be called after {@link #showOpenDialog()}.
122    * 
123    * @return path to selected file or null if dialog cancelled.
124    */
125   public Path getPath() {
126     if (getFile() == null) {
127       return null;
128     } else {
129       return Paths.get(getDirectory(), getFile());
130     }
131   }
132 
133   /**
134    * Show dialog and wait for user action.
135    * 
136    * @return path to selected file or null if cancelled.
137    */
138   public Path showOpenDialog() {
139     setMultipleMode(false);
140     setMode(FileDialog.LOAD);
141     setTitle("Open paramater file " + qcf.toString());
142     setFilenameFilter(qcf);
143     setVisible(true);
144     return getPath();
145   }
146 
147   /**
148    * Implement filter of FileDialog.
149    * 
150    * <p>Define also default extensions for new and old file format.
151    * 
152    * @author p.baniukiewicz
153    *
154    */
155   class QuimpConfigFilefilter implements FilenameFilter {
156     private String[] ext;
157 
158     /**
159      * Allow to provide list of accepted extensions with dot.
160      * 
161      * @param ext list of extensions .ext, .ext
162      */
163     public QuimpConfigFilefilter(String... ext) {
164       this.ext = ext;
165     }
166 
167     /**
168      * Default constructor.
169      * 
170      * <p>Set active extension using choicebox in Quimp_Bar
171      * 
172      */
173     public QuimpConfigFilefilter() {
174       if (QuimP.newFileFormat.get() == true) {
175         ext = new String[1];
176         ext[0] = FileExtensions.newConfigFileExt;
177       } else {
178         ext = new String[2];
179         ext[0] = FileExtensions.configFileExt;
180         ext[1] = FileExtensions.newConfigFileExt;
181       }
182     }
183 
184     /*
185      * (non-Javadoc)
186      * 
187      * @see java.io.FilenameFilter#accept(java.io.File, java.lang.String)
188      */
189     @Override
190     public boolean accept(File dir, String name) {
191       boolean ret = false;
192       for (String e : ext) {
193         ret = ret || (name.endsWith(e.toLowerCase()) || name.endsWith(e)); // any true will set flag
194       }
195       return ret;
196     }
197 
198     /**
199      * Get stored extension.
200      * 
201      * @return the extension
202      */
203     public String[] getExt() {
204       return ext;
205     }
206 
207     /**
208      * Return defined extensions.
209      * 
210      * <p>Can be used for filling bar of file selectors.
211      * 
212      * @see java.lang.Object#toString()
213      */
214     @Override
215     public String toString() {
216       return Arrays.toString(ext);
217     }
218 
219   }
220 
221 }