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