1 |
/* |
2 |
* JSampler - a java front-end for LinuxSampler |
3 |
* |
4 |
* Copyright (C) 2009 Grigor Iliev <grigor@grigoriliev.com> |
5 |
* |
6 |
* This file is part of JSampler. |
7 |
* |
8 |
* JSampler is free software; you can redistribute it and/or modify |
9 |
* it under the terms of the GNU General Public License version 2 |
10 |
* as published by the Free Software Foundation. |
11 |
* |
12 |
* JSampler is distributed in the hope that it will be useful, |
13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 |
* GNU General Public License for more details. |
16 |
* |
17 |
* You should have received a copy of the GNU General Public License |
18 |
* along with JSampler; if not, write to the Free Software |
19 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
20 |
* MA 02111-1307 USA |
21 |
*/ |
22 |
package org.jsampler.view; |
23 |
|
24 |
import java.io.File; |
25 |
import java.io.FilenameFilter; |
26 |
|
27 |
import javax.swing.filechooser.FileFilter; |
28 |
|
29 |
/** |
30 |
* |
31 |
* @author Grigor Iliev |
32 |
*/ |
33 |
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 |
public static class Rgd extends JSFileFilter { |
130 |
public |
131 |
Rgd() { super(".rgd"); } |
132 |
|
133 |
public String |
134 |
getDescription() { return "Rosegarden Device File (*.rgd)"; } |
135 |
} |
136 |
|
137 |
|
138 |
public static class MidiMaps extends JSFileFilter { |
139 |
private static final String[] exts = { ".lscp", ".txt", ".html", "rgd" }; |
140 |
|
141 |
public |
142 |
MidiMaps() { super(exts); } |
143 |
|
144 |
public String |
145 |
getDescription() { return "Midi Instrument Maps"; } |
146 |
} |
147 |
} |