Skip to content

Commit 6949a32

Browse files
frauzufallctrueden
authored andcommitted
searchbar: adds exit button
1 parent e1cf87f commit 6949a32

File tree

1 file changed

+25
-10
lines changed

1 file changed

+25
-10
lines changed

src/main/java/org/scijava/ui/swing/search/SwingSearchBar.java

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,10 @@
3737
import java.awt.Font;
3838
import java.awt.GridLayout;
3939
import java.awt.Window;
40-
import java.awt.event.ActionListener;
4140
import java.awt.event.FocusEvent;
4241
import java.awt.event.FocusListener;
4342
import java.awt.event.KeyAdapter;
4443
import java.awt.event.KeyEvent;
45-
import java.beans.PropertyChangeListener;
4644
import java.net.URL;
4745
import java.util.Collections;
4846
import java.util.Comparator;
@@ -102,6 +100,9 @@ public class SwingSearchBar extends JTextField {
102100

103101
private static final String DEFAULT_MESSAGE = "Click here to search";
104102
private static final int MAX_RESULTS = 8;
103+
104+
private static final Color SEARCHBAR_FONT_COLOR = new Color(0, 0, 0);
105+
private static final Color SEARCHBAR_FONT_DEFAULT_COLOR = new Color(150, 150, 150);
105106

106107
private static final Color SELECTED_COLOR = new Color(70, 152, 251);
107108
private static final Color HEADER_COLOR = new Color(234, 234, 234);
@@ -123,16 +124,19 @@ public class SwingSearchBar extends JTextField {
123124
private SwingSearchPanel searchPanel;
124125

125126
private DocumentListener documentListener;
127+
128+
private final JButton exitBtn;
126129

127130
public SwingSearchBar(final Context context, final Window window, final Container parent) {
128131
super(DEFAULT_MESSAGE, 12);
129132
this.parent = parent;
130133
this.window = window;
131134
context.inject(this);
132135
setText(DEFAULT_MESSAGE);
136+
setForeground(SEARCHBAR_FONT_DEFAULT_COLOR);
133137

134138
setBorder(BorderFactory.createCompoundBorder(
135-
BorderFactory.createLineBorder(new Color(237,237,237), 5),
139+
BorderFactory.createEmptyBorder(),
136140
BorderFactory.createEmptyBorder(5,5,5,5)));
137141

138142
addActionListener(e -> run());
@@ -161,15 +165,25 @@ public void changedUpdate(final DocumentEvent e) {
161165
@Override
162166
public void focusGained(final FocusEvent e) {
163167
if (DEFAULT_MESSAGE.equals(getText())) setText("");
164-
setForeground(new Color(0,0,0));
168+
setForeground(SEARCHBAR_FONT_COLOR);
165169
}
166170

167171
@Override
168172
public void focusLost(final FocusEvent e) {
169-
if (getText().equals("")) reset();
173+
if (getText().equals("")) {
174+
reset();
175+
}
170176
}
171177

172178
});
179+
180+
exitBtn = new JButton("🞩");
181+
exitBtn.setBackground(new Color(255,255,255));
182+
exitBtn.setBorder(BorderFactory.createLineBorder(new Color(255,255,255),5));
183+
exitBtn.addActionListener(ae -> {
184+
reset();
185+
loseFocus();
186+
});
173187
}
174188

175189
/** Called externally to bring the search bar into focus. */
@@ -216,7 +230,6 @@ private void search() {
216230
}
217231

218232
searchPanel = new SwingSearchPanel(); // Spawns the SearchOperation!
219-
searchPanel.setBorder(BorderFactory.createEmptyBorder(0,5,0,5));
220233
parent.add(searchPanel, "south,height 300!", getParent().getComponentCount()-1);
221234
parent.doLayout();
222235
parent.revalidate();
@@ -246,7 +259,7 @@ private void reset() {
246259
loseFocus();
247260
getDocument().removeDocumentListener(documentListener);
248261
setText(DEFAULT_MESSAGE);
249-
setForeground(new Color(150,150,150));
262+
setForeground(SEARCHBAR_FONT_DEFAULT_COLOR);
250263
getDocument().addDocumentListener(documentListener);
251264
}
252265
else {
@@ -303,7 +316,7 @@ public SwingSearchPanel() {
303316
headerOuterPane.setLayout(new GridLayout(1, 1));
304317
headerOuterPane.add(headerInnerPane);
305318
headerOuterPane.setBackground(list.getBackground());
306-
headerOuterPane.setBorder(new EmptyBorder(PAD, 0, 0, 0));
319+
headerOuterPane.setBorder(new EmptyBorder(index == 0? 0 : PAD, 0, 0, 0));
307320
return headerOuterPane;
308321
}
309322
final JPanel item = new JPanel();
@@ -329,8 +342,10 @@ public SwingSearchPanel() {
329342
final JPanel detailsButtons = new JPanel();
330343
detailsButtons.setLayout(new MigLayout());
331344

332-
detailsPane.setLayout(new MigLayout("wrap","[grow]", "[][grow][]"));
333-
detailsPane.setBorder(BorderFactory.createEmptyBorder(PAD, PAD, PAD, PAD));
345+
detailsPane.setLayout(new MigLayout("wrap, ins 0","[grow]", "[][grow][]"));
346+
detailsPane.setBorder(null);
347+
348+
detailsPane.add(exitBtn, "push, al right, wrap");
334349
detailsPane.add(detailsTitle);
335350
detailsPane.add(detailsProps, "grow,span");
336351
detailsPane.add(detailsButtons, "grow");

0 commit comments

Comments
 (0)