Commit | Line | Data |
---|---|---|
3cdf3fd8 NR |
1 | |
2 | package be.nikiroo.fanfix_swing.gui; | |
3 | ||
4 | import java.awt.BorderLayout; | |
5 | import java.awt.event.ActionEvent; | |
6 | import java.awt.event.ActionListener; | |
7 | import java.awt.event.KeyAdapter; | |
8 | import java.awt.event.KeyEvent; | |
9 | ||
10 | import javax.swing.JButton; | |
3cdf3fd8 NR |
11 | import javax.swing.JTextField; |
12 | import javax.swing.SwingUtilities; | |
13 | ||
2a03ecc0 | 14 | import be.nikiroo.fanfix_swing.gui.utils.ListenerPanel; |
3cdf3fd8 NR |
15 | import be.nikiroo.fanfix_swing.gui.utils.UiHelper; |
16 | import be.nikiroo.fanfix_swing.images.IconGenerator; | |
17 | import be.nikiroo.fanfix_swing.images.IconGenerator.Icon; | |
18 | import be.nikiroo.fanfix_swing.images.IconGenerator.Size; | |
19 | ||
20 | /** | |
21 | * A generic search/filter bar. | |
22 | * | |
23 | * @author niki | |
24 | */ | |
2a03ecc0 | 25 | public class SearchBar extends ListenerPanel { |
3cdf3fd8 NR |
26 | static private final long serialVersionUID = 1L; |
27 | ||
28 | private JButton search; | |
29 | private JTextField text; | |
30 | private JButton clear; | |
31 | ||
32 | private boolean realTime; | |
33 | ||
34 | /** | |
35 | * Create a new {@link SearchBar}. | |
36 | */ | |
37 | public SearchBar() { | |
38 | setLayout(new BorderLayout()); | |
39 | ||
30c4d6ed NR |
40 | // TODO: option for this |
41 | realTime = true; | |
42 | ||
3cdf3fd8 NR |
43 | search = new JButton(IconGenerator.get(Icon.search, Size.x16)); |
44 | UiHelper.setButtonPressed(search, realTime); | |
45 | search.addActionListener(new ActionListener() { | |
46 | @Override | |
47 | public void actionPerformed(ActionEvent e) { | |
48 | realTime = !realTime; | |
49 | UiHelper.setButtonPressed(search, realTime); | |
50 | text.requestFocus(); | |
51 | ||
52 | if (realTime) { | |
2a03ecc0 | 53 | fireActionPerformed(getText()); |
3cdf3fd8 NR |
54 | } |
55 | } | |
56 | }); | |
57 | ||
58 | text = new JTextField(); | |
59 | text.addKeyListener(new KeyAdapter() { | |
60 | @Override | |
61 | public void keyTyped(final KeyEvent e) { | |
62 | super.keyTyped(e); | |
63 | SwingUtilities.invokeLater(new Runnable() { | |
64 | @Override | |
65 | public void run() { | |
66 | boolean empty = (text.getText().isEmpty()); | |
67 | clear.setVisible(!empty); | |
68 | ||
69 | if (realTime) { | |
2a03ecc0 | 70 | fireActionPerformed(getText()); |
3cdf3fd8 NR |
71 | } |
72 | } | |
73 | }); | |
74 | } | |
75 | }); | |
76 | text.addActionListener(new ActionListener() { | |
77 | @Override | |
78 | public void actionPerformed(ActionEvent e) { | |
79 | if (!realTime) { | |
2a03ecc0 | 80 | fireActionPerformed(getText()); |
3cdf3fd8 NR |
81 | } |
82 | } | |
83 | }); | |
84 | ||
85 | clear = new JButton(IconGenerator.get(Icon.clear, Size.x16)); | |
86 | clear.setBackground(text.getBackground()); | |
87 | clear.setVisible(false); | |
88 | clear.addActionListener(new ActionListener() { | |
89 | @Override | |
90 | public void actionPerformed(ActionEvent e) { | |
91 | text.setText(""); | |
92 | clear.setVisible(false); | |
93 | text.requestFocus(); | |
94 | ||
2a03ecc0 | 95 | fireActionPerformed(getText()); |
3cdf3fd8 NR |
96 | } |
97 | }); | |
98 | ||
99 | add(search, BorderLayout.WEST); | |
100 | add(text, BorderLayout.CENTER); | |
101 | add(clear, BorderLayout.EAST); | |
102 | } | |
103 | ||
3cdf3fd8 NR |
104 | /** |
105 | * Return the current text displayed by this {@link SearchBar}. | |
106 | * | |
107 | * @return the text | |
108 | */ | |
109 | public String getText() { | |
110 | return text.getText(); | |
111 | } | |
112 | } |