Skip to content

Commit

Permalink
Continuous search
Browse files Browse the repository at this point in the history
  • Loading branch information
dzmipt committed Jan 20, 2024
1 parent 312e566 commit 53de207
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 16 deletions.
3 changes: 2 additions & 1 deletion notes.md
Original file line number Diff line number Diff line change
@@ -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
-----
Expand Down
24 changes: 24 additions & 0 deletions src/studio/ui/DocumentChangeListener.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
11 changes: 10 additions & 1 deletion src/studio/ui/EditorPane.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
13 changes: 3 additions & 10 deletions src/studio/ui/EditorsPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;
Expand Down Expand Up @@ -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();
}
}

}
2 changes: 1 addition & 1 deletion src/studio/ui/search/SearchAction.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package studio.ui.search;

public enum SearchAction {Find, Replace, ReplaceAll}
public enum SearchAction {Find, FindContinues, Replace, ReplaceAll}
18 changes: 18 additions & 0 deletions src/studio/ui/search/SearchPanel.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
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;

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 {
Expand All @@ -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)
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 1 addition & 3 deletions test-integration/studio/ui/SearchTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

0 comments on commit 53de207

Please sign in to comment.