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

📄 rtextfilechooser.java

📁 具有不同语法高亮的编辑器实例
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
		for (int i=0; i<count; i++) {
			String item = (String)encodingComboBox.getItemAt(i);
			Charset cs2 = Charset.forName(item);
			if (cs1.equals(cs2)) {
				encodingComboBox.setSelectedIndex(i);
				return;
			}
		}

	}


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


	/**
	 * Refreshes either the list view or table view, whichever is visible.
	 */
	private final void refreshView() {
		refreshView(false);
	}


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


	/**
	 * Refreshes the view (list, table, or details).
	 * This should be called with a parameter of <code>true</code> when the
	 * user types in a wildcard expression for a file name.
	 *
	 * @param useGlobFilter Whether or not to use the glob file filter.
	 */
	private final void refreshView(boolean useGlobFilter) {

		if (!guiInitialized)
			return;

		ensureCurrentDirectoryExists();

		File[] files = fileSystemView.getFiles(currentDirectory,
						!showHiddenFiles); // "useFileHiding".

		if (files!=null) {

			int num = files.length;
			Vector dirList = new Vector();
			Vector fileList = new Vector();

			// First, separate the directories from regular files so we can
			// sort them individually.  This part of the code could be made
			// more compact, but it isn't just for a tad more speed.
			FileFilter filter = (useGlobFilter ? globFilter : currentFileFilter);
			if (fileSelectionMode==DIRECTORIES_ONLY) {
				for (int i=0; i<num; i++)
					if (files[i].isDirectory())
						dirList.add(files[i]);
			}
			else { // FILES_AND_DIRECTORIES or FILES_ONLY.
				for (int i=0; i<num; i++)
					if (files[i].isDirectory())
						dirList.add(files[i]);
					else if (filter.accept(files[i]))
						fileList.add(files[i]);
			}


			// Details mode automagically sorts its data via the table's model;
			// however, list mode doesn't, so we'll go ahead and sort for it.
			if (mode!=DETAILS_MODE) {
				Collections.sort(fileList);
				Collections.sort(dirList);
			}

			if (fileSelectionMode!=DIRECTORIES_ONLY)
				dirList.addAll(fileList);

			view.setDisplayedFiles(dirList);

		}
		else { // files==null.
			view.clearDisplayedFiles();
		}

		// Ensure JScrollPane is at "beginning" and clear any selection.
		viewScrollPane.getViewport().setViewPosition(new Point(0,0));
		view.clearSelection();

	}


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


	/**
	 * Removes a filter from the list of user choosable file filters.
	 *
	 * @return <code>true</code> if the file filter was removed.
	 *
	 * @see #addChoosableFileFilter
	 * @see #getChoosableFileFilters
	 */ 
	public boolean removeChoosableFileFilter(FileFilter f) {
		if (fileFilters.contains(f)) {
			if(getFileFilter() == f)
				setFileFilter(null);
			fileFilters.removeElement(f);
			return true;
		}
		return false;
	}


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


	/**
	 * Saves this file chooser's preferences (colors used for filenames, etc.).
	 * A program should call this method when shutting down if it wishes for
	 * its file chooser to have the same properties the next time the
	 * application is started.
	 */
	public void savePreferences() {
		FileChooserPreferences.save(this);
	}


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


	/**
	 * Sets 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 set the associated color.
	 * @param color The new color to associate with this extension.  A value
	 *        of <code>null</code> makes the default color used.
	 * @see #getColorForExtension
	 * @see #clearExtensionColorMap
	 */
	public void setColorForExtension(String extension, Color color) {
		if (IGNORE_CASE)
			extension = extension.toLowerCase();
		customColors.put(extension, color);
	}


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


	/**
	 * Sets the directory this <code>RTextFileChooser</code> is currently in.
	 *
	 * @param dir The new current directory.  If this value isn't a valid
	 *            directory, The user's home directory is used.
	 * @see #getCurrentDirectory
	 */
	public void setCurrentDirectory(File dir) {

		// We update several pieces of this dialog box that have listeners
		// attached to them that do things like call setCurrentDirectory.
		// So, to keep from going into an infinite loop, we set this state
		// variable to true so all listeners know not to do stuff.
		isChangingDirectories = true;

		if (guiInitialized) {
			setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
		}

		// We need to check if it is a root because, for example, if they
		// choose "A:" on a PC but there is no disk in the drive,
		// dir.isDirectory() returns false.
		if (dir.isDirectory() || RootManager.getInstance().isRoot(dir)) {
			// We need to get the canonical path for when the user types
			// ".." or ".", etc.
			try {
				currentDirectory = new File(dir.getCanonicalPath());
			} catch (Exception e) {
				// This happens, for example, when the user selects "A:\"
				// on Windows and the drive doesn't contain a disk.
				if (RootManager.getInstance().isRoot(dir)) // As in the case of A:\ above.
					currentDirectory = new File(dir.getAbsolutePath());
				else
					currentDirectory = new File(System.getProperty("user.dir"));
			}
		}
		else
			currentDirectory = new File(System.getProperty("user.dir"));

		// We erase any selected files and refresh the list display.
		selectedFiles = null;

		if (guiInitialized) {
			refreshView();

			// Enable/disable the "Up one level" button appropriately.
			File parentFile = currentDirectory.getParentFile();
			upOneLevelButton.setEnabled(parentFile!=null &&
						parentFile.isDirectory() && parentFile.canRead());

			// Enable the "Create new folder" button iff we can write to
			// this directory.
			// FIXME:  Always returns false?
			//newFolderButton.setEnabled(fileSystemView.createFileObject(
			//					currentDirectory, "foo").canWrite());

			// Update the "Look in" combo box.
			populateLookInComboBox();

			// Set the listed "selected" files to nothing.
			fileNameTextField.setText(null);
			fileNameTextField.requestFocusInWindow();

			// Make the "Accept" button disabled.
			acceptButton.setEnabled(false);

			// Make the file name combo box use the same current directory.
			fileNameTextField.setCurrentDirectory(currentDirectory);

			setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));

		}

		isChangingDirectories = false;

	}


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


	/**
	 * Sets the directory this <code>RTextFileChooser</code> is currently in.
	 *
	 * @param dir The new current directory.  If this value isn't a valid
	 *            directory, The user's home directory is used.
	 * @see #getCurrentDirectory
	 */
	public void setCurrentDirectory(String dir) {
		setCurrentDirectory(new File(dir));
	}


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


	/**
	 * Sets the color used for unknown file types.
	 *
	 * @param color The color.
	 * @see #getDefaultFileColor
	 */
	public void setDefaultFileColor(final Color color) {
		defaultFileColor = color;
	}


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


	/**
	 * Sets the encoding selected by this file chooser.
	 *
	 * @param encoding The desired encoding.  If this value is invalid or
	 *        not supported by this OS, a system default is used.
	 * @see #getEncoding
	 */
	public void setEncoding(String encoding) {
		this.encoding = encoding==null ? getDefaultEncoding() : encoding;
		refreshEncodingComboBox();
	}


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


	/**
	 * Sets the current file filter. The file filter is used by the
	 * file chooser to filter out files from the user's view.
	 *
	 * @param filter The new current file filter to use.
	 * @see #getFileFilter
	 */
	public void setFileFilter(FileFilter filter) {
		setFileFilterImpl(filter, true);
	}


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


	/**
	 * Sets the current file filter. The file filter is used by the
	 * file chooser to filter out files from the user's view.
	 *
	 * @param filter The new current file filter to use.
	 * @param cacheIfGUINotRealized If the GUI isn't created, whether or not
	 *        the selected file filter should be set to this filter when it
	 *        is created.  This parameter is here because
	 *        <code>addChoosableFileFilter</code> calls this method, but that
	 *        method should not change the selected file filter.
	 * @see #getFileFilter
	 */
	protected void setFileFilterImpl(FileFilter filter,
							boolean cacheIfGUINotRealized) {

		// Add the file filter to the filter combo if it isn't already there.
		if (filter!=null && !fileFilters.contains(filter)) {
			int size = fileFilters.size();
			if (size==0)
				fileFilters.add(filter);
			else
				fileFilters.insertElementAt(filter, size);
			if (guiInitialized) {
				populatefilterComboBox();
			}
		}

		// Everything below this is GUI-related, so don't do it if the GUI
		// hasn't been created yet.
		if (!guiInitialized) {
			if (cacheIfGUINotRealized)
				filterToSelect = filter;
			return;
		}

		filterComboBox.setSelectedItem(filter);

		if (filter != null) {
			if (isMultiSelectionEnabled() && selectedFiles!=null &&
					selectedFiles.length>0)
			{
				Vector fList = new Vector();
				boolean failed = false;
				int num = selectedFiles.length;
				for (int i=0; i<num; i++) {
					if (filter.accept(selectedFiles[i])) {
						fList.add(selectedFiles[i]);
					}
					else
						failed = true;
				}
				if (failed)
					setSelectedFiles((fList.size() == 0) ? null :
						(File[])fList.toArray(new File[fList.size()]));
			}
			else if (selectedFiles!=null && selectedFiles.length>0 &&
				selectedFiles[0]!=null && !filter.accept(selectedFiles[0]))
				setSelectedFile(null);
		}

	}


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


	/**
	 * Sets whether the user can select files only, directories only, or
	 * both files and directories from this file chooser.
	 *
	 * @param mode One of <code>FILES_ONLY</code>, <code>DIRECTORIES_ONLY</code>,
	 *             or <code>FILES_AND_DIRECTORIES</code>.
	 * @see #getFileSelectionMode
	 */
	public void setFileSelectionMode(int mode) {
		if (fileSelectionMode==mode)
			return;
		if (mode!=FILES_ONLY && mode!=DIRECTORIES_ONLY && mode!=FILES_AND_DIRECTORIES)
			mode = FILES_ONLY;
		fileSelectionMode = mode;
		refreshView();
	}


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


	/**
	 * Sets whether the filename text field is "file system aware."
	 *
	 * @param aware Whether the filename text field will be helpful and
	 *        provide possible filename matches while you are typing in it.
	 *        This feature can be disabled for slow networks.
	 * @see #getFileSystemAware
	 */
	public void setFileSystemAware(boolean aware) {
		// 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.
		fileSystemAware = aware;
		if (fileNameTextField!=null)
			fileNameTextField.setFileSystemAware(aware);
	}


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


	/**
	 * Sets the color used to display hidden files' names.
	 *
	 * @param color The new color to use for the names of hidden files.
	 * @throws NullPointerException If <code>color</code> is <code>null</code>.
	 * @see #getHiddenFileColor
	 */
	public void setHiddenFileColor(Color color) {
		if (color==null)
			throw new NullPointerException("Hidden file color cannot be " +
									"null");
		hiddenFileColor = color;
	}


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


	/**
	 * Adds a color to associate 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 to associate an image and color with.
	 * @param c The color to associate with this extension.
	 */
	public void setInfoForExtension(final String extension, final Color c) {
		String ext = IGNORE_CASE ? extension.t

⌨️ 快捷键说明

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