basicfilefilter.java

来自「Java Bytecode Editor 是一个 JAVA 的字节码反汇编和修改」· Java 代码 · 共 74 行

JAVA
74
字号
/*
    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public
    License as published by the Free Software Foundation; either
    version 2 of the license, or (at your option) any later version.
*/

package org.gjt.jclasslib.mdi;

import javax.swing.filechooser.FileFilter;
import java.io.File;

/**
    Configurable file filter for a <tt>JFileChooser</tt>.
 
    @author <a href="mailto:jclasslib@ej-technologies.com">Ingo Kegel</a>
    @version $Revision: 1.1 $ $Date: 2005/11/01 13:18:24 $
*/
public class BasicFileFilter extends FileFilter {

    private String[] extensions;
    private String description;

    /**
        Constructor.
        @param extensions the file extensions.
        @param description the description.
     */
    public BasicFileFilter(String[] extensions, String description) {

        this.extensions = extensions;

        StringBuffer buffer = new StringBuffer(description);
        buffer.append(" (");
        for (int i = 0; i < extensions.length; i++) {
            if (i > 0) {
                buffer.append(", ");
            }
            buffer.append("*.");
            buffer.append(extensions[i]);
        }
        buffer.append(")");

        this.description = buffer.toString();
    }

    /**
        Constructor.
        @param extension the file extension.
        @param description the description.
     */
    public BasicFileFilter(String extension, String description) {

        this(new String[] {extension}, description);
    }

    public boolean accept(File file) {

        if (extensions == null)
            return true;

        for (int i = 0; i < extensions.length; i++) {
            if (file.isDirectory() || file.getName().endsWith(extensions[i]))
                return true;
        }
        return false;
    }

    public String getDescription() {
        return description + "";
    }
}

⌨️ 快捷键说明

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