Skip to content

Commit d0b569d

Browse files
committed
TextEditor: filter by regex is now tested upon pressing the enter key,
and also suffixed with ^.* and prefixed with .*$ if necessary, for convenience. When the regular expression pattern doesn't compile, its text renders in red, and a warning is shown in the log window with the error message (which can be surprisingly helpful towards fixing the regex). Text resets to black upon pressing any key.
1 parent 4bdaf38 commit d0b569d

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

src/main/java/org/scijava/ui/swing/script/TextEditor.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -706,6 +706,7 @@ public void focusGained(FocusEvent e) {
706706
}
707707
});
708708
filter.addKeyListener(new KeyAdapter() {
709+
Pattern pattern = null;
709710
@Override
710711
public void keyPressed(final KeyEvent ke) {
711712
if (ke.getKeyCode() == KeyEvent.VK_ENTER) {
@@ -716,11 +717,21 @@ public void keyPressed(final KeyEvent ke) {
716717
}
717718
if ('/' == text.charAt(0)) {
718719
// Interpret as a regular expression
720+
// Attempt to compile the pattern
719721
try {
720-
final Pattern pattern = Pattern.compile(text.substring(1));
721-
tree.setFileFilter((f) -> pattern.matcher(f.getName()).matches());
722+
String regex = text.substring(1);
723+
if ('^' != regex.charAt(1)) regex = "^.*" + regex;
724+
if ('$' != regex.charAt(regex.length() -1)) regex += ".*$";
725+
pattern = Pattern.compile(regex);
726+
filter.setForeground(Color.black);
722727
} catch (final PatternSyntaxException pse) {
728+
log.warn(pse.getLocalizedMessage());
723729
filter.setForeground(Color.red);
730+
pattern = null;
731+
return;
732+
}
733+
if (null != pattern) {
734+
tree.setFileFilter((f) -> pattern.matcher(f.getName()).matches());
724735
}
725736
} else {
726737
// Interpret as a literal match

0 commit comments

Comments
 (0)