Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
FileExtensionFilter.java
1 package ij.io;
2 import java.awt.*;
3 import java.awt.image.*;
4 import java.io.*;
5 import java.net.URL;
6 import java.net.*;
7 import java.util.zip.*;
8 import java.util.Locale;
9 import javax.swing.*;
10 import javax.swing.filechooser.*;
11 import ij.*;
12 import ij.gui.*;
13 import ij.process.*;
14 import ij.plugin.frame.*;
15 import ij.text.TextWindow;
16 import ij.util.Java2;
17 import java.util.LinkedList;
18 import java.util.Iterator;
19 
20 public class FileExtensionFilter extends javax.swing.filechooser.FileFilter
21 {
22  private String[] _extensions = null;
23  private String _description = "";
24  private boolean _showExtensions = true;
25 
26  FileExtensionFilter(String description, String[] extensions)
27  {
28  this._description = description;
29  this._extensions = extensions;
30  }
31 
32  FileExtensionFilter(String description, LinkedList extensions)
33  {
34  String[] exts = new String[extensions.size()];
35  int i=0;
36  for (Iterator itr = extensions.iterator(); itr.hasNext(); i++) {
37  exts[i] = (String)itr.next();
38  }
39  this._description = description;
40  this._extensions = exts;
41  }
42 
43 
44  public boolean accept(File f)
45  {
46  if (f.isDirectory()) return true;
47  String extension = f.getPath();
48  extension = extension.substring(extension.lastIndexOf(".")+1).toLowerCase();
49  for (int i=0; i < _extensions.length; i++) {
50  if (extension.equalsIgnoreCase(_extensions[i])) {
51  return true;
52  }
53  }
54  return false;
55  }
56 
57  public String getDescription()
58  {
59  return _description + (_showExtensions ? " (" + _getExtensionList() + ")" : "");
60  }
61 
62  private String _getExtensionList()
63  {
64  StringBuffer sb = new StringBuffer();
65  for (int i=0; i < _extensions.length; i++) {
66  sb.append("*.");
67  sb.append(_extensions[i]);
68  sb.append(", ");
69  }
70  return sb.substring(0, sb.length()-2); // remove the final comma
71  }
72 
73  public String[] getExtensions()
74  {
75  return _extensions;
76  }
77 
78  public void setShowExtensions(boolean b)
79  {
80  _showExtensions = b;
81  }
82 
83 }