new class
authoramwaterhouse <Andrew Waterhouse>
Fri, 18 Mar 2005 16:08:11 +0000 (16:08 +0000)
committeramwaterhouse <Andrew Waterhouse>
Fri, 18 Mar 2005 16:08:11 +0000 (16:08 +0000)
src/jalview/io/JalviewFileFilter.java [new file with mode: 0755]

diff --git a/src/jalview/io/JalviewFileFilter.java b/src/jalview/io/JalviewFileFilter.java
new file mode 100755 (executable)
index 0000000..85ac084
--- /dev/null
@@ -0,0 +1,118 @@
+package jalview.io;\r
+\r
+import javax.swing.filechooser.*;\r
+import java.io.File;\r
+import java.util.*;\r
+\r
+public class JalviewFileFilter extends FileFilter\r
+{\r
+      private static String TYPE_UNKNOWN = "Type Unknown";\r
+      private static String HIDDEN_FILE = "Hidden File";\r
+      public static Hashtable suffixHash = new Hashtable();\r
+\r
+      private Hashtable filters = null;\r
+      private String description = "no description";\r
+      private String fullDescription = "full description";\r
+      private boolean useExtensionsInDescription = true;\r
+\r
+      public JalviewFileFilter() {\r
+          this.filters = new Hashtable();\r
+      }\r
+\r
+      public JalviewFileFilter(String extension) {\r
+          this(extension,null);\r
+      }\r
+\r
+      public JalviewFileFilter(String extension, String description) {\r
+          this();\r
+          if(extension!=null) addExtension(extension);\r
+           if(description!=null) setDescription(description);\r
+      }\r
+\r
+      public JalviewFileFilter(String[] filts) {\r
+          this(filts, null);\r
+      }\r
+\r
+      public JalviewFileFilter(String[] filts, String description) {\r
+          this();\r
+          for (int i = 0; i < filts.length; i++) {\r
+              // add filters one by one\r
+              addExtension(filts[i]);\r
+          }\r
+           if(description!=null) setDescription(description);\r
+      }\r
+\r
+      // takes account of the fact that database is a directory\r
+      public boolean accept(File f) {\r
+          if(f != null) {\r
+              String extension = getExtension(f);\r
+              if(f.isDirectory()) {\r
+                 if(extension == null || extension.equals("")) {\r
+                    return true;\r
+                 } else {\r
+                    if(filters.get(getExtension(f)) != null) {\r
+                       return true;\r
+                    }\r
+                 }\r
+              }\r
+              if(extension != null && filters.get(getExtension(f)) != null) {\r
+                  return true;\r
+              }\r
+          }\r
+          return false;\r
+      }\r
+\r
+       public String getExtension(File f) {\r
+          if(f != null) {\r
+              String filename = f.getName();\r
+              int i = filename.lastIndexOf('.');\r
+              if(i>0 && i<filename.length()-1) {\r
+                  return filename.substring(i+1).toLowerCase();\r
+              };\r
+          }\r
+          return "";\r
+      }\r
+\r
+      public void addExtension(String extension) {\r
+          if(filters == null) {\r
+              filters = new Hashtable(5);\r
+          }\r
+          filters.put(extension.toLowerCase(), this);\r
+          fullDescription = null;\r
+      }\r
+\r
+\r
+      public String getDescription() {\r
+          if(fullDescription == null) {\r
+              if(description == null || isExtensionListInDescription()) {\r
+                   fullDescription = description==null ? "(" : description + " (";\r
+                  // build the description from the extension list\r
+                  Enumeration extensions = filters.keys();\r
+                  if(extensions != null) {\r
+                      fullDescription += "." + (String) extensions.nextElement();\r
+                      while (extensions.hasMoreElements()) {\r
+                          fullDescription += ", " + (String) extensions.nextElement();\r
+                      }\r
+                  }\r
+                  fullDescription += ")";\r
+              } else {\r
+                  fullDescription = description;\r
+              }\r
+          }\r
+          return fullDescription;\r
+      }\r
+\r
+      public void setDescription(String description) {\r
+          this.description = description;\r
+          fullDescription = null;\r
+      }\r
+\r
+      public void setExtensionListInDescription(boolean b) {\r
+          useExtensionsInDescription = b;\r
+          fullDescription = null;\r
+      }\r
+\r
+      public boolean isExtensionListInDescription() {\r
+          return useExtensionsInDescription;\r
+      }\r
+  }\r