content: /********************************************************************************
   * The contents of this file are subject to the GNU General Public License      *
   * (GPL) Version 2 or later (the "License"); you may not use this file except   *
   * in compliance with the License. You may obtain a copy of the License at      *
   * http://www.gnu.org/copyleft/gpl.html                                         *
   *                                                                              *
   * Software distributed under the License is distributed on an "AS IS" basis,   *
   * without warranty of any kind, either expressed or implied. See the License   *
   * for the specific language governing rights and limitations under the         *
   * License.                                                                     *
   *                                                                              *
   * This file was originally developed as part of the software suite that        *
   * supports the book "The Elements of Computing Systems" by Nisan and Schocken, *
   * MIT Press 2005. If you modify the contents of this file, please document and *
   * mark your changes clearly, for the benefit of others.                        *
   ********************************************************************************/
  
  package HackGUI;
  
  import java.io.File;
  import javax.swing.filechooser.FileFilter;
  
  /**
   * A file filter for text files (.txt).
   */
  public class TextFileFilter extends FileFilter {
  
      /**
       * Determines which files this filter should accept.
       */
      public boolean accept(File f) {
          if (f.isDirectory()) {
              return true;
          }
  
          String extension = getExtension(f);
          if (extension != null) {
              if (extension.equals("txt") )
                      return true;
              else {
                  return false;
              }
          }
  
          return false;
      }
  
  
      /*
       * Get the extension of a file.
       */
      public static String getExtension(File f) {
          String ext = null;
          String s = f.getName();
          int i = s.lastIndexOf('.');
  
          if (i > 0 &&  i < s.length() - 1) {
              ext = s.substring(i+1).toLowerCase();
          }
          return ext;
      }
  
      // The description of this filter
      public String getDescription() {
          return "Text Files";
      }
  }
  