diff --git a/notes.md b/notes.md index cdfb4472..4643f9a5 100644 --- a/notes.md +++ b/notes.md @@ -1,4 +1,5 @@ -* Fixed bug with sequential replace action in the editor +* Implement continuous search + * Fixed the bug with sequential replace action in the editor `dz4.0` 2024.01.09 ----- diff --git a/src/studio/ui/DocumentChangeListener.java b/src/studio/ui/DocumentChangeListener.java new file mode 100644 index 00000000..4ba4a3df --- /dev/null +++ b/src/studio/ui/DocumentChangeListener.java @@ -0,0 +1,24 @@ +package studio.ui; + +import javax.swing.event.DocumentEvent; +import javax.swing.event.DocumentListener; + +abstract public class DocumentChangeListener implements DocumentListener { + + abstract public void documentChanged(DocumentEvent e); + + @Override + public void insertUpdate(DocumentEvent e) { + documentChanged(e); + } + + @Override + public void removeUpdate(DocumentEvent e) { + documentChanged(e); + } + + @Override + public void changedUpdate(DocumentEvent e) { + documentChanged(e); + } +} diff --git a/src/studio/ui/EditorPane.java b/src/studio/ui/EditorPane.java index 1c172923..f6e6049c 100644 --- a/src/studio/ui/EditorPane.java +++ b/src/studio/ui/EditorPane.java @@ -113,10 +113,19 @@ public void search(SearchContext context, SearchAction action) { SearchResult result; if (action == SearchAction.Find) { result = SearchEngine.find(textArea, context); + } else if (action == SearchAction.FindContinues) { + textArea.setSelectionEnd(textArea.getSelectionStart()); + result = SearchEngine.find(textArea, context); } else { try { if (action == SearchAction.Replace) { - result = SearchEngine.replace(textArea, context); + int selStart = textArea.getSelectionStart(); + int selEnd = textArea.getSelectionEnd(); + textArea.setSelectionEnd(selStart); + result = SearchEngine.find(textArea, context); + if (selStart == textArea.getSelectionStart() && selEnd == textArea.getSelectionEnd()) { + result = SearchEngine.replace(textArea, context); + } } else { //ReplaceAll result = SearchEngine.replaceAll(textArea, context); } diff --git a/src/studio/ui/EditorsPanel.java b/src/studio/ui/EditorsPanel.java index 565f4a34..379de04e 100644 --- a/src/studio/ui/EditorsPanel.java +++ b/src/studio/ui/EditorsPanel.java @@ -14,7 +14,6 @@ import javax.swing.*; import javax.swing.event.DocumentEvent; -import javax.swing.event.DocumentListener; import javax.swing.filechooser.FileNameExtensionFilter; import javax.swing.text.JTextComponent; import java.awt.*; @@ -481,21 +480,15 @@ public interface EditorTabAction { } - private static class MarkingDocumentListener implements DocumentListener { + private static class MarkingDocumentListener extends DocumentChangeListener { private final EditorTab editor; public MarkingDocumentListener(EditorTab editor) { this.editor = editor; } - private void update() { + @Override + public void documentChanged(DocumentEvent e) { editor.setModified(true); } - public void changedUpdate(DocumentEvent evt) { update(); } - public void insertUpdate(DocumentEvent evt) { - update(); - } - public void removeUpdate(DocumentEvent evt) { - update(); - } } } diff --git a/src/studio/ui/search/SearchAction.java b/src/studio/ui/search/SearchAction.java index 51d89d5f..d572ea3b 100644 --- a/src/studio/ui/search/SearchAction.java +++ b/src/studio/ui/search/SearchAction.java @@ -1,3 +1,3 @@ package studio.ui.search; -public enum SearchAction {Find, Replace, ReplaceAll} +public enum SearchAction {Find, FindContinues, Replace, ReplaceAll} diff --git a/src/studio/ui/search/SearchPanel.java b/src/studio/ui/search/SearchPanel.java index d4fd76c8..b0e36e16 100644 --- a/src/studio/ui/search/SearchPanel.java +++ b/src/studio/ui/search/SearchPanel.java @@ -1,6 +1,9 @@ package studio.ui.search; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; import org.fife.ui.rtextarea.SearchContext; +import studio.ui.DocumentChangeListener; import studio.ui.GroupLayoutSimple; import studio.ui.UserAction; import studio.ui.Util; @@ -8,6 +11,7 @@ import javax.swing.*; import javax.swing.border.Border; import javax.swing.border.EtchedBorder; +import javax.swing.event.DocumentEvent; import java.awt.event.KeyEvent; public class SearchPanel extends JPanel { @@ -23,6 +27,8 @@ public class SearchPanel extends JPanel { private final EditorPaneLocator editorPaneLocator; + private final static Logger log = LogManager.getLogger(); + private static final Border ICON_BORDER = BorderFactory.createCompoundBorder( BorderFactory.createEtchedBorder(EtchedBorder.LOWERED), BorderFactory.createEmptyBorder(1,1,1,1) @@ -85,6 +91,13 @@ public SearchPanel(EditorPaneLocator editorPaneLocator) { im.put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER,0),"replaceAction"); im.put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE,0),"closeAction"); + txtFind.getDocument().addDocumentListener(new DocumentChangeListener() { + @Override + public void documentChanged(DocumentEvent e) { + findContinues(); + } + }); + GroupLayoutSimple layout = new GroupLayoutSimple(this); layout.setAutoCreateGaps(false); layout.setStacks( @@ -150,6 +163,11 @@ private void find(boolean forward) { doSearch(context, SearchAction.Find); } + private void findContinues() { + SearchContext context = buildSearchContext(); + doSearch(context, SearchAction.FindContinues); + } + private void markAll() { SearchContext context = buildSearchContext(); context.setMarkAll(true); diff --git a/test-integration/studio/ui/SearchTest.java b/test-integration/studio/ui/SearchTest.java index 839fbf3c..09aa819e 100644 --- a/test-integration/studio/ui/SearchTest.java +++ b/test-integration/studio/ui/SearchTest.java @@ -71,9 +71,7 @@ public void openFile() throws IOException { @Test public void testSequentialSearch() throws BadLocationException { - searchPanel.textBox("FindField").setText("select"); - - searchPanel.button("FindButton").click(); + searchPanel.textBox("FindField").enterText("select"); Selection selection = getSelection(editor); assertEquals(new Selection(1,0,1,6), selection);