New: SearchBar
[nikiroo-utils.git] / src / be / nikiroo / utils / ui / SearchBar.java
CommitLineData
7a55041d
NR
1package be.nikiroo.utils.ui;
2
3import java.awt.BorderLayout;
4import java.awt.event.ActionEvent;
5import java.awt.event.ActionListener;
6import java.awt.event.KeyAdapter;
7import java.awt.event.KeyEvent;
8import java.io.IOException;
9import java.io.InputStream;
10
11import javax.swing.ImageIcon;
12import javax.swing.JButton;
13import javax.swing.JTextField;
14import javax.swing.SwingUtilities;
15
16import be.nikiroo.utils.IOUtils;
17
18/**
19 * A generic search/filter bar.
20 *
21 * @author niki
22 */
23public class SearchBar extends ListenerPanel {
24 static private final long serialVersionUID = 1L;
25 static private ImageIcon searchIcon;
26 static private ImageIcon clearIcon;
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 // TODO: make an option to change the default setting here:
41 // (can already be manually toggled by the user)
42 realTime = true;
43
44 if (searchIcon == null)
45 searchIcon = getIcon("search-16x16.png");
46 if (clearIcon == null)
47 clearIcon = getIcon("clear-16x16.png");
48
49 search = new JButton(searchIcon);
50 if (searchIcon == null) {
51 search.setText("[s]");
52 }
53 UIUtils.setButtonPressed(search, realTime);
54 search.addActionListener(new ActionListener() {
55 @Override
56 public void actionPerformed(ActionEvent e) {
57 realTime = !realTime;
58 UIUtils.setButtonPressed(search, realTime);
59 text.requestFocus();
60
61 if (realTime) {
62 fireActionPerformed(getText());
63 }
64 }
65 });
66
67 text = new JTextField();
68 text.addKeyListener(new KeyAdapter() {
69 @Override
70 public void keyTyped(final KeyEvent e) {
71 super.keyTyped(e);
72 SwingUtilities.invokeLater(new Runnable() {
73 @Override
74 public void run() {
75 boolean empty = (text.getText().isEmpty());
76 clear.setVisible(!empty);
77
78 if (realTime) {
79 fireActionPerformed(getText());
80 }
81 }
82 });
83 }
84 });
85 text.addActionListener(new ActionListener() {
86 @Override
87 public void actionPerformed(ActionEvent e) {
88 if (!realTime) {
89 fireActionPerformed(getText());
90 }
91 }
92 });
93
94 clear = new JButton(clearIcon);
95 if (clearIcon == null) {
96 clear.setText("(c)");
97 }
98 clear.setBackground(text.getBackground());
99 clear.setVisible(false);
100 clear.addActionListener(new ActionListener() {
101 @Override
102 public void actionPerformed(ActionEvent e) {
103 text.setText("");
104 clear.setVisible(false);
105 text.requestFocus();
106
107 fireActionPerformed(getText());
108 }
109 });
110
111 add(search, BorderLayout.WEST);
112 add(text, BorderLayout.CENTER);
113 add(clear, BorderLayout.EAST);
114 }
115
116 /**
117 * Return the current text displayed by this {@link SearchBar}, or an empty
118 * {@link String} if none.
119 *
120 * @return the text, cannot be NULL
121 */
122 public String getText() {
123 // Should usually not be NULL, but not impossible
124 String text = this.text.getText();
125 return text == null ? "" : text;
126 }
127
128 @Override
129 public void setEnabled(boolean enabled) {
130 search.setEnabled(enabled);
131 clear.setEnabled(enabled);
132 text.setEnabled(enabled);
133 super.setEnabled(enabled);
134 }
135
136 private ImageIcon getIcon(String name) {
137 InputStream in = IOUtils.openResource(SearchBar.class, name);
138 if (in != null) {
139 try {
140 return new ImageIcon(IOUtils.toByteArray(in));
141 } catch (IOException e) {
142 e.printStackTrace();
143 }
144 }
145
146 return null;
147 }
148}