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

Last change on this file since 12620 was 12620, checked in by Don-vip, 7 years ago

see #15182 - deprecate all Main logging methods and introduce suitable replacements in Logging for most of them

File size: 2.5 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.actions.search.SearchCompiler;
12import 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 */
18public 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}
Note: See TracBrowser for help on using the repository browser.