3
* This file is part of jReality. jReality is open source software, made
4
* available under a BSD license:
6
* Copyright (c) 2003-2006, jReality Group: Charles Gunn, Tim Hoffmann, Markus
7
* Schmies, Steffen Weissmann.
11
* Redistribution and use in source and binary forms, with or without
12
* modification, are permitted provided that the following conditions are met:
14
* - Redistributions of source code must retain the above copyright notice, this
15
* list of conditions and the following disclaimer.
17
* - Redistributions in binary form must reproduce the above copyright notice,
18
* this list of conditions and the following disclaimer in the documentation
19
* and/or other materials provided with the distribution.
21
* - Neither the name of jReality nor the names of its contributors nor the
22
* names of their associated organizations may be used to endorse or promote
23
* products derived from this software without specific prior written
26
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
27
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
30
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36
* POSSIBILITY OF SUCH DAMAGE.
41
package opencog.spacetime.ui.viewerapp;
44
import java.util.HashSet;
45
import java.util.LinkedHashSet;
48
import javax.imageio.ImageIO;
50
import opencog.spacetime.reader.Readers;
57
public class FileFilter extends javax.swing.filechooser.FileFilter {
59
private HashSet<String> extensions;
60
private String preferred;
61
private String description;
62
private boolean showExtensionList = true;
70
public FileFilter(String description, String... extensions) {
71
setDescription(description);
72
this.extensions = new LinkedHashSet<String>();
73
for (int i = 0; i < extensions.length; i++)
74
addExtension(extensions[i]);
79
public boolean accept(File f) {
80
if (f == null) return false;
81
if(f.isDirectory()) return true;
83
String extension = getFileExtension(f);
84
return (extension != null &&
85
(extensions.contains(extension) ||
86
extensions.contains(extension.toLowerCase()) ||
87
extensions.contains(extension.toUpperCase())) );
92
public String getDescription() {
94
if (!showExtensionList || extensions.isEmpty())
97
String extensionList = null;
98
for (String extension : extensions) {
99
if (extensionList == null) extensionList = " (*."+extension;
100
else extensionList += ", *."+extension;
102
extensionList += ")";
104
return description + extensionList;
108
public void setDescription(String description) {
109
this.description = description;
113
public void addExtension(String extension) {
114
extensions.add(extension);
118
public void setPreferredExtension(String preferred) {
119
addExtension(preferred);
120
this.preferred = preferred;
124
public String getPreferredExtension() {
125
if (preferred == null && !extensions.isEmpty())
126
return extensions.iterator().next(); //return first in set
132
public static String getFileExtension(File f) {
134
String filename = f.getName();
135
int i = filename.lastIndexOf('.');
136
if(i>0 && i<filename.length()-1)
137
return filename.substring(i+1).toLowerCase();
143
public boolean isShowExtensionList() {
144
return showExtensionList;
148
public void setShowExtensionList(boolean showExtensionList) {
149
this.showExtensionList = showExtensionList;
153
public static FileFilter createJRealityDataFilter() {
154
FileFilter f = new FileFilter("jReality 3D data files") {
156
public boolean accept(File f) {
157
if (f.isDirectory()) return true;
158
String filename = f.getName().toLowerCase();
159
return (Readers.findFormat(filename) != null);
162
f.setShowExtensionList(false);
168
public static javax.swing.filechooser.FileFilter[] createImageWriterFilters() {
170
//get existing writer formats
171
String writerFormats[] = ImageIO.getWriterFormatNames();
172
//usually [bmp, jpg, jpeg, png, wbmp]
173
String[] known = new String[]{"bmp","jpg","jpeg","png", "wbmp","tiff","tif"};
174
//get remaining formats ignoring case
175
Set<String> special = new HashSet<String>();
176
outer: for (int i = 0; i < writerFormats.length; i++) {
177
final String ext = writerFormats[i].toLowerCase();
178
for (int j = 0; j < known.length; j++) {
179
if (known[j].equals(ext)) continue outer;
184
Set<FileFilter> filters = new LinkedHashSet<FileFilter>();
186
FileFilter general = new FileFilter("All Image Files", known);
187
for (String s : special) general.addExtension(s);
188
general.setPreferredExtension("png");
189
filters.add(general);
191
filters.add(new FileFilter("PNG Image", "png"));
192
filters.add(new FileFilter("JPEG Image", "jpg", "jpeg"));
193
//add tiff filter if writer exists
194
try { Class.forName("javax.media.jai.JAI");
195
filters.add(new FileFilter("TIFF Image", "tiff", "tif"));
196
} catch (ClassNotFoundException e) {}
197
filters.add(new FileFilter("BMP Image", "bmp"));
198
filters.add(new FileFilter("Wireless BMP Image", "wbmp"));
199
//add filters for special writer formats
200
for (String s : special)
201
filters.add(new FileFilter(s.toUpperCase()+" Image", s));
204
FileFilter[] ff = new FileFilter[filters.size()];
205
return filters.toArray(ff);
209
public static javax.swing.filechooser.FileFilter[] createImageReaderFilters() {
211
//get existing reader formats
212
String readerFormats[] = ImageIO.getReaderFormatNames();
213
//usually [bmp, gif, jpg, jpeg, png, wbmp]
214
String[] known = new String[]{"bmp", "gif", "jpg","jpeg","png", "wbmp"};
215
//get remaining formats ignoring case
216
Set<String> special = new HashSet<String>();
217
outer: for (int i = 0; i < readerFormats.length; i++) {
218
final String ext = readerFormats[i].toLowerCase();
219
for (int j = 0; j < known.length; j++) {
220
if (known[j].equals(ext)) continue outer;
225
Set<FileFilter> filters = new LinkedHashSet<FileFilter>();
227
FileFilter general = new FileFilter("All Image Files", known);
228
for (String s : special) general.addExtension(s);
229
general.setPreferredExtension("png");
230
filters.add(general);
232
filters.add(new FileFilter("PNG Image", "png"));
233
filters.add(new FileFilter("JPEG Image", "jpg", "jpeg"));
234
filters.add(new FileFilter("GIF Image", "gif"));
235
// //add tiff filter if writer exists
236
// try { Class.forName("javax.media.jai.JAI");
237
// filters.add(new FileFilter("TIFF Image", "tiff", "tif"));
238
// } catch (ClassNotFoundException e) {}
239
filters.add(new FileFilter("BMP Image", "bmp"));
240
filters.add(new FileFilter("Wireless BMP Image", "wbmp"));
241
//add filters for special writer formats
242
for (String s : special)
243
filters.add(new FileFilter(s.toUpperCase()+" Image", s));
246
FileFilter[] ff = new FileFilter[filters.size()];
247
return filters.toArray(ff);
b'\\ No newline at end of file'