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