/[svn]/jsampler/trunk/src/org/jsampler/view/JSFileFilter.java
ViewVC logotype

Diff of /jsampler/trunk/src/org/jsampler/view/JSFileFilter.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1914 by iliev, Sun Mar 22 18:11:39 2009 UTC revision 1915 by iliev, Thu Jun 11 09:35:29 2009 UTC
# Line 21  Line 21 
21   */   */
22  package org.jsampler.view;  package org.jsampler.view;
23    
24    import java.io.File;
25  import java.io.FilenameFilter;  import java.io.FilenameFilter;
26    
27  import javax.swing.filechooser.FileFilter;  import javax.swing.filechooser.FileFilter;
28    
29  /**  /**
# Line 29  import javax.swing.filechooser.FileFilte Line 31  import javax.swing.filechooser.FileFilte
31   * @author Grigor Iliev   * @author Grigor Iliev
32   */   */
33  public abstract class JSFileFilter extends FileFilter implements FilenameFilter {  public abstract class JSFileFilter extends FileFilter implements FilenameFilter {
34            private final String[] fileExts;
35    
36            public
37            JSFileFilter(String fileExt) {
38                    if(fileExt == null) {
39                            throw new IllegalArgumentException("fileExt must be non-null");
40                    }
41    
42                    fileExts = new String[1];
43                    fileExts[0] = fileExt;
44            }
45    
46            public
47            JSFileFilter(String[] fileExts) {
48                    if(fileExts == null) {
49                            throw new IllegalArgumentException("fileExts must be non-null");
50                    }
51    
52                    if(fileExts.length < 1) {
53                            throw new IllegalArgumentException("fileExts length can't be zero");
54                    }
55    
56                    this.fileExts = fileExts;
57            }
58    
59            /**
60             * Returns <code>true</code> if the specified file is a LSCP script.
61             * The file is recognized by its extension.
62             * @return <code>true</code> if the specified file is a LSCP script;
63             * <code>false</code> otherwise.
64             */
65            public boolean
66            accept(File f) {
67                    if(f.isDirectory()) return true;
68                    return acceptFile(f.getName());
69    
70            }
71    
72            public boolean
73            accept(File dir, String name) {
74                    return acceptFile(name);
75            }
76    
77            /**
78             * Gets the first extension in the list.
79             */
80            public String
81            getExtension() { return fileExts[0]; }
82    
83            protected boolean
84            acceptFile(String fileName) {
85                    boolean b = false;
86                    for(String ext : fileExts) {
87                            b = b || acceptFile(fileName, ext);
88                    }
89                    return b;
90            }
91    
92            private boolean
93            acceptFile(String fileName, String ext) {
94                    int i = fileName.lastIndexOf('.');
95                    if(i == -1) return false;
96                    fileName = fileName.substring(i);
97    
98                    return fileName.equalsIgnoreCase(ext);
99            }
100    
101            public static class Lscp extends JSFileFilter {
102                    public
103                    Lscp() { super(".lscp"); }
104    
105                    /**
106                     * The description of this filter.
107                     * @return The description of this filter: <b>LSCP Script Files (*.lscp)</b>.
108                     */
109                    public String
110                    getDescription() { return "LSCP Script Files (*.lscp)"; }
111            }
112    
113            public static class Text extends JSFileFilter {
114                    public
115                    Text() { super(".txt"); }
116    
117                    public String
118                    getDescription() { return "Text Files (*.txt)"; }
119            }
120    
121            public static class Html extends JSFileFilter {
122                    public
123                    Html() { super(".html"); }
124    
125                    public String
126                    getDescription() { return "Web Pages (*.html)"; }
127            }
128    
129    
130            public static class MidiMaps extends JSFileFilter {
131                    private static final String[] exts = { ".lscp", ".txt", ".html" };
132    
133                    public
134                    MidiMaps() { super(exts); }
135    
136                    public String
137                    getDescription() { return "Midi Instrument Maps"; }
138            }
139  }  }

Legend:
Removed from v.1914  
changed lines
  Added in v.1915

  ViewVC Help
Powered by ViewVC