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

📄 rtextutilities.java~1~

📁 具有不同语法高亮的编辑器实例
💻 JAVA~1~
📖 第 1 页 / 共 2 页
字号:
	}


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


	/**
	 * Returns the directory in which the user's macros are stored.
	 *
	 * @return The macro directory, or <code>null</code> if it cannot be found
	 *         or created.
	 */
	public static final File getMacroDirectory() {

		File f = new File(System.getProperty("user.home") + "/.rtext/macros");
		if (!f.isDirectory()) {
			boolean result = f.mkdirs();
			if (!f.isDirectory())
				return null;
		}
		return f;

	}


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


	/**
	 * Returns the name of the macro in the specified file.
	 *
	 * @param macroFile A file containing an <code>RTextArea</code> macro.
	 *        If this file is <code>null</code>, then <code>null</code>
	 *        is returned.
	 * @return The name of the macro.
	 */
	public static final String getMacroName(File macroFile) {
		String name = null;
		if (macroFile!=null) {
			name = macroFile.getName();
			if (name.endsWith(MACRO_EXTENSION)) { // Should always happen.
				name = name.substring(0,
							name.length()-MACRO_EXTENSION.length());
			}
		}
		return name;
	}


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


	/**
	 * Converts a <code>String</code> representing a wildcard file filter into
	 * another <code>String</code> containing a regular expression good for
	 * finding files that match the wildcard expressions.<p>
	 *
	 * Example: For<p>
	 * <code>String regEx = RTextUtilities.getRegularExpressionForFileFilter("*.c");
	 * </code><p>
	 * <code>regEx</code> will contain <code>^.*\.c$</code>.
	 *
	 * @param fileFilter The file filter for which to create equivalent regular
	 *        expressions.  This filter can currently only contain the
	 *        wildcards '*' and '?'.
	 * @param showErrorDialog If <code>true</code>, an error dialog is
	 *        displayed if an error occurs.
	 * @return A <code>String</code> representing an equivalent regular
	 *         expression for the string passed in.  If an error occurs,
	 *         <code>null</code> is returned.
	 */
	public static String getRegularExpressionForFileFilter(String fileFilter,
										boolean showErrorDialog) {

		String pattern = "^" + fileFilter;
		pattern = pattern.replaceAll("\\.", "\\\\.");	// Replace all '.' with '\.'.
		pattern = pattern.replaceAll("\\*", ".*");		// Replace all '*' with '.*'.
		pattern = pattern.replaceAll("\\?", ".");		// Replace all '?' with '.'.
		pattern = pattern.replaceAll("\\$", "\\\\\\$");		// Replace all '$' with '\$'.
		// Add end anchor, since we'll always have the end of the filename (either a character or '*').
		pattern += "$";
		try {
			Pattern.compile(pattern);
		} catch (PatternSyntaxException pse) {
			if (showErrorDialog) {
				JOptionPane.showMessageDialog(null,
					"Error in the regular expression '" + pattern +
					"' formed from parameter '" + fileFilter + "':\n" +
					pse + "\nPlease use only valid filename characters " +
					"or wildcards (* or ?).\n" +
					"If you have, please report this error at: " +
					"http://sourceforge.net/projects/rtext",
					"Error", JOptionPane.ERROR_MESSAGE);
			}
			return null;
		}

		return pattern;

	}


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


	/**
	 * Returns a regular expression for a line of text, suitable for searching
	 * for that line of text in a file.
	 *
	 * @param line The line for which to get a regular expression.
	 * @param showErrorDialog Whether or not to display a dialog showing
	 *                        an error in the regex compilation, if any.
	 * @return The line as a regular expression.  If there is an exception
	 *         thrown during compiling the expression, <code>null</code> is
	 *         returned.
	 */
	public static String getRegularExpressionForLine(String line,
										boolean showErrorDialog) {

		String pattern = line;

		// Escape all chars that need to be escaped (i.e., that are special
		// characters for regular expressions).
		pattern = pattern.replaceAll("\\\\", "\\\\\\\\");
		pattern = pattern.replaceAll("\\.", "\\\\.");
		pattern = pattern.replaceAll("\\{", "\\\\{").replaceAll("\\}", "\\\\}");
		pattern = pattern.replaceAll("\\(", "\\\\(").replaceAll("\\)", "\\\\)");
		pattern = pattern.replaceAll("\\[", "\\\\[").replaceAll("\\]", "\\\\]");
		pattern = pattern.replaceAll("\\*", "\\\\*");
		pattern = pattern.replaceAll("\\$", "\\\\\\$"); // Replace "$" with "\$"; confusing isn't it?
		pattern = pattern.replaceAll("\\?", "\\\\?");
		pattern = pattern.replaceAll("\\+", "\\\\+");

		try {
			Pattern.compile(pattern);
		} catch (PatternSyntaxException pse) {
			if (showErrorDialog) {
				JOptionPane.showMessageDialog(null,
					"Error in the regular expression '" + pattern +
					"' formed from parameter '" + line + "':\n" +
					pse + "\nPlease report this error at: " +
					"http://sourceforge.net/projects/rtext",
					"Error", JOptionPane.ERROR_MESSAGE);
			}
			return null;
		}

		return pattern;

	}


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


	/**
	 * Returns all macro files saved in the macro directory.
	 *
	 * @return An array of files containing macros in the macro directory.  If
	 *         the macro directory cannot be found or is empty, an empty array
	 *         is returned.
	 */
	/*
	 * FIXME:  Have me return the file list in alphabetical order (as this is
	 *         not guaranteed by File.listFiles()).
	 */
	public static final File[] getSavedMacroFiles() {

		File macroDir = getMacroDirectory();

		// If the macro directory exists...
		if (macroDir!=null && macroDir.isDirectory()) {

			File[] allFiles = macroDir.listFiles();

			// And if there are files in it...
			if (allFiles!=null && allFiles.length>0) {

				// Remember all of the files that end in ".macro".
				int count = allFiles.length;
				DynamicIntArray dia = new DynamicIntArray();
				for (int i=0; i<count; i++) {
					if (allFiles[i].getName().endsWith(MACRO_EXTENSION))
						dia.add(i);
				}

				// Put the ".macro" files into their own array.
				count = dia.getSize();
				File[] macroFiles = new File[count];
				for (int i=0; i<count; i++)
					macroFiles[i] = allFiles[dia.get(i)];

				return macroFiles;

			}

		}

		// Otherwise, the macro directory couldn't be created for some reason
		// or it was empty.
		return new File[0];

	}


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


	/**
	 * Opens all files in the specified directory tree in RText.
	 *
	 * @param rtext The RText instance in which to open the files.
	 * @param directory The top of the directory tree, all files in which
	 *        you want opened in RText.
	 */
	public static void openAllFilesIn(RText rtext, File directory) {
		if (directory!=null && directory.isDirectory()) {
			File[] files = directory.listFiles();
			int count = files.length;
			for (int i=0; i<count; i++) {
				if (files[i].isDirectory()) {
					openAllFilesIn(rtext, files[i]);
				}
				else {
					rtext.openFile(files[i].getAbsolutePath());
				}
			}
		}
	}


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


	/**
	 * Sets the Look and Feel for all open RText instances.
	 *
	 * @param rtext An RText instance to display a message if an exception is
	 *        thrown.
	 * @param lnfClassName The class name of the Look and Feel to set.
	 */
	public static void setLookAndFeel(final RText rtext, String lnfClassName) {
		// Only set the Look and Feel if we're not already using that Look.
		String current = UIManager.getLookAndFeel().getClass().getName();
		if (lnfClassName!=null && !current.equals(lnfClassName)) {
			try {
				// Get the class loader used by the UIManager.
				ClassLoader cl = (ClassLoader)UIManager.
							getLookAndFeelDefaults().get("ClassLoader");
				// Load the Look and Feel class.  Note that we cannot
				// simply use its name for some reason (Exceptions are
				// thrown).
				Class c = cl.loadClass(lnfClassName);
				LookAndFeel lnf = (LookAndFeel)c.newInstance();
				UIManager.setLookAndFeel(lnf);
				// Re-save the class loader BEFORE calling
				// updateLookAndFeels(), as UIManager.setLookAndFeel()
				// resets this value to null, and we need this class loader
				// as it's the one that's aware of our 3rd party JARs.
				UIManager.getLookAndFeelDefaults().put("ClassLoader", cl);
				StoreKeeper.updateLookAndFeels(lnf);
			} catch (Exception e) {
				rtext.displayException(e);
			}
		}
	}


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

}

⌨️ 快捷键说明

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