001 /* ======================================================================== 002 * JCommon : a free general purpose class library for the Java(tm) platform 003 * ======================================================================== 004 * 005 * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors. 006 * 007 * Project Info: http://www.jfree.org/jcommon/index.html 008 * 009 * This library is free software; you can redistribute it and/or modify it 010 * under the terms of the GNU Lesser General Public License as published by 011 * the Free Software Foundation; either version 2.1 of the License, or 012 * (at your option) any later version. 013 * 014 * This library is distributed in the hope that it will be useful, but 015 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 016 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 017 * License for more details. 018 * 019 * You should have received a copy of the GNU Lesser General Public 020 * License along with this library; if not, write to the Free Software 021 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 022 * USA. 023 * 024 * [Java is a trademark or registered trademark of Sun Microsystems, Inc. 025 * in the United States and other countries.] 026 * 027 * ------------------------ 028 * ExtensionFileFilter.java 029 * ------------------------ 030 * (C) Copyright 2000-2004, by Object Refinery Limited. 031 * 032 * Original Author: David Gilbert (for Object Refinery Limited); 033 * Contributor(s): -; 034 * 035 * $Id: ExtensionFileFilter.java,v 1.4 2005/11/16 15:58:41 taqua Exp $ 036 * 037 * Changes (from 26-Oct-2001) 038 * -------------------------- 039 * 26-Oct-2001 : Changed package to com.jrefinery.ui.* (DG); 040 * 26-Jun-2002 : Updated imports (DG); 041 * 14-Oct-2002 : Fixed errors reported by Checkstyle (DG); 042 * 043 */ 044 package org.jfree.ui; 045 046 import java.io.File; 047 048 import javax.swing.filechooser.FileFilter; 049 050 /** 051 * A filter for JFileChooser that filters files by extension. 052 * 053 * @author David Gilbert 054 */ 055 public class ExtensionFileFilter extends FileFilter { 056 057 /** A description for the file type. */ 058 private String description; 059 060 /** The extension (for example, "png" for *.png files). */ 061 private String extension; 062 063 /** 064 * Standard constructor. 065 * 066 * @param description a description of the file type; 067 * @param extension the file extension; 068 */ 069 public ExtensionFileFilter(final String description, final String extension) { 070 this.description = description; 071 this.extension = extension; 072 } 073 074 /** 075 * Returns true if the file ends with the specified extension. 076 * 077 * @param file the file to test. 078 * 079 * @return A boolean that indicates whether or not the file is accepted by the filter. 080 */ 081 public boolean accept(final File file) { 082 083 if (file.isDirectory()) { 084 return true; 085 } 086 087 final String name = file.getName().toLowerCase(); 088 if (name.endsWith(this.extension)) { 089 return true; 090 } 091 else { 092 return false; 093 } 094 095 } 096 097 /** 098 * Returns the description of the filter. 099 * 100 * @return a description of the filter. 101 */ 102 public String getDescription() { 103 return this.description; 104 } 105 106 }