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.io.FileFilter; |
46 |
import java.util.Arrays; |
47 |
import java.util.Collections; |
48 |
import java.util.List; |
49 |
import java.util.logging.Level; |
50 |
import java.util.logging.Logger; |
51 |
|
52 |
/** |
53 |
* Support methods for {@link FileFilter file filters} that can be used, for |
54 |
* example, in Open File dialog. <p> See issue #209998. </p> <p> The following |
55 |
* example shows how to implement and register a custom filter. </p> |
56 |
* <pre> |
57 |
* {@literal |
58 |
* |
59 |
* @}{@code ServiceProvider(service=FileFilter.class, path=FileFilterSupport.FILE_FILTER_LOOKUP_PATH) |
60 |
* public class TXTFilter extends FileFilter { |
61 |
* private static final String[] EXTENSIONS = new String[] {".txt"}; |
62 |
* |
63 |
* public boolean accept(File f) { |
64 |
* return FileFilterSupport.accept(f, EXTENSIONS); |
65 |
* } |
66 |
* |
67 |
* public String getDescription() { |
68 |
* return FileFilterSupport.constructFilterDisplayName(<bundle-lookup>, EXTENSIONS); |
69 |
* } |
70 |
* }} |
71 |
* </pre> |
72 |
* |
73 |
* @author jhavlin, jlahoda |
74 |
* @since 7.49 |
75 |
*/ |
76 |
public final class FileFilterSupport { |
77 |
|
78 |
/** |
79 |
* Path to use when registering custom filters. |
80 |
*/ |
81 |
public static final String FILE_FILTER_LOOKUP_PATH = |
82 |
"OpenFileDialogFilter"; //NOI18N |
83 |
/** |
84 |
* The logger. |
85 |
*/ |
86 |
private static final Logger LOG = Logger.getLogger( |
87 |
FileFilterSupport.class.getName()); |
88 |
|
89 |
/** |
90 |
* Hide the default constructor. |
91 |
*/ |
92 |
private FileFilterSupport() { |
93 |
} |
94 |
|
95 |
/** |
96 |
* Construct description for {@link FileFilter} that accepts files with |
97 |
* specified extension. |
98 |
* |
99 |
* @param displayName Human readable display name (e.g. "HTML files") |
100 |
* @param extensions List of allowed extensions (e.g. {".htm", ".html"}). |
101 |
* |
102 |
* @return Display name (description) for the filter. |
103 |
*/ |
104 |
public static String constructFilterDisplayName(String displayName, |
105 |
String... extensions) { |
106 |
StringBuilder sb = new StringBuilder(displayName); |
107 |
sb.append(" "); //NOI18N |
108 |
sb.append(Arrays.asList(extensions).toString()); |
109 |
return sb.toString(); |
110 |
} |
111 |
|
112 |
/** |
113 |
* Construct description for {@link FileFilter} that accepts files of |
114 |
* specified MIME type. |
115 |
* |
116 |
* @param mimeType MIME type of the file filter. |
117 |
* |
118 |
* @return Display name (description) for the filter. |
119 |
* |
120 |
*/ |
121 |
public static String constructFilterDisplayName(String mimeType) { |
122 |
return constructFilterDisplayName(getMimeDisplayName(mimeType), |
123 |
toExtArray(FileUtil.getMIMETypeExtensions(mimeType))); |
124 |
} |
125 |
|
126 |
/** |
127 |
* Check whether passed file is accepted by filter for specified list of |
128 |
* extensions. |
129 |
* |
130 |
* @param file File whose extension is checked. |
131 |
* @param extensions List of allowed extensions. |
132 |
* |
133 |
* @return True if the file ends with one of allowed extensions, false |
134 |
* otherwise. |
135 |
* |
136 |
* @see FileFilterSupport |
137 |
*/ |
138 |
public static boolean accept(File file, String... extensions) { |
139 |
if (file != null) { |
140 |
if (file.isDirectory()) { |
141 |
return true; |
142 |
} |
143 |
for (String ext : extensions) { |
144 |
if (compareSuffixes(file.getName(), ext)) { |
145 |
return true; |
146 |
} |
147 |
} |
148 |
} |
149 |
return false; |
150 |
} |
151 |
|
152 |
/** |
153 |
* Check whether passed file is accepted by filter for specified MIME type. |
154 |
* |
155 |
* @param file File that is checked for a MIME type. |
156 |
* @param mimeType Accepted MIME type. |
157 |
* |
158 |
* @return True if file {@code file} is of MIME type {@code mimeType}, false |
159 |
* otherwise. |
160 |
* |
161 |
* @see FileFilterSupport |
162 |
*/ |
163 |
public static boolean accept(File file, String mimeType) { |
164 |
List<String> exts = FileUtil.getMIMETypeExtensions(mimeType); |
165 |
return accept(file, toExtArray(exts)); |
166 |
} |
167 |
|
168 |
/** |
169 |
* Get display name for a MIME type. |
170 |
* |
171 |
* @param mimeType MIME type (e.g. "java"). |
172 |
* @return Display name for the MIME type (e.g. "Java Files"), or the MIME |
173 |
* type itself if no display name has been set. |
174 |
*/ |
175 |
private static String getMimeDisplayName(String mimeType) { |
176 |
try { |
177 |
FileObject factoriesFO = FileUtil.getConfigFile( |
178 |
"Loaders/" + mimeType + "/Factories"); //NOI18N |
179 |
if (factoriesFO != null) { |
180 |
FileObject[] children = factoriesFO.getChildren(); |
181 |
for (FileObject child : children) { |
182 |
String childName = child.getNameExt(); |
183 |
String displayName = FileUtil.getConfigRoot(). |
184 |
getFileSystem().getStatus(). |
185 |
annotateName(childName, |
186 |
Collections.singleton(child)); |
187 |
if (!childName.equals(displayName)) { |
188 |
return displayName; |
189 |
} |
190 |
} |
191 |
} |
192 |
} catch (Exception e) { |
193 |
LOG.log(Level.WARNING, null, e); |
194 |
} |
195 |
return mimeType; |
196 |
} |
197 |
|
198 |
/** |
199 |
* Check whether the given filename has required suffex. |
200 |
*/ |
201 |
private static boolean compareSuffixes(String fileName, String suffix) { |
202 |
return fileName.toUpperCase().endsWith(suffix.toUpperCase()); |
203 |
} |
204 |
|
205 |
/** |
206 |
* Convert a list of strings to a string array. |
207 |
* |
208 |
* @param listOfStrings List of extensions, without starting perios (e.g. |
209 |
* "txt", "java"). |
210 |
* @return Array of extensions, with starting period (e.g. ".txt", ".java"). |
211 |
*/ |
212 |
private static String[] toExtArray(List<String> listOfStrings) { |
213 |
String[] array = new String[listOfStrings.size()]; |
214 |
int index = 0; |
215 |
for (String ext : listOfStrings) { |
216 |
array[index++] = "." + ext; //NOI18N |
217 |
} |
218 |
return array; |
219 |
} |
220 |
} |