- package org.jr.swing.filter;
-
- /**
- * Copyright: Copyright (c) 2002-2004
- * Company: JavaResearch(http://www.javaresearch.org)
- * 最后更新日期:2003年4月2日
- * @author Cherami
- */
-
- import java.io.*;
-
- import org.jr.util.*;
-
- /**
- * 扩展名文件过滤器。
- * <b>此类在0.6版本的时候方式重大变化,通过继承自CombineFileFilter实现了双重功能。</b>
- * @since 0.4
- */
-
- public class FileTypeFilter
- extends CombineFileFilter{
- String[] suffixList;
- String description;
- boolean caseSensitive = false;
- /**
- * 使用指定的扩展名构造一个FileTypeFilter。
- * @param suffix 扩展名
- * @since 0.5
- */
- public FileTypeFilter(String suffix) {
- this(suffix,SWING);
- }
-
- /**
- * 使用指定的扩展名和类型构造一个FileTypeFilter。
- * @param suffix 扩展名
- * @param type 过滤器类型
- * @since 0.6
- */
- public FileTypeFilter(String suffix,int type) {
- super(type);
- suffixList = new String[1];
- suffixList[0] = suffix;
- }
- /**
- * 使用指定的扩展名数组构造一个FileTypeFilter。
- * @param suffixList 扩展名数组
- * @since 0.4
- */
- public FileTypeFilter(String[] suffixList) {
- this(suffixList,SWING);
- }
-
- /**
- * 使用指定的扩展名数组构造一个FileTypeFilter。
- * @param suffixList 扩展名数组
- * @param type 过滤器类型
- * @since 0.6
- */
- public FileTypeFilter(String[] suffixList,int type) {
- super(type);
- this.suffixList = suffixList;
- }
-
- /**
- * 使用指定的扩展名数组构造一个FileTypeFilter。
- * @param suffixList 扩展名数组
- * @param description 过滤器的描述文本
- * @since 0.4
- */
- public FileTypeFilter(String[] suffixList, String description) {
- super(SWING);
- this.suffixList = suffixList;
- this.description = description;
- }
-
- /**
- * 使用指定的扩展名数组和是否大小写敏感构造一个FileTypeFilter。
- * @param suffixList 扩展名数组
- * @param caseSensitive 是否大小写敏感
- * @since 0.4
- */
- public FileTypeFilter(String[] suffixList, boolean caseSensitive) {
- this(suffixList,caseSensitive,SWING);
- }
-
- /**
- * 使用指定的扩展名数组和是否大小写敏感构造一个FileTypeFilter。
- * @param suffixList 扩展名数组
- * @param caseSensitive 是否大小写敏感
- * @param type 过滤器类型
- * @since 0.6
- */
- public FileTypeFilter(String[] suffixList, boolean caseSensitive,int type) {
- super(type);
- this.suffixList = suffixList;
- this.caseSensitive = caseSensitive;
- }
-
- /**
- * 使用指定的扩展名数组和是否大小写敏感构造一个FileTypeFilter。
- * @param suffixList 扩展名数组
- * @param caseSensitive 是否大小写敏感
- * @param description 过滤器的描述文本
- * @since 0.4
- */
- public FileTypeFilter(String[] suffixList, boolean caseSensitive,
- String description) {
- super(SWING);
- this.suffixList = suffixList;
- this.caseSensitive = caseSensitive;
- this.description = description;
- }
-
- /**
- * 设置是否大小写敏感。
- * @param caseSensitive 是否大小写敏感
- * @since 0.4
- */
- public void setCaseSensitive(boolean caseSensitive) {
- this.caseSensitive = caseSensitive;
- }
-
- /**
- * 是否大小写敏感
- * @return 大小写敏感时返回true,否则返回false
- * @since 0.4
- */
- public boolean getCaseSensitive() {
- return caseSensitive;
- }
-
- /**
- * 判断指定的文件是否可以被接受。
- * @param file 需要判断的文件
- * @return 扩展名符合指定的要求时返回true,如果是目录也返回true,否则返回false
- * @since 0.6
- */
- protected boolean acceptFile(File file) {
- String filename = file.getName();
- int dot = filename.lastIndexOf(".");
- if (dot == -1) {
- return false;
- }
- return StringUtil.contains(suffixList, filename.substring(dot + 1),
- caseSensitive);
- }
-
- /**
- * 返回过滤器的描述字符串。
- * @return 所有扩展名的列表
- * @since 0.4
- */
- public String getDescription() {
- if (description != null) {
- return description;
- }
- return StringUtil.combineStringArray(suffixList, "/");
- }
-
- }