~seh999/jcog/proto3

« back to all changes in this revision

Viewing changes to spacetime/src.ui/opencog/spacetime/ui/viewerapp/FileFilter.java

  • Committer: SeH
  • Date: 2009-09-19 22:59:48 UTC
  • Revision ID: seh999@gmail.com-20090919225948-q3ab80xa57i74mm6
start of major jReality refactoring

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/**
2
 
 *
3
 
 * This file is part of jReality. jReality is open source software, made
4
 
 * available under a BSD license:
5
 
 *
6
 
 * Copyright (c) 2003-2006, jReality Group: Charles Gunn, Tim Hoffmann, Markus
7
 
 * Schmies, Steffen Weissmann.
8
 
 *
9
 
 * All rights reserved.
10
 
 *
11
 
 * Redistribution and use in source and binary forms, with or without
12
 
 * modification, are permitted provided that the following conditions are met:
13
 
 *
14
 
 * - Redistributions of source code must retain the above copyright notice, this
15
 
 *   list of conditions and the following disclaimer.
16
 
 *
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.
20
 
 *
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
24
 
 *   permission.
25
 
 *
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.
37
 
 *
38
 
 */
39
 
 
40
 
 
41
 
package opencog.spacetime.ui.viewerapp;
42
 
 
43
 
import java.io.File;
44
 
import java.util.HashSet;
45
 
import java.util.LinkedHashSet;
46
 
import java.util.Set;
47
 
 
48
 
import javax.imageio.ImageIO;
49
 
 
50
 
import opencog.spacetime.reader.Readers;
51
 
 
52
 
 
53
 
 
54
 
/**
55
 
 * @author msommer
56
 
 */
57
 
public class FileFilter extends javax.swing.filechooser.FileFilter {
58
 
  
59
 
  private HashSet<String> extensions;
60
 
  private String preferred;
61
 
  private String description;
62
 
  private boolean showExtensionList = true;
63
 
  
64
 
 
65
 
  public FileFilter() {
66
 
    this(null);
67
 
  }
68
 
 
69
 
  
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]);
75
 
  }
76
 
  
77
 
 
78
 
  @Override
79
 
  public boolean accept(File f) {
80
 
    if (f == null) return false;
81
 
    if(f.isDirectory()) return true;
82
 
      
83
 
    String extension = getFileExtension(f);
84
 
    return (extension != null && 
85
 
        (extensions.contains(extension) ||
86
 
            extensions.contains(extension.toLowerCase()) ||
87
 
            extensions.contains(extension.toUpperCase())) );
88
 
  }
89
 
 
90
 
 
91
 
  @Override
92
 
  public String getDescription() {
93
 
    
94
 
    if (!showExtensionList || extensions.isEmpty()) 
95
 
      return description;
96
 
    
97
 
    String extensionList = null;
98
 
    for (String extension : extensions) {
99
 
      if (extensionList == null) extensionList = " (*."+extension;
100
 
      else extensionList += ", *."+extension;
101
 
    }
102
 
    extensionList += ")";
103
 
    
104
 
    return description + extensionList;
105
 
  }
106
 
  
107
 
  
108
 
  public void setDescription(String description) {
109
 
    this.description = description;
110
 
  }
111
 
  
112
 
  
113
 
  public void addExtension(String extension) {
114
 
    extensions.add(extension);
115
 
  }
116
 
 
117
 
  
118
 
  public void setPreferredExtension(String preferred) {
119
 
    addExtension(preferred);
120
 
    this.preferred = preferred;
121
 
  }
122
 
  
123
 
  
124
 
  public String getPreferredExtension() {
125
 
    if (preferred == null && !extensions.isEmpty())
126
 
      return extensions.iterator().next();  //return first in set
127
 
 
128
 
    return preferred;
129
 
  }
130
 
  
131
 
  
132
 
  public static String getFileExtension(File f) {
133
 
    if (f != null) {
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();
138
 
    }
139
 
    return null;
140
 
  }
141
 
  
142
 
 
143
 
  public boolean isShowExtensionList() {
144
 
    return showExtensionList;
145
 
  }
146
 
 
147
 
 
148
 
  public void setShowExtensionList(boolean showExtensionList) {
149
 
    this.showExtensionList = showExtensionList;
150
 
  }
151
 
  
152
 
  
153
 
  public static FileFilter createJRealityDataFilter() {
154
 
          FileFilter f = new FileFilter("jReality 3D data files") {
155
 
                  @Override
156
 
                  public boolean accept(File f) {
157
 
                          if (f.isDirectory()) return true;
158
 
                          String filename = f.getName().toLowerCase();
159
 
                          return (Readers.findFormat(filename) != null);
160
 
                  }
161
 
          };
162
 
          f.setShowExtensionList(false);
163
 
          
164
 
          return f;
165
 
  }
166
 
  
167
 
  
168
 
  public static javax.swing.filechooser.FileFilter[] createImageWriterFilters() {
169
 
        
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;
180
 
                        }
181
 
                        special.add(ext);
182
 
                }
183
 
 
184
 
                Set<FileFilter> filters = new LinkedHashSet<FileFilter>();
185
 
                //add general filter
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);
190
 
                //add known filter
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));
202
 
 
203
 
                //convert to array
204
 
                FileFilter[] ff = new FileFilter[filters.size()];
205
 
                return filters.toArray(ff);
206
 
  }
207
 
  
208
 
  
209
 
  public static javax.swing.filechooser.FileFilter[] createImageReaderFilters() {
210
 
        
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;
221
 
                        }
222
 
                        special.add(ext);
223
 
                }
224
 
 
225
 
                Set<FileFilter> filters = new LinkedHashSet<FileFilter>();
226
 
                //add general filter
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);
231
 
                //add known filter
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));
244
 
 
245
 
                //convert to array
246
 
                FileFilter[] ff = new FileFilter[filters.size()];
247
 
                return filters.toArray(ff);
248
 
  }
249
 
  
250
 
}
 
 
b'\\ No newline at end of file'