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

📄 findinfilesdialog.java

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

	}


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


	/**
	 * Adds information on a match (or verbose search information) to the
	 * search table.<p>
	 *
	 * We assume this method is being called by {@link FindInFilesThread},
	 * not the EDT, so the match data is added via
	 * <code>SwingUtilities.invokeLater</code>.  Match data should never be
	 * gathered on the EDT since it is a potentially long process to gather it.
	 *
	 * @param matchData Data about the found text.
	 */
	void addMatchData(final MatchData matchData) {
		final String dirName = inFolderTextField.getText();
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				resultsTable.addMatchData(matchData, dirName);
			}
		});
	}


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


	/**
	 * Adds a find-in-files listener to this find in files dialog.
	 *
	 * @param listener The listener to add.
	 * @see  #removeFindInFilesListener
	 */
	public void addFindInFilesListener(FindInFilesListener listener) {
		eventListenerList.add(FindInFilesListener.class, listener);
	}


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


	/**
	 * Adds a file filter to the "In files:" combo box.
	 *
	 * @param filter The filter to add.
	 */
	public void addInFilesComboBoxFilter(String filter) {
		inFilesComboBox.addItem(filter);
	}


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


	/**
	 * Clears the search results table.  This method can be called from
	 * threads other than the EDT.
	 */
	void clearSearchResults() {
		if (SwingUtilities.isEventDispatchThread()) {
			resultsTable.clear();
		}
		else {
			SwingUtilities.invokeLater(new Runnable() {
				public void run() { resultsTable.clear(); }
			});
		}
	}


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


	/**
	 * This function actually performs a search through the given directory.
	 */
	private void doFindInFiles() {

		// First, ensure that the directory they selected actually exists.
		String dirPath = inFolderTextField.getText();
		final File directory = new File(dirPath);
		if (!directory.isDirectory()) {
			JOptionPane.showMessageDialog(this,
						resources.getString("ErrorDirNotExist") + dirPath,
						resources.getString("ErrorDialogTitle"),
						JOptionPane.ERROR_MESSAGE);
			inFolderTextField.selectAll();
			inFolderTextField.requestFocusInWindow();
			return;
		}

		// Next, if we're doing a regex search, ensure we have a valid
		// regex to search for.
		if (regExpCheckBox.isSelected()) {
			try {
				Pattern.compile(getSearchString());
			} catch (Exception e) {
				JOptionPane.showMessageDialog(null,
					"Invalid regular expression:\n" + e + "\nPlease " +
					"check your regular expression search string.",
					resources.getString("ErrorDialogTitle"),
					JOptionPane.ERROR_MESSAGE);
				return;
			}
		}

		// Show the hourglass cursor, as we may have a wait ahead of us.
		setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));

		// Disable the buttons so the user doesn't think they can use them
		// while we're searching.
		setSearching(true);

		final boolean doVerboseOutput = verboseCheckBox.isSelected();

		// Start searching!
		setWorkerThread(new FindInFilesThread(this, directory));
		getWorkerThread().start();

	}


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


	/**
	 * Returns whether or not the "Find" button should be enabled.
	 *
	 * @return Whether the Find button should be enabled.
	 */
	protected boolean enableFindButton() {
		return getWorkerThread()==null && 
				getLength(getTextComponent(findTextComboBox))>0 &&
				getLength(getTextComponent(inFilesComboBox))>0 &&
				getLength(inFolderTextField)>0;
	}


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


	/**
	 * Notifies all find-in-files listeners of a find-in-files event in this
	 * dialog.
	 *
	 * @param e The event to notify all listeners about.
	 */
	protected void fireFindInFilesEvent(FindInFilesEvent e) {

		// Guaranteed to return a non-null array
		Object[] listeners = eventListenerList.getListenerList();

		// Process the listeners last to first, notifying
		// those that are interested in this event
		for (int i = listeners.length-2; i>=0; i-=2) {
			if (listeners[i]==FindInFilesListener.class) {
				((FindInFilesListener)listeners[i+1]).
									findInFilesFileSelected(e);
			}
		}

	}


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


	/**
	 * Returns the resource bundle for this dialog.
	 *
	 * @return The resource bundle.
	 */
	ResourceBundle getBundle() {
		return resources;
	}


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


	/**
	 * Returns whether to check subfolders.
	 *
	 * @return Whether or not to check subfolders.
	 * @see #getMatchCase
	 * @see #getMatchWholeWord
	 * @see #getUseRegEx
	 */
	boolean getCheckSubfolders() {
		return subfoldersCheckBox.isSelected();
	}


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


	/**
	 * Returns whether the user wants verbose output about their search.
	 *
	 * @return Whether the user wants verbose output about their search.
	 */
	boolean getDoVerboseOutput() {
		return verboseCheckBox.isSelected();
	}


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


	/**
	 * Returns the contents of the "In Files:" combo box.
	 *
	 * @return The contents.
	 */
	String getInFilesComboBoxContents() {
		return (String)inFilesComboBox.getSelectedItem();
	}


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


	/**
	 * Returns the length of the text in a text component.
	 *
	 * @param c The text component.
	 * @return The number of characters in that text component.
	 */
	private static final int getLength(JTextComponent c) {
		return c.getDocument().getLength();
	}


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


	/**
	 * Returns whether matches should be case-sensitive.
	 *
	 * @return Whether or not matches should be case-sensitive.
	 * @see #getCheckSubfolders
	 * @see #getMatchWholeWord
	 * @see #getUseRegEx
	 */
	boolean getMatchCase() {
		return caseCheckBox.isSelected();
	}


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


	/**
	 * Returns whether matches should be whole word.
	 *
	 * @return Whether or not matches should be whole word.
	 * @see #getCheckSubfolders
	 * @see #getMatchCase
	 * @see #getUseRegEx
	 */
	boolean getMatchWholeWord() {
		return wholeWordCheckBox.isSelected();
	}


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


	/**
	 * Returns a <code>Vector</code> containing all of the search
	 * strings in the "Find what" combo box's history.
	 *
	 * @return The search strings in the Find combo box's history.
	 */
	public Vector getSearchStrings() {
		int itemCount = findTextComboBox.getItemCount();
		Vector vector = new Vector(itemCount);
		for (int i=0; i<itemCount; i++)
			vector.add(findTextComboBox.getItemAt(i));
		return vector;
	}


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


	/**
	 * Returns whether each line that matched the search criteria should be
	 * shown (as opposed to just a match count for each file).
	 *
	 * @return Whether or not each matched line should be shown.
	 */
	boolean getShowMatchingLines() {
		return matchingLinesRadioButton.isSelected();
	}


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


	/**
	 * Returns the text editor component for the specified combo box.
	 *
	 * @param combo The combo box.
	 * @return The text component.
	 */
	protected static JTextComponent getTextComponent(JComboBox combo) {
		return (JTextComponent)combo.getEditor().getEditorComponent();
	}


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


	/**
	 * Returns whether regular expressions should be used in searches.
	 *
	 * @return Whether or not regular expressions should be used in searches.
	 * @see #getCheckSubfolders
	 * @see #getMatchCase
	 * @see #getMatchWholeWord
	 */
	boolean getUseRegEx() {
		return regExpCheckBox.isSelected();
	}


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


	/**
	 * Synchronizes access to our "worker" thread.
	 *
	 * @return The thread that is currently searching, or <code>null</code> if
	 *         no searching is going on.
	 */
	protected synchronized FindInFilesThread getWorkerThread() {
		return workerThread;
	}


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


	/**
	 * Removes a find-in-files listener to this find in files dialog.
	 *
	 * @param listener The listener to remove
	 * @see  #addFindInFilesListener
	 */
	public void removeFindInFilesListener(FindInFilesListener listener) {
		eventListenerList.remove(FindInFilesListener.class, listener);
	}


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


	/**
	 * Called by the searching thread when searching was terminated early for
	 * some reason.
	 *
	 * @param message A message describing why searching was terminated.
	 */
	void searchCompleted(String message) {
		setStatusText(message);
		searchCompleted(-1);
	}


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


	/**
	 * Called by the searching thread when searching has completed.
	 *
	 * @param time The time in milliseconds the search took.
	 */
	void searchCompleted(final long time) {

		SwingUtilities.invokeLater(new Runnable() { public void run() {

			setWorkerThread(null);

			// Return the cursor to the regular one.

⌨️ 快捷键说明

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