提交 6d54945c 编写于 作者: J Jan Peter Stotz

chore(gui): regex search: make searchField background red in case of invalid regex entered

上级 7fa878d3
package jadx.gui.ui;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.Collections;
......@@ -38,10 +35,12 @@ import jadx.gui.treemodel.JNode;
import jadx.gui.utils.NLS;
import jadx.gui.utils.TextStandardActions;
import jadx.gui.utils.layout.WrapLayout;
import jadx.gui.utils.search.SearchSettings;
import jadx.gui.utils.search.TextSearchIndex;
public class SearchDialog extends CommonSearchDialog {
private static final long serialVersionUID = -5105405456969134105L;
private static final Color SEARCHFIELD_ERROR_COLOR = new Color(255, 150, 150);
private static final Logger LOG = LoggerFactory.getLogger(SearchDialog.class);
......@@ -81,6 +80,8 @@ public class SearchDialog extends CommonSearchDialog {
private final transient SearchPreset searchPreset;
private final transient Set<SearchOptions> options;
private Color searchFieldDefaultBgColor;
private transient JTextField searchField;
private transient Disposable searchDisposable;
......@@ -183,6 +184,7 @@ public class SearchDialog extends CommonSearchDialog {
private void initUI() {
searchField = new JTextField();
searchFieldDefaultBgColor = searchField.getBackground();
searchField.setAlignmentX(LEFT_ALIGNMENT);
TextStandardActions.attach(searchField);
......@@ -304,7 +306,16 @@ public class SearchDialog extends CommonSearchDialog {
}
LOG.debug("search event: {}", text);
showSearchState();
return index.buildSearch(text, options);
try {
Flowable<JNode> result = index.buildSearch(text, options);
if (searchField.getBackground() == SEARCHFIELD_ERROR_COLOR) {
searchField.setBackground(searchFieldDefaultBgColor);
}
return result;
} catch (SearchSettings.InvalidSearchTermException e) {
searchField.setBackground(SEARCHFIELD_ERROR_COLOR);
return Flowable.empty();
}
}
private void processSearchResults(java.util.List<JNode> results) {
......
......@@ -52,14 +52,13 @@ public class SearchSettings {
return this.regexPattern;
}
public boolean preCompile() {
public boolean preCompile() throws InvalidSearchTermException {
if (useRegex) {
try {
int flags = ignoreCase ? Pattern.CASE_INSENSITIVE : 0;
this.regexPattern = Pattern.compile(searchString, flags);
} catch (Exception e) {
LOG.warn("Invalid Regex: {}", this.searchString, e);
return false;
throw new InvalidSearchTermException("Invalid Regex: " + this.searchString, e);
}
}
return true;
......@@ -105,4 +104,11 @@ public class SearchSettings {
public void setActiveCls(JClass activeCls) {
this.activeCls = activeCls;
}
public static class InvalidSearchTermException extends Exception {
public InvalidSearchTermException(String message, Throwable cause) {
super(message, cause);
}
}
}
......@@ -109,7 +109,8 @@ public class TextSearchIndex {
this.codeIndex.removeForCls(cls);
}
public Flowable<JNode> buildSearch(String text, Set<SearchDialog.SearchOptions> options) {
public Flowable<JNode> buildSearch(String text, Set<SearchDialog.SearchOptions> options)
throws SearchSettings.InvalidSearchTermException {
boolean ignoreCase = options.contains(IGNORE_CASE);
boolean useRegex = options.contains(USE_REGEX);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册