Initial commit (missing a lot of things):
[nikiroo-utils.git] / src / be / nikiroo / fanfix_swing / gui / SearchBar.java
... / ...
CommitLineData
1
2package be.nikiroo.fanfix_swing.gui;
3
4import java.awt.BorderLayout;
5import java.awt.event.ActionEvent;
6import java.awt.event.ActionListener;
7import java.awt.event.KeyAdapter;
8import java.awt.event.KeyEvent;
9
10import javax.swing.JButton;
11import javax.swing.JPanel;
12import javax.swing.JTextField;
13import javax.swing.SwingUtilities;
14
15import be.nikiroo.fanfix_swing.gui.utils.UiHelper;
16import be.nikiroo.fanfix_swing.images.IconGenerator;
17import be.nikiroo.fanfix_swing.images.IconGenerator.Icon;
18import be.nikiroo.fanfix_swing.images.IconGenerator.Size;
19
20/**
21 * A generic search/filter bar.
22 *
23 * @author niki
24 */
25public class SearchBar extends JPanel {
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
40 search = new JButton(IconGenerator.get(Icon.search, Size.x16));
41 UiHelper.setButtonPressed(search, realTime);
42 search.addActionListener(new ActionListener() {
43 @Override
44 public void actionPerformed(ActionEvent e) {
45 realTime = !realTime;
46 UiHelper.setButtonPressed(search, realTime);
47 text.requestFocus();
48
49 if (realTime) {
50 fireActionPerformed();
51 }
52 }
53 });
54
55 text = new JTextField();
56 text.addKeyListener(new KeyAdapter() {
57 @Override
58 public void keyTyped(final KeyEvent e) {
59 super.keyTyped(e);
60 SwingUtilities.invokeLater(new Runnable() {
61 @Override
62 public void run() {
63 boolean empty = (text.getText().isEmpty());
64 clear.setVisible(!empty);
65
66 if (realTime) {
67 fireActionPerformed();
68 }
69 }
70 });
71 }
72 });
73 text.addActionListener(new ActionListener() {
74 @Override
75 public void actionPerformed(ActionEvent e) {
76 if (!realTime) {
77 fireActionPerformed();
78 }
79 }
80 });
81
82 clear = new JButton(IconGenerator.get(Icon.clear, Size.x16));
83 clear.setBackground(text.getBackground());
84 clear.setVisible(false);
85 clear.addActionListener(new ActionListener() {
86 @Override
87 public void actionPerformed(ActionEvent e) {
88 text.setText("");
89 clear.setVisible(false);
90 text.requestFocus();
91
92 fireActionPerformed();
93 }
94 });
95
96 add(search, BorderLayout.WEST);
97 add(text, BorderLayout.CENTER);
98 add(clear, BorderLayout.EAST);
99 }
100
101 /**
102 * Adds the specified action listener to receive action events from this
103 * {@link SearchBar}.
104 *
105 * @param listener the action listener to be added
106 */
107 public synchronized void addActionListener(ActionListener listener) {
108 listenerList.add(ActionListener.class, listener);
109 }
110
111 /**
112 * Removes the specified action listener so that it no longer receives action
113 * events from this {@link SearchBar}.
114 *
115 * @param listener the action listener to be removed
116 */
117 public synchronized void removeActionListener(ActionListener listener) {
118 listenerList.remove(ActionListener.class, listener);
119 }
120
121 /**
122 * Notify the listeners of an action.
123 */
124 protected void fireActionPerformed() {
125 ActionEvent e = new ActionEvent(this, ActionEvent.ACTION_PERFORMED, getText());
126 Object[] listeners = listenerList.getListenerList();
127 for (int i = listeners.length - 2; i >= 0; i -= 2) {
128 if (listeners[i] == ActionListener.class) {
129 ((ActionListener) listeners[i + 1]).actionPerformed(e);
130 }
131 }
132 }
133
134 /**
135 * Return the current text displayed by this {@link SearchBar}.
136 *
137 * @return the text
138 */
139 public String getText() {
140 return text.getText();
141 }
142}