source: josm/trunk/src/org/openstreetmap/josm/gui/widgets/CompileSearchTextDecorator.java@ 13661

Last change on this file since 13661 was 12846, checked in by bastiK, 7 years ago

see #15229 - use Config.getPref() wherever possible

File size: 2.6 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.widgets;
3
4import java.awt.Color;
5
6import javax.swing.UIManager;
7import javax.swing.event.DocumentEvent;
8import javax.swing.event.DocumentListener;
9import javax.swing.text.JTextComponent;
10
11import org.openstreetmap.josm.data.osm.search.SearchCompiler;
12import org.openstreetmap.josm.data.osm.search.SearchParseError;
13import 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 */
19public 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}
Note: See TracBrowser for help on using the repository browser.