import javax.swing.text.JTextComponent; //导入方法依赖的package包/类
public void actionPerformed(final ActionEvent evt, final JTextComponent target) {
if (target != null) {
if (!target.isEditable() || !target.isEnabled()) {
target.getToolkit().beep();
return;
}
EditorUI editorUI = Utilities.getEditorUI(target);
Caret caret = target.getCaret();
final BaseDocument doc = Utilities.getDocument(target);
if(caret instanceof EditorCaret) {
EditorCaret editorCaret = (EditorCaret) caret;
if(editorCaret.getCarets().size() > 1) {
target.getToolkit().beep();
return;
}
}
// Possibly remove selection
if (Utilities.isSelectionShowing(caret)) {
target.replaceSelection(null);
}
final int caretOffset = caret.getDot();
final String s = editorUI.getWordMatch().getMatchWord(caretOffset, matchNext);
final String prevWord = editorUI.getWordMatch().getPreviousWord();
if (s != null) {
doc.runAtomicAsUser (new Runnable () {
public void run () {
DocumentUtilities.setTypingModification(doc, true);
try {
int offset = caretOffset;
boolean removePrevWord = (prevWord != null && prevWord.length() > 0);
if (removePrevWord) {
offset -= prevWord.length();
}
// Create position due to possible text replication (e.g. for variable renaming)
Position pos = doc.createPosition(offset);
doc.remove(offset, prevWord.length());
doc.insertString(pos.getOffset(), s, null);
} catch (BadLocationException e) {
target.getToolkit().beep();
} finally {
DocumentUtilities.setTypingModification(doc, false);
}
}
});
}
}
}