customfilter.java

来自「JavaExplorer是一个独立于平台的浏览器」· Java 代码 · 共 260 行

JAVA
260
字号
/**  * File and FTP Explorer  * Copyright 2002  * BOESCH Vincent  *  * This program 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.  *  * This program is distributed in the hope that it will be useful,  * but WITHOUT ANY WARRANTY; without even the implied warranty of  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the  * GNU General Public License for more details.  *  * You should have received a copy of the GNU General Public License  * along with this program; if not, write to the Free Software  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.  */package javaexplorer.util.filter;import java.io.*;import java.util.*;import javaexplorer.model.XFile;public class CustomFilter implements XFileFilter {    private boolean _booSearchRecursive = true;    private boolean _booUseCaseSensitive = false;    private Vector filters = new Vector(5, 5);    /**     *  Constructor for the CustomFilter object     *     *@param  e_filter               Description     *      of Parameter     *@param  e_booSearchRecursive   Description     *      of Parameter     *@param  e_booUseCaseSensitive  Description     *      of Parameter     */    public CustomFilter(String e_filter, boolean e_booSearchRecursive,        boolean e_booUseCaseSensitive) {        addFilter(e_filter);        _booSearchRecursive = e_booSearchRecursive;        _booUseCaseSensitive = e_booUseCaseSensitive;    }    /**     *  Constructor for the CustomFilter object     */    public CustomFilter() {    }    /**     *  Description of the Method     *     *@param  pathname  Description of Parameter     *@return           Description of the     *      Returned Value     */    public boolean accept(File pathname) {        //Le filtre accepte comme caractere generique : *        if (pathname.isDirectory()) {            return _booSearchRecursive;        }        boolean accept = false;        for (int i = 0; (i < filters.size()) && !accept; i++) {            accept = accept ||                matches((String) filters.elementAt(i), pathname.getName());        }        return accept;    }    /**     *  Description of the Method     *     *@param  pathname  Description of Parameter     *@return           Description of the     *      Returned Value     */    public boolean accept(XFile pathname) {        //Le filtre accepte comme caractere generique : *        if (pathname.isDirectory()) {            return _booSearchRecursive;        }        boolean accept = false;        for (int i = 0; (i < filters.size()) && !accept; i++) {            accept = accept ||                matches((String) filters.elementAt(i), pathname.getName());        }        return accept;    }    /**     *  Adds a feature to the Filter attribute     *  of the CustomFilter object     *     *@param  e_filter  The feature to be added     *      to the Filter attribute     */    public void addFilter(String e_filter) {        filters.addElement(e_filter);    }    /**     *  Description of the Method     *     *@param  regexp  Description of Parameter     *@param  val     Description of Parameter     *@return         Description of the Returned     *      Value     */    public boolean beginsWith(String regexp, String val) {        if (regexp.equals("*")) {            return true;        }        int index = regexp.indexOf("*");        if (index == -1) {            return (_booUseCaseSensitive ? (val.indexOf(regexp) == 0)                                         : (val.toUpperCase().indexOf(regexp.toUpperCase()) == 0));        } else {            if (index == 0) {                return endsWith(regexp.substring(1), val);            } else {                return beginsWith(regexp.substring(0, index), val) &&                endsWith(regexp.substring(index + 1), val);            }        }    }    /**     *  Description of the Method     */    public void clearFilters() {        filters.removeAllElements();    }    /**     *  Description of the Method     *     *@param  regexp  Description of Parameter     *@param  val     Description of Parameter     *@return         Description of the Returned     *      Value     */    public boolean contains(String regexp, String val) {        if (regexp.equals("*")) {            return true;        }        int index = regexp.indexOf("*");        if (index == -1) {            return (_booUseCaseSensitive ? (val.indexOf(regexp) != -1)                                         : (val.toUpperCase().indexOf(regexp.toUpperCase()) != -1));        } else {            if (index == 0) {                return endsWith(regexp.substring(1), val);            } else {                return contains(regexp.substring(0, index), val) &&                endsWith(regexp.substring(index + 1), val);            }        }    }    /**     *  Description of the Method     *     *@param  regexp  Description of Parameter     *@param  val     Description of Parameter     *@return         Description of the Returned     *      Value     */    public boolean endsWith(String regexp, String val) {        if (regexp.equals("*")) {            return true;        }        int index = regexp.indexOf("*");        if (index == -1) {            return (_booUseCaseSensitive ? val.endsWith(regexp)                                         : val.toUpperCase().endsWith(regexp.toUpperCase()));        } else {            if (index == 0) {                return endsWith(regexp.substring(1), val);            } else {                return contains(regexp.substring(0, index), val) &&                endsWith(regexp.substring(index + 1), val);            }        }    }    /**     *  Description of the Method     *     *@param  regexp  Description of Parameter     *@param  val     Description of Parameter     *@return         Description of the Returned     *      Value     */    public boolean matches(String regexp, String val) {        if (regexp.equals("*")) {            return true;        }        int index = regexp.indexOf("*");        if (index == -1) {            return (_booUseCaseSensitive ? (regexp.compareTo(val) == 0)                                         : (regexp.compareToIgnoreCase(val) == 0));        } else {            if (index == 0) {                return endsWith(regexp.substring(1), val);            } else {                if (index == (regexp.length() - 1)) {                    return endsWith(regexp.substring(0, regexp.length() - 1),                        val);                } else {                    return beginsWith(regexp.substring(0, index), val) &&                    endsWith(regexp.substring(index + 1), val);                }            }        }    }    /**     *  Sets the SearchRecursive attribute     *  of the CustomFilter object     *     *@param  e_booSearchRecursive  The new     *      SearchRecursive value     */    public void setSearchRecursive(boolean e_booSearchRecursive) {        _booSearchRecursive = e_booSearchRecursive;    }    /**     *  Sets the UseCaseSensitive attribute     *  of the CustomFilter object     *     *@param  e_booUseCaseSensitive  The new     *      UseCaseSensitive value     */    public void setUseCaseSensitive(boolean e_booUseCaseSensitive) {        _booUseCaseSensitive = e_booUseCaseSensitive;    }}

⌨️ 快捷键说明

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