[ Index ]

PHP Cross Reference of Drupal 6 (yi-drupal)

title

Body

[close]

/sites/all/modules/imagex/postlet/rev165/ -> UploaderFileFilter.java (source)

   1  /*    Copyright (C) 2005 Simon David Rycroft
   2  
   3      This program is free software; you can redistribute it and/or
   4      modify it under the terms of the GNU General Public License
   5      as published by the Free Software Foundation; either version 2
   6      of the License, or (at your option) any later version.
   7  
   8      This program is distributed in the hope that it will be useful,
   9      but WITHOUT ANY WARRANTY; without even the implied warranty of
  10      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11      GNU General Public License for more details.
  12  
  13      You should have received a copy of the GNU General Public License
  14      along with this program; if not, write to the Free Software
  15      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
  16  
  17  import java.io.File;
  18  import java.util.Hashtable;
  19  import java.util.Enumeration;
  20  import javax.swing.filechooser.FileFilter;
  21  
  22  public class UploaderFileFilter extends FileFilter {
  23  
  24      private Hashtable filters = null;
  25      private String description = null;
  26      private String fullDescription = null;
  27      private boolean useExtensionsInDescription = true;
  28  
  29      public UploaderFileFilter() {
  30          this.filters = new Hashtable();
  31      }
  32  
  33      public boolean accept(File f) {
  34          if(f != null) {
  35              if(f.isDirectory()) {
  36                  return true;
  37              }
  38              String extension = getExtension(f);
  39              if(extension != null && filters.get(getExtension(f)) != null) {
  40                  return true;
  41              }
  42          }
  43          return false;
  44      }
  45  
  46      public String getExtension(File f) {
  47          if(f != null) {
  48              String filename = f.getName();
  49              int i = filename.lastIndexOf('.');
  50              if(i>0 && i<filename.length()-1) {
  51                  return filename.substring(i+1).toLowerCase();
  52              }
  53          }
  54          return null;
  55      }
  56  
  57      public void addExtension(String extension) {
  58          if(filters == null) {
  59              filters = new Hashtable(5);
  60          }
  61          filters.put(extension.toLowerCase(), this);
  62          fullDescription = null;
  63      }
  64  
  65      public String getDescription() {
  66          if(fullDescription == null) {
  67              if(description == null || isExtensionListInDescription()) {
  68                  fullDescription = description==null ? "(" : description + " (";
  69                  // build the description from the extension list
  70                  Enumeration extensions = filters.keys();
  71                  if(extensions != null) {
  72                      fullDescription += "." + (String) extensions.nextElement();
  73                      while (extensions.hasMoreElements()) {
  74                          fullDescription += ", ." + (String) extensions.nextElement();
  75                      }
  76                  }
  77                  fullDescription += ")";
  78              } else {
  79                  fullDescription = description;
  80              }
  81          }
  82          return fullDescription;
  83      }
  84  
  85      public void setDescription(String description) {
  86          this.description = description;
  87          fullDescription = null;
  88      }
  89  
  90      public void setExtensionListInDescription(boolean b) {
  91          useExtensionsInDescription = b;
  92          fullDescription = null;
  93      }
  94  
  95      public boolean isExtensionListInDescription() {
  96          return useExtensionsInDescription;
  97      }
  98  }


Generated: Mon Jul 9 18:01:44 2012 Cross-referenced by PHPXref 0.7