fileexchangefilter.java

来自「JAVA Servlet2.3外文书籍源码」· Java 代码 · 共 77 行

JAVA
77
字号
package firewall.client;

import java.io.File;
import java.util.*;
import javax.swing.filechooser.*;
/**
 * Title:        Professional Java Servlet Programming - Chapter 2
 * Description:  Filter class for the file chooser
 * Copyright:    Copyright (c) 2001
 * Company:
 * @author Andrew Harbourne-Thomas
 * @version 1.0
 */
public class FileExchangeFilter extends FileFilter {

    private Hashtable filters = null;
    private String description = "Files";

    /**
     * Constructs a file filter based on the String array of extensions and
     * the description.
     *
     * @param extensions -  a String array of file extensions
     * @param description - description of the filter extensions
     */
    public FileExchangeFilter(String[] extensions, String description) {
	filters = new Hashtable();
	for (int i = 0; i < extensions.length; i++) {
          filters.put(extensions[i].toLowerCase(), this);
	}
 	if(description!=null) {
          this.description = description;
 	}
    }

    /**
     * determines if a file meets the filters criteria
     * @return true if the file is acceptable or is a directory,
     *         false otherwise
     */
    public boolean accept(File file) {
	if(file != null) {
	    if(file.isDirectory()) {
		return true;
	    }
	    String extension = getExtension(file);
	    if(extension != null && filters.get(getExtension(file)) != null) {
		return true;
	    }
	}
	return false;
    }

    /**
     * Returns the file extension
     * @return the extension of the file (eg "txt")
     */
     public String getExtension(File file) {
	if(file != null) {
	    String filename = file.getName();
	    int dot = filename.lastIndexOf('.');
	    if(dot > 0 && dot < filename.length() - 1) {
		return filename.substring(dot + 1).toLowerCase();
	    }
	}
	return null;
    }

    /**
     * Returns the filters description
     * @return the filters description
     */
    public String getDescription() {
	return description;
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?