⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 wildcardfilefilter.java

📁 具有不同语法高亮的编辑器实例
💻 JAVA
字号:
package org.fife.ui.rtextfilechooser;

import java.io.File;
import java.util.regex.Pattern;
import javax.swing.filechooser.FileFilter;


/**
 * A file filter that takes a wildcard string for its filter.  This class is
 * pretty much 100% ripped off from Java's BasicFileChooserUI's wildcard filter
 * class.
 */
class WildcardFileFilter extends FileFilter {

	Pattern pattern;
	String globPattern;


/*****************************************************************************/

	/**
	 * Sets the pattern to match with.
	 *
	 * @param globPattern The pattern to match with.
	 */
	public void setPattern(String globPattern) {

		char[] gPat = globPattern.toCharArray();
		int gpatLength = gPat.length;
		char[] rPat = new char[gpatLength * 2];
		boolean isWin32 = (File.separatorChar == '\\');
		boolean inBrackets = false;
		int j = 0;

		this.globPattern = globPattern;

		if (isWin32) {

			// On Windows, ending with *.* is equal to ending with *
			int len = gpatLength;
			if (globPattern.endsWith("*.*"))
				len -= 2;


			for (int i=0; i<len; i++) {

				switch(gPat[i]) {

					// Change "*" into ".*" for the regex.
					case '*':
						rPat[j++] = '.';
						rPat[j++] = '*';
						break;

					// Change "\" into "\\" for the regex.
					case '\\':
						rPat[j++] = '\\';
						rPat[j++] = '\\';
						break;

					// backslash-out any chars with special meaning for
					// the regex, but are valid Windows filename chars;
					// then add this char (and the blackshash if any).
					default:
						if ("+()^$.{}[]".indexOf(gPat[i]) >= 0)
						    rPat[j++] = '\\';
						rPat[j++] = gPat[i];
						break;

				}

			} // End of for(int i=0; i<len; i++).

		} // End of if (isWin32).

		// Non-Windows (probably UNIX-based).
		else {

			for (int i = 0; i < gpatLength; i++) {

				switch(gPat[i]) {

					case '*':
						if (!inBrackets)
							rPat[j++] = '.';
						rPat[j++] = '*';
						break;

					case '?':
						rPat[j++] = inBrackets ? '?' : '.';
						break;

					case '[':
						inBrackets = true;
						rPat[j++] = gPat[i];

						if (i < gpatLength - 1) {
							switch (gPat[i+1]) {
								case '!':
								case '^':
									rPat[j++] = '^';
									i++;
									break;

								case ']':
									rPat[j++] = gPat[++i];
									break;
							}
						}
						break;

					case ']':
						rPat[j++] = gPat[i];
						inBrackets = false;
						break;

					case '\\':
						if (i == 0 && gpatLength > 1 && gPat[1] == '~')
							rPat[j++] = gPat[++i];
						else {
							rPat[j++] = '\\';
							if (i < gPat.length - 1 && "*?[]".indexOf(gPat[i+1]) >= 0)
								rPat[j++] = gPat[++i];
							else
								rPat[j++] = '\\';
						}
						break;

					default:
						if (!Character.isLetterOrDigit(gPat[i]))
							rPat[j++] = '\\';
						rPat[j++] = gPat[i];
						break;

				} // End of switch(gPat[i]).

			} // End of for (int i = 0; i < gPat.length; i++).

		} // End of else.

		this.pattern = Pattern.compile(new String(rPat, 0, j),
										Pattern.CASE_INSENSITIVE);

	}


/*****************************************************************************/


	/**
	 * Returns whether or not the sepcified file is matched by this filter.
	 *
	 * @param f The file to see whether or not it passes this filter.
	 * @return Whether or not this filter accepts <code>f</code>.
	 */
	public boolean accept(File f) {
		if (f==null)
			return false;
		if (f.isDirectory())
			return true;
		return pattern.matcher(f.getName()).matches();
	}


/*****************************************************************************/


	/**
	 * Returns a description of the file filter.
	 */
	public String getDescription() {
		return "You never see me!";
	}


/*****************************************************************************/

}

⌨️ 快捷键说明

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