1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.widgets;
|
---|
3 |
|
---|
4 | import java.awt.Color;
|
---|
5 |
|
---|
6 | import javax.swing.UIManager;
|
---|
7 | import javax.swing.event.DocumentEvent;
|
---|
8 | import javax.swing.event.DocumentListener;
|
---|
9 | import javax.swing.text.JTextComponent;
|
---|
10 |
|
---|
11 | import org.openstreetmap.josm.actions.search.SearchCompiler;
|
---|
12 | import org.openstreetmap.josm.tools.Logging;
|
---|
13 |
|
---|
14 | /**
|
---|
15 | * Decorates a text component with an execution to the search compiler. Afterwards, a {@code "filter"} property change
|
---|
16 | * will be fired and the compiled search can be accessed with {@link #getMatch()}.
|
---|
17 | */
|
---|
18 | public final class CompileSearchTextDecorator implements DocumentListener {
|
---|
19 |
|
---|
20 | private final JTextComponent textComponent;
|
---|
21 | private final String originalToolTipText;
|
---|
22 | private SearchCompiler.Match filter;
|
---|
23 |
|
---|
24 | private CompileSearchTextDecorator(JTextComponent textComponent) {
|
---|
25 | this.textComponent = textComponent;
|
---|
26 | this.originalToolTipText = textComponent.getToolTipText();
|
---|
27 | textComponent.getDocument().addDocumentListener(this);
|
---|
28 | }
|
---|
29 |
|
---|
30 | /**
|
---|
31 | * Decorates a text component with an execution to the search compiler. Afterwards, a {@code "filter"} property change
|
---|
32 | * will be fired and the compiled search can be accessed with {@link #getMatch()}.
|
---|
33 | * @param f the text component to decorate
|
---|
34 | * @return an instance of the decorator in order to access the compiled search via {@link #getMatch()}
|
---|
35 | */
|
---|
36 | public static CompileSearchTextDecorator decorate(JTextComponent f) {
|
---|
37 | return new CompileSearchTextDecorator(f);
|
---|
38 | }
|
---|
39 |
|
---|
40 | private void setFilter() {
|
---|
41 | try {
|
---|
42 | textComponent.setBackground(UIManager.getColor("TextField.background"));
|
---|
43 | textComponent.setToolTipText(originalToolTipText);
|
---|
44 | filter = SearchCompiler.compile(textComponent.getText());
|
---|
45 | } catch (SearchCompiler.ParseError ex) {
|
---|
46 | textComponent.setBackground(new Color(255, 224, 224));
|
---|
47 | textComponent.setToolTipText(ex.getMessage());
|
---|
48 | filter = SearchCompiler.Always.INSTANCE;
|
---|
49 | Logging.debug(ex);
|
---|
50 | }
|
---|
51 | textComponent.firePropertyChange("filter", 0, 1);
|
---|
52 | }
|
---|
53 |
|
---|
54 | /**
|
---|
55 | * Returns the compiled search
|
---|
56 | * @return the compiled search
|
---|
57 | */
|
---|
58 | public SearchCompiler.Match getMatch() {
|
---|
59 | return filter;
|
---|
60 | }
|
---|
61 |
|
---|
62 | @Override
|
---|
63 | public void insertUpdate(DocumentEvent e) {
|
---|
64 | setFilter();
|
---|
65 | }
|
---|
66 |
|
---|
67 | @Override
|
---|
68 | public void removeUpdate(DocumentEvent e) {
|
---|
69 | setFilter();
|
---|
70 | }
|
---|
71 |
|
---|
72 | @Override
|
---|
73 | public void changedUpdate(DocumentEvent e) {
|
---|
74 | setFilter();
|
---|
75 | }
|
---|
76 | }
|
---|