📄 findinfilesdialog.java
字号:
setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
// Re-enable the buttons since the user can do stuff again.
setSearching(false);
// If searching completed normally (e.g., wasn't terminated).
if (time!=-1) {
// Make the status bar indicate that searching completed.
String temp = MessageFormat.format(searchingCompleteString,
new Object[] { ""+(time/1000.0f) });
setStatusText(temp);
// Update the results list and notify the user if the
// message wasn't found at all.
if (resultsTable.getRowCount()==0) {
String searchString = (String)findTextComboBox.
getSelectedItem();
JOptionPane.showMessageDialog(FindInFilesDialog.this,
resources.getString("SearchStringNotFound") +
searchString + "'.",
resources.getString("InfoDialogTitle"),
JOptionPane.INFORMATION_MESSAGE);
}
}
resultsTable.refreshColumnWidths();
resultsTable.revalidate();
}});
}
/*****************************************************************************/
/**
* This function should be called to update match case, whole word, etc.
*
* @param searchString The text the user wants to search for.
* @param matchCase Whether the "match case" checkbox should be checked.
* @param wholeWord Whether the "whole word" checkbox should be checked.
*/
public void setSearchParameters(String searchString, boolean matchCase,
boolean wholeWord, boolean regExp) {
findTextComboBox.addItem(searchString);
findTextComboBox.setSelectedIndex(0);
caseCheckBox.setSelected(matchCase);
wholeWordCheckBox.setSelected(wholeWord);
regExpCheckBox.setSelected(regExp);
}
/*****************************************************************************/
/**
* This function should be called to update match case, whole word, etc.
*/
public void setSearchParameters(Vector findComboBoxStrings,
boolean matchCase, boolean wholeWord, boolean regExp) {
findTextComboBox.removeAllItems();
int size = findComboBoxStrings.size();
for (int i=size-1; i>=0; i--) {
findTextComboBox.addItem(findComboBoxStrings.get(i));
}
if (size>0)
findTextComboBox.setSelectedIndex(0);
caseCheckBox.setSelected(matchCase);
wholeWordCheckBox.setSelected(wholeWord);
regExpCheckBox.setSelected(regExp);
}
/*****************************************************************************/
/**
* Enables or disables widgets in the dialog as appropriate.
*
* @param searching Whether searching is starting.
*/
protected void setSearching(boolean searching) {
boolean enabled = !searching;
findButton.setEnabled(enabled);
browseButton.setEnabled(enabled);
if (searching) {
cancelButton.setText(resources.getString("Stop"));
cancelButton.setMnemonic((int)resources.
getString("StopMnemonic").charAt(0));
}
else {
cancelButton.setText(resources.getString("Close"));
cancelButton.setMnemonic((int)resources.
getString("CloseMnemonic").charAt(0));
}
findTextComboBox.setEnabled(enabled);
inFilesComboBox.setEnabled(enabled);
inFolderTextField.setEnabled(enabled);
}
/*****************************************************************************/
/**
* Sets the <code>String</code> to search for.
*
* @param newSearchString The <code>String</code> to put into the
* search field.
*/
public void setSearchString(String newSearchString) {
findTextComboBox.addItem(newSearchString);
findTextComboBox.setSelectedIndex(0);
}
/*****************************************************************************/
/**
* Sets the text in the status bar.
*
* @param text The text to display.
*/
public void setStatusText(final String text) {
// Check whether dialog is visible in case search table is
// docked on another window.
if (isVisible()) {
if (SwingUtilities.isEventDispatchThread()) {
statusBar.setStatusMessage(text);
}
else {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
statusBar.setStatusMessage(text);
}
});
}
}
}
/*****************************************************************************/
/**
* Displays or hides the Find in Files dialog. Note that you should use
* this method and not <code>show()</code> to properly display this dialog.
*
* @param visible Whether the dialog should be displayed or hidden.
*/
public void setVisible(boolean visible) {
super.setVisible(visible);
// If they're making the dialog visible, make sure the status text
// is "Ready" and not something left over from the last time the
// dialog was visible.
if (visible==true) {
setStatusText(defaultStatusText);
}
// Give the "Find" text field focus.
if (SwingUtilities.isEventDispatchThread()) {
findButton.setEnabled(enableFindButton());
findTextComboBox.requestFocusInWindow();
Component editor = getTextComponent(findTextComboBox);
if (editor instanceof JTextComponent)
((JTextComponent)editor).selectAll();
}
else {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
findButton.setEnabled(enableFindButton());
findTextComboBox.requestFocusInWindow();
Component editor = getTextComponent(findTextComboBox);
if (editor instanceof JTextComponent)
((JTextComponent)editor).selectAll();
}
});
}
}
/*****************************************************************************/
/**
* Synchronizes access to our "worker" thread.
*
* @param workerThread The new worker thread.
* @see #getWorkerThread
*/
private synchronized void setWorkerThread(FindInFilesThread thread) {
this.workerThread = thread;
}
/*****************************************************************************/
/**
* Called whenever the user changes the Look and Feel, etc.
* This is overridden so we can reinstate the listeners that are evidently
* lost on the JTextField portion of our combo box.
*/
public void updateUI() {
// Create listeners for the combo boxes.
FindInFilesFocusAdapter focusAdapter = new FindInFilesFocusAdapter();
FindInFilesKeyListener keyListener = new FindInFilesKeyListener();
FindInFilesDocumentListener docListener = new FindInFilesDocumentListener();
// Fix the Find What combo box's listeners.
JTextComponent textField = getTextComponent(findTextComboBox);
textField.addFocusListener(focusAdapter);
textField.addKeyListener(keyListener);
textField.getDocument().addDocumentListener(docListener);
// Fix the In Files combo box's listeners.
textField = getTextComponent(inFilesComboBox);
textField.addFocusListener(focusAdapter);
textField.addKeyListener(keyListener);
textField.getDocument().addDocumentListener(docListener);
// Fix the In Folders combo box's listeners.
inFolderTextField.addFocusListener(focusAdapter);
inFolderTextField.getDocument().addDocumentListener(docListener);
}
/*****************************************************************************/
/**************************** PRIVATE INNER CLASSES **************************/
/*****************************************************************************/
/**
* Listens for changes in the "Find what" text field.
*/
private class FindInFilesDocumentListener implements DocumentListener {
public void insertUpdate(DocumentEvent e) {
findButton.setEnabled(enableFindButton());
}
public void removeUpdate(DocumentEvent e) {
findButton.setEnabled(enableFindButton());
}
public void changedUpdate(DocumentEvent e) {
}
}
/*****************************************************************************/
/**
* Listens for the text field gaining focus.
*/
private class FindInFilesFocusAdapter extends FocusAdapter {
public void focusGained(FocusEvent e) {
Component component = e.getComponent();
((JTextField)component).selectAll();
if (component==getTextComponent(findTextComboBox))
// Remember what it originally was, in case they tabbed out.
lastSearchString = (String)findTextComboBox.getSelectedItem();
else if (component==getTextComponent(inFilesComboBox))
// Remember what it originally was, in case they tabbed out.
lastInFilesString = (String)inFilesComboBox.getSelectedItem();
}
}
/*****************************************************************************/
/**
* Listens for the user to double-click on the results JList. This class
* is what sends out <code>FindInFilesEvent</code>s.
*/
class FindInFilesDialogMouseListener extends MouseAdapter {
FindInFilesTable table;
FindInFilesDialogMouseListener(FindInFilesTable table) {
this.table = table;
}
public void mouseClicked(MouseEvent e) {
if (e.getButton()==MouseEvent.BUTTON1 && e.getClickCount()==2) {
int row = table.getSelectedRow();
if (row==-1)
return;
MatchData data = table.getMatchDataForRow(row);
String fileName = data.getFileName();
// Might be a directory if Verbose is enabled.
if (!(new File(fileName).isFile())) {
UIManager.getLookAndFeel().provideErrorFeedback(null);
return;
}
String lineStr = data.getLineNumber();
int line = -1;
if (!lineStr.equals("--")) { // e.g. "3" or "5-7".
if (lineStr.indexOf('-')>-1) {
lineStr =
lineStr.substring(0, lineStr.indexOf('-'));
}
try {
line = Integer.parseInt(lineStr);
} catch (NumberFormatException nfe) {
nfe.printStackTrace();
}
}
fireFindInFilesEvent(new FindInFilesEvent(
FindInFilesDialog.this, fileName, line));
}
}
}
/*****************************************************************************/
/**
* Listens for key presses in the Find In Files dialog.
*/
private class FindInFilesKeyListener extends KeyAdapter {
// Listens for a user releasing a key.
public void keyReleased(KeyEvent e) {
// This is an ugly hack to get around JComboBox's
// insistance on eating the first Enter keypress
// it receives when it has focus and its selected item
// has changed since the last time it lost focus.
if (e.getKeyCode()==KeyEvent.VK_ENTER) {
Object source = e.getSource();
// If they pressed Enter in the 'Find What' combo box.
if (source==getTextComponent(findTextComboBox)) {
String inFilesString = (String)inFilesComboBox.getSelectedItem();
lastInFilesString = inFilesString; // Just in case it changed too.
String searchString = (String)findTextComboBox.getSelectedItem();
if (searchString != lastSearchString) {
findButton.doClick();
lastSearchString = searchString;
getTextComponent(findTextComboBox).selectAll();
}
}
// If they pressed enter in the 'In Files' combo box.
else if (source==getTextComponent(inFilesComboBox)) {
String searchString = (String)findTextComboBox.getSelectedItem();
lastSearchString = searchString; // Just in case it changed too.
String inFilesString = (String)inFilesComboBox.getSelectedItem();
if (inFilesString != lastInFilesString) {
findButton.doClick();
lastInFilesString = inFilesString;
getTextComponent(inFilesComboBox).selectAll();
}
}
}
}
}
/*****************************************************************************/
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -