📄 abstractsearchdialog.java
字号:
* The reason this method is here is to attempt to speed up
* <code>FindInFilesDialog</code>; since it repeatedly calls
* this method instead of <code>getNextMatchPos</code>, it gets better
* performance as it no longer has to allocate a lower-cased string for
* every call.<br>
*/
protected static final int getNextMatchPosImpl(String searchFor,
String searchIn, int startPos, boolean goForward,
boolean matchCase, boolean lookForWholeWord,
int tempChange) {
// Get just the first part of _searchIn (what we're interested in).
if (!goForward)
startPos--; // Otherwise we'll keep getting the same match.
int temp = startPos;
// We'll return from the loop, trust me.
while (true) {
// Find the next location of searchFor in our searchIn.
if (goForward)
temp = searchIn.indexOf(searchFor, temp);
else
temp = searchIn.lastIndexOf(searchFor, temp);
// If _searchFor was found...
if (temp != -1) {
// If we don't have to worry about "whole word," or it is
// indeed a whole word, we're done.
if ((lookForWholeWord==false) ||
isWholeWord(searchIn, temp, searchFor.length()))
return temp;
// Otherwise, update our starting point and continue.
else {
temp += tempChange;
continue;
}
} // End of if (temp != -1).
// If we got here, searchFor wasn't found, and so temp==-1.
return temp;
} // End of while (true).
}
/*****************************************************************************/
/**
* Searches <code>searchIn</code> for an occurance of <code>regEx</code>
* either forwards or backwards, matching case or not.
*
* @param regEx The regular expression to look for.
* @param searchIn The string to search in.
* @param startPos the position in <code>searchIn</code> at which to begin
* searching.
* @param goForward Whether to search forward. If <code>false</code>,
* search backward.
* @param matchCase Whether or not to do a case-sensitive search for
* <code>regEx</code>.
* @param lookForWholeWord If <code>true</code>, <code>regEx</code>
* occurances embedded in longer words in <code>searchIn</code>
* don't count as matches.
* @return A <code>Point</code> representing the starting and ending
* position of the match, or <code>null</code> if no match was
* found.
* @see #getNextMatchPos
*/
public static Point getNextMatchPosRegEx(String regEx, String searchIn,
int startPos, boolean goForward,
boolean matchCase,
boolean lookForWholeWord) {
// Make a pattern that takes into account whether or not to match case.
int flags = matchCase ? 0 : (Pattern.CASE_INSENSITIVE|Pattern.UNICODE_CASE);
Pattern pattern = null;
try {
pattern = Pattern.compile(regEx, flags);
} catch (Exception e) {
JOptionPane.showMessageDialog(null,
"Invalid regular expression:\n" + e +
"\nPlease check your regular expression search string.",
"Error", JOptionPane.ERROR_MESSAGE);
return null;
}
// Make a Matcher to find the regEx instances.
String _searchIn = ( goForward ? searchIn.substring(startPos,searchIn.length()) :
searchIn.substring(0,startPos));
Matcher matcher = pattern.matcher(_searchIn);
// This is a list of all matches.
ArrayList matches = new ArrayList();
// Popuplate the matches list.
while (matcher.find()) {
if (goForward)
// Add startPos for the real program.
matches.add(new Point(matcher.start()+startPos, matcher.end()+startPos));
else
matches.add(new Point(matcher.start(), matcher.end()));
}
// If no matches were found, just go ahead and return null.
if (matches.isEmpty())
return null;
// Get some variables ready.
int numMatches = matches.size();
int pos = goForward ? 0 : numMatches-1;
// If they're not looking for a "whole word" just return the first match.
if (lookForWholeWord==false)
return (Point)matches.get(pos);
// Otherwise, go through the matches first-to-last or last-to-first,
// depending on whether we're searching forward or backward.
while (pos>=0 && pos<numMatches) {
Point tempMatch = (Point)matches.get(pos);
if (isWholeWord(searchIn, tempMatch.x,tempMatch.y-tempMatch.x/*+1*/))
return tempMatch;
if (goForward)
pos++;
else
pos--;
}
// If we didn't find a match after all that, return null.
return null;
}
/*********************************************************************/
/**
* Returns the <code>java.lang.String</code> to search for.
*
* @return The <code>java.lang.String</code> the user wants to search for.
*/
public String getSearchString() {
return (String)findTextComboBox.getSelectedItem();
}
/*********************************************************************/
/**
* Returns the <code>Strings</code> contained in the "Find what" combo
* box.
*
* @return A <code>java.util.Vector</code> of strings found in the "Find
* what" combo box. If that combo box is empty, than a
* zero-length <code>Vector</code> is returned.
*/
public Vector getSearchStrings() {
// First, ensure that the item in the combo box editor is indeed in the combo box.
int selectedIndex = findTextComboBox.getSelectedIndex();
if (selectedIndex==-1) {
findTextComboBox.addItem(getSearchString());
}
// If they just searched for an item that's already in the list other than
// the first, move it to the first position.
else if (selectedIndex>0) {
Object item = findTextComboBox.getSelectedItem();
findTextComboBox.removeItem(item);
findTextComboBox.insertItemAt(item, 0);
findTextComboBox.setSelectedIndex(0);
}
int itemCount = findTextComboBox.getItemCount();
Vector vector = new Vector(itemCount);
for (int i=0; i<itemCount; i++)
vector.add(findTextComboBox.getItemAt(i));
return vector;
}
/*****************************************************************************/
/**
* If the characters around
* <code>substr(searchIn,startPos,startPos+searchStringLength)</code>
* aren't space characters, then the "whole word" isn't found, so return
* <code>false</code> (the word isn't a "whole word").
*/
private static final boolean isWholeWord(String searchIn, int startPos,
int searchStringLength) {
char charBefore;
char charAfter;
try {
charBefore = searchIn.charAt(startPos - 1);
} catch (IndexOutOfBoundsException e) { charBefore = ' '; }
try {
charAfter = searchIn.charAt(startPos + searchStringLength);
} catch (IndexOutOfBoundsException e) { charAfter = ' '; }
if (Character.isWhitespace(charBefore) && Character.isWhitespace(charAfter))
return true;
else
return false;
}
/*****************************************************************************/
/**
* Adds keyboard actions specific to this dialog. Currently all this does
* is make the search dialog disappear when the Escape key is pressed.
*/
protected void registerExtraKeyboardActions() {
JRootPane rootPane = getRootPane();
InputMap inputMap = rootPane.getInputMap(
JComponent.WHEN_IN_FOCUSED_WINDOW);
ActionMap actionMap = rootPane.getActionMap();
// Make the escape key cancel the dialog.
inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), "OnEsc");
actionMap.put("OnEsc", new javax.swing.AbstractAction() {
public void actionPerformed(ActionEvent e) {
setVisible(false);
}
}
);
}
/*****************************************************************************/
/**
* Sets the text on the Cancel button.
*
* @param text The text for the Cancel button.
* @see #getCancelButtonText
*/
public final void setCancelButtonText(String text) {
cancelButton.setText(text);
}
/*****************************************************************************/
/**
* Sets the text for the "Match Case" check box.
*
* @param text The text for the "Match Case" check box.
* @see #getMatchCaseCheckboxText
*/
public final void setMatchCaseCheckboxText(String text) {
caseCheckBox.setText(text);
}
/*****************************************************************************/
/**
* Sets the text for the "Regular Expression" check box.
*
* @param text The text for the "Regular Expression" check box.
* @see #getRegularExpressionCheckboxText
*/
public final void setRegularExpressionCheckboxText(String text) {
regExpCheckBox.setText(text);
}
/*****************************************************************************/
/**
* Sets the <code>java.lang.String</code> to search for.
*
* @param newSearchString The <code>java.lang.String</code> to put into
* the search field.
*/
public void setSearchString(String newSearchString) {
findTextComboBox.addItem(newSearchString);
findTextComboBox.setSelectedIndex(0);
}
/*****************************************************************************/
/**
* Sets the text for the "Whole Word" check box.
*
* @param text The text for the "Whole Word" check box.
* @see #getWholeWordCheckboxText
*/
public final void setWholeWordCheckboxText(String text) {
wholeWordCheckBox.setText(text);
}
/*****************************************************************************/
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -