public void doReplacement() { EditWindow currentWindow = editor.getActiveWindow(); if (currentWindow == null || getFindTextField().getText().length() == 0) { // launch error dialog? return; } JEditorPane editorPane = currentWindow.getEditorPane(); String text = editorPane.getSelectedText(); if (text == null) { // no selection return; } Matcher m = getCurrentPattern().matcher(text); if (m.matches()) { String replacement = getReplaceTextField().getText(); if (getRegexButton().isSelected()) { replacement = m.replaceFirst(replacement); } editorPane.replaceSelection(replacement); } }
public void doReplaceAll() { EditWindow currentWindow = editor.getActiveWindow(); if (currentWindow == null || getFindTextField().getText().length() == 0) { // launch error dialog? return; } JEditorPane editorPane = currentWindow.getEditorPane(); String text = editorPane.getText(); String replacement = getReplaceTextField().getText(); editorPane.setText(getCurrentPattern().matcher(text).replaceAll(replacement)); }
public void findNext() { EditWindow currentWindow = editor.getActiveWindow(); if (currentWindow == null || getFindTextField().getText().length() == 0) { // launch error dialog? return; } Pattern p = getCurrentPattern(); JEditorPane editorPane = currentWindow.getEditorPane(); // for some reason, getText() trims off \r but the indexes in // the editor pane don't. String text = editorPane.getText().replaceAll("\\r", ""); Matcher m = p.matcher(text); int index = editorPane.getSelectionEnd(); if (!(m.find(index) || m.find())) { return; } editorPane.setSelectionStart(m.start()); editorPane.setSelectionEnd(m.end()); }
public void showDialog(boolean showReplace) { getReplaceLabel().setVisible(showReplace); getReplaceTextField().setVisible(showReplace); getReplaceButton().setVisible(showReplace); getReplaceAllButton().setVisible(showReplace); if (showReplace) { setTitle("Replace"); getRootPane().setDefaultButton(getReplaceButton()); getReplaceFindButton().setText("Find..."); getReplaceFindButton().setMnemonic('d'); getReplaceFindButton().setDisplayedMnemonicIndex(3); } else { setTitle("Find"); getRootPane().setDefaultButton(getFindNextButton()); getReplaceFindButton().setText("Replace..."); getReplaceFindButton().setMnemonic('R'); getReplaceFindButton().setDisplayedMnemonicIndex(0); } pack(); if (!initLoc && editor != null) { Rectangle bounds = editor.getBounds(); Dimension size = getSize(); setLocation( (int) (bounds.getX() + (bounds.getWidth() - size.getWidth()) / 2), (int) (bounds.getY() + (bounds.getHeight() - size.getHeight()) / 2)); initLoc = true; } // Bugfix [2664844] - Editor: Find (set cursor position) getFindTextField().requestFocus(); setVisible(true); }