Line 0
Link Here
|
|
|
1 |
/* |
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
3 |
* |
4 |
* Copyright 2012 Oracle and/or its affiliates. All rights reserved. |
5 |
* |
6 |
* Oracle and Java are registered trademarks of Oracle and/or its affiliates. |
7 |
* Other names may be trademarks of their respective owners. |
8 |
* |
9 |
* The contents of this file are subject to the terms of either the GNU |
10 |
* General Public License Version 2 only ("GPL") or the Common |
11 |
* Development and Distribution License("CDDL") (collectively, the |
12 |
* "License"). You may not use this file except in compliance with the |
13 |
* License. You can obtain a copy of the License at |
14 |
* http://www.netbeans.org/cddl-gplv2.html |
15 |
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the |
16 |
* specific language governing permissions and limitations under the |
17 |
* License. When distributing the software, include this License Header |
18 |
* Notice in each file and include the License file at |
19 |
* nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this |
20 |
* particular file as subject to the "Classpath" exception as provided |
21 |
* by Oracle in the GPL Version 2 section of the License file that |
22 |
* accompanied this code. If applicable, add the following below the |
23 |
* License Header, with the fields enclosed by brackets [] replaced by |
24 |
* your own identifying information: |
25 |
* "Portions Copyrighted [year] [name of copyright owner]" |
26 |
* |
27 |
* If you wish your version of this file to be governed by only the CDDL |
28 |
* or only the GPL Version 2, indicate your decision by adding |
29 |
* "[Contributor] elects to include this software in this distribution |
30 |
* under the [CDDL or GPL Version 2] license." If you do not indicate a |
31 |
* single choice of license, a recipient has the option to distribute |
32 |
* your version of this file under either the CDDL, the GPL Version 2 or |
33 |
* to extend the choice of license to its licensees as provided above. |
34 |
* However, if you add GPL Version 2 code and therefore, elected the GPL |
35 |
* Version 2 license, then the option applies only if the new code is |
36 |
* made subject to such option by the copyright holder. |
37 |
* |
38 |
* Contributor(s): |
39 |
* |
40 |
* Portions Copyrighted 2012 Sun Microsystems, Inc. |
41 |
*/ |
42 |
package org.openide.filesystems; |
43 |
|
44 |
import java.io.File; |
45 |
import java.util.Arrays; |
46 |
import java.util.Collections; |
47 |
import java.util.List; |
48 |
import java.util.logging.Level; |
49 |
import java.util.logging.Logger; |
50 |
import javax.swing.filechooser.FileFilter; |
51 |
|
52 |
/** |
53 |
* Class for registering custom {@link FileFilter file filters} that will be |
54 |
* used in Open File dialog. |
55 |
* <p>Example implementation:</p> |
56 |
* <pre> |
57 |
* {@literal @}{@code ServiceProvider(service=FileFilterFactory.class) |
58 |
* public class TXTFilterFactory extends FileFilterFactory { |
59 |
* |
60 |
* public FileFilter createFileFilter() { |
61 |
* return Support.createFilterForExtensions(<bundle-lookup>, ".txt"); //NOI18N |
62 |
* // or return Support.createFilterForMimeType("text/plain"); //NOI18N |
63 |
* } |
64 |
* }} |
65 |
* </pre> |
66 |
* |
67 |
* @author jhavlin, jlahoda |
68 |
* @since 7.64 |
69 |
*/ |
70 |
public interface FileFilterFactory { |
71 |
|
72 |
/** |
73 |
* Create a new instance of {@link FileFilter}. You should use support |
74 |
* method {@link Support#createFilterForMimeType(String)} or |
75 |
* {@link Support#createFilterForExtensions(String, String[])} to ensure |
76 |
* consistent behavior and appearance. |
77 |
*/ |
78 |
public FileFilter createFileFilter(); |
79 |
|
80 |
/** |
81 |
* Support methods for creating custom file filters. |
82 |
*/ |
83 |
public static class Support { |
84 |
|
85 |
/** |
86 |
* The logger. |
87 |
*/ |
88 |
private static final Logger LOG = Logger.getLogger( |
89 |
FileFilterFactory.class.getName()); |
90 |
|
91 |
/** |
92 |
* Hide the default constructor. |
93 |
*/ |
94 |
private Support() { |
95 |
} |
96 |
|
97 |
/** |
98 |
* Create file filter that accepts files of specified MIME type. |
99 |
* |
100 |
* @param mimeType MIME type of accepted files (e.g. "text/plain"). |
101 |
*/ |
102 |
public static FileFilter createFilterForMimeType(String mimeType) { |
103 |
return new MimeFilter(mimeType); |
104 |
} |
105 |
|
106 |
/** |
107 |
* Create file filter that accepts files with specified extensions. |
108 |
* |
109 |
* @param displayName Display name of the filter (e.g. "Text Files"). |
110 |
* @param allowedExtensions Array of allowed extensions (e.g. {".txt", |
111 |
* ".log"}) |
112 |
*/ |
113 |
public static FileFilter createFilterForExtensions(String displayName, |
114 |
String... allowedExtensions) { |
115 |
return new ExtensionFilter(displayName, allowedExtensions); |
116 |
} |
117 |
|
118 |
/** |
119 |
* Construct description for {@link FileFilter} that accepts files with |
120 |
* specified extension. |
121 |
* |
122 |
* @param displayName Human readable display name (e.g. "HTML files") |
123 |
* @param extensions List of allowed extensions (e.g. {".htm", |
124 |
* ".html"}). |
125 |
* |
126 |
* @return Display name (description) for the filter. |
127 |
*/ |
128 |
public static String constructFilterDisplayName(String displayName, |
129 |
String... extensions) { |
130 |
StringBuilder sb = new StringBuilder(displayName); |
131 |
sb.append(" "); //NOI18N |
132 |
sb.append(Arrays.asList(extensions).toString()); |
133 |
return sb.toString(); |
134 |
} |
135 |
|
136 |
/** |
137 |
* Construct description for {@link FileFilter} that accepts files of |
138 |
* specified MIME type. |
139 |
* |
140 |
* @param mimeType MIME type of the file filter. |
141 |
* |
142 |
* @return Display name (description) for the filter. |
143 |
* |
144 |
*/ |
145 |
public static String constructFilterDisplayName(String mimeType) { |
146 |
return constructFilterDisplayName(getMimeDisplayName(mimeType), |
147 |
toExtArray(FileUtil.getMIMETypeExtensions(mimeType))); |
148 |
} |
149 |
|
150 |
/** |
151 |
* Check whether passed file is accepted by filter for specified list of |
152 |
* extensions. |
153 |
* |
154 |
* @param file File whose extension is checked. |
155 |
* @param extensions List of allowed extensions. |
156 |
* |
157 |
* @return True if the file ends with one of allowed extensions, false |
158 |
* otherwise. |
159 |
* |
160 |
* @see FileFilterSupport |
161 |
*/ |
162 |
public static boolean accept(File file, String... extensions) { |
163 |
if (file != null) { |
164 |
if (file.isDirectory()) { |
165 |
return true; |
166 |
} |
167 |
for (String ext : extensions) { |
168 |
if (compareSuffixes(file.getName(), ext)) { |
169 |
return true; |
170 |
} |
171 |
} |
172 |
} |
173 |
return false; |
174 |
} |
175 |
|
176 |
/** |
177 |
* Check whether passed file is accepted by filter for specified MIME |
178 |
* type. |
179 |
* |
180 |
* @param file File that is checked for a MIME type. |
181 |
* @param mimeType Accepted MIME type. |
182 |
* |
183 |
* @return True if file {@code file} is of MIME type {@code mimeType}, |
184 |
* false otherwise. |
185 |
* |
186 |
* @see FileFilterSupport |
187 |
*/ |
188 |
public static boolean accept(File file, String mimeType) { |
189 |
List<String> exts = FileUtil.getMIMETypeExtensions(mimeType); |
190 |
return accept(file, toExtArray(exts)); |
191 |
} |
192 |
|
193 |
/** |
194 |
* Get display name for a MIME type. |
195 |
* |
196 |
* @param mimeType MIME type (e.g. "java"). |
197 |
* @return Display name for the MIME type (e.g. "Java Files"), or the |
198 |
* MIME type itself if no display name has been set. |
199 |
*/ |
200 |
private static String getMimeDisplayName(String mimeType) { |
201 |
try { |
202 |
FileObject factoriesFO = FileUtil.getConfigFile( |
203 |
"Loaders/" + mimeType + "/Factories"); //NOI18N |
204 |
if (factoriesFO != null) { |
205 |
FileObject[] children = factoriesFO.getChildren(); |
206 |
for (FileObject child : children) { |
207 |
String childName = child.getNameExt(); |
208 |
String displayName = FileUtil.getConfigRoot(). |
209 |
getFileSystem().getStatus(). |
210 |
annotateName(childName, |
211 |
Collections.singleton(child)); |
212 |
if (!childName.equals(displayName)) { |
213 |
return displayName; |
214 |
} |
215 |
} |
216 |
} |
217 |
} catch (Exception e) { |
218 |
LOG.log(Level.WARNING, null, e); |
219 |
} |
220 |
return mimeType; |
221 |
} |
222 |
|
223 |
/** |
224 |
* Check whether the given filename has required suffex. |
225 |
*/ |
226 |
private static boolean compareSuffixes(String fileName, String suffix) { |
227 |
return fileName.toUpperCase().endsWith(suffix.toUpperCase()); |
228 |
} |
229 |
|
230 |
/** |
231 |
* Convert a list of strings to a string array. |
232 |
* |
233 |
* @param listOfStrings List of extensions, without starting perios |
234 |
* (e.g. "txt", "java"). |
235 |
* @return Array of extensions, with starting period (e.g. ".txt", |
236 |
* ".java"). |
237 |
*/ |
238 |
private static String[] toExtArray(List<String> listOfStrings) { |
239 |
String[] array = new String[listOfStrings.size()]; |
240 |
int index = 0; |
241 |
for (String ext : listOfStrings) { |
242 |
array[index++] = "." + ext; //NOI18N |
243 |
} |
244 |
return array; |
245 |
} |
246 |
|
247 |
/** |
248 |
* Type of filters returned by {@link #createFilterForMimeType(String)}. |
249 |
*/ |
250 |
private static class MimeFilter extends FileFilter { |
251 |
|
252 |
private String mimeType; |
253 |
|
254 |
public MimeFilter(String mimeType) { |
255 |
this.mimeType = mimeType; |
256 |
} |
257 |
|
258 |
@Override |
259 |
public boolean accept(File f) { |
260 |
return accept(f, mimeType); |
261 |
} |
262 |
|
263 |
@Override |
264 |
public String getDescription() { |
265 |
return constructFilterDisplayName(mimeType); |
266 |
} |
267 |
} |
268 |
|
269 |
/** |
270 |
* Type of filters returned by |
271 |
* {@link #createFilterForExtensions(String, String[])}. |
272 |
*/ |
273 |
private static class ExtensionFilter extends FileFilter { |
274 |
|
275 |
private String displayName; |
276 |
private String[] extensions; |
277 |
|
278 |
public ExtensionFilter(String displayName, String[] extensions) { |
279 |
this.displayName = displayName; |
280 |
this.extensions = extensions; |
281 |
} |
282 |
|
283 |
@Override |
284 |
public boolean accept(File f) { |
285 |
return accept(f, extensions); |
286 |
} |
287 |
|
288 |
@Override |
289 |
public String getDescription() { |
290 |
return constructFilterDisplayName(displayName, extensions); |
291 |
} |
292 |
} |
293 |
} |
294 |
} |