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

📄 rtextfilechooser.java

📁 具有不同语法高亮的编辑器实例
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
	private void ensureAbsoluteFilePaths(File[] files) {
		int num = files.length;
		for (int i=0; i<num; i++) {
			if (!files[i].isAbsolute())
				files[i] = new File(currentDirectory + separator +
										files[i].getPath());
		}
	}


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


	/**
	 * Makes sure that <code>currentDirectory</code> still exists.  If it
	 * doesn't, it is set to be the user's home directory.
	 */
	private void ensureCurrentDirectoryExists() {
		// If the "current directory" was removed from under us since the last
		// time we were displayed, we'll just default to the user's directory.
		if (!currentDirectory.isDirectory()) {
			currentDirectory = new File(System.getProperty("user.dir"));
		}
	}


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


	/**
	 * Returns the "canonical" path for a given directory.  For example, if
	 * the given file has a name "org", we will prepend this with the file
	 * chooser's <code>currentDirectory</code>.
	 *
	 * @param file The file for which you want a canonical path.
	 */
	private final File getCanonicalFileFor(File file) {
		// Should return a valid root if a directory "fileName"
		// exists, or null if it doesn't.
		File root = RootManager.getInstance().getRootForFile(file);
		if (root!=null)
			return file;
		else
			return new File(currentDirectory + separator + file.getAbsolutePath());
	}


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


	/**
	 * Gets the list of user-choosable file filters.
	 *
	 * @return A <code>FileFilter</code> array containing all the choosable
	 *         file filters.
	 * @see #addChoosableFileFilter
	 * @see #removeChoosableFileFilter
	 */
	public FileFilter[] getChoosableFileFilters() {
		FileFilter[] filterArray = new FileFilter[fileFilters.size()];
		fileFilters.copyInto(filterArray);
		return filterArray;
	}


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


	/**
	 * Returns the color associated with a file type.  This method will take
	 * care of the extension's case (i.e., if running on Windows, all
	 * file extensions will be compared lower-case to ensure case doesn't
	 * matter).
	 *
	 * @param extension The extension for which to get the associated image.
	 * @return The color associated with this extension, or <code>null</code>
	 *         if there is none.
	 * @see #setColorForExtension
	 * @see #clearExtensionColorMap
	 */
	public Color getColorForExtension(final String extension) {
		return (Color)customColors.get(
				IGNORE_CASE ? extension.toLowerCase() : extension);
	}


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


	/**
	 * Returns the directory this <code>RTextFileChooser</code> is currently
	 * in.
	 *
	 * @return The current directory.
	 * @see #setCurrentDirectory
	 */
	public final File getCurrentDirectory() {
		return currentDirectory;
	}


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


	/**
	 * Returns the hashmap containing all file extensions and the colors used
	 * to color the names of files of those types.  Note that changes
	 * to this hashmap will cause changes to the file chooser (it is a shallow
	 * copy).<p>
	 * So, for example, with the returned <code>HashMap</code> you can iterate
	 * through its keys (the extensions) to get the color used by this
	 * file chooser to display files with those extensions.
	 *
	 * @return The hash map of file extensions to file information.
	 */
	public HashMap getCustomColorsMap() {
		return customColors;
	}


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


	/**
	 * Returns the default encoding for this operating system.
	 *
	 * @return The default encoding.
	 */
	public static final String getDefaultEncoding() {
		// NOTE:  We could just use System.getProperty("file.encoding"),
		// but according to a Java "tech tip"
		// (http://java.sun.com/developer/JDCTechTips/2003/tt0110.html),
		// this property is not required to be set.  This is the way that
		// should always work.
		String encoding = System.getProperty("file.encoding");
		if (encoding==null) {
			try {
				FileWriter w = new FileWriter("dummy");
				encoding = w.getEncoding();
				w.close();
			} catch (IOException ioe) {
				encoding = "US-ASCII";
			}
		}
		return encoding;
	}


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


	/**
	 * Returns the color used to paint the name of files with unknown type.
	 *
	 * @return The color used.
	 * @see #setDefaultFileColor
	 */
	public Color getDefaultFileColor() {
		return defaultFileColor;
	}


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


	/**
	 * Returns the description for the file displayed in the file chooser.
	 *
	 * @param file The file for which to get the description.
	 * @return The description.
	 */
	public String getDescription(File file) {
		String desc = fileSystemView.getSystemTypeDescription(file);
		if (desc==null) {
			if (file.isDirectory())
				desc = directoryText;
			else
				desc = fileText;
		}
		return desc;
	}


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


	/**
	 * Returns a string representing the encoding chosen by the user.
	 *
	 * @return A string representing the chosen encoding.
	 * @see #setEncoding
	 */
	public String getEncoding() {
		if (encoding==null)
			encoding = RTextFileChooser.getDefaultEncoding();
		return encoding;
	}


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


	/**
	 * Returns this file chooser.  Used by the inner classes below.
	 *
	 * @return This file chooser.
	 */
	private final RTextFileChooser getFileChooser() {
		return this;
	}


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


	/**
	 * Returns the currently-active file filter.
	 *
	 * @return The currently active file filter.
	 * @see #setFileFilter
	 */
	public FileFilter getFileFilter() {
		return currentFileFilter;
	}


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


	/**
	 * Returns the file selection mode.
	 *
	 * @return One of <code>FILES_ONLY</code>, <code>DIRECTORIES_ONLY</code>,
	 *         or <code>FILES_AND_DIRECTORIES</code>.
	 * @see #setFileSelectionMode
	 */
	public int getFileSelectionMode() {
		return fileSelectionMode;
	}


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


	/**
	 * Returns the files from the "File Name" combo box.  This method is
	 * called when the user clicks the accept button but has no files
	 * highlighted in the view.
	 */
	private File[] getFilesFromFileNameTextField() {

		File[] files = null;

		String text = fileNameTextField.getText();
		if (text==null)
			return null;
		text = text.trim();
		if (text.length()==0)
			return null;

		// Just a single file...
		if (text.charAt(0)!='"') {

			// Create the file.
			files = new File[1];
			files[0] = fileSystemView.createFileObject(text);

			// Make real file name for the "exists" call below.
			if (!files[0].isAbsolute())
				files[0] = fileSystemView.getChild(currentDirectory, text);

			// If the file doesn't exist and contains wildcards, do a
			// wildcard filter!
			if (!files[0].exists() && isGlobPattern(text)) {
				if (globFilter==null)
					globFilter = new WildcardFileFilter();
				try {
					globFilter.setPattern(text);
					refreshView(true); // Show all files matching this pattern.
				} catch (Exception e) {
					// Invalid pattern was entered...
					JOptionPane.showMessageDialog(this, "Invalid pattern: " + text +
						"\n" + e,
						"Error", JOptionPane.ERROR_MESSAGE);
				}
				return null; // This is called from actionPerformed and will simply bail out.
			} // End of if (!files[0].exists() && isGlobPattern(files[0])).

		} // End of if (text.charAt(0)!='"').

		// Multiple files surrounded by '"''s.
		else {

			ArrayList fileNames = new ArrayList();
			int i;

			// Parse the text for filenames in '"''s.
			while ( (i = text.indexOf('"', 1)) != -1 ) {
				fileNames.add(text.substring(1,i));
				text = text.substring(i);	// => '" "File2.txt"'.
				i = text.indexOf('"', 1);	// => j == 2.
				if (i==-1) {
					if (text.length()>1)
						return null;		// i.e., text was originally '"File1.txt" File2.txt'
					text = "";			// To trick part below.
					break;
				}
				text = text.substring(i);	// => '"file2.txt"'.
			}
			// "Leftovers" if they left off the ending '"'.
			if (text.length()>1)
				fileNames.add(text.substring(1));

			int numFileNames = fileNames.size();
			files = new File[numFileNames];
			for (i=0; i<numFileNames; i++)
				files[i] = new File((String)fileNames.get(i));

		}

		return files;

	}


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


	/**
	 * Returns a string representation of a file size, such as "842 bytes",
	 * "1.43 KB" or "3.4 MB".
	 *
	 * @param file The file for which you want its size converted into an
	 *             appropriate string.
	 * @return The string.  Note that this will be invalid if <code>file</code>
	 *         is a directory.
	 */
	private static final String getFileSizeStringFor(final File file) {

		long size = file.length();
		int count = 0;
		long tempSize = size;

		// Keep dividing by 1024 until you get the largest unit that goes into
		// this file's size.
		while ( (tempSize = size >> 10) >= 1) {
			size = tempSize;
			count++;
		}

		String suffix = null;
		switch (count) {
			case 0 : suffix = "bytes"; break;
			case 1 : suffix = "KB"; break;
			case 2 : suffix = "MB"; break;
			case 3 : suffix = "GB"; break;
			case 4 : suffix = "TB"; break;
		}
		return size + " " + suffix;

	}


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


	/**
	 * Returns whether or not the filename text field is file-system aware.
	 *
	 * @return Whether the filename text field provides possible matches
	 *        while you are typing in it.
	 * @see #setFileSystemAware
	 */
	public boolean getFileSystemAware() {
		// We keep this property in a boolean instead of querying the
		// text field directly because the text field may not yet be
		// created.  We delay creation of GUI objects until a file chooser
		// is actually displayed, to help speed up the creation of the
		// file chooser option panel.
		return fileSystemAware;
	}


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


	/**
	 * Returns the file system view for this file chooser.
	 *
	 * @return The file system view.
	 */
	public FileSystemView getFileSystemView() {
		return fileSystemView;
	}


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


	/**
	 * Returns the file type info (color) to use when painting
	 * the specified file in this file chooser.
	 *
	 * @param file The file.
	 * @return The file's color to use when painting it.  The color is
	 *         the color with which to paint the file's name when it is not
	 *         "selected" in the view.
	 */
	public FileTypeInfo getFileTypeInfoFor(File file) {

		if (file.isDirectory()) {
			tempInfo.labelTextColor = Color.BLACK;
		}
		else {
			// If there's an extension, check for a custom color.
			String fileName = file.getName();
			String extension = fileName.substring(fileName.lastIndexOf('.')+1);
			if (extension!=null && extension.length()>0) {
				if (IGNORE_CASE)
					extension = extension.toLowerCase();
				Color color = (Color)customColors.get(extension);
				if (color==null)
					color = defaultFileColor;
				tempInfo.labelTextColor = color;
			}
			// No extension => use defaults.
			else {
				tempInfo.labelTextColor = defaultFileColor;
			}
		}

		tempInfo.icon = iconManager.getIcon(file);
		return tempInfo;

	}

⌨️ 快捷键说明

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